Most layers of Caffe are written by C + +, and the network can be trained quickly with the help of C + + efficiency. But sometimes we need to write a bit of input layer to deal with a variety of different data input, for example, because you need to take chunks in the image and do not want to write Lmdb, this time you can consider using Python to write a layer directly. and the input layer does not need GPU acceleration, it is also easier to write. How to use the Python layer
Let's take a look at an online example (from http://chrischoy.github.io/research/caffe-python-layer/)
Layer {
type: ' Python '
name: ' Loss ' top
: ' Loss '
bottom: ' IPX ' bottom
: ' ipy ' Python_param
{ # The module name--usually the filename--that needs to being in
$PYTHONPATH
module: ' Pyloss '
# the layer Nam E--The class name in the module
layer: ' Euclideanlosslayer '
}
# Set loss weight so Caffe knows this is a Loss layer
Loss_weight:1
}
The type is only Python, and then the Top,bottom is the same as the common layer, and module is your Python module name, usually the filename, and then layer is the name of the class you defined.
General setup, reshape, ForWord, Backword Four functions are required, other functions are supplemented by their own requirements, the four function formats are as follows:
def setup (self, bottom, top), Def reshape (self, bottom, top), Def forward (self, bottom, top)
def backward (self, top, propagate_down, bottom):
Here is an example of the code published in the fully convolutional Networks for semantic segmentation paper, explaining how the Python layer should be written.
Import Caffe Import NumPy as NP from PIL import Image import random Class Vocsegdatalayer (Caffe. Layer): "" "" "" Load (input image, label image) pairs from PASCAL VOC one-at-a-time while reshaping the net to preserve di Mensions. Use the to feed the data to a fully convolutional network. "" "Def setup (self, bottom, top):" "" "Setup data layer according to parameters:-Voc_dir:path to PASCAL VOC Year dir-split:train/val/test-mean:tuple of mean values to subtract-randomize:load in random order (default: True)-Seed:seed for randomization (default:none/current time) for PASCAL VOC semantic segmentation. Example params = Dict (voc_dir= "/path/to/pascal/voc2011", mean= (104.00698793, 116.66876762, 122.67891434), split= "Val" "" "" "# config params = eval (self.param_str) Self.voc_dir = params[' Voc_dir '] self.split =
params[' Split '] Self.mean = Np.array (params[' mean ']) Self.random = Params.get (' randomize ', True) SElf.seed = Params.get (' seed ', None) # Two tops:data and label if Len (top)!= 2:raise
Ion ("Need to define two Tops:data and label.")
# data layers have no bottoms if Len (bottom)!= 0:raise ("Do not Exception a define.")
# Load indices for images and labels Split_f = ' {}/imagesets/segmentation/{}.txt '. Format (Self.voc_dir, Self.split) self.indices = open (Split_f, ' R '). Read (). Splitlines () self.idx = 0 # make Eva L Deterministic if ' train ' not in self.split:self.random = False # Randomization:seed and P ick if Self.random:random.seed (self.seed) self.idx = Random.randint (0, Len (self.indices)- 1) def