BenWhetton / keras-surgeon

Pruning and other network surgery for trained Keras models.
Other
403 stars 107 forks source link

problem with delete_layer #40

Open weiwenying opened 4 years ago

weiwenying commented 4 years ago

try:
  # Colab only
  %tensorflow_version 2.x
except Exception:
    pass
import tensorflow as tf

from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tfkerassurgeon.operations import delete_layer

x = input_image = Input(shape=(224, 224, 3))
x = Conv2D(3, (3, 3), padding="same", activation="relu")(x)
x = Conv2D(3, (3, 3), padding="same", activation="relu")(x)
x = Conv2D(3, (3, 3), padding="same", activation="relu")(x)
predictions = Dense(5, activation='softmax')(x)
model = Model(inputs=input_image, outputs=predictions)

model = delete_layer(model, model.get_layer(model.layers[1].name))

Throw an exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-6-c63b4938f63f> in <module>()
     31 name = model.layers[1].name
     32 name = tf.constant(name)
---> 33 model = delete_layer(model, model.get_layer(name))

5 frames
/tensorflow-2.0.0/python3.6/tensorflow_core/python/framework/ops.py in __hash__(self)
    711     if (Tensor._USE_EQUALITY and executing_eagerly_outside_functions() and
    712         (g is None or g._building_function)):  # pylint: disable=protected-access
--> 713       raise TypeError("Tensor is unhashable if Tensor equality is enabled. "
    714                       "Instead, use tensor.experimental_ref() as the key.")
    715     else:

TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.
ShravanKarthik94 commented 4 years ago

Facing the same issue. Is there support for models created using Keras FunctionalAPI?

Details and call stack listed below, any inputs much appreciated.

Traceback (most recent call last): File "test_coco.py", line 98, in model_new = delete_channels(unet_inst.model, layer_0, [6,8,9,12]) File "/home/shravan/.local/lib/python3.5/site-packages/kerassurgeon/operations.py", line 105, in delete_channels return surgeon.operate() File "/home/shravan/.local/lib/python3.5/site-packages/kerassurgeon/surgeon.py", line 154, in operate sub_output_nodes) File "/home/shravan/.local/lib/python3.5/site-packages/kerassurgeon/surgeon.py", line 266, in _rebuild_graph outputs, output_masks = zip([_rebuild_rec(n) for n in output_nodes]) File "/home/shravan/.local/lib/python3.5/site-packages/kerassurgeon/surgeon.py", line 266, in outputs, output_masks = zip([_rebuild_rec(n) for n in output_nodes]) File "/home/shravan/.local/lib/python3.5/site-packages/kerassurgeon/surgeon.py", line 218, in _rebuild_rec if node_output in self._replace_tensors.keys(): File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/framework/ops.py", line 713, in hash raise TypeError("Tensor is unhashable if Tensor equality is enabled. " TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.

Information on versions of different modules: Keras 2.3.0 tensorflow-estimator (2.0.0) tensorflow-gpu (2.0.0) tensorflow-hub (0.6.0) tensorflow-model-optimization (0.1.3) tensorflow-probability (0.8.0rc0) tensorflow-serving-api-gpu (1.14.0) kerassurgeon (0.1.3)

Sayam753 commented 4 years ago

Hi @weiwenying and @ShravanKarthik94 I ran into similar issues but changing the %tensorflow_version to 1.x in Google Colab solved this problem. I do not know why tfkerassurgeon did not work for TFv2.

ShravanKarthik94 commented 4 years ago

Hi @Sayam753 , I couldn't get this to work either. My guess is this isn't being maintained anymore.

nickion commented 4 years ago

Came across the same issue. It may be tinkering with internals, but following from posts in https://stackoverflow.com/questions/49546922/keras-replacing-input-layer for replacing the input layer it appears to work to do model._layers.pop(0) (note the underscore) then binding a new input layer to the existing model and creating a new model with the new input tensor.