-
Prune和Bharat的答案给出了一个
"Python"
层的总体目的:一个通用层,它是用python而不是c
++实现的。我打算将此答案作为使用
"Python"
图层的教程。
"Python"
图层教程什么是
"Python"
层?前提条件
为了使用
'Python"
图层,您需要使用flag编译caffeWITH_PYTHON_LAYER := 1
设置在
'Makefile.config'
。如何实现
"Python"
一层?阿
"Python"
层应被实现为衍生自Python类caffe.Layer
基类。此类 必须 具有以下四个方法: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
这些方法是什么?
def setup(self, bottom, top)
:当caffe构建网络时,将一次调用此方法。此功能应检查输入数量(len(bottom)
)和输出数量(len(top)
)是否符合预期。
您还应该在此处分配网络的内部参数(即self.add_blobs()
),有关更多信息,请参见此线程。
此方法可以访问self.param_str
-从原型文件传递到图层的字符串。有关更多信息,请参见此线程。def reshape(self, bottom, top)
:每当caffe重塑网络时,都会调用此方法。此函数应分配输出(每个top
Blob)。输出的形状通常与bottom
s的形状有关。def forward(self, bottom, top)
:实施从bottom
到的向前传递top
。def backward(self, top, propagate_down, bottom)
:此方法实现了反向传播,将梯度从传播top
到bottom
。propagate_down
是一个布尔向量,len(bottom)
指示bottom
应将梯度传播到s中的哪一个。您可以在这篇文章中找到有关的更多信息
bottom
和top
输入。
示例
您可以在此处,此处和此处看到一些简化的python层示例。
可以在此处找到“移动平均值”输出层的示例。可训练参数
"Python"
层可以具有可训练参数(如"Conv"
,"InnerProduct"
等)。
您可以在此线程和此线程中找到有关添加可训练参数的更多信息。在caffe
git中也有一个非常简化的示例。如何
"Python"
在原型文件中添加图层?有关详细信息,请参见Bharat的答案。
您需要将以下内容添加到您的原型中: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 } }
如何
"Python"
使用pythonicNetSpec
界面添加图层?很简单:
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"'})
如何使用一层网
"Python"
?您无需担心从caffe调用python代码。Caffe使用boost API从编译的c ++调用python代码。
您需要做什么?
确保实现您的图层的python模块在其中,$PYTHONPATH
以便在caffeimport
时可以找到它。
举例来说,如果你的模块my_python_layer.py
中/path/to/my_python_layer.py
,然后PYTHONPATH=/path/to:$PYTHONPATH $CAFFE_ROOT/build/tools/caffe train -solver my_solver.prototxt
应该工作正常。
如何测试我的图层?
在使用该图层之前,应始终对其进行测试。
测试forward
功能完全取决于您,因为每个层都有不同的功能。
测试该backward
方法很 容易 ,因为该方法仅实现了一个渐变,forward
可以自动进行数值测试!
签出test_gradient_for_python_layer
测试实用程序: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!
特别通知
值得一提的是,python代码仅在CPU上运行。因此,如果您计划在网络 中间 放置一个Python层,那么当您计划使用GPU时,性能将会 大大
下降。发生这种情况是因为caffe需要在调用python层之前将blob从GPU复制到CPU,然后再复制回GPU才能进行向前/向后传递。
如果python层是输入层或最顶层的损失层,则这种降级的意义就不那么明显了。
更新:
2017年9月19日,PR#5904合并为master。此PR通过python接口公开blob的GPU指针。您可以直接从python直接访问blob._gpu_data_ptr和blob._gpu_diff_ptr,后果
自负 。