TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string

Question:

I am running this code from the tutorial here: https://keras.io/examples/vision/image_classification_from_scratch/

with a custom dataset, that is divided in 2 datasets as in the tutorial. However, I got this error:

TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string.

I made this casting. I tried this:

is_jfif = str(tf.compat.as_bytes("JFIF")) in fobj.peek(10)

but nothing changed as far as the error
I am trying all day to figure out how to solve it, without any success. Can someone help me? Thank you…

Asked By: just_learning

||

Answers:

The names of the files are in the float32 format.
Renaming all the images in the dataset solves the problem.
Loop over all the files with os.rename().

Answered By: i_swarup

Simplest way I found is to create a subfolder and copy the files to that subfolder.
i.e. Lets assume your files are 0.jpg, 1.jpg,2.jpg….2000.jpg and in directory named "patterns".

Seems like the Keras API does not accept it as the files are named by numbers and for Keras it is in float32.

To overcome this issue, either you can rename the files as one answer suggests, or you can simply create a subfolder under "patterns" (i.e. "patterndir"). So now your image files are under …patternspatterndir

Keras (internally) possibly using the subdirectory name and may be attaching it in front of the image file thus making it a string (sth like patterndir_01.jpg, patterndir_02.jpg) [Note this is my interpretation, does not mean that it is true]

When you compile it this time, you will see that it works and you will get a compiler message as:

Found 2001 files belonging to 1 classes.
Using 1601 files for training.
Found 2001 files belonging to 1 classes.
Using 400 files for validation.

My code looks like this

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

#Generate a dataset

image_size = (28, 28)
batch_size = 32

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "patterns",
    validation_split=0.2,
    subset="training",
    seed=1337,
    image_size=image_size,
    batch_size=batch_size,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "patterns",
    validation_split=0.2,
    subset="validation",
    seed=1337,
    image_size=image_size,
    batch_size=batch_size,
)
Answered By: Matt Allen

In my case, I simply did not have enough samples in the training directories. There was one per category and I got the error.

Answered By: Shtefan

You have to check the several things after this exception appeared:

  1. Do you have enough data for training?

If you only have limited data in your training set, this exception would appear. I guess if you want to split the data, the amount of the data should be divisible by 10 (Take validation_split=0.1 for example).

  1. Do your image in valid format?

This method only allows formats in (‘.bmp’, ‘.gif’, ‘.jpeg’, ‘.jpg’, ‘.png’). Invalid format would appear this exception.

Honestly, the exception doesn’t give much information of what’s happening exactly. Hopefully would update in near future.

Answered By: Brady Huang

Just make a subdirectory and move your files there.

So if the files are here:

'/home/dataset_28/'

Put them here:

'/home/dataset_28/files/'

And then do this:

from tensorflow.keras.preprocessing import image_dataset_from_directory
image_dataset_from_directory('/home/dataset_28/', batch_size=1, image_size=(28, 28))
Answered By: Gabriel

I was just hitting this TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string. error too with tensorflow==2.4.4.

I played around with validation_split:

  • Error happens: validation_split=0.001
    • I did this in an effort to have 0 images in the validation set.
  • Error doesn’t happen: validation_split=0.2
    • This results in 1 image used for validation

Conclusion: a known root cause of this error is 0 images inside the validation set.


Failed Fixes

Per this answer I renamed my files via os.rename to 1.jpg, 2.jpg, 3.jpg, … didn’t work :/

Per this answer talking about one image/category, that’s wrong, it’s fine to have just one image inside a category.

One of the issues is related to the image downloading. If a designated image document is not downloaded, it also show the same error.

Answered By: Mike Chen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.