Title:
“ValueError: An InputLayer should be passed either a batchInputShape or an inputShape” - TensorFlow Model Issue
Description:
I’m working on training a TensorFlow model for image classification (Rock-Paper-Scissors). When modifying the model and adding extra layers, I get this error:
ValueError: An InputLayer should be passed either a batchInputShape or an inputShape.
My Code:
...
b_model=tf.keras.applications.MobileNetV2(
include_top=False,
weights='imagenet',
input_shape=(224,224,3)
)
b_model.trainable=False
model=tf.keras.Sequential([
b_model,
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(3,activation="softmax")
])
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),
loss="categorical_crossentropy",
metrics=["accuracy"]
)
model.summary()
...
img_size = (224, 224)
batch_size = 32
train_dataset = tf.keras.preprocessing.image_dataset_from_directory(
directory="/content/filtered_data/train",
image_size=img_size,
batch_size=batch_size,
label_mode="categorical"
)
val_dataset = tf.keras.preprocessing.image_dataset_from_directory(
directory="/content/filtered_data/valid",
image_size=img_size,
batch_size=batch_size,
label_mode="categorical"
)
history = model.fit(train_dataset, epochs=20, validation_data=val_dataset)
model.save("temp/model.h5")
!tensorflowjs_converter --input_format=keras ./temp/model.h5 ./tfjs_model
What I Tried:
Checked that
base_model
has an input shape.
Verified that
include_top=False
in MobileNetV2.
Tried explicitly adding an
InputLayer()
.
My Question:
- Why am I still getting this error?
- Is my model structure correct?
- Do I need to manually add
InputLayer()
?
Thanks in advance!