UnimplementedError: Cast string to float is not supported

  • Context: Python 
  • Thread starter Thread starter BRN
  • Start date Start date
  • Tags Tags
    Error Float String
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 5K views
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.
 
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.
 
Reply
  • Like
Likes   Reactions: BRN
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
 
Reply
  • 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!
 
Reply
  • Like
Likes   Reactions: berkeman