I want to send the Keras model after training to another python function which is saved in another python file? How can I send the model as an argument? Thank you.
If I understand it correctly, you want to transfer the model created in script A to script B so it can be used there.
To my experience, the easiest way of using a Keras model in a different script, is to save the model to disk as a file. As described here in the Keras docs:.
from keras.models import load_model
model.save('my_model.h5') # creates a HDF5 file 'my_model.h5'
del model # deletes the existing model
# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')
Passing on the model to a different Python file (i.e. via a commandline argument), can be then be done by passing the filename of where you saved that model to the second script. This script can then load the model from disk and use it.
In case you only have 1 model at a time, you could pick a filename and hardcode it into your functions. For example:
Script A
# assuming you already have a model stored in 'model'
model.save('my_stored_model.h5')
Script B (which accesses the saved model)
from keras.models import load_model
def function_a():
model = load_model('my_stored_model.h5')
return model.predict(...)
Related
I have defined a deep learning model my_unet() in tensorflow. During training I set save_weigths=False since I wanted to save the entire model (not only the wieghts bu the whole configuration). The generated file is path_to_model.hdf5.
However, when loading back the model I used the earlier version (I forgot to update it) in which I first called the model and then load the model using:
model = my_unet()
model.load_weights(path_to_model.hdf5)
Instead of simply using: model = tf.keras.models.load_model(path_to_model.hdf5) to load the entire model.
Both ways of loading the model and the weights provided the same predictions when run in some dummy data and there were no errors.
My question is: Why loading the entire model using model.load_weights() does not generate any problem? What is the structure of the hdf5 file and how theese two ways of loading exactly work? Where can I find this information?
You can please see the documentation here for any future reference: http://davis.lbl.gov/Manuals/HDF5-1.8.7/UG/03_DataModel.html
I have a tensorflow Functional keras model made up of 2 separate Sequential models like so:
I want to deploy the model with tensorflow-serving and therefore need to save the model in 'tf' format. When I save the model, it creates the new model directory with the correct files and directories (assets, saved_model.pb, variables). However, when I try to re-load the model using model = tf.keras.models.load_model('./my_model_v1') it will not load. I don't get any error message but it just seems to sit there. I have left it run for over 30 minutes and nothing happens.
When I save the model in h5 format, it will load in seconds. Likewise, saving the individual sequential models in 'tf' format and then subsequently loading completes in a matter of seconds.
Why can I not load a model made up of these layers?
My impression is that it only saves the model's architecture, so I should be able to call it before I start training? And then save_weights() saves the weights I need to restore the model? Any more details on this?
At what stage can I call to_json()? I.e. do I have to call compile() first? Can it be before fit() ?
As mentioned in Keras docs it only saves the architecture of the model:
Saving/loading only a model's architecture
If you only need to save the architecture of a model, and not its
weights or its training configuration, you can do:
# save as JSON
json_string = model.to_json()
# save as YAML
yaml_string = model.to_yaml()
The generated JSON / YAML files are human-readable and can be manually
edited if needed.
You can then build a fresh model from this data:
# model reconstruction from JSON:
from keras.models import model_from_json
model = model_from_json(json_string)
# model reconstruction from YAML
from keras.models import model_from_yaml
model = model_from_yaml(yaml_string)
I have saved a keras model as a h5py file and now want to load it from disk.
When training the model I use:
from keras.models import Sequential
model = Sequential()
H = model.fit(....)
When the model is trained, I want to load it from disk with
model = load_model()
How can I get H from the model variable? It unfortunately does not have a history parameter that I can just call. Is it because the save_model function doesn't save history?
Unfortunately it seems that Keras hasn't implemented the possibility of loading the history directly from a loaded model. Instead you have to set it up in advance. This is how I solved it using CSVLogger (it's actually very convenient storing the entire training history in a separate file. This way you can always come back later and plot whatever history you want instead of being dependent on a variable you can easily lose stored in the RAM):
First we have to set up the logger before initiating the training.
from keras.callbacks import CSVLogger
csv_logger = CSVLogger('training.log', separator=',', append=False)
model.fit(X_train, Y_train, callbacks=[csv_logger])
The entire log history will now be stored in the file 'training.log' (the same information you would get, by in your case, calling H.history). When the training is finished, the next step would simply be to load the data stored in this file. You can do that with pandas read_csv:
import pandas as pd
log_data = pd.read_csv('training.log', sep=',', engine='python')
From here on you can treat the data stored in log_data just as you would by loading it from K.history.
More information in Keras callbacks docs.
Using pickle to save the history object threw a whole host of errors. As it turns out you can instead use pickle on H.history instead of H to save your history file!
Kind of annoying having to have a model and a history file saved, but whatever
I want to distribute my trained deep-learning model which is made using Keras.
The partner want to use it without complicated environment construction, such as installing python, keras, etc...
How can I do this? Should I make an .exe file?
Maybe you want to look into this tutoiral.
Simplest code possible:
from keras.models import load_model
model.save('my_model.h5') # creates a HDF5 file 'my_model.h5'
# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')