Caffe中的空间反射填充

时间:2017-06-29 16:25:41

标签: neural-network deep-learning caffe conv-neural-network torch

如何在火炬中实现如何在Caffe中实现空间反射填充?

  (x): nn.SpatialReflectionPadding(l=1, r=1, t=1, b=1)
  (x): nn.SpatialConvolution(64 -> 64, 3x3)
  (x): nn.ReLU

1 个答案:

答案 0 :(得分:2)

这样做的一种方法是使用Caffe的Python Layer。然后,您可以自己设置功能并根据需要进行自定义。但是,此层只能在CPU中运行,因此可能会降低模型速度,尤其是在网络中间使用时。

在下文中,我使用Python层定义了一个零到一个填充输入层,您可以根据自己的需要进行修改:

removeAll()

然后,在您的原型文件中,您可以将其用作:

import caffe
import numpy as np

class SpatialReflectionPadding(caffe.Layer):

def setup(self,bottom,top):
    if len(bottom) != 1: # check that a single bottom blob is given
        raise Exception("Expected a single blob")       
    if len(bottom[0].shape) != 4: # check that it is 4D
        raise Exception("Expected 4D blob")
    params = eval(self.param_str) # get the params given in the prototxt
    self.l = params["l"]
    self.r = params["r"]
    self.t = params["t"]
    self.b = params["b"]

def reshape(self,bottom,top):
    top[0].reshape(bottom[0].shape[0],bottom[0].shape[1],bottom[0].shape[2]+self.t+self.b,bottom[0].shape[3]+self.r+self.l) # set the shape of the top blob based on the shape of the existing bottom blob

def forward(self,bottom,top):
    for i in range(0,top[0].shape[2]):
        for j in range(0,top[0].shape[3]):
            if (i < self.t or i >= self.t+bottom[0].shape[2]) or (j < self.l or j >= self.l+bottom[0].shape[3]):
                top[0].data[:,:,i,j] = 0 # for the padded part, set the value to 0
            else:
                top[0].data[:,:,i,j] = bottom[0].data[:,:,i-self.t,j-self.l] # for the rest, copy the value from the bottom blob

def backward(self,top,propagate_down,bottom):
    bottom[0].diff[...] = np.full(bottom[0].shape,1) * top[0].diff[:,:,self.t:self.t+bottom[0].shape[2],self.l:self.l+bottom[0].shape[3]] # set the gradient for backward pass

我不是100%确定它是否正常工作,但是当我使用它时,它似乎是这样做的。无论如何,它应该给出一个关于如何进行的想法和出发点。另外,您可以参考this question and its answers

相关问题