【问题标题】:Creating model for gradCAM leads to "Graph disconnected"为 gradCAM 创建模型导致“图形断开连接”
【发布时间】:2022-11-20 23:09:44
【问题描述】:

我必须遵循我用来对图片进行分类的模型。通过 gradCAM,我想可视化图片中的重要区域。该代码与来自 keras 的预训练 VGG16 模型完美配合。一旦加载我自己的预训练模型,我就会收到“图形断开连接”错误。

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


Model: "multi_class_vgg16"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         [(None, 224, 224, 3)]     0         
_________________________________________________________________
tf.cast (TFOpLambda)         (None, 224, 224, 3)       0         
_________________________________________________________________
tf.__operators__.getitem (Sl (None, 224, 224, 3)       0         
_________________________________________________________________
tf.nn.bias_add (TFOpLambda)  (None, 224, 224, 3)       0         
_________________________________________________________________
vgg16 (Functional)           (None, 7, 7, 512)         14714688  
_________________________________________________________________
global_average_pooling2d (Gl (None, 512)               0         
_________________________________________________________________
dense (Dense)                (None, 256)               131328    
_________________________________________________________________
dropout (Dropout)            (None, 256)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 64)                16448     
_________________________________________________________________
dropout_1 (Dropout)          (None, 64)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 32)                2080      
_________________________________________________________________
dropout_2 (Dropout)          (None, 32)                0         
_________________________________________________________________
dense_3 (Dense)              (None, 5)                 165       
=================================================================
Total params: 14,864,709
Trainable params: 150,021
Non-trainable params: 14,714,688
_________________________________________________________________

这是我获取模型中最后一个 conv_layers 的代码。

# find last conv layer name and get model that ends with it
for layer in reversed(model.layers):
    # 'conv5_block3_out' with shape (None, 7, 7, 2048)
    if len(layer.output_shape) == 4:
        last_conv_layer = model.get_layer(layer.name)
        break
# returns conv fmaps and model prediction when called
gradModel = Model(inputs=[model.input], outputs=[last_conv_layer.output, model.output])

这是出现错误的地方。

之后我想计算这个梯度:

with tf.GradientTape() as tape:
    # get predictions from conv model and model
    (fmaps, preds) = gradModel(image)
    # get logit of winner-class
    y_c = preds[:,np.argmax(preds[:,])]

grads = tape.gradient(y_c, fmaps)[0]
grads.shape

为什么会这样?

【问题讨论】:

  • 有人有想法吗?
  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: python tensorflow keras graph


【解决方案1】:

你解决你的问题了吗? 如果那么请告诉我哪些改变有效。

【讨论】:

  • Neloy Barman,请不要添加我也是作为答案。它实际上并没有提供问题的答案。如果您有不同但相关的问题,请ask它(如果它有助于提供上下文,请参考此问题)。如果你对这个具体问题感兴趣,你可以upvote它,留下comment,或者一旦你有足够的reputation就开始bounty
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 2019-07-14
  • 1970-01-01
  • 2020-10-16
  • 2015-04-28
相关资源
最近更新 更多