John G.
John G.

Reputation: 97

tf.keras.utils.image_dataset_from_directory, but labels are from csv?

Please tell me where I'm going wrong. I am working on the kaggle dog breed classification challenge and I want to try one-hot encoding vs label encoding. The images are not split in the images directory so I can not use 'inferred' with tf.keras.utils.image_dataset_from_directory, the labels are in a separate csv file that I put into a df (label_int).

cat_count = 120 #depth or the number of categories
oh_input = tf.one_hot(label_int, cat_count) #apply one-hot encoding

label_int is a list of all my labels in integer form, in the same order as my images in the directory. oh_input is a list of one-hot vectors.

batch_size = 32
img_height = 224
img_width = 224

train_ds = tf.keras.utils.image_dataset_from_directory(train_dir,labels = oh_input,label_mode = 'categorical',validation_split=0.2,subset="training",seed=123,image_size=(img_height, img_width),batch_size=batch_size)

val_ds = tf.keras.utils.image_dataset_from_directory(train_dir,labels = oh_input,label_mode = 'categorical',validation_split=0.2,subset="validation",seed=123,image_size=(img_height, img_width),batch_size=batch_size)

The error:

154 if kwargs:

155 raise TypeError(f'Unknown keywords argument(s): {tuple(kwargs.keys())}') --> 156 if labels not in ('inferred', None):

...

100 dtype = dtypes.as_dtype(dtype).as_datatype_enum 101 ctx.ensure_initialized() --> 102 return ops.EagerTensor(value, ctx.device_name, dtype) TypeError: Cannot convert 'inferred' to EagerTensor of dtype int64

I have tried other datatypes in the tf.one_hot function and none of them working. I'm guessing I am just missing some fundamental syntax error.

https://www.tensorflow.org/api_docs/python/tf/keras/utils/image_dataset_from_directory

I have also tried labels= *(a numpy array of integer values) with label_mode =int and that gave me another error. I also tried labels = None and it says 'Found 10222 files belonging to 1 classes.' so I'm not sure if I can add in the labels later to the fit function.

Upvotes: 1

Views: 1275

Answers (1)

Soroush Mirzaei
Soroush Mirzaei

Reputation: 351

In this case you could use keras.preprocessing.image.ImageDataGenerator() class and then use flow_from_directory() or flow_from_dataframe() as it's functions depend on the case you are working on.

Upvotes: 2

Related Questions