2017-08-04 115 views
1

我正在尝试使用Python实现本文 - https://arxiv.org/abs/1610.02391。为此,我想获得相对于最后一个卷积层的特定类输出的渐变。我遇到了backward()函数的以下用法。[Caffe-CNN]如何获得输出的梯度w.r.t.到卷积层?

label = np.zeros((1, 6)) 
label[0, interested_class] = 1 
net.backward(**{net.output[0]: label}) 

假设我的网络中有六个类。

但是,它将渐变w.r.t给输入层。

我试图使用下面的用法,但它没有给出理想的输出。

label = np.zeros((1,6)) 
label[0,interested_class] = 1 
net.backward(end=conv, **{net.output[0]:label}) 

准确地说,我想输出层w.r.t CONV层值的梯度。

任何帮助,高度赞赏!

回答

1

我想通过阅读this postthis discussion来解决这个问题。这是代码。

layer_name = 'conv' #Convolutional layer of interest 
class_label= 4 # the class of interest 
label = np.zeros((1,6)) 
label[0,interested_class] = 1  
grads= net.backward(diffs= [layer_name], **{net.outputs[0]:diff}) 
gradients = grads[layer_name] 

希望这会对您有所帮助!

+0

我为此实现撰写了一篇博文(http://sandaw89.blogspot.sg/2017/08/gradcam-implementation-in-pycaffe.html)。任何人都可以从那里找到更多细节。 – Sandareka