无法使用 Tensorflow 数据集加载文件

Can't Load Files with Tensorflow Dataset

我正在尝试使用 tf.Dataset 加载输入图像和标签图像。因此,我正在关注 https://www.tensorflow.org/programmers_guide/datasets 上的 Tensorflow 数据集教程。但是我得到这个错误:

ValueError: 'images' contains no shape.

我打印了一些行:

这是我的input_files['images/kitchen-1687121_1280.jpg', 'images/room-1706801_1280.jpg']

input_filename: Tensor("arg0:0", shape=(), dtype=string)

image_string: Tensor("ReadFile:0", shape=(), dtype=string)

class DatasetImporter():

    def __init__(self, inputs_path, labels_path):
        self.inputs_path = inputs_path
        self.labels_path = labels_path
        self.dataset = None

    def _get_files(self, path):
        return sorted([path+"/"+f for f in listdir(path) if isfile(join(path, f))])

    def _parse_function(self, input_filename, label_filename):
        print(input_filename) # Tensor("arg0:0", shape=(), dtype=string)
        image_string = tf.read_file(input_filename)
        print(image_string) # Tensor("ReadFile:0", shape=(), dtype=string)
        image_decoded = tf.image.decode_image(image_string)
        image_resized = tf.image.resize_images(image_decoded, [28, 28]) # *ERROR* in this line 
        # Similar for the label...
        return image_resized, label

    def loadData(self):
        input_files = self._get_files(dsi.inputs_path)
        label_files = self._get_files(dsi.labels_path)
        print(input_files) # ['images/kitchen-1687121_1280.jpg', 'images/room-1706801_1280.jpg']
        self.dataset = tf.data.Dataset.from_tensor_slices((tf.constant(input_files), tf.constant(label_files)))
        self.dataset = dataset.map(self._parse_function)

dsi = DatasetImporter("images", "labels")
dsi.loadData()

问题是因为您使用 tf.image.decode_image 而不是 tf.image.decode_jpeg。由于 here and here.

描述的一些问题,第一个 return 没有任何形状

我写了一个更广泛的答案here