0

I have trained a KERAS model (EfficientNetB5) on an image dataset, and have saved the new model with new layers into a .keras file. However, I keep getting an input error.

# Load efficientnet model without the top layer
efficientnetbasemodel = tf.keras.applications.EfficientNetB5(include_top=False,input_shape=(400, 400, 3))

# Freeze the base model layers
efficientnetbasemodel.trainable = False

#add new layers for training
name="efficientnet"
efficientnetmodel=tf.keras.Sequential([tf.keras.Input(shape=(None, None, 3), name="input_layer"),data_augmentation,efficientnetbasemodel,tf.keras.layers.GlobalAveragePooling2D(),
                                       tf.keras.layers.Dense(128, activation='relu'),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(len(class_names), activation='softmax')], name=name)

# Compile the model
efficientnetmodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

efficientnetmodel.fit(train_data,epochs=10,validation_data=val_data)

efficientnetmodel.save("efficientnetmodel.keras")

When I try to load the model again

from tensorflow.keras.models import load_model

# Load the model
model = load_model('efficientnetmodel.keras')

I get this error.

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

1 Answer 1

3

This error is caused by a mismatch between the number of inputs expected by a layer in a Keras model and the number of inputs given to it. The dense_8 layer expects a single input, but it seems to have been given two inputs.

NOTE: This error may occur if two different inputs are given to the same layer anywhere in the above code.

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense

input_tensor = Input(shape=(None, None, 2048))
x = Dense(512, activation='relu')(input_tensor)
# There may be a case of accidentally using the same dense_8 
output_tensor = Dense(10, activation='softmax')(x)

model = Model(inputs=input_tensor, outputs=output_tensor)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.