Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extremely slow model load with keras

I have a set of Keras models (30) that I trained and saved using:

 model.save('model{0}.h5'.format(n_model))

When I try to load them, using load_model, the time required for each model is quite large and incremental. The loading is done as:

models = {}
for i in range(30):
    start = time.time()
    models[i] = load_model('model{0}.h5'.format(ix)) 
    end = time.time()
    print "Model {0}: seconds {1}".format(ix, end - start)

And the output is:

...
Model 9: seconds 7.38966012001
Model 10: seconds 9.99283003807
Model 11: seconds 9.7262301445
Model 12: seconds 9.17000102997
Model 13: seconds 10.1657290459
Model 14: seconds 12.5914049149
Model 15: seconds 11.652477026
Model 16: seconds 12.0126030445
Model 17: seconds 14.3402299881
Model 18: seconds 14.3761711121
...

Each model is really simple: 2 hidden layers with 10 neurons each (size ~50Kb). Why is the loading taking so much and why is the time increasing? Am I missing something (e.g. close function for the model?)

SOLUTION

I found out that to speed up the loading of the model is better to store the structure of the networks and the weights into two distinct files: The saving part:

model.save_weights('model.h5')
model_json = model.to_json()
with open('model.json', "w") as json_file:
    json_file.write(model_json)
json_file.close()

The loading part:

from keras.models import model_from_json
json_file = open("model.json", 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights("model.h5")
like image 334
Titus Pullo Avatar asked Nov 23 '17 12:11

Titus Pullo


3 Answers

I solved the problem by clearing the keras session before each load

from keras import backend as K
for i in range(...):
  K.clear_session()
  model = load_model(...)
like image 157
GearLux Avatar answered Nov 13 '22 22:11

GearLux


I tried with K.clear_session(), and it does boost the loading time each time.
However, my models loaded in this way are not able to use model.predict function due to the following error:
ValueError: Tensor Tensor("Sigmoid_2:0", shape=(?, 17), dtype=float32) is not an element of this graph.
Github #2397 provide a detailed discussion for this. The best solution for now is to predict the data right after loading the model, instead of loading a dozens of models at the same time. After predicting each time you can use K.clear_session() to release the GPU, so that next loading won't take more time.

like image 32
Wentai Chen Avatar answered Nov 13 '22 22:11

Wentai Chen


Although I'm too late to the party, on Google's Facenet (89MB), I have got some interesting results as follows,

I tried every option mentioned in the above answers, But found out that Keras in Tensorflow is slightly faster than vanilla Keras, and the results might be better on a stronger CPU.

My laptop is really old, with a configuration of 4th gen i5 (4120U), 8GB 1600MHz DDR3, and a normal SATA SSD, and I'm using Tensorflow 1.15.2 CPU version.

# Old method -- 16 to 17 seconds
from keras import backend as K
K.clear_session()
model = load_model('models/facenet_v1.h5', compile=False)
#------------------------------------------------------

# Method 1 -- 16 to 18 seconds 
from keras import backend as K
K.clear_session()
with open('models/facenet_v1.json', 'r') as j:
    json_model = j.read()

model = model_from_json(json_model)
model.load_weights('models/facenet_v1_weights.h5')
#------------------------------------------------------

# Method 2 -- 9 to 11 seconds -> Best
tf.keras.backend.clear_session()
model = tf.keras.models.load_model('models/facenet_v1.h5', compile=False)

And apart from this definitely, you'll get much better results if you have a GPU.

like image 2
Omkar Avatar answered Nov 14 '22 00:11

Omkar