Python 文件的实施;深层卷积网络:可视化图像分类模型和显著性图”;,Simonyan等人

Python 文件的实施;深层卷积网络:可视化图像分类模型和显著性图”;,Simonyan等人,python,visualization,gradient,deep-learning,caffe,Python,Visualization,Gradient,Deep Learning,Caffe,在卷积神经网络中梯度数据的可视化中,利用Caffe框架,已经可视化了所有类的梯度数据,对特定类的梯度进行处理是很有趣的。在“bvlc_reference_caffenet”模型中的deploy.prototxt文件中,我设置了: force_backward: true 并评论了最后一部分: layer { name: "prob" type: "Softmax" bottom: "fc8" top: "prob" } ,这是在: layer { name: "fc8

在卷积神经网络中梯度数据的可视化中,利用Caffe框架,已经可视化了所有类的梯度数据,对特定类的梯度进行处理是很有趣的。在“bvlc_reference_caffenet”模型中的deploy.prototxt文件中,我设置了:

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()
,我们向最后一层前进,然后调用:

out = net.forward()
backout = net.backward()
,实现了梯度的可视化。首先,我想问一下,这就是所谓的显著性图,如果我想对一个特定的类进行反向操作,例如,281代表一只猫。我该怎么办

提前感谢您的指导

p.S.得益于杨青在过滤器可视化方面的笔记本代码

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,它更完整,可以可视化显著性图以及类模型的可视化和反向传播中的梯度可视化