简体   繁体   中英

How to reduce size of keras saved model?

Then I'm building models on base of pretrained models from tf-hub or keras.applications (I'm using the same convolutional core) size of the resulting model file(s) size after model.save() or tf.saved_model.save is about 10 Mb. After I'm trained it model size increased to 30Mb!

If I save the model after training, recreate the model again and load weights from the trained model it will predict as expected and have file(s) size about 10Mb.

Code I'm using to create model.

MODULE_HANDLE = 'https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/4'
model = tf.keras.Sequential([
    hub.KerasLayer(MODULE_HANDLE, trainable=True),    
    tf.keras.layers.Dense(train_gen.num_classes, activation='softmax')
    ])
model.build((None,)+IMAGE_SIZE)

I expect that this is related to saving training information, such as the optimizer state. So, is there the right way to save the model for inference, without that save-recreate-load_weights trick?

Try setting include_optimizer=False in tf.keras.Model.save() or tf.keras.models.save_model() , respectively.

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