While using the model in frontend(nextjs) i am getting this error when i try to load the model:->ValueError: An InputLayer should be passed either a batchInputShape or an inputShape

:small_blue_diamond: Title:

“ValueError: An InputLayer should be passed either a batchInputShape or an inputShape” - TensorFlow Model Issue

:small_blue_diamond: 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.

:small_blue_diamond: 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

:small_blue_diamond: What I Tried:

:white_check_mark: Checked that base_model has an input shape.
:white_check_mark: Verified that include_top=False in MobileNetV2.
:white_check_mark: Tried explicitly adding an InputLayer().

:small_blue_diamond: My Question:

  • Why am I still getting this error?
  • Is my model structure correct?
  • Do I need to manually add InputLayer()?

Thanks in advance! :pray:

Hi @Aich007, I have tried to execute the above given code and was able to convert the tensorflow model to tfjs with out any error in colab. Please refer to this gist for working code example. Thank You.

Yes, there is no error while running the code (i.e, while creating the model and export it to json and bin file in Google colab).

I am getting the error when I am trying to import the model in frontend (I am using Nextjs).
I installed npm i @tensorflow/tfjs and used await tf.loadLayersModel() to load the model. And there in console the mentioned ERROR is showing. I tried another model to train still same error.

[Thank you @Kiran_Sai_Ramineni to give your precious time to check for the error]

Hi @Aich007, Could you please let me know the keras version you are using. In case if you are using keras 3, please try to downgrade to kears 2 and train the model, save it, then convert it to tfjs model and try to load it. Thank You.