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.GlobalMaxPool1D(),
tf.keras.layers.Dropout(dropout_prob),
tf.keras.layers.Dense(num_nodes2,activation = 'relu',activity_regularizer=tf.keras.regularizers.L2(0.01)),
tf.keras.layers.Dropout(dropout_prob),
tf.keras.layers.Dense(num_nodes3,activation = 'softmax'),
tf.keras.layers.Dropout(dropout_prob),
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'])
model.summary()
tf.keras.utils.plot_model(model)
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
f.close()
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')
f.close()
return model, meta_data
model, loaded_labels_string = load_model_ext(modelpath)
Can you help me?
Thank you