You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pruning models from tf.keras.applications (efficientnet, resnet etc) #656
I am finetuning the tf.keras.applications model on my data for the problem of image classification. I was able to complete training and test results look good. In order to deploy the model, I wanted to perform some pruning and optimization. I tried following the Keras Pruning tutorial, but I could not figure out how to enable pruning on the efficientnet layer. If we could have a tutorial on this or a worked out example, that would be great, thank you!
Can you follow the example of pruning the whole model and try prune_low_magnitude(your model)? Then post the model summary or let us know if you run into any issues. Thanks!
Thank you @liyunlu0618 , I have tried following the tutorial, here is the model.summary() and the error I get on using prune_low_magnitude() on my model: model.summary()
Error ValueError: Please initialize 'Prune' with a supported layer. Layers should either be a 'PrunableLayer' instance, or should be supported by the PruneRegistry. You passed: <class 'tensorflow.python.keras.engine.functional.Functional'>
Thank you @liyunlu0618 , I am trying to get the tf-nightly installed and running. (never used tf-nightly before, always used stable)
Here is what I am doing:
Uninstall current stable tensorflow-2.4.1 (pip uninstall tensorflow)
@liyunlu0618 Not sure how exactly, but now the tensorflow-nightly seems to be installed correctly. (I made sure by printing the version of tensorflow)
I am still getting the same error. It seems like there might be difference between the <class 'tensorflow.python.keras.engine.functional.Functional'> objects and Recursive sequential layers object(I peeked into the PR). Also I am wondering that, since Pruning is a part of tensorflow_model_optimization, do I have to take any additional steps to update that to nightly as well?
Here is my code snippet and error log for reference. It is unchanged from my last comment:
importtensorflow_model_optimizationastfmotprint("Tensorflow_version : ",tf.__version__)
prune_low_magnitude=tfmot.sparsity.keras.prune_low_magnitude# Compute end step to finish pruning after 2 epochs.batch_size=128epochs=2end_step=np.ceil(TRAIN_IMG_COUNT/batch_size).astype(np.int32) *epochs# Define model for pruning.pruning_params= {
'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(initial_sparsity=0.50,
final_sparsity=0.80,
begin_step=0,
end_step=end_step)
}
model_for_pruning=prune_low_magnitude(model, **pruning_params)
# `prune_low_magnitude` requires a recompile.model_for_pruning.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy,
metrics=['accuracy'])
Output Log (clipped):
Tensorflow_version : 2.6.0-dev20210407
ValueError Traceback (most recent call last)
<ipython-input-16-cf5cd8efdcbd> in <module>
23
24
---> 25 model_for_pruning = prune_low_magnitude(model, **pruning_params)
26
27 # `prune_low_magnitude` requires a recompile.
.
.
(Trimmed)
.
.
ValueError: Please initialize `Prune` with a supported layer. Layers should either be a `PrunableLayer` instance, or should be supported by the PruneRegistry. You passed: <class 'tensorflow.python.keras.engine.functional.Functional'>
Activity
Xhark commentedon Apr 6, 2021
Hi liyunlu0618@, Would you please take a look?
liyunlu0618 commentedon Apr 6, 2021
Can you follow the example of pruning the whole model and try prune_low_magnitude(your model)? Then post the model summary or let us know if you run into any issues. Thanks!
ameyparanjape commentedon Apr 6, 2021
Thank you @liyunlu0618 , I have tried following the tutorial, here is the model.summary() and the error I get on using prune_low_magnitude() on my model:
model.summary()Error
ValueError: Please initialize 'Prune' with a supported layer. Layers should either be a 'PrunableLayer' instance, or should be supported by the PruneRegistry. You passed: <class 'tensorflow.python.keras.engine.functional.Functional'>liyunlu0618 commentedon Apr 7, 2021
Thanks for reporting the error. I'm working on a change to support pruning a model recursively. Will let you know once it's checked-in.
ameyparanjape commentedon Apr 7, 2021
Thank you @liyunlu0618
liyunlu0618 commentedon Apr 7, 2021
The PR is merged.
#658
You can try later with a nightly build and let us know if it works. Thanks!
ameyparanjape commentedon Apr 7, 2021
Thank you @liyunlu0618 , I am trying to get the
tf-nightlyinstalled and running. (never usedtf-nightlybefore, always usedstable)Here is what I am doing:
tensorflow-2.4.1(pip uninstall tensorflow)tf_nightly-2.6.0.dev20210407) (pip install -U tf-nightly --user)(Please advise if I am not on the right track!)
I got an error, I'm not sure if I am missing any additional step to get the installation right.
Here is what my output/error log looks like:
ameyparanjape commentedon Apr 8, 2021
@liyunlu0618 Not sure how exactly, but now the
tensorflow-nightlyseems to be installed correctly. (I made sure by printing the version of tensorflow)I am still getting the same error. It seems like there might be difference between the
<class 'tensorflow.python.keras.engine.functional.Functional'>objects and Recursive sequential layers object(I peeked into the PR). Also I am wondering that, since Pruning is a part oftensorflow_model_optimization, do I have to take any additional steps to update that tonightlyas well?Here is my code snippet and error log for reference. It is unchanged from my last comment:
Output Log (clipped):
liyunlu0618 commentedon Apr 8, 2021
Can you try a nightly build of tf-mot?
ameyparanjape commentedon Apr 8, 2021
Do you mind addressing how to use/install tf-mot nightly? I could not find any documentation on that.
Currently I have this from
pip freeze:liyunlu0618 commentedon Apr 20, 2021
You may have to build from source at the moment:
https://www.tensorflow.org/model_optimization/guide/install#installing_from_source
Will let you know once we have a new release.
Sai-222777 commentedon Aug 17, 2025
Hi, is this issue resolved? I tried pruning EfficientNetB0, and I get the error that pruning of 'Functional' layer is not supported.