【问题标题】:How to create a Tensorflow Dataset without labels? Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string如何创建没有标签的 TensorFlow 数据集? 'ReadFile' Op 的输入'filename' 的 float32 类型与预期的字符串类型不匹配
【发布时间】:2020-12-03 17:05:50
【问题描述】:

使用 Tensorflow 2.3,我正在尝试创建一个没有标签的 tf.data.Dataset。 我的 .png 文件位于文件夹“./Folder/”中。为了创建最小的工作示例,我认为唯一相关的行是我调用 tf.keras.preprocessing.image_dataset_from_directory 的行。 The class definition is here.

    dataset = tf.keras.preprocessing.image_dataset_from_directory('./Folder/',label_mode=None,batch_size=100)

当 Python 解释器到达上述行时,它会返回以下错误消息:

    Traceback (most recent call last):
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py", line 465, in _apply_op_helper
    values = ops.convert_to_tensor(
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1473, in convert_to_tensor
    raise ValueError(
ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: <tf.Tensor 'args_0:0' shape=() dtype=float32>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "04-vaeAnomalyScores.py", line 135, in <module>
    historicKLD, encoder, decoder, vae = artVAE_Instance.run_autoencoder()  # Train
  File "/media/roi/9b168630-3b62-4215-bb7d-fed9ba179dc7/images/largePatches/artvae.py", line 386, in run_autoencoder
    trainingDataSet = self.loadImages(self.trainingDir)
  File "/media/roi/9b168630-3b62-4215-bb7d-fed9ba179dc7/images/largePatches/artvae.py", line 231, in loadImages
    dataset = tf.keras.preprocessing.image_dataset_from_directory(dir[:-1]+'Downscaled/',label_mode=None,batch_size=self.BATCH_SIZE)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/keras/preprocessing/image_dataset.py", line 192, in image_dataset_from_directory
    dataset = paths_and_labels_to_dataset(
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/keras/preprocessing/image_dataset.py", line 219, in paths_and_labels_to_dataset
    img_ds = path_ds.map(
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1695, in map
    return MapDataset(self, map_func, preserve_cardinality=True)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 4041, in __init__
    self._map_func = StructuredFunctionWrapper(
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 3371, in __init__
    self._function = wrapper_fn.get_concrete_function()
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2938, in get_concrete_function
    graph_function = self._get_concrete_function_garbage_collected(
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2906, in _get_concrete_function_garbage_collected
    graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3213, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3065, in _create_graph_function
    func_graph_module.func_graph_from_py_func(
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 986, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 3364, in wrapper_fn
    ret = _wrapper_helper(*args)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 3299, in _wrapper_helper
    ret = autograph.tf_convert(func, ag_ctx)(*nested_args)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 255, in wrapper
    return converted_call(f, args, kwargs, options=options)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 532, in converted_call
    return _call_unconverted(f, args, kwargs, options)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 339, in _call_unconverted
    return f(*args, **kwargs)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/keras/preprocessing/image_dataset.py", line 220, in <lambda>
    lambda x: path_to_image(x, image_size, num_channels, interpolation))
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/keras/preprocessing/image_dataset.py", line 228, in path_to_image
    img = io_ops.read_file(path)
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/ops/gen_io_ops.py", line 574, in read_file
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
  File "/home/roi/.local/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py", line 492, in _apply_op_helper
    raise TypeError("%s expected type of %s." %
TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string.

非常感谢您的帮助。

【问题讨论】:

  • This answer 建议您的个人 .png 文件的名称可能是问题所在,重命名它们可以解决问题。
  • 感谢您的回答。我已阅读此答案,但这不是错误的根源。我已将图像重命名为 001.png 002.png 003.png ...
  • 从链接的答案看来,号码名称是个问题。如果是这样,使用字符串前缀重命名或将图像放在具有字符串名称的子目录中可能会解决此问题。
  • 再次感谢您,但添加前缀后,我仍然得到同样的错误。文件名是 image0.png , image1.png ...这看起来像一个 tensorflow 错误。奇怪的是还没有人报告。
  • 有同样的问题。不知道怎么解决?

标签: dataset


【解决方案1】:

我发现解决此问题的一种方法是将所有图像放在您要提供给image_dataset_from_directory 的目录中的另一个子目录中。

以您的示例为例,您将创建一个新文件夹,我们将其命名为 new_folder,在 ./Folder/ 内放置所有图像,这样现在所有图像的路径都是 ./Folder/new_folder/。然后,您可以使用与您在问题中所做的完全相同的参数调用 image_dataset_from_directory 方法:

tf.keras.preprocessing.image_dataset_from_directory(
  './Folder/',
  label_mode=None,
  batch_size=100
)

我发现这对我有用,所以希望其他人也会觉得它有帮助!

【讨论】:

    猜你喜欢
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2021-01-06
    相关资源
    最近更新 更多