Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'Conv2D' object has no attribute 'kernel' on running MNIST Keras pruning Example #2

Closed
rishab-sharma opened this issue May 21, 2019 · 1 comment
Labels
technique:pruning Regarding tfmot.sparsity.keras APIs and docs

Comments

@rishab-sharma
Copy link

rishab-sharma commented May 21, 2019

When I am trying to run the MNIST Example of keras pruning Link, I get the following Traceback.
Tensorflow Version:
Name: tensorflow Version: 2.0.0a0

Traceback

--> 156   layerwise_model = build_layerwise_model(input_shape, **pruning_params)
    157   sequential_model = build_sequential_model(input_shape)
    158   sequential_model = prune.prune_low_magnitude(

<ipython-input-9-8bf64bf9319d> in build_layerwise_model(input_shape, **pruning_params)
     73       l.Dropout(0.4),
     74       prune.prune_low_magnitude(
---> 75           l.Dense(num_classes, activation='softmax'), **pruning_params)
     76   ])
     77 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    454     self._setattr_tracking = False  # pylint: disable=protected-access
    455     try:
--> 456       result = method(self, *args, **kwargs)
    457     finally:
    458       self._setattr_tracking = previous_value  # pylint: disable=protected-access

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/sequential.py in __init__(self, layers, name)
    106     if layers:
    107       for layer in layers:
--> 108         self.add(layer)
    109 
    110   @property

/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    454     self._setattr_tracking = False  # pylint: disable=protected-access
    455     try:
--> 456       result = method(self, *args, **kwargs)
    457     finally:
    458       self._setattr_tracking = previous_value  # pylint: disable=protected-access

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/sequential.py in add(self, layer)
    167           # and create the node connecting the current layer
    168           # to the input layer we just created.
--> 169           layer(x)
    170           set_inputs = True
    171 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    592           # Build layer if applicable (if the `build` method has been
    593           # overridden).
--> 594           self._maybe_build(inputs)
    595           # Explicitly pass the learning phase placeholder to `call` if
    596           # the `training` argument was left unspecified by the user.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   1711     # Only call `build` if the user has manually overridden the build method.
   1712     if not hasattr(self.build, '_is_default'):
-> 1713       self.build(input_shapes)
   1714     # We must set self.built since user defined build functions are not
   1715     # constrained to set self.built.

/usr/local/lib/python3.6/dist-packages/tensorflow_model_optimization/python/core/sparsity/keras/pruning_wrapper.py in build(self, input_shape)
    173     weight_vars, mask_vars, threshold_vars = [], [], []
    174 
--> 175     self.prunable_weights = self.layer.get_prunable_weights()
    176 
    177     # For each of the prunable weights, add mask and threshold variables

/usr/local/lib/python3.6/dist-packages/tensorflow_model_optimization/python/core/sparsity/keras/prune_registry.py in get_prunable_weights()
    167 
    168     def get_prunable_weights():
--> 169       return [getattr(layer, weight) for weight in cls._weight_names(layer)]
    170 
    171     def get_prunable_weights_rnn():  # pylint: disable=missing-docstring

/usr/local/lib/python3.6/dist-packages/tensorflow_model_optimization/python/core/sparsity/keras/prune_registry.py in <listcomp>(.0)
    167 
    168     def get_prunable_weights():
--> 169       return [getattr(layer, weight) for weight in cls._weight_names(layer)]
    170 
    171     def get_prunable_weights_rnn():  # pylint: disable=missing-docstring

AttributeError: 'Conv2D' object has no attribute 'kernel'

I am not using absl-py to run the main(), because I am using colab, therefore, I have also commented out the eager execution line.

Can anyone please help me this, where I am going wrong, or is this a bug?

@rishab-sharma
Copy link
Author

Going through this, solved the issue

! pip uninstall -y tensorflow
! pip uninstall -y tf-nightly
! pip install -U tf-nightly-gpu

! pip install tensorflow-model-optimization

import tensorflow as tf
tf.enable_eager_execution()

@alanchiao alanchiao added the technique:pruning Regarding tfmot.sparsity.keras APIs and docs label Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
technique:pruning Regarding tfmot.sparsity.keras APIs and docs
Projects
None yet
Development

No branches or pull requests

2 participants