论文 "Deep inside convolutional networks: Visualising image classification models and saliency maps" 的实现,Simonyan 等人

Implementation of the paper "Deep inside convolutional networks: Visualising image classification models and saliency maps", Simonyan et al

在卷积神经网络梯度数据的可视化中,采用 Caffe 框架,已经对所有 classes 的梯度数据进行了可视化,对特定 class 采取梯度很有趣。在 deploy.prototxt 文件中 "bvlc_reference_caffenet" 模型中,我设置了:

force_backward: true

并评论了最后一部分:

layer {
  name: "prob" 
  type: "Softmax"
  bottom: "fc8"
  top: "prob" 
}

,这是之前的:

layer {
  name: "fc8"
  type: "InnerProduct"
  bottom: "fc7"
  top: "fc8"
  inner_product_param {
  num_output: 1000
 }
}

,并添加代替它:

 layer {
   name: "loss"
   type: "SoftmaxWithLoss"
   bottom: "fc8"
   bottom: "label"
   top: "prob"
}

,在python代码中调用:

out = net.forward()

,我们前进到最后一层,然后调用:

backout = net.backward()

,得到渐变的可视化。首先,我想问一下这叫做显着图,如果我想针对特定的 class 进行反向操作,例如281是给猫的。我该怎么办?

提前感谢您的指导。

P.S。受益于 Yangqing 为其笔记本在过滤器可视化方面的代码。

imagenetMeanFile = caffe_root  +'python/caffe/imagenet/ilsvrc_2012_mean.npy'
caffe.set_mode_cpu()
net = caffe.Net(caffe_root +   'models/bvlc_reference_caffenet/deploy.prototxt',
            caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',
            caffe.TRAIN)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1)) # mean pixel 
transformer.set_raw_scale('data', 255)  # the reference model operates on images in [0,255] range instead of [0,1]
transformer.set_channel_swap('data', (2,1,0))  # the reference model has channels in BGR order instead of RGB

使用以下代码可以完成:

label_index = 281  # Index for cat class
caffe_data = np.random.random((1,3,227,227))
caffeLabel = np.zeros((1,1000,1,1))
caffeLabel[0,label_index,0,0] = 1;

bw = net.backward(**{net.outputs[0]: caffeLabel})

同样完整的可视化你可以参考我的github,它更完整和可视化显着图以及class模型的可视化和反向传播中的梯度可视化。

https://github.com/smajida/Deep_Inside_Convolutional_Networks