2

I'm using the library KerasSurgeon to prune a neural network. When I call the method delete_channels() I get the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-17-c459ab9ad129> in <module>
      6         apoz = identify.get_apoz(model, layer, x_test)
      7         high_apoz_channels = identify.high_apoz(apoz, "both")
----> 8         model = delete_channels(model, layer, high_apoz_channels)
      9 
     10         model.compile(optimizer=adam,
~/anaconda3/lib/python3.8/site-packages/kerassurgeon/operations.py in delete_channels(model, layer, channels, node_indices, copy)
    103     surgeon = Surgeon(model, copy)
    104     surgeon.add_job('delete_channels', layer, node_indices=node_indices, channels=channels)
--> 105     return surgeon.operate()
~/anaconda3/lib/python3.8/site-packages/kerassurgeon/surgeon.py in operate(self)
    152             # Rebuild submodel up to this node
    153             sub_output_nodes = node_utils.parent_nodes(node)
--> 154             outputs, output_masks = self._rebuild_graph(self.model.inputs,
    155                                                         sub_output_nodes)
    156 
~/anaconda3/lib/python3.8/site-packages/kerassurgeon/surgeon.py in _rebuild_graph(self, graph_inputs, output_nodes, graph_input_masks)
    276         # Call the recursive _rebuild_rec method to rebuild the submodel up to
    277         # each output layer
--> 278         outputs, output_masks = zip(*[_rebuild_rec(n) for n in output_nodes])
    279         return utils.single_element(outputs), output_masks
    280 
~/anaconda3/lib/python3.8/site-packages/kerassurgeon/surgeon.py in <listcomp>(.0)
    276         # Call the recursive _rebuild_rec method to rebuild the submodel up to
    277         # each output layer
--> 278         outputs, output_masks = zip(*[_rebuild_rec(n) for n in output_nodes])
    279         return utils.single_element(outputs), output_masks
    280 
~/anaconda3/lib/python3.8/site-packages/kerassurgeon/surgeon.py in _rebuild_rec(node)
    213             layer = node.outbound_layer
    214             logging.debug('getting inputs for: {0}'.format(layer.name))
--> 215             node_output = utils.single_element(node.output_tensors)
    216             # First check for conditions to bottom out the recursion
    217             # Check for replaced tensors before any other checks:
~/anaconda3/lib/python3.8/site-packages/kerassurgeon/utils.py in single_element(x)
    141         return x
    142 
--> 143     if len(x) == 1:
    144         x = x[0]
    145     return x
~/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/keras_tensor.py in __len__(self)
    238 
    239   def __len__(self):
--> 240     raise TypeError('Keras symbolic inputs/outputs do not '
    241                     'implement `__len__`. You may be '
    242                     'trying to pass Keras symbolic inputs/outputs '
TypeError: Keras symbolic inputs/outputs do not implement `__len__`. You may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model. This error will also get raised if you try asserting a symbolic input/output directly.

In the last commit of the library I saw that the author of the library gave support to tensorflow v2. (I'm using Version 2.4.1) Searching in another question I founded that the problem it's that Tensorflow 2.0 overloads len and raises an exception. What can I do to prevent this error and not downgrading to an older Tensorflow version? Here you can see the neural network design:

# Create LeNet model
model = Sequential()
model.add(Conv2D(20,
                [3, 3],
                input_shape=[28, 28, 1],
                activation='relu',
                name='conv_1'))
model.add(MaxPooling2D())
model.add(Conv2D(50, [3, 3], activation='relu', name='conv_2'))
model.add(MaxPooling2D())
model.add(layers.Permute((2, 1, 3)))
model.add(Flatten())
model.add(Dense(500, activation='relu', name='dense_1'))
model.add(Dense(10, activation='softmax', name='dense_2'))
model.summary()

Thanks in advance.

Luk
  • 21
  • 1

0 Answers0