我正在尝试使用Grad-CAM方法可视化(部分重新训练的)ResNet-152的主要响应焦点。我在下面的代码行部分中花了很多心思来理解教科书,对它们进行了略微简化以方便阅读:
...
# last convolution layer
last_conv_layer = model.get_layer('res5c_branch2c')
# visualize response foci
for index in range(dataset_validation['images'].shape[0]):
# get input
img = np.expand_dims(dataset_validation['images'][index], axis=0)
# already tried (*): tf.contrib.keras.backend.set_learning_phase(False)
# query network response
estimate = model.predict(img)
# get top-3 result indicees
t3_indicees = (np.squeeze(estimate.argsort()))[-3:][::-1]
# loop over best 3 responses
for resi in range(3):
m_out = model.output[:, t3_indicees[resi]]
# already tried (**): k.set_learning_phase(False)
grads = k.gradients(m_out, last_conv_layer.output)[0]
pooled_grads = k.mean(grads, axis=(0, 1, 2))
iterate = k.function([model.input], [pooled_grads, last_conv_layer.output[0]])
pooled_grads_value, conv_layer_output_value = iterate([img])
...
代码在最后一行崩溃:
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'bn_conv1/keras_learning_phase' with dtype bool
[[Node: bn_conv1/keras_learning_phase = Placeholder[dtype=DT_BOOL, shape=<unknown>, _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
[[Node: strided_slice/_4465 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_14658_strided_slice", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
我已经进行了各种尝试来解决或至少更好地理解该问题,包括:
什么是进一步进行调试的好方法?我真的很想了解这个问题,但实际上我现在的想法已经用光了。
非常感谢您提供任何有益的建议!