-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
model.save and load giving different result #4875
Comments
I'm having a similar problem, but it has to do with setting stateful=True. If I do that, the prediction from the original model is different from the prediction of the saved and reloaded model. `# DEPENDENCIES from keras.models import Sequential TRAINING AND VALIDATION FILESxTrain = np.random.rand(200,10) ADD 3RD DIMENSION TO DATAxTrain = xTrain.reshape(len(xTrain), 1, xTrain.shape[1]) CREATE MODELmodel = Sequential() model.compile(loss="mean_squared_error", optimizer="rmsprop") PREDICT RESULTS ON VALIDATION DATAyFit = model.predict(xVal, batch_size=10, verbose=1) SAVE MODELmodel.save('my_model.h5') RELOAD MODELfrom keras.models import load_model DO IT AGAINdel model |
Same problem. Is there sth wrong with the function "save model"? |
I am having the exact same issue. Does anyone know exactly what the problem is? |
Same issue using json format for saving a very simple model. I get different results on my test data before and after save/loading the model. classifier = train(model, trainSet, devSet) TEST BEFORE SAVINGtest(model, classifier, testSet) save model to jsonmodel_json = classifier.to_json() load model fron jsonjson_file = open('../data/local/SICK-Classifier', 'r') TEST AFTER SAVINGtest(model, classifier, testSet) |
I crosschecked all these functions - they seem to be working properly. |
I ran into a similar issue. After saving my model, the weights were changed and my predictions became random. For my own case, it came down to how I was mixing vanilla Tensorflow with Keras. It turns out that Keras implicitly runs tf.global_variables_initializer if you don't tell it that you will do so manually. This means that in trying to save my model, it was first re-initializing all of the weights. The flag to prevent Keras from doing this is _MANUAL_VAR_INIT in the tensorflow backend. You can turn it on like this, before training your model:
Hope this helps! |
Hi @kswersky, Thanks for your answer. I am using Keras 2.0 with Tensorflow 1.0 setup. I am building model in Keras and using Tensorflow pipeline for training and testing. When you load the keras model, it might reinitialize the weights. I avoided tf.global_variables_initializer() and used load_weights('saved_model.h5'). Then model got the saved weights and I was able to reproduce correct results. I did not have to do the _manual_var_init step. (its a very good answer for just Keras) May be I confused people who are just using Keras. |
Use model.model.save() instead of model.save() |
I'm stuck with the same problem. The only solution for now is move to python 2.7 ? @pras135 , if I do as you suggest I cannot perform model.predict_classes(x), AttributeError: 'Model' object has no attribute 'predict_classes' |
Same issue here. When I load a saved model my predictions are random. |
model_1 = model.model.save('abcd.h5') # save the model as abcd.h5 Hope it helps. |
@pras135 What you suggested is in the same session, and it does work. Unfortunately I need this to work in separate sessions, and if you do the following: (in first python session) I receive the following error: AttributeError: 'Model' object has no attribute 'predict_classes' |
@lotempeledGong That should not happen. Check if |
@deeiip thanks, but this still doesn't work for me. However this is not my main problem here. What I want eventually is to train a model, save it, close the python session and in a new python session load the trained model and obtain the same accuracy. Currently when I try to do this, the loaded model gives random predictions and it is as though it wasn't trained. |
@lotempeledGong I'm facing exactly the same issue you refer here. However, I'm using Tensorflow 1.1 and TFlearn 0.3 on Windows10 with Python 3.5.2. |
@deeiip have you solved this problem? how to? |
@kswersky l add the from keras.backend import manual_variable_initialization |
@HarshaVardhanP how to avoided tf.global_variables_initializer() before load model? |
@chenlihuang Now, I am using tf.global_variables_initializer(). It will init net variables. And use load_weights() to load the saved weights. This seems easier for me than using load_model(). |
@HarshaVardhanP can you give some ideas for me on the keras level . l don't care the backend on TF. |
EDIT: i've noticed loading my model doesnt give different results so I guess I dont need the workaround. |
Unfortunately, I've run into the same issue that many others on here seem to have encountered -- I've trained what seems to be an extremely powerful text classifier (based on cross-validation, at least, with a healthy-sized dataset), but upon loading a saved model -- either using load_model or model.load_weights -- my model's performance is now completely worthless when tested in a new session. There's absolutely no way this is the same model that I trained. I've tried every suggestion offered here, but to no avail. Super disheartening to see what appeared to be such good results and have no ability to actually use the classifier in other sessions/settings. I hope this gets addressed soon. (P.S. I'm running with Keras 2.0.4 with Tensorflow 1.1.0 backend on Python 3.5.) |
@gokceneraslan @fchollet Many of us are facing this issue. Could you please take a look ? Thanks! |
I do not see any issue with model serialization using the save_model() and load_model() functions from the latest Tensorflow packaged Keras. For example:
|
Hi @kevinjos The issue is not with using the saved model in the same session. If i save a model from session 1 and load it in session 2 and use two exactly the same data to perform inference, the results are different. To be more specific, the inference results from the session in which the model was built is much better compared to results from a different session using the same model. How is tensorflow packaged keras different from vanilla keras itself ? |
@Chandrahas1991 When I run a similar code as above with a fresh tf session I get the same results. Have you tried the suggestion offered by @kswersky to set a flag to prevent automatic variable initialization? Could the issue with serialization apply only to LSTM layers? Or more specifically stateful LSTM layers? Have you tried using only the Keras code packaged with TensorFlow?
|
There are already tests in Keras to check if model saving/loading works. Unless you write a short and fully reproducible code (along with the data) and fully describe your working environment (keras/python version, backend), it's difficult to pinpoint the cause of all issues mentioned here. I've just sent another PR to add more tests about problems described here. @deeiip can you check #7024 and see if it's similar to the code that you use to reproduce this? With my setup, (keras master branch, python 3.6, TF backend) I cannot reproduce any model save/load issues with either mlps or convlstms, even if I restart the session in between. Some people mentioned reproducibility problems about stateful RNNs. As far as I know, RNN states are not saved via |
Well I have made it work for me. The issue presented itself only when I had a vectorization layer inside my model. I finally had to export the vectorization layer separately, and being careful to export it as a ragged tensor (check this out). Then I reconstructed the model, loading weights for vectorization layer and inner model, and all good! Hope this helps |
I have encountered the same issue as well, and none of the solutions suggested work for me. Has there been a fix yet? |
Did anyone inspect the weights themselves to see if they changed as they were loaded in memory to see if they changed between different calls to loading the saved model? |
I had the identical problem like 99% of you. But since it turns out that in most of the cases the cause of the problem is different, I thought to share how I fixed mine. |
@popkristina Yes, this is the problem I found a long time ago. Python deliberately makes sets and dictionaries use randomized orderings per creation, because it is so easy to write code that accidentally depends on the enumeration order of a particular set or dict. Keras now has text 'preprocessing' layers to do this enumeration in a way that saves the enumeration order into the model. |
`base_model = Arch(32, outdim=8, t=32, dropout=0.1) x = np.random.uniform(0, 1, 2 * 32 * 160 * 160 * 3) basey = base_model.predict(x) loaded_model = tf.keras.models.load_model('./base_model') I tested the above code with a trained model, I realize I don't get identical output but very close result Base ModelPREDICTIONS Loaded ModelPREDICTIONS |
I still get the issue. Is there a solution for this? |
I got it right with checkpoints, here |
i think the problem lay in data Transformation |
I get the same issue, 5 years later |
I'm also dealing with this issue. FYI, I've created a detailed description of what I'm dealing with in this StackOverflow EDIT: FYI, I recently discovered that my problem was caused by a bug on my side, not Keras. |
Even I'm facing this issue when I'm trying to save and load a model with custom layers using TF SavedModel format. I've explained the issue in the detail here Sample colab to reproduce issue Tensorflow version - 2.9.1 |
I'm facing the same issue too |
Maybe we should add some sanity check for |
I've had the same problem and changed two things in my jupyter notebook:
also I tried the consequences of calling
Apparently all three models gave the same results (in another Jupyter Notebook / Kernel) as the training / validation / test predictions directly after training. |
hi ,i followed the same steps as you mentioned above ,it works fine in the same teriminal but when i restart the terminal or open the new jupyter notebook for testing it gives the random prediction only. |
I forgotten /=255 。。。。 |
Found out that the H5 file was the issue. We should save it in SavedModel format because of these limitations of the H5 file format: Solution (Don't include .h5): |
Still having this problem. I tried all the suggested solutions and they don't work. I built a model within Jupyter Notebook and saved it to my computer using model.save(). Then I run a python application which loads my model with load_model() and uses it for prediction. However, the predictions are entirely random. Something is still wrong with either saving or loading the model. Anyone find new solutions? |
i solved this problem in my case on keras ocr captcha predict model. (you can search it in google to see code) max_length = max([len(label) for label in raw_labels]) and full code become like this: images = sorted(list(map(str, list(data_dir.glob("*.png"))))) for predict on images which the name of them is different from the number in the image you have to set characters .which will be like this: data_dir = Path("captcha2") now you can try on your captcha images data and save model for use it in another session! |
If someone is still facing this issue. Here is the solution below: The problem is not in the saved model but in the character list that you are using to map number back to string. Everytime you restart the notebook, it resets the character list and when you load your model, it can't accurately map the numbers back to string. To resolve this issue you need to save character list. Please follow the below code.
Now when you map back the numbers to string after prediction, it will retain the original order and will predict accurately. If you still didn't understand the logic, you can watch my video in which I explained this project from scratch and resolved all the issues that you are facing. |
@furqan4545 Thanks for pointing that out, I thought my problem was due to the saving and loading process, but it turns out to be exactly what you mentioned. ^^ |
this was also my case... Saving: Loading: |
I faced a very similar issue: I save a model, I run a program that loads the model and uses it to make a prediction, and the prediction varies from run to run. Run the same code with the same model and same inputs three times in a row, get three different results. Unlike the OP, my results were all reasonable like a well-trained model would have given, but as a deterministic (but large) algebra problem, they should have been the same every time. I solved it! I don't know why this works.
Code that resulted in the same predictions each time the code was run:
I wrote two programs at different times and I had done the import statements differently. One gave repeatable BTW: my models were .keras format, saved with |
I tried many things.
|
I faced a similar issue and the only solution I found was to save the model in SaveModel format https://www.tensorflow.org/tutorials/keras/save_and_load#savedmodel_format. Weights are being reinitialized when the model is saved in .keras format. |
I am trying to save a simple LSTM model for text classification. The input of the model is padded vectorized sentences.
For saving I'm using the following snippet:
But whenever I'm trying to load the same model
I'm getting random accuracy like an untrained model. same result even when trying to load weights separately.
If I set the weights
like above. It is working if
model
andmodel2
are run under same session (same notebook session). If I serializelstmweights
and try to load it from different place, again I'm getting result like untrained model. It seems saving only the weights are not enough. So why model.save is not working. Any known point?The text was updated successfully, but these errors were encountered: