注册
登录
Caffe
caffe中的“ Python”层是什么?
返回
caffe中的“ Python”层是什么?
作者:
狗头军师
发布时间:
2024-12-30 01:27:55 (1月前)
Caffe具有图层类型“Python”。
例如,此图层类型可以用作损失图层。
在其他情况下,它用作输入层。
这是什么层类型?
该层如何使用?
收藏
举报
2 条回复
1#
回复此人
v-star*위위
|
2020-07-30 14-37
35 Prune和Bharat的答案给出了一个"Python"层的总体目的:一个通用层,它是用python而不是c ++实现的。 我打算将此答案用作使用"Python"图层的教程。 "Python"图层教程 什么是"Python"层? 请查看Prune和Bharat的出色回答。 前提条件 为了使用'Python"图层,您需要使用flag编译caffe WITH_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重塑网络时,都会调用此方法。此函数应分配输出(每个topBlob)。输出的形状通常与bottoms的形状有关。 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"层可以具有可训练参数(如"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"使用pythonic NetSpec界面添加图层? 很简单: ``` 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合并为master。此PR通过python接口公开blob的GPU指针。您可以直接从python直接访问blob._gpu_data_ptr和blob._gpu_diff_ptr,后果自负。
编辑
登录
后才能参与评论