UnimplementedError: Cast string to float is not supported

In summary, the speaker encountered an error while trying to create a dataset using two different image folders and train a cycleGAN model. They shared their code and the error message, and received advice to search for a solution in the context of Tensorflow or file a bug report. Ultimately, they discovered that the problem was caused by reading the file as a string rather than a tensor, and were able to resolve it by using tf.image.decode_png to convert it to a tensor.
  • #1
BRN
108
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:
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[i].imshow(image1)
        axs[i+num_cols].imshow(image2)
        
    plt.show()

I receive this error:
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]

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
  • #2
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 BRN
  • #3
For example, googling "tensorflow cast string to float" returns a lot of hits.
 
  • Like
Likes BRN, Vanadium 50 and jedishrfu
  • #4
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.
 
  • #5
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:
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
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
  • #6
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

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

Thank you all!
 
  • Like
Likes berkeman

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
Back
Top