我有一个大的B / W图像数据集,有两个类,其中目录的名称是类的名称:
SELECTION
包含label = selection的所有图像;NEUTRAL
包含label = neutral的所有图像。我需要在TensorFlow数据集中加载所有这些图像,以便在this教程中更改MNIST数据集。
我试图按照this指南,它看起来不错,但有一些问题,我不知道如何解决。按照指南我到达这里:
from __future__ import absolute_import, division, print_function
import os
import pathlib
import IPython.display as display
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
np.set_printoptions(threshold=np.nan)
tf.enable_eager_execution()
tf.__version__
os.system('clear')
#### some tries for the SELECTION dataset ####
data_root = pathlib.Path('/Users/matteo/Desktop/DATASET_X/SELECTION/TRAIN_IMG')
all_image_paths = []
all_image_labels = []
for item in data_root.iterdir():
item_tmp = str(item)
if 'selection.png' in item_tmp:
all_image_paths.append(str(item))
all_image_labels.append(0)
image_count = len(all_image_paths)
label_names = ['selection', 'neutral']
label_to_index = dict((name, index) for index, name in enumerate(label_names))
img_path = all_image_paths[0]
img_raw = tf.read_file(img_path)
img_tensor = tf.image.decode_png(
contents=img_raw,
channels=1
)
print(img_tensor.numpy().min())
print(img_tensor.numpy().max())
#### it works fine till here ####
#### trying to make a function ####
#### problems from here ####
def load_and_decode_image(path):
print('[LOG:load_and_decode_image]: ' + str(path))
image = tf.read_file(path)
image = tf.image.decode_png(
contents=image,
channels=3
)
return image
image_path = all_image_paths[0]
label = all_image_labels[0]
image = load_and_decode_image(image_path)
print('[LOG:image.shape]: ' + str(image.shape))
path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)
print('shape: ', repr(path_ds.output_shapes))
print('type: ', path_ds.output_types)
print()
print('[LOG:path_ds]:' + str(path_ds))
如果我只加载一个项目它可以工作,但当我尝试做:
path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)
如果我打印path_ds.shape
它返回shape: TensorShape([])
所以它似乎不起作用。如果我尝试继续使用此块的教程
image_ds = path_ds.map(load_and_decode_image, num_parallel_calls=AUTOTUNE)
plt.figure(figsize=(8, 8))
for n, image in enumerate(image_ds.take(4)):
print('[LOG:n, image]: ' + str(n) + ', ' + str(image))
plt.subplot(2, 2, n+1)
plt.imshow(image)
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.xlabel(' selection'.encode('utf-8'))
plt.title(label_names[label].title())
plt.show()
它给我以下错误:
It's not possible open ' < string >': The file was not found (file: // /Users/matteo/Documents/GitHub/Cnn_Genetic/cnn_genetic/<string > ).
但问题是我不知道这个文件是什么以及它为什么要寻找它。我不是要绘制我的图像,但我想知道为什么它不起作用。如果我复制/粘贴教程代码我有同样的问题所以我认为新的tf版本存在问题。
所以......如果有人能告诉我哪里出错了,我会非常感激。谢谢你的时间。
您的问题是path_ds应该是图像路径作为字符串,但您尝试将它们转换为张量列表。
所以要获得张力,你只需要:
image_ds = all_image_paths.map(load_and_decode_image, num_parallel_calls=AUTOTUNE)