What is a `“Python”` layer in caffe?

https://stackoverflow.com/questions/41344168/what-is-a-python-layer-in-caffe/41481539#41481539

Q:

Caffe has a layer type "Python".

For instance, this layer type can be used as a loss layer.
On other occasions it is used as an input layer.

What is this layer type?
How can this layer be used?

A:

 

Prune's and Bharat's answers gives the overall purpose of a "Python" layer: a general purpose layer which is implemented in python rather than c++.

I intend this answer to serve as a tutorial for using "Python" layer.


A Tutorial for "Python" layer

what is a 'Python" layer?

Please see the excellent answers of Prune and Bharat.

Pre-requisite

In order to use 'Python" layer you need to compile caffe with flag

WITH_PYTHON_LAYER := 1

set in 'Makefile.config'.

How to implement a "Python" layer?

"Python" layer should be implemented as a python class derived from caffe.Layer base class. This class must have the following four methods:

import caffe
class my_py_layer(caffe.Layer):
  def setup(self, bottom, top):
    pass

  def reshape(self, bottom, top):
    pass

  def forward(self, bottom, top):
    pass

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

What are these methods?

def setup(self, bottom, top): This method is called once when caffe builds the net. This function should check that number of inputs (len(bottom)) and number of outputs (len(top)) is as expected.
You should also allocate internal parameters of the net here (i.e., self.add_blobs()), see this thread for more information.
This method has access to self.param_str - a string passed from the prototxt to the layer. See this thread for more information.

def reshape(self, bottom, top): This method is called whenever caffe reshapes the net. This function should allocate the outputs (each of the top blobs). The outputs' shape is usually related to the bottoms' shape.

def forward(self, bottom, top): Implementing the forward pass from bottom to top.

def backward(self, top, propagate_down, bottom): This method implements the backpropagation, it propagates the gradients from top to bottompropagate_down is a Boolean vector of len(bottom) indicating to which of the bottoms the gradient should be propagated.

Some more information about bottom and top inputs you can find in this post.
You can see some examples of simplified python layers herehere and here.

Trainable parameters
"Python" layer can have trainable parameters (like "Conv""InnerProduct", etc.).
You can find more information on adding trainable parameters in this thread and this one. There's also a very simplified example in caffe git.

How to add a "Python" layer in a prototxt?

See Bharat's answer for details.
You need to add the following to your prototxt:

layer {
  name: 'rpn-data'
  type: 'Python'  
  bottom: 'rpn_cls_score'
  bottom: 'gt_boxes'
  bottom: 'im_info'
  bottom: 'data'
  top: 'rpn_labels'
  top: 'rpn_bbox_targets'
  top: 'rpn_bbox_inside_weights'
  top: 'rpn_bbox_outside_weights'
  python_param {
    module: 'rpn.anchor_target_layer'  # python module name where your implementation is
    layer: 'AnchorTargetLayer'   # the name of the class implementation
    param_str: "'feat_stride': 16"   # optional parameters to the layer
  }
}

How to add a "Python" layer using pythonic NetSpec interface?

It's very simple:

import caffe
from caffe import layers as L

ns = caffe.NetSpec()
# define layers here...
ns.rpn_labels, ns.rpn_bbox_targets, \
  ns.rpn_bbox_inside_weights, ns.rpn_bbox_outside_weights = \
    L.Python(ns.rpn_cls_score, ns.gt_boxes, ns.im_info, ns.data, 
             name='rpn-data',
             ntop=4, # tell caffe to expect four output blobs
             python_param={'module': 'rpn.anchor_target_layer',
                           'layer': 'AnchorTargetLayer',
                           'param_str': '"\'feat_stride\': 16"'})

How to use a net with a "Python" layer?

Invoking python code from caffe is nothing you need to worry about. Caffe uses boost API to call python code from compiled c++.
What do you do need to do?
Make sure the python module implementing your layer is in $PYTHONPATH so that when caffe imports it - it can be found.
For instance, if your module my_python_layer.py is in /path/to/my_python_layer.py then

PYTHONPATH=/path/to:$PYTHONPATH $CAFFE_ROOT/build/tools/caffe train -solver my_solver.prototxt

should work just fine.

How to test my layer?

You should always test your layer before putting it to use.
Testing the forward function is entirely up to you, as each layer has a different functionality.
Testing the backward method is easy, as this method only implements a gradient of forward it can be numerically tested automatically!
Check out test_gradient_for_python_layer testing utility:

import numpy as np
from test_gradient_for_python_layer import test_gradient_for_python_layer

# set the inputs
input_names_and_values = [('in_cont', np.random.randn(3,4)), 
                          ('in_binary', np.random.binomial(1, 0.4, (3,1))]
output_names = ['out1', 'out2']
py_module = 'folder.my_layer_module_name'
py_layer = 'my_layer_class_name'
param_str = 'some params'
propagate_down = [True, False]

# call the test
test_gradient_for_python_layer(input_names_and_values, output_names, 
                               py_module, py_layer, param_str, 
                               propagate_down)

# you are done!

Special Notice

It is worth while noting that python code runs on CPU only. Thus, if you plan to have a Python layer in the middle of your net you will see a significant degradation in performance if you plan on using GPU. This happens because caffe needs to copy blobs from GPU to CPU before calling python layer and then copy back to GPU to proceed with the forward/backward pass.
This degradation is far less significant if the python layer is either an input layer or the topmost loss layer.

shareedit
 
    
thank you very much for the great explanation! Will a python layer also work on a system where no python is installed? (can I deploy just the caffe binaries then?) – mojovski Jan 7 at 23:54
    
@mojovski I think you need Python libraries for this to work. – Shai Jan 7 at 23:56
    
@Shai I think there is a bug in pyloss layer github.com/BVLC/caffe/blob/master/examples/pycaffe/layers/… I think the last line should be bottom[i].diff[...] = sign * top[0].diff[0] * self.diff / bottom[i].num Am I right? Thanks.– kli_nlpr Mar 15 at 1:35
    
@kli_nlpr it does seem odd that top.diff is not taken into account. you may open an issue in github to investigate this point. – Shai Mar 15 at 5:47
    
@Shai I have created a PR here github.com/BVLC/caffe/pull/5407 – kli_nlpr Mar 15 at 6:49 

posted on 2017-12-08 16:47  塔上的樹  阅读(538)  评论(0编辑  收藏  举报