hyper
hyper

Reputation: 23

ValueError while loading my Keras model after doing Transfer Learning

I have developed a sign language recognition model using the ResNet50 architecture as the base model for recognizing Urdu sign language. The model architecture is defined as follows:

base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(256, 256, 3))

model = tf.keras.Sequential([
        tf.keras.layers.Input(shape=(256,256,3)),
        tf.keras.layers.Lambda(preprocess_input_resnet),
        base_model,
        tf.keras.layers.GlobalAveragePooling2D(),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(37, activation='softmax')
    ])

After training the model for a few epochs, I fine-tuned it by setting certain layers to be trainable:

base_learning_rate = 0.0001

base_model.trainable = True
fine_tune_at = 100

# Freeze layers before fine-tuning
for layer in base_model.layers[:fine_tune_at]:
    layer.trainable = False

model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),
              optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate / 10),
              metrics=['accuracy'])

history_fine = model.fit(train_dataset, epochs=total_epochs, validation_data=validation_dataset)

Issue: When I try to load the model using load_model('model.h5'), I encounter the following ValueError:

ValueError: Layer "dense" expects 1 input(s), but it received 2 input tensors. Inputs received: [<KerasTensor shape=(None, 8, 8, 2048), dtype=float32, sparse=False, name=keras_tensor_564>, <KerasTensor shape=(None, 8, 8, 2048), dtype=float32, sparse=False, name=keras_tensor_565>]

The error suggests that the Dense layer is receiving two input tensors instead of one.

I have checked the model architecture by running model.summary() and confirmed that the layers are structured as intended. However, I’m confused about the source of the second tensor (keras_tensor_565). It seems that there may be an unintended connection or duplication somewhere in the architecture.

I also tried to save & load the model in .h5 and .keras format. But the same error exists in both formats while loading the model.

model1.save("model.h5")
model = load_model('model.h5', custom_objects={'preprocess_input': preprocess_input})

Upvotes: 0

Views: 41

Answers (0)

Related Questions