digits tutorial 2自定义python layer

参考链接:https://github.com/NVIDIA/DIGITS/tree/digits-5.0/examples/python-layer

主要定义了可以去掉图像1/4像素【图片的右下角】的python layer。

具体的python定义是:

import caffe
import random

class BlankSquareLayer(caffe.Layer):

    def setup(self, bottom, top):
        assert len(bottom) == 1,            'requires a single layer.bottom'
        assert bottom[0].data.ndim >= 3,    'requires image data'
        assert len(top) == 1,               'requires a single layer.top'

    def reshape(self, bottom, top):
        # Copy shape from bottom
        top[0].reshape(*bottom[0].data.shape)

    def forward(self, bottom, top):
        # Copy all of the data
        top[0].data[...] = bottom[0].data[...]
        # Then zero-out one fourth of the image
        height = top[0].data.shape[-2]
        width = top[0].data.shape[-1]
        h_offset = random.randrange(height/2)
        w_offset = random.randrange(width/2)
	#set the right-down to zero
        top[0].data[...,
                h_offset:(h_offset + height/2),
                w_offset:(w_offset + width/2),
                ] = 0

    def backward(self, top, propagate_down, bottom):
        pass

使用之前的get start教程,训练Mnist

然后选择之前定义的python_layer.py,对于LeNet进行fine-turing,在scale和conv1之间加入python layer

layer {
  name: "blank_square"
  type: "Python"
  bottom: "scaled"
  top: "scaled"
  python_param {
    module: "digits_python_layers"
    layer: "BlankSquareLayer"
  }
}

猜你喜欢

转载自blog.csdn.net/mdjxy63/article/details/81158157