Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot load fine-tuned model #19518

Open
lwind18 opened this issue Apr 15, 2024 · 4 comments
Open

cannot load fine-tuned model #19518

lwind18 opened this issue Apr 15, 2024 · 4 comments
Assignees
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@lwind18
Copy link

lwind18 commented Apr 15, 2024

I train the model from a scratch and name it as modelA. Then use the new training data to train this trained model (modelA). The training process looks good. Then when I load the model using tf.keras.models.load_model, I ran into this issue:
TypeError: Layer input_spec must be an instance of InputSpec. Got: <keras.initializers.initializers_v2.GlorotUniform object at 0x7f6c285f0130>
Does anyone can help me out with this issue? Where is wrong with this fine-tuned model? I remembered that I didn't use GlorotUniform when I built the model.
Thanks!

@SuryanarayanaY SuryanarayanaY added the type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. label Apr 16, 2024
@SuryanarayanaY
Copy link
Collaborator

Hi @lwind18 ,

For me it seems the issue is with version mismatch. Could you please provide more details like on which Keras version earlier model was trained and which version you are trying to reload? Right now Keras3 designed to support multiple backends there might be design changes.

@lwind18
Copy link
Author

lwind18 commented Apr 17, 2024

The model trained with keras 2.6 an also tensorflow 2.6. Then I tried load this model under keras 2.6 and tensorflow 2.6, and I tried 2.8, 2.9, 2.10, 2.11 and they ended with the same error.
So you suggestion is to update keras and tensorflow to 2.13 or later, and train the model again. Then it shouldn't have this issue?

@SuryanarayanaY
Copy link
Collaborator

The model trained with keras 2.6 an also tensorflow 2.6. Then I tried load this model under keras 2.6 and tensorflow 2.6, and I tried 2.8, 2.9, 2.10, 2.11 and they ended with the same error.

If you have trained and loading the model in same version then it should load without any error. If there are versions mismatches wrt training and reloading the model then such problem may arise.

Please double check your environment details while reloading the model its pointing to correct version of TF/Keras that the model trained. Please submit a minimal code snippet for reproduction.

@lwind18
Copy link
Author

lwind18 commented Apr 22, 2024

The model trained with keras 2.6 an also tensorflow 2.6. Then I tried load this model under keras 2.6 and tensorflow 2.6, and I tried 2.8, 2.9, 2.10, 2.11 and they ended with the same error.

If you have trained and loading the model in same version then it should load without any error. If there are versions mismatches wrt training and reloading the model then such problem may arise.

Please double check your environment details while reloading the model its pointing to correct version of TF/Keras that the model trained. Please submit a minimal code snippet for reproduction.

I used the newer tensorflow version (higher than v2.11) to train the model and load it. It gave me the same error.
Here is the snippet of code:
model_path = './trained_model.model'
traind_model = tf.keras.models.load_model(model_path, compile= False)
loss = myloss.multiclass_weighted_cross_entropy(num_classes, class_weights=weights,is_logits=True)
metrics = [tf.keras.metrics.SparseCategoricalAccuracy()]
learning_rate = 0.0001
optimizer=tf.keras.optimizers.RMSprop(learning_rate=learning_rate, rho=momentum )
model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
model_history = traind_model.fit(train_dataset, validation_data=validation_dataset, epochs=EPOCHS, verbose=2)
model.save(model_name)
ERROR message:
Traceback (most recent call last):
File "", line 1, in
File "./python/3.11/envs/rasteriobak/lib/python3.11/site-packages/keras/src/saving/saving_api.py", line 262, in load_model
return legacy_sm_saving_lib.load_model(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "./python/3.11/envs/rasteriobak/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "./python/3.11/envs/rasteriobak/lib/python3.11/site-packages/keras/src/engine/base_layer.py", line 1295, in input_spec
raise TypeError(
TypeError: Layer input_spec must be an instance of InputSpec. Got: <keras.src.initializers.initializers.GlorotUniform object at 0x1553943ab610>
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

2 participants