i am fine-tuning and training Xception model. then saving model using model.save(). After loding the model using model.load_model(), i am getting this error “Input 0 of layer “dense_1″ is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (128,)”.
If i dont save model and make predictions just after the training, then it is working perfect. But issue arises after saving and then loading the model to make predictions. Please help me where i am doing wrong
base_model =keras.applications.Xception(input_shape=(256, 256, 3), include_top=False, weights='imagenet',
pooling= 'max')
model = Sequential([
base_model,
Dropout(0.5),
Dense(128, activation='relu'),
Dropout(0.25),
Dense(4, activation='softmax')
])
model.compile(optimizer=’adam’, loss=’categorical_crossentropy’, metrics=[‘accuracy’])
lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),
cooldown=0,
patience=5,
min_lr=0.5e-6)
checkpoint = ModelCheckpoint(filepath='//content/gdrive/MyDrive/xception2FE1_adam_LR_Norm_10epoch.keras',
verbose=1, save_best_only=True)
early = EarlyStopping(monitor='val_loss', min_delta=0, patience=8, verbose=1, mode='auto')
history = model.fit(train_ds,
epochs = 10, verbose=1,
validation_data = valid_ds, callbacks=[lr_reducer, checkpoint, early])
model.save('/content/gdrive/MyDrive/XceptionFeatExtr_LR_NORM_10.keras')
print('model is saved')
new_model = tensorflow.keras.models.load_model('/content/gdrive/MyDrive/XceptionFeatExtr_LR_NORM_10.keras')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-13-9165482b3bac> in <cell line: 1>()
----> 1 new_model = tensorflow.keras.models.load_model('/content/gdrive/MyDrive/XceptionFeatExtr_LR_NORM_10.keras')
2 print(new_model.summary())
11 frames
/usr/local/lib/python3.10/dist-packages/keras/src/layers/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
200 if spec.min_ndim is not None:
201 if ndim is not None and ndim < spec.min_ndim:
--> 202 raise ValueError(
203 f'Input {input_index} of layer "{layer_name}" '
204 "is incompatible with the layer: "
ValueError: Input 0 of layer "dense_1" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (128,)
hareem chughtai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.