简体   繁体   中英

keras > always the same prediction value after loading saved model

I am training some model via keras with tensorflow backend.

When I call predict right after training on the same object it works fine and gives different values for different inputs. But when I save the model to a file, then load it from another python session, predict always returns the same value for different inputs.

I use ModelCheckpoint for saving the model, then load_model for loading. I have also tried to save and load architecture separately to a json file with to_json and model_from_json functions. Sample code:

Saving part

with open("model.json", "w") as textFile:
   print(model.to_json(), file = textFile)

model.fit(X_train, y_train, epochs=iterationCount, batch_size=64, validation_split=0.2, callbacks = [ModelCheckpoint(filepath='model.h5', verbose=0, save_best_only=True)])

Loading part

with open('model.json') as json_file:
    model = model_from_json(json_file.read())

model.load_weights('model.h5')

Any ideas to solve this? Is there anything that I am missing?

Unfortunately many people (like me) have been complaining on a keras bug that affects the save_weights and load_weights functions.

I ended up avoiding to use these function. If I want to load/store my model I just do the following:

from keras.models import load_model

trained_model.save(file_path)
loaded_model = load_model(file_path)

The save functions saves your weights, but also your network structure and the state of the optimizer (it makes sure that you can keep training model from exactly where you paused).

keras model save/load has two ways (two ways I feel comfortable to use). Saving both model structure and weights model.save(file_path) and only save model weights model.save_weights(file_path) . So I save/load model like following:

from keras.models import load_model
trained_model.save(file_path)
loaded_model = load_model(file_path)

trained_model.save_weights(file_path)
# We have to define model first before loading weights
trained_model.load_weights(file_path)

Here, load_weights(file_path, by_name=True) allows you to load weights into a different structure with some layer names in common.

I had the same issue, I solved by setting a fixed seed to tensorflow, numpy and python.

import tensorflow as tf
import numpy as np
import random as python_random
tf.random.set_random_seed(42)
np.random.seed(42)
python_random.seed(42)

Be careful! Different versions of tensorflow may required a different way to set a seed!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM