I'm getting this error when trying to load the model I trained. Can you help?

This is the code snippet I use:
#load model

model = model_from_json(open(“/content/sample_data/trained_tf_model.json”, “r”).read())

#load weights

model.build(input_shape=(None, 224, 224, 3))

model=model.load_weights(“/content/sample_data/trained_tf_model.h5”)
The error I get is this:
ValueError: Layer count mismatch when loading weights from file. Model expected 0 layers, found 3 saved layers.

Hi @Bayram_Gulcan, This error occurs due to mismatch of model layers and weights you are trying to load. For example, if i save the model with one architecture and then try to assign those weights to another model with a different architecture then this error occurs.

model=keras.Sequential([
                       keras.Input(shape=(28,28,1)),
                       keras.layers.Conv2D(32,kernel_size=(3,3),activation='relu',),
                       keras.layers.MaxPooling2D(pool_size=(2,2)),
                       keras.layers.Conv2D(64,kernel_size=(3,3),activation='relu'),
                       keras.layers.MaxPooling2D(pool_size=(2,2)),
                       keras.layers.GlobalAveragePooling2D(),
                       keras.layers.Dropout(0.5),
                       keras.layers.Dense(10,activation='softmax')
])

If i try to assign above model weights to another model having different architecture

model=keras.Sequential([
                        keras.Input(shape=(28,28,1)),
                        keras.layers.Conv2D(32,kernel_size=(3,3),activation='relu',),
                        keras.layers.MaxPooling2D(pool_size=(2,2)),
                        keras.layers.Conv2D(64,kernel_size=(3,3),activation='relu'),
                       #maxpooling was removed here
                        keras.layers.GlobalAveragePooling2D(),
                        keras.layers.Dropout(0.5),
                        keras.layers.Dense(10,activation='softmax')
])

Then the error occurs. Thank You.

Thank you very much. @Kiran_Sai_Ramineni . For your answer. Respects.

The error you’re encountering, ValueError: Layer count mismatch when loading weights from file. Model expected 0 layers, found 3 saved layers, suggests that there’s a discrepancy between the architecture of the model you’re trying to load and the weights file. This can happen for a few reasons, but it’s often due to the model not being properly defined or constructed before attempting to load the weights.

When you use model_from_json to load a model architecture from a JSON file, the model’s architecture should be completely defined by the JSON content. After loading the architecture, you need to compile the model before you can successfully load the weights with load_weights. However, you don’t need to build the model explicitly since loading the architecture from JSON should already define the input shape and the entire model structure.

Here’s a revised version of your code snippet that should work:

pythonCopy code

from tensorflow.keras.models import model_from_json

# Load model architecture from JSON file
with open("/content/sample_data/trained_tf_model.json", "r") as json_file:
    model_json = json_file.read()

model = model_from_json(model_json)

# Compile the model (necessary if you plan to train or evaluate the model; adjust according to your original compile parameters)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Load weights into the model
model.load_weights("/content/sample_data/trained_tf_model.h5")