ValueError: `prune_low_magnitude` can only prune an object of the following types: keras.models.Sequential, keras functional model, keras.layers.Layer, list of keras.layers.Layer. You passed an object of type: Conv2D.
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 38/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- keras, python, tensorflow
- Domain
- machine-learning
Research direction
Start with the prune_model function and the prune_low_magnitude call shown in mbv2_prune.py. Reproduce the failure using MobileNetV2 with TensorFlow 2.16.2, TensorFlow Model Optimization 0.8.0, and Python 3.10.16, then inspect why Conv2D is rejected despite inheriting from Layer. Done means the documented reproduction no longer raises this ValueError and the affected layers can be pruned.
Written by the indexing model from the issue text.
Description
Describe the bug
I am trying to prune MobileNetV2 model using prune_low_magnitude but am running into the following error.
2025-01-19 17:41:03.439078: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Loading MobileNetV2 model...
Pruning the model...
Traceback (most recent call last):
File "/Users/esp/Projects/gen_models/mbv2_prune.py", line 89, in <module>
full_workflow()
File "/Users/esp/Projects/gen_models/mbv2_prune.py", line 81, in full_workflow
pruned_model = prune_model(model)
File "/Users/esp/Projects/gen_models/mbv2_prune.py", line 27, in prune_model
pruned_layer = prune_low_magnitude(tf.keras.layers.Conv2D(
File "/Users/esp/Projects/gen_models/venv/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/keras/metrics.py", line 74, in inner
raise error
File "/Users/esp/Projects/gen_models/venv/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/keras/metrics.py", line 69, in inner
results = func(*args, **kwargs)
File "/Users/esp/Projects/gen_models/venv/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/sparsity/keras/prune.py", line 216, in prune_low_magnitude
raise ValueError(
ValueError: `prune_low_magnitude` can only prune an object of the following types: keras.models.Sequential, keras functional model, keras.layers.Layer, list of keras.layers.Layer. You passed an object of type: Conv2D.
System information
TensorFlow version (installed from source or binary): 2.16.2
TensorFlow Model Optimization version (installed from source or binary): 0.8.0
Python version: 3.10.16
Describe the expected behavior
The layers should be pruned as Conv2D inherits Layer which as the error says is supported.
Describe the current behavior
Even though Conv2D inherits Layer which as the error says is supported, the code fails.
Code to reproduce the issue
def load_model_mobilenetv2():
model = MobileNetV2(weights="imagenet", include_top=True, alpha=0.35, input_shape=(224, 224, 3))
return model
def prune_model(base_model):
pruning_schedule = PolynomialDecay(
initial_sparsity=0.0,
final_sparsity=0.5,
begin_step=0,
end_step=1000
)
pruned_model = Sequential()
for layer in base_model.layers:
if isinstance(layer, tf.keras.layers.Dense):
pruned_layer = prune_low_magnitude(layer, pruning_schedule)
pruned_model.add(pruned_layer)
elif isinstance(layer, tf.keras.layers.Conv2D):
pruned_layer = prune_low_magnitude(tf.keras.layers.Conv2D(
layer.filters,
layer.kernel_size,
layer.strides,
layer.padding,
layer.data_format,
layer.dilation_rate,
layer.groups,
layer.activation,
layer.use_bias,
layer.kernel_initializer,
layer.bias_initializer,
layer.kernel_regularizer,
layer.bias_regularizer,
layer.activity_regularizer,
layer.kernel_constraint,
layer.bias_constraint,
), pruning_schedule)
pruned_model.add(pruned_layer)
else:
pruned_model.add(layer)
# This doesn't work too
# for layer in base_model.layers:
# if isinstance(layer, tf.keras.layers.Dense) or isinstance(layer, tf.keras.layers.Conv2D):
# pruned_layer = prune_low_magnitude(layer, pruning_schedule)
# pruned_model.add(pruned_layer)
# else:
# pruned_model.add(layer)
pruned_model.build(input_shape=(None, 224, 224, 3))
pruned_model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss="categorical_crossentropy",
metrics=["accuracy"]
)
return pruned_model
- Dominant language
- Python
- Stars
- 1.6k
- Forks
- 349
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tensorflow/model-optimization
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
tensorflow/model-optimization#1301 · 1 comment ·
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 48/100
tensorflow/model-optimization#1272 ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 50/100
tensorflow/model-optimization#1270 · 2 comments ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 48/100
tensorflow/model-optimization#1241 ·
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 35/100
tensorflow/model-optimization#1182 · 1 comment ·
All issues in tensorflow/model-optimization
Similar issues
-
essnmx good first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
-
[Feature] 奇物选择添加优先级 Open
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
syfoud/Simulated_Scepter#174 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Giskard-AI/giskard-oss#2840 · 1 comment ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success Openarea: repo bug perceived difficulty: 2
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
yeti-platform/yeti#1380 ·