GradCAM visualization:无效参数错误:必须为占位符张量“X”输入一个值,数据类型为float和shape[X]

2024-06-08 12:17:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在CNN的分类任务中可视化重要区域。在

我使用VGG16+我自己的顶层(一个全球平均池层和一个密集层)

model_vgg16_conv = VGG16(weights='imagenet', include_top=False, input_shape=(100, 100, 3))

model = models.Sequential()

model.add(model_vgg16_conv)

model.add(Lambda(global_average_pooling, output_shape=global_average_pooling_shape))
model.add(Dense(4, activation = 'softmax', init='uniform'))

在编译和装配模型之后,我尝试使用Grad CAM制作新图像:

^{pr2}$

之后我就要执行死刑了

[conv_outputs, predictions] = get_output([image])

导致以下错误:

InvalidArgumentError: You must feed a value for placeholder tensor 'vgg16_input' with dtype float and shape [?,100,100,3] [[{{node vgg16_input}}]] [[dense_1/Softmax/_233]]

其他信息

def global_average_pooling(x):
    return K.mean(x, axis = (2, 3))

def global_average_pooling_shape(input_shape):
    return input_shape[0:2]

模型摘要:

Layer (type)                 Output Shape              Param #   
=================================================================
vgg16 (Model)                (None, 3, 3, 512)         14714688  
_________________________________________________________________
lambda_1 (Lambda)            (None, 3)                 0         
_________________________________________________________________
dense_1 (Dense)              (None, 4)                 16        
=================================================================
Total params: 14,714,704
Trainable params: 16
Non-trainable params: 14,714,688

VGG模型摘要:

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 100, 100, 3)       0   
...

我刚接触Grad CAM,我不确定我是在监督什么,还是误解了整个概念。在


Tags: lambda模型noneaddinputoutputmodelparams
1条回答
网友
1楼 · 发布于 2024-06-08 12:17:32

对于Sequential,使用add()方法添加层。在本例中,由于模型对象是直接添加的,因此现在有两个输入到模型中—一个通过Sequential,另一个通过model_gg16_conv

>>> layer = model.layers[0]
>>> layer.get_input_at(0)
<tf.Tensor 'input_1:0' shape=(?, ?, ?, 3) dtype=float32>
>>> layer.get_input_at(1)
<tf.Tensor 'vgg16_input:0' shape=(?, ?, ?, 3) dtype=float32>

由于使用K.function时,只提供了一个输入,因此出现了“vgg16_input”缺少输入的错误。这会有用的

^{pr2}$

但函数式API可以在这种情况下使用:

model_vgg16_conv = VGG16(weights='imagenet', include_top=False, input_shape=(100, 100, 3))
gavg = Lambda(global_average_pooling, output_shape=global_average_pooling_shape)(model_vgg16_conv.output)
output = Dense(4, activation = 'softmax', init='uniform')(gavg)
model_f = Model(model_vgg16_conv.input, output)

final_conv_layer = model_f.get_layer("block5_conv3")
get_output = K.function([model_f.input], [final_conv_layer.output, model_f.output])
[conv_outputs, predictions] = get_output([image])

相关问题 更多 >