自己的数据集ValueError:Tensor转换请求dtype float32

时间:2018-07-02 15:01:59

标签: python image tensorflow type-conversion jpeg

我正在尝试使用自己的数据集来训练GAN网络。我在以.jpg格式加载自己的数据集时遇到问题。我已经可以使用现有的jpg数据集,但看不到有效数据集和无效数据集之间jpg编码的区别。

照片是使用Windows计算机转换的,并重命名为001.jpg 002.jpg等。为进行培训,我使用的是Linux计算机。

我正在使用以下代码的python程序加载并将图像转换为张量:

def _image_batch(image_paths,
                 batch_size,
                 load_size=286,
                 crop_size=256,
                 channels=3,
                 prefetch_batch=2,
                 drop_remainder=True,
                 num_threads=16,
                 shuffle=True,
                 buffer_size=4096,
                 repeat=-1):
    def _parse_func(path):
        img = tf.read_file(path)
        img = tf.image.decode_jpeg(img, channels=channels)
        img = tf.image.random_flip_left_right(img)
        img = tf.image.resize_images(img, [load_size, load_size])
        img = (img - tf.reduce_min(img)) / (tf.reduce_max(img) - tf.reduce_min(img))
        img = tf.random_crop(img, [crop_size, crop_size, channels])
        img = img * 2 - 1
        return img

完整错误是:

Traceback (most recent call last):
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 510, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1040, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 883, in _TensorTensorConversionFunction
    (dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("arg0:0", shape=(), dtype=float32)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "train.py", line 112, in <module>
    a_test_pool = data.ImageData(sess, a_test_img_paths, batch_size, load_size=load_size, crop_size=crop_size)
  File "/mnt/storage/scratch/ag17634/CycleGAN-Tensorflow-Pytorch/data.py", line 35, in __init__
    repeat)
  File "/mnt/storage/scratch/ag17634/CycleGAN-Tensorflow-Pytorch/data.py", line 68, in _image_batch
    dataset = dataset.map(_parse_func, num_parallel_calls=num_threads)
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 840, in map
    return ParallelMapDataset(self, map_func, num_parallel_calls)
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1857, in __init__
    super(ParallelMapDataset, self).__init__(input_dataset, map_func)
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1826, in __init__
    self._map_func.add_to_graph(ops.get_default_graph())
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 488, in add_to_graph
    self._create_definition_if_needed()
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 321, in _create_definition_if_needed
    self._create_definition_if_needed_impl()
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 338, in _create_definition_if_needed_impl
    outputs = self._func(*inputs)
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1791, in tf_map_func
    ret = map_func(nested_args)
  File "/mnt/storage/scratch/ag17634/CycleGAN-Tensorflow-Pytorch/data.py", line 57, in _parse_func
    img = tf.read_file(path)
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/ops/gen_io_ops.py", line 527, in read_file
    "ReadFile", filename=filename, name=name)
  File "/mnt/storage/software/languages/anaconda/Anaconda3-5.2.0-tflow-1.7/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 533, in _apply_op_helper
    (prefix, dtypes.as_dtype(input_arg.type).name))
TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string.

2 个答案:

答案 0 :(得分:0)

这听起来像是一个转换问题。 我想你可能必须打电话       str(input) 在您作为文件名传递的输入上。

答案 1 :(得分:0)

我和你有同样的错误。

我解决了:正确更改路径名。

我想在您的情况下,您应该检查代码中的“路径”名称

img = tf.read_file(path)