Closed tirthasheshpatel closed 12 months ago
Can you print model.summary()
on each side (before saving/loading)?
Here's the output of model summary.
Model summary before saving:
Model: "my_model"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━┩
│ input_layer (InputLayer) │ (None, None, None, 3) │ 0 │
├────────────────────────────────────┼───────────────────────────────┼─────────────┤
│ conv2d (Conv2D) │ (None, None, None, 1) │ 4 │
└────────────────────────────────────┴───────────────────────────────┴─────────────┘
Total params: 4 (16.00 B)
Trainable params: 4 (16.00 B)
Non-trainable params: 0 (0.00 B)
Model summary before loading:
Model: "my_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, None, None, 3)] 0
conv2d (Conv2D) (None, None, None, 1) 4
=================================================================
Total params: 4 (16.00 Byte)
Trainable params: 4 (16.00 Byte)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
This is probably caused by a naming discrepancy. We should fix it. @nkovela1 do you have cycles to take a look?
Also -- as a workaround, you can do weights = model.get_weights(); new_model.set_weights(weights)
. This will work.
I found the cause -- basically some change in TF seems to have broken the save file namespace in tf.keras (introduction of a _layer_checkpoint_dependencies
path which overrides layers
. We have to fix this on the tf.keras side. Things are nominal in Keras Core.
Sure thing, I just created a Colab to inspect the h5 file and found that same discrepancy with _layer_checkpoint_dependencies
: https://colab.sandbox.google.com/drive/1Ir1AQp6DUtYXk-nomRVgjM11ukTPXnnt
This is now fixed at HEAD but the issue will persist in TF 2.13 and TF 2.14. Use get_weights()
/set_weights()
as a workaround.
Sounds good, thanks for the quick fix!
I'm not following. Would you mind elaborating on where I should insert the code for the get_weights()/set_weights() workaround. I am using a script to load and use the model and that's wear the issue is happening.
`import keras import numpy as np import tensorflow as tf from keras.models import load_model import matplotlib.pyplot as plt
class_names = ['apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum', 'carrot', 'cauliflower', 'chilli pepper', 'corn', 'cucumber', 'eggplant', 'garlic', 'ginger', 'grapes', 'jalepeno', 'kiwi', 'lemon', 'lettuce', 'mango', 'onion', 'orange', 'paprika', 'pear', 'peas', 'pineapple', 'pomegranate', 'potato', 'raddish', 'soy beans', 'spinach', 'sweetcorn', 'sweetpotato', 'tomato', 'turnip', 'watermelon']
image_path = "download.jpg"
model = keras.models.load_model("TrainedModel.keras")
img = tf.keras.utils.load_img( image_path, target_size=(180, 180) ) img_array = tf.keras.utils.img_to_array(img) img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array) score = tf.nn.softmax(predictions[0])
print( "This image most likely belongs to {} with a {:.2f} percent confidence." .format(class_names[np.argmax(score)], 100 * np.max(score)) ) `
Saving doesn't work between
tf.keras
andkeras_core
. More specifically, weights saved inkeras_core
don't load intf.keras
and vice versa.Here's a MRE:
Saving Script:
Model summary before saving:
Loading script:
Model summary before loading:
Error:
Versions: