Reputation: 373
I'm trying to use my own dataset to train a GAN network. I'm having issues with loading my own dataset in .jpg format. I have existing jpg datasets that work, I can't see a difference in the jpg encoding between the working and not working datasets.
The photos are converted using a windows machine and renamed to 001.jpg 002.jpg etc. For training I'm using a linux machine.
The python program I'm using the following code to load and convert images to tensors:
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
The full error is:
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.
Upvotes: 2
Views: 5975
Reputation: 23473
Maybe your question is missing the line where you call your function, _image_batch
but in any case I encountered your error too when doing the writing.
Originally I had
image_path = "original.jpg"
img = tf.io.read_file(image_path)
img = tf.image.decode_jpeg(img)
img_resized = tf.image.resize(img, [224, 224])
resized_filename = "resized.jpg"
tf.io.write_file(str(resized_filename), img_resized)
which caused
ValueError: Tensor conversion requested dtype string for Tensor with dtype float32
Change that ended up working for me was
image_path = "original.jpg"
img = tf.io.read_file(image_path)
img = tf.image.decode_jpeg(img)
img_resized = tf.image.resize(img, [224, 224])
img_encoded = tf.image.encode_jpeg(tf.cast(img_resized, tf.uint8))
resized_filename = "resized.jpg"
tf.io.write_file(resized_filename, img_encoded)
Upvotes: 0
Reputation: 1111
I have got same error with you.
I solve it: change the path name correctly.
I guess in your case, you should check you "path" name in codes
img = tf.read_file(path)
Upvotes: 1
Reputation: 1293
This sounds like a converting issue.
I think you may have to call
str(input)
on the input you are passing as filename.
Upvotes: 0