caffe中添加自定义的layer
2018-03-22 11:31
423 查看
有两种方式,一种是使用python layer相对简单,另一种是使用C++。
1.python layer
参考 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 be in $PYTHONPATH
module: 'pyloss'
# the layer name -- the class name in the module
layer: 'EuclideanLossLayer'
}
# set loss weight so Caffe knows this is a loss layer
loss_weight: 1
}module的名字就是自定义的layer的python文件的文件名,比如上面的新文件就是pyloss.py,文件需在$PYTHONPAT路径下。layer的名字就是新定义的类名,比如上面的类名就是EuclideanLossLayer。该类一般必须包含四个函数,分别是setup、reshape、forword、backword。
下面是示例,解释python层该怎么写。创建pyloss.py,并定义EuclideanLossLayer。import caffe
import numpy as np
class EuclideadLossLayer(caffe.Layer):#EuclideadLossLayer没有权值,反向传播过程中不需要进行权值的更新。如果需要定义需要更新自身权值的层,最好还是使用C++
def setup(self,bottom,top):
#在网络运行之前根据相关参数参数进行layer的初始化
if len(bottom) !=2:
raise exception("Need two inputs to compute distance")
def reshape(self,bottom,top):
#在forward之前调用,根据bottom blob的尺寸调整中间变量和top blob的尺寸
if bottom[0].count !=bottom[1].count:
raise exception("Inputs must have the same dimension.")
self.diff=np.zeros_like(bottom[0].date,dtype=np.float32)
top[0].reshape(1)
def forward(self,bottom,top):
#网络的前向传播
self.diff[...]=bottom[0].data-bottom[1].data
top[0].data[...]=np.sum(self.diff**2)/bottom[0].num/2.
def backward(self,top,propagate_down,bootm):
#网络的前向传播
for i in range(2):
if not propagate_down[i]:
continue
if i==0:
sign=1
else:
sign=-1
bottom[i].diff[...]=sign*self.diff/bottom[i].num
https://github.com/BVLC/caffe/issues/684
https://chrischoy.github.io/research/making-caffe-layer/
Here's roughly the process I follow.
Add a class declaration for your layer to the appropriate one of
Implement your layer in
(Optional) Implement the GPU versions
Add your layer to
Make your layer createable by adding it to
Write tests in
1.python layer
参考 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 be in $PYTHONPATH
module: 'pyloss'
# the layer name -- the class name in the module
layer: 'EuclideanLossLayer'
}
# set loss weight so Caffe knows this is a loss layer
loss_weight: 1
}module的名字就是自定义的layer的python文件的文件名,比如上面的新文件就是pyloss.py,文件需在$PYTHONPAT路径下。layer的名字就是新定义的类名,比如上面的类名就是EuclideanLossLayer。该类一般必须包含四个函数,分别是setup、reshape、forword、backword。
下面是示例,解释python层该怎么写。创建pyloss.py,并定义EuclideanLossLayer。import caffe
import numpy as np
class EuclideadLossLayer(caffe.Layer):#EuclideadLossLayer没有权值,反向传播过程中不需要进行权值的更新。如果需要定义需要更新自身权值的层,最好还是使用C++
def setup(self,bottom,top):
#在网络运行之前根据相关参数参数进行layer的初始化
if len(bottom) !=2:
raise exception("Need two inputs to compute distance")
def reshape(self,bottom,top):
#在forward之前调用,根据bottom blob的尺寸调整中间变量和top blob的尺寸
if bottom[0].count !=bottom[1].count:
raise exception("Inputs must have the same dimension.")
self.diff=np.zeros_like(bottom[0].date,dtype=np.float32)
top[0].reshape(1)
def forward(self,bottom,top):
#网络的前向传播
self.diff[...]=bottom[0].data-bottom[1].data
top[0].data[...]=np.sum(self.diff**2)/bottom[0].num/2.
def backward(self,top,propagate_down,bootm):
#网络的前向传播
for i in range(2):
if not propagate_down[i]:
continue
if i==0:
sign=1
else:
sign=-1
bottom[i].diff[...]=sign*self.diff/bottom[i].num
2.C++方式
参考https://github.com/BVLC/caffe/issues/684
https://chrischoy.github.io/research/making-caffe-layer/
Here's roughly the process I follow.
Add a class declaration for your layer to the appropriate one of
common_layers.hpp,
data_layers.hpp,
loss_layers.hpp,
neuron_layers.hpp, or
vision_layers.hpp. Include an inline implementation of
typeand the
*Blobs()methods to specify blob number requirements. Omit the
*_gpudeclarations if you'll only be implementing CPU code.
Implement your layer in
layers/your_layer.cpp.
SetUpfor initialization: reading parameters, allocating buffers, etc.
Forward_cpufor the function your layer computes
Backward_cpufor its gradient
(Optional) Implement the GPU versions
Forward_gpuand
Backward_gpuin
layers/your_layer.cu.
Add your layer to
proto/caffe.proto, updating the next available ID. Also declare parameters, if needed, in this file.
Make your layer createable by adding it to
layer_factory.cpp.
Write tests in
test/test_your_layer.cpp. Use
test/test_gradient_check_util.hppto check that your Forward and Backward implementations are in numerical agreement.
相关文章推荐
- 在caffe中添加自定义的layer
- 在caffe中添加自定义的layer
- (15)caffe总结之自定义Layer的实现
- 原始caffe添加CenterLoss_layer
- 深度学习笔记(1):caffe 添加新层 attention LSTM layer和LSTM layer代码精读
- caffe中添加自己的layer(以triplet loss为例)
- 如何在caffe中添加新的Layer
- caffe 加权交叉熵损失函数层(weighted sigmoid_cross_entropy_loss_layer)添加方法
- 【深夜福利】Caffe 增加自定义 Layer 及其 ProtoBuffer 参数
- 【caffe实用技巧与bug排查】三步添加新layer及可能出现的bug
- Marble 添加自定义Layer
- Caffe python layer 的自定义
- 怎样在caffe中添加layer以及caffe中triplet loss layer的实现
- 17.2.23 caffe如何自定义一个layer
- 使用Caffe 增加自定义 Layer 及其 ProtoBuffer 参数
- 在caffe中添加新的Layer
- 如何在caffe中添加新的Layer
- 如何在caffe中添加新的Layer
- Caffe 单独测试添加的layer
- 17.3.9 caffe自定义一个Layer会遇到的一些理解方面的问题