Created
May 19, 2019 12:20
-
-
Save Tony607/b1a9d4d1afda06d8b2cae43ce65f8fbf to your computer and use it in GitHub Desktop.
How to compress your Keras model x5 smaller with TensorFlow model optimization | DLology
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import tensorflow as tf | |
from tensorflow_model_optimization.sparsity import keras as sparsity | |
# Backend agnostic way to save/restore models | |
# _, keras_file = tempfile.mkstemp('.h5') | |
# print('Saving model to: ', keras_file) | |
# tf.keras.models.save_model(model, keras_file, include_optimizer=False) | |
# Load the serialized model | |
loaded_model = tf.keras.models.load_model(keras_file) | |
epochs = 4 | |
end_step = np.ceil(1.0 * num_train_samples / batch_size).astype(np.int32) * epochs | |
print(end_step) | |
new_pruning_params = { | |
'pruning_schedule': sparsity.PolynomialDecay(initial_sparsity=0.50, | |
final_sparsity=0.90, | |
begin_step=0, | |
end_step=end_step, | |
frequency=100) | |
} | |
new_pruned_model = sparsity.prune_low_magnitude(loaded_model, **new_pruning_params) | |
new_pruned_model.summary() | |
new_pruned_model.compile( | |
loss=tf.keras.losses.categorical_crossentropy, | |
optimizer='adam', | |
metrics=['accuracy']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment