简体   繁体   中英

keras.models.load_model takes too long to import

I have a python project where I import keras.models.load_model:

from keras.models import load_model

This causes my project to launch in 3 to 4 seconds, how can I decrease the import time?

Based on the benchmarks from this article , the format in which you save your weights affects how fast your model instantiates and loads saved weights.

It seems that the .h5 format is faster than the SavedModel format. However, if speed is really paramount to the function of your project then you may want to look at taking one of these steps, although it definitely involves some work:

  1. Reducing the precision of your model
  2. Reducing your model size by pruning weights
  3. Using TensorFlow lite (however note that some models aren't supported)

Agree with @evantkchong.

h5 format is a light-weight alternative to SavedModel. As it is light weight, it loads faster than SavedModel or tf format. However, there are limitations that are important to consider.

If your model is simple, saving in h5 format and loading it is better option. But, if you have complex custom model, then SavedModel / tf format is better.

The following is from Tensorflow website

  • External losses & metrics added via model.add_loss() & model.add_metric() are not saved (unlike SavedModel). If you have such losses & metrics on your model and you want to resume training, you need to add these losses back yourself after loading the model. Note that this does not apply to losses/metrics created inside layers via self.add_loss() & self.add_metric(). As long as the layer gets loaded, these losses & metrics are kept, since they are part of the call method of the layer.
  • The computation graph of custom objects such as custom layers is not included in the saved file. At loading time, Keras will need access to the Python classes/functions of these objects in order to reconstruct the model. See Custom objects.

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