Load saved model

i saved a model with metadata with tensorflow, but when i reload model it don’t run properly, the results are not good.

this is my model:

def train_model(x_train, y_train, num_nodes,num_nodes2,num_nodes3, dropout_prob, Olr,lr,es, batch_size, epochs):
  model = tf.keras.Sequential([tf.keras.layers.Embedding(vocab_length,embedding_dim,input_length = max_len,mask_zero=True),
          tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(num_nodes3,return_sequences = True)), #256
          tf.keras.layers.Dense(num_nodes2,activation = 'relu',activity_regularizer=tf.keras.regularizers.L2(0.01)),
          tf.keras.layers.Dense(num_nodes3,activation = 'softmax'),
          tf.keras.layers.Dense(num_classes,activation = 'sigmoid')])

  for layer in model.layers:
    print(f"Layer name: {layer.name}, Layer: {layer}")

  model.compile(loss = 'categorical_crossentropy',optimizer = tf.keras.optimizers.Adam(Olr),metrics = ['accuracy'])
  history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.2,callbacks = [lr, es])
  return model, history

model, history = train_model(x_train, y_train, num_nodes,num_nodes2,num_nodes3, dropout_prob, Olr,lr, es,batch_size, num_epochs)

and save it in this way:

def save_model_ext(model, filepath, overwrite=True, meta_data=None):
    save_model(model, filepath, overwrite)
    if meta_data is not None:
        f = h5py.File(filepath, mode='a')
        f.attrs['my_meta_data'] = meta_data

labels_string = json.dumps(CLASS_NAMES)
save_model_ext(model, 'MCT_FUMATORI_LSTM_250.h5', meta_data=labels_string)

and the accuracy is 0.9762

But when I reload model the accuracy is 0.45
I reload model in this way:

def load_model_ext(filepath, custom_objects=None):
    model = load_model(filepath, custom_objects=None)
    f = h5py.File(filepath, mode='r')
    meta_data = None
    if 'my_meta_data' in f.attrs:
        meta_data = f.attrs.get('my_meta_data')
    return model, meta_data

model, loaded_labels_string = load_model_ext(modelpath)

Can you help me?
Thank you

Hi @Francesca_Pisani . Your code is not is not easy to read given how it is rendered in your question.
I don’t quite see where is save model() defined (this line save_model(model, filepath, overwrite)).
I may be wrong, but it doesn’t look you are not using built-in Tensorflow/Keras to save and load factories.
Did you read this blog: Save and load models (Сохранение и загрузка моделей  |  TensorFlow Core)?

HI @Francesca_Pisani , I’ve formatted your code just to make it easier to read

also, can you test your save/load without adding the metadata just to see if it affects the results you are getting? (just for testing)

As a last detail, the h5 format is a legacy format, it would be better to save as a .keras

there are these two docs to help:

Saving a model: Save, serialize, and export models  |  TensorFlow Core

Saving additional assets: Customizing Saving and Serialization  |  TensorFlow Core

hope this helps

1 Like