我正在使用keras-vis的可视化凸轮来创建带导向的gradcam图像。 grad-cam与vgg16完美配合。但是当我为inceptionv3使用相同的代码时,它无法正常工作。
from keras.applications.inception_v3 import InceptionV3
from vis.utils import utils
from keras.preprocessing import image
import numpy as np
from keras import activations
from matplotlib import pyplot as plt
%matplotlib inline
from vis.visualization import visualize_cam,overlay
#build the inceptionv3 model with imagenet weights
model = InceptionV3(weights='imagenet',include_top=True)
# Utility to search for layer index by name
layer_idx = utils.find_layer_idx(model,'predictions')
#swap with softmax with linear classifier for the reasons mentioned above
model.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)
from vis.utils import utils
from matplotlib import pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize']=(18,6)
img1 = utils.load_img('images/ouzel1.jpg',target_size=(299,299))
img2 = utils.load_img('images/ouzel2.jpg',target_size=(299,299))
f, ax = plt.subplots(1,2)
ax[0].imshow(img1)
ax[1].imshow(img2)
plt.show()
from vis.visualization import visualize_cam
for modifier in [None, 'guided', 'relu']:
plt.figure()
f, ax = plt.subplots(1, 2)
plt.suptitle("vanilla" if modifier is None else modifier)
for i, img in enumerate([img1, img2]):
# 20 is the imagenet index corresponding to `ouzel`
heatmap = visualize_cam(model, layer_idx, filter_indices=20,
seed_input=img, backprop_modifier=modifier,
#penultimate_layer_idx = 299 # corresponding to "conv2d_94"
)
# Lets overlay the heatmap onto original image.
ax[i].imshow(overlay(img, heatmap))
通过注释掉#penultimate_layer这一行,我也得到了相同的输出,这是不正确的。谁能告诉我这是什么问题?给出了引导梯度凸轮结果,然后给出了原始图像。
问题是热图必须在鸟(ouzel)上。
答案 0 :(得分:0)
我遇到了同样的问题,但是后来我发现InceptionV3对这些图像进行了错误分类。检查:
>>> model.predict(np.stack([img1, img2], 0)).argmax(axis=1)
array([110, 725])
使用VGG时:
>>> model.predict(np.stack([img1, img2], 0)).argmax(axis=1)
array([20, 20])