Skip to content

Instantly share code, notes, and snippets.

@Tony607
Created May 19, 2019 12:22
Show Gist options
  • Save Tony607/134537ee3c6a4837faffb0c95e835e6c to your computer and use it in GitHub Desktop.
Save Tony607/134537ee3c6a4837faffb0c95e835e6c to your computer and use it in GitHub Desktop.
How to compress your Keras model x5 smaller with TensorFlow model optimization | DLology
import tempfile
import zipfile
_, new_pruned_keras_file = tempfile.mkstemp(".h5")
print("Saving pruned model to: ", new_pruned_keras_file)
tf.keras.models.save_model(final_model, new_pruned_keras_file, include_optimizer=False)
# Zip the .h5 model file
_, zip3 = tempfile.mkstemp(".zip")
with zipfile.ZipFile(zip3, "w", compression=zipfile.ZIP_DEFLATED) as f:
f.write(new_pruned_keras_file)
print(
"Size of the pruned model before compression: %.2f Mb"
% (os.path.getsize(new_pruned_keras_file) / float(2 ** 20))
)
print(
"Size of the pruned model after compression: %.2f Mb"
% (os.path.getsize(zip3) / float(2 ** 20))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment