Extremely slow model load with keras Extremely slow model load with keras python python

Extremely slow model load with keras


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

from keras import backend as Kfor i in range(...):  K.clear_session()  model = load_model(...)


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.


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 secondsfrom keras import backend as KK.clear_session()model = load_model('models/facenet_v1.h5', compile=False)#------------------------------------------------------# Method 1 -- 16 to 18 seconds from keras import backend as KK.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 -> Besttf.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.