Keras 3.3.3 Unexpected error when loading the .h5 model #19967
-
On Windows 10 I encountered an error when loading the .h5 model, which complained that:
But the concatenated layers did have the matched sizes and the axis dimension. After the training, the model was saved using the keras.models.Model.save method. This error is consistently reproducible, for the backend of either 'torch' or 'tensorflow'. So I wrote a simple test script. To provide more context of this issue, we have had many legacy .h5 model files in our application, using Tensorflow 2.x (<= 2.14). We have to load them for the back-compatibility. As Keras 3+ recommended, we may convert and re-save them as .keras files in future. ` import keras.ops as kops from keras.layers import BatchNormalization, Concatenate, Conv2D, Dense, Flatten, Input TEST_INPUT_CHANNEL = 3 def make_dummy_model():
def f_dummy(x): if "main" == name:
` The following is the full stack trace output.
For the dummy model summary, I'm also attaching the screenshot of the output. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
I'm not sure what happened with the python code part, which is segmented in the post. My apologies if I had mis-used the editing tools in the discussion board. |
Beta Was this translation helpful? Give feedback.
Hello Jeff,
Ah, I understand your concerns. The choice of
.h5
format for saving models is rather weird. Technically,.h5
format is supposed to ONLY save the model weights (which is likely some tensors, matrices, compressed using various techniques, sparsity, etc.). You can load an.h5
model ONLY if you have the architecture and the preset available! For example - The model architecture and exact presets define a number of layers and their specifications. If in that case, you have those specifications, then you need a function to define an empty model for you (Let's say -make_test_model()
) to return the Keras Model with empty/random initialized weights and then loadh5
over them.In your …