UnimplementedError: Cast string to float is not supported

  • Context: Python 
  • Thread starter Thread starter BRN
  • Start date Start date
  • Tags Tags
    Error Float String
Click For Summary

Discussion Overview

The discussion revolves around an error encountered while attempting to load and normalize image data for a TensorFlow model. Participants explore the cause of the error, which involves casting a string to a float, and propose potential solutions related to image file handling and dataset creation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an error message indicating that casting a string to float is not supported, suggesting it may be related to how the dataset is created.
  • Another participant recommends searching for the error in the context of TensorFlow, mentioning the possibility of a bug report or existing solutions.
  • Some participants suggest re-downloading the training data in case of corruption and checking for TensorFlow updates.
  • A participant points out that the function tf.io.read_file reads the file as a string and proposes using tf.io.decode_png to convert it to a tensor, which is necessary for further processing.
  • Later, another participant confirms that the issue was indeed due to not decoding the image and provides the corrected code that includes the use of tf.image.decode_png.

Areas of Agreement / Disagreement

There is no consensus on a single solution initially, but later contributions clarify the necessary steps to resolve the issue, indicating a shift towards agreement on the importance of decoding the image files properly.

Contextual Notes

The discussion highlights the need for proper handling of image data types in TensorFlow, specifically the transition from string representations to tensor formats. There are unresolved aspects regarding the initial dataset creation process and potential issues with data integrity.

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.
 
  • Like
Likes   Reactions: BRN
For example, googling "tensorflow cast string to float" returns a lot of hits.
 
  • Like
Likes   Reactions: 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   Reactions: 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!
 
  • Like
Likes   Reactions: berkeman

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K