sicara / tf-explain

Interpretability Methods for tf.keras models with Tensorflow 2.x
https://tf-explain.readthedocs.io
MIT License
1.02k stars 112 forks source link

Value Error: Graph disconnected #165

Open mohamedabdallah1996 opened 3 years ago

mohamedabdallah1996 commented 3 years ago

I face this error after putting GradCAMCallback in the fit function. I don't understand why this happens because it raises this error after training one epoch. if the graph is disconnected I think the model will be broken and it won't be able to finish even one epoch.

this is my code:

def encoding_model():
      base_model = VGGFace(model='vgg16', include_top=False, input_shape=(224, 224, 3), pooling='avg')
      base_model.trainable = False
      flatten = Flatten()(base_model.layers[-2].output)
      fc1 = Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.1))(flatten)    
      drop1 = Dropout(0.3)(fc1)
      output = Dense(128)(drop1)

      model = Model(inputs=base_model.input, outputs=output)
      return model

def build_model():
      input1 = Input(input_shape)
      input2 = Input(input_shape)

      encoding = encoding_model()

      output1 = encoding.call(input1)
      output2 = encoding.call(input2)
      euclidean_distance = Lambda(euclidean_distance)([output1, output2]) 

      model = Model(inputs=[input1, input2], outputs=euclidean_distance)
      model.compile(...)

      return model`
`

image

full error message: ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='input_10'), name='input_10', description="created by layer 'input_10'") at layer "conv1_1". The following previous layers were accessed without issue: ['conv1_1', 'conv1_1']

andreafortini commented 3 years ago

I faced the same problem when I trying to build a model with transfer learning as your case and I trying to access the layers inside the inner model. The inner model is signed as "Functional" if you check model.summary().

Try to don't leave default value layer_name=None into constructor of GradCAMCallback and put the last convolutional layer of your model: "conv5_3"