I have a dataset of around 3500 images, divided into 3 folders, that I loaded into Google Collab from my google drive, and I'm trying to make them into an ML algorithm using keras and tensorflow with the following code:
train = tf.keras.preprocessing.image_dataset_from_directory(
path,
labels = "inferred",
label_mode = "categorical",
color_mode = "rgb",
batch_size = 32,
image_size = (140,140),
seed = 1234,
subset = "training",
validation_split = 0.2
)
shape = tf.TensorShape([None,140,140,3])
print(shape)
valid = tf.keras.preprocessing.image_dataset_from_directory(
path,
labels = "inferred",
label_mode = "categorical",
color_mode = "rgb",
batch_size = 32,
image_size = (140,140),
seed = 1234,
subset = "validation",
validation_split = 0.2
)
print(train)
print(valid)
print(tf.keras.utils.image_dataset_from_directory(path, labels='inferred'))
from keras.models import Sequential
from keras.layers import Dense
from tensorflow import keras
#from tensorflow.keras import layers
model = Sequential()
model.add(Dense(256, activation = "softmax", input_shape = (140,140,3)))
model.add(Dense(64, activation = "softmax"))
model.add(Dense(32, activation = "softmax"))
#model.add(Dense(3, activation = "softmax"))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
print(model.summary())
keras.utils.plot_model(model, "my_first_model_with_shape_info.png", show_shapes=True)
#print(tf.keras.utils.plot_model(model))
model.fit(train, validation_data = valid, epochs = 50, batch_size = 32)
However when I run the code I get this error:
ValueError: Shapes (None, 3) and (None, 140, 140, 32) are incompatible
I tried fixing this by adding the (None,140,140,3) shape to the "train" variable but I'm not sure how to do that, so does anyone know how to make the shape of my "train" and "valid" variables compatible with the model I made? Thank you.
For reference this is the train variable:
train = tf.keras.preprocessing.image_dataset_from_directory(
path,
labels = "inferred",
label_mode = "categorical",
color_mode = "rgb",
batch_size = 32,
image_size = (140,140),
seed = 1234,
subset = "training",
validation_split = 0.2
)
when I print "train" out however, I get this
<BatchDataset element_spec=(TensorSpec(shape=(None, 140, 140, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None, 3), dtype=tf.float32, name=None))>
So can someone also explain what a BatchDataset element is, and how do I edit its shape in the first place? thanks.