[英]What is a `“Python”` layer in caffe?
Prune和Bharat的答案给出了"Python"
层的总体目的:通用层,用python而不是c ++实现。
我打算将这个答案作为使用"Python"
层的教程。
"Python"
图层教程 "Python"
图层? 为了使用'Python"
层,你需要用flag编译caffe
WITH_PYTHON_LAYER := 1
在'Makefile.config'
设置。
"Python"
层? "Python"
层应该实现为从caffe.Layer
基类派生的python类。 该类必须具有以下四种方法:
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
- 从原型文本传递到self.param_str
的字符串。 有关更多信息,请参阅此主题 。
def reshape(self, bottom, top)
:只要caffe重塑网络,就会调用此方法。 此函数应分配输出(每个top
blob)。 输出的形状通常与bottom
的形状有关。
def forward(self, bottom, top)
:从bottom
到top
实施前进传球。
def backward(self, top, propagate_down, bottom)
:此方法实现反向传播,它从top
bottom
传播渐变。 propagate_down
是len(bottom)
的布尔向量,指示应该传播渐变的bottom
。
有关bottom
和top
输入的更多信息,您可以在本文中找到。
例子
你可以在 这里和这里看到一些简化的python层的例子。
可以在此处找到“移动平均”输出层的示例。
可训练的参数
"Python"
层可以具有可训练的参数(如"Conv"
, "InnerProduct"
等)。
您可以在此主题和此 主题中找到有关添加可训练参数的更多信息。 在caffe git中还有一个非常简单的例子。
"Python"
图层? 有关细节,请参阅巴拉特的回答。
您需要将以下内容添加到您的原型文本中:
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
}
}
NetSpec
接口添加"Python"
图层? 这很简单:
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
这样当caffe import
它时 - 它就可以找到了。
例如,如果您的模块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合并为主人。 这个PR通过python接口暴露了blob的GPU指针。 您可以直接从python访问blob._gpu_data_ptr和blob._gpu_diff_ptr, 风险自负 。
很简单,它是一个层, 您可以在其中提供实现代码,而不是使用其中一个预定义类型 - 这些类型都由高效函数支持。
如果要定义自定义丢失函数,请继续:自己编写,然后使用Python类型创建图层。 如果您有非标准的输入需求,也许是一些特定于数据的预处理,没问题:自己编写,并使用Python类型创建图层。
Python层与需要编译的C ++层不同,它们的参数需要添加到proto文件中,最后需要在layer_factory中注册层。 如果你编写一个python层,你不必担心这些事情。 层参数可以定义为字符串,可以在python中作为字符串访问。 例如:如果图层中有参数,则可以使用'self.param_str'访问它,前提是在原型文件中定义了param_str。 与其他图层一样,您需要使用以下函数定义类:
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'
layer: 'AnchorTargetLayer'
param_str: "'feat_stride': 16"
}
}
这里,层的名称是rpn-data,bottom和top分别是层的输入和输出细节。 python_param定义了Python层的参数。 'module'指定图层的文件名。 如果名为“anchor_target_layer.py”的文件位于名为“rpn”的文件夹中,则参数将为“rpn.anchor_target_layer”。 'layer'参数是您的类的名称,在这种情况下它是'AnchorTargetLayer'。 'param_str'是图层的参数,其中包含键'feat_stride'的值16。
与C ++ / CUDA层不同,截至目前,Python层在caffe中的多GPU设置中不起作用,因此使用它们是一个缺点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.