Python UnimplementedError: Cast string to float is not supported

AI Thread Summary
The discussion revolves around troubleshooting an error encountered while loading and normalizing images for a dataset intended for training a cycleGAN model. The user initially faced an "UnimplementedError" related to casting a string to a float, which stemmed from using `tf.io.read_file` that reads the image file as a string rather than a tensor. The solution involved using `tf.image.decode_png` to convert the image string to a tensor before normalization. The corrected code successfully resolves the issue, allowing the user to proceed with their project. Additionally, suggestions were made to check for TensorFlow updates and to search for similar issues online for further insights.
BRN
Messages
107
Reaction score
10
Hello everyone,
I have this problem that I can't solve:

I have two types of images contained in two different folders. I have to create a dataset with these images and train a cycleGAN model, but for simplicity we assume that I want to print them on monitor and forget the cycleGAN.

My code is this:
[CODE lang="python" title="load and print"]input_path_A = './data/img_test_A/'
input_path_B = './data/img_test_B/'
EPOCHS = 50
buffer_size = 1000
batch_size = 2

def load_and_norm(filename):

img = tf.io.read_file(filename)
img = tf.cast(img, tf.float32) / 127.5 - 1 # normalization to [-1, 1]

return img

def load_dataset(ds_folder, batch_size, buffer_size):

img_filenames = tf.data.Dataset.list_files(os.path.join(ds_folder, "*.png"))
img_dataset = img_filenames.map(load_and_norm)

img_dataset = img_dataset.batch(batch_size).shuffle(buffer_size)

return img_dataset

def show_img(dataset1, dataset2):
iterator1 = iter(dataset1)
iterator2 = iter(dataset2)

num_rows = 4
num_cols = 2

fig, axs = plt.subplots(num_rows, num_cols, figsize=(10, 10))
axs = axs.flatten()

for i in range(num_rows*num_cols):
image1 = next(iterator1)
image2 = next(iterator2)

axs.imshow(image1)
axs[i+num_cols].imshow(image2)

plt.show()[/CODE]

I receive this error:
[CODE title="error"]2023-03-20 18:37:30.450694: W tensorflow/core/framework/op_kernel.cc:1722] OP_REQUIRES failed at cast_op.cc:121 : UNIMPLEMENTED: Cast string to float is not supported

---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
/tmp/ipykernel_18739/534026073.py in <module>
2 print("Starting epoch", epoch + 1)
3
----> 4 for x, y in train_dataset:
5 show_img(x, y)

~/.local/lib/python3.9/site-packages/tensorflow/python/data/ops/dataset_ops.py in __iter__(self)
488 if context.executing_eagerly() or ops.inside_function():
489 with ops.colocate_with(self._variant_tensor):
--> 490 return iterator_ops.OwnedIterator(self)
491 else:
492 raise RuntimeError("`tf.data.Dataset` only supports Python-style "

~/.local/lib/python3.9/site-packages/tensorflow/python/data/ops/iterator_ops.py in __init__(self, dataset, components, element_spec)
724 "When `dataset` is provided, `element_spec` and `components` must "
725 "not be specified.")
--> 726 self._create_iterator(dataset)
727
728 self._get_next_call_count = 0

~/.local/lib/python3.9/site-packages/tensorflow/python/data/ops/iterator_ops.py in _create_iterator(self, dataset)
749 output_types=self._flat_output_types,
750 output_shapes=self._flat_output_shapes))
--> 751 gen_dataset_ops.make_iterator(ds_variant, self._iterator_resource)
752 # Delete the resource when this object is deleted
753 self._resource_deleter = IteratorResourceDeleter(

~/.local/lib/python3.9/site-packages/tensorflow/python/ops/gen_dataset_ops.py in make_iterator(dataset, iterator, name)
3239 return _result
3240 except _core._NotOkStatusException as e:
-> 3241 _ops.raise_from_not_ok_status(e, name)
3242 except _core._FallbackException:
3243 pass

~/.local/lib/python3.9/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
7105 def raise_from_not_ok_status(e, name):
7106 e.message += (" name: " + name if name is not None else "")
-> 7107 raise core._status_to_exception(e) from None # pylint: disable=protected-access
7108
7109

UnimplementedError: Cast string to float is not supported
[[{{node Cast}}]] [Op:MakeIterator][/CODE]

I don't understand what the problem is, but I think it is due to how the dataset is created.

How can it be resolved?

Thank you all.
 
Technology news on Phys.org
I think you should try to search on this error in the context of Tensorflow as there is either a bug report on it or someone else has figured out what went wrong.

If not then you should file a bug report with Tensorflow.
 
For example, googling "tensorflow cast string to float" returns a lot of hits.
 
  • Like
Likes BRN, Vanadium 50 and jedishrfu
You could try downloading your training data again in case it got corrupted somehow.

Also check if there are any updates to tensorflow that you haven't installed.
 
BRN said:
Hello everyone,
I have this problem that I can't solve:

I have two types of images contained in two different folders. I have to create a dataset with these images and train a cycleGAN model, but for simplicity we assume that I want to print them on monitor and forget the cycleGAN.

My code is this:
[CODE lang="python" title="load and print"]

def load_and_norm(filename):

img = tf.io.read_file(filename)
img = tf.cast(img, tf.float32) / 127.5 - 1 # normalization to [-1, 1]

return img
[/CODE]
tf.io.read_file reads the file as a string, and not as a tensor.
You'll need tf.io.decode_png to convert it to a tensor
https://www.tensorflow.org/api_docs/python/tf/io/decode_png
 
  • Like
Likes jedishrfu and BRN
willem2 said:
tf.io.read_file reads the file as a string, and not as a tensor.
You'll need tf.io.decode_png to convert it to a tensor
https://www.tensorflow.org/api_docs/python/tf/io/decode_png
That's right, this was the problem.

Here the correct code

[CODE lang="python" title="correct code"]def load_and_norm(filename):

img = tf.io.read_file(filename) # get only filename string
img = tf.image.decode_png(img, channels = 3) # necesary converting to tensor
img = tf.cast(img, tf.float32) / 127.5 - 1 # normalization to [-1, 1]

return img[/CODE]

Thank you all!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top