caffe添加层的步骤
来源:互联网 发布:qt access数据库 2003 编辑:程序博客网 时间:2024/05/14 21:08
转载地址:http://blog.csdn.net/happyflyy/article/details/54866037
针对2017年2月时caffe官网版本。
1. caffe官网中添加新层的流程
官方增加新层的流程可见如下链接:
https://github.com/BVLC/caffe/wiki/Development
- Add a class declaration for your layer to
include/caffe/layers/your_layer.hpp
.- Include an inline implementation of
type
overriding the methodvirtual inline const char* type() const { return "YourLayerName"; }
replacingYourLayerName
with your layer’s name. - Implement the
{*}Blobs()
methods to specify blob number requirements; see /caffe/include/caffe/layers.hpp to enforce strict top and bottom Blob counts using the inline{*}Blobs()
methods. - Omit the
*_gpu
declarations if you’ll only be implementing CPU code.
- Include an inline implementation of
- Implement your layer in
src/caffe/layers/your_layer.cpp
.- (optional)
LayerSetUp
for one-time initialization: reading parameters, fixed-size allocations, etc. Reshape
for computing the sizes of top blobs, allocating buffers, and any other work that depends on the shapes of bottom blobsForward_cpu
for the function your layer computesBackward_cpu
for its gradient (Optional – a layer can be forward-only)
- (optional)
- (Optional) Implement the GPU versions
Forward_gpu
andBackward_gpu
inlayers/your_layer.cu
. - If needed, declare parameters in
proto/caffe.proto
, using (and then incrementing) the “next available layer-specific ID” declared in a comment abovemessage LayerParameter
- Instantiate and register your layer in your cpp file with the macro provided in
layer_factory.hpp
. Assuming that you have a new layerMyAwesomeLayer
, you can achieve it with the following command:
- 1
- 2
- 3
- Note that you should put the registration code in your own cpp file, so your implementation of a layer is self-contained.
- Optionally, you can also register a Creator if your layer has multiple engines. For an example on how to define a creator function and register it, see
GetConvolutionLayer
incaffe/layer_factory.cpp
. - Write tests in
test/test_your_layer.cpp
. Usetest/test_gradient_check_util.hpp
to check that your Forward and Backward implementations are in numerical agreement.
2. 增加新层实践
因为官网的只给出了流程,并没给出具体例子,因此将结合添加maxout层对整个流程进行说明。
旧版本添加maxout以及maxout原理可参考下面博客。
http://blog.csdn.net/kuaitoukid/article/details/41865803
Step1: 确定要添加的层的基类
相比于上面博客中旧版caffe对层的分类,现在的caffe中层的分类有所改变,去掉了vision层,直接由layer层派生。除此之外还有loss层,neuron层,以及data层。
- loss层和data层顾名思义,不加赘述
- 输入blob和输出blob的大小一样,从neuron层派生。例如激活层ReLU,以及逐点操作的exp层和power层。需要实现虚函数SetUp
,Forward_cpu
,Backward_cpu
。
- 输入blob和输出blob的大小不一样,直接从layer层派生。例如conv层,将要添加的maxout层。需要实现虚函数SetUp
,Reshape
,Forward_cpu
,Backward_cpu
Step2: caffe.proto定义该层的参数
- 添加Maxout LayerParameter的ID
在message LayerParameter
最后一行添加MyMaxoutParameter
,并将ID按顺序设置为没有用过的数字。
- 1
message LayerParameter
的注释中有最后添加的层名以及可用的ID号,为了便于以后使用,建议更改一下。
- 1
- 2
- 3
- 4
- 添加Maxout layer的参数消息
在caffe.proto任意位置添加Maxout layer的参数消息:
- 1
- 2
- 3
- 4
- 5
- 添加Maxout layer的层名
在message V1LayerParameter
中的enum LayerType
添加Maxout层的层名:
- 1
同时添加:
- 1
数字只要不重复就可以。
Step3: 增加maxout层的头文件到./include/caffe/layers/mymaxout.hpp
主要在MyMaxoutLayer
类中定义构造函数和SetUp
,Reshape
,Forward_cpu
,Backward_cpu
函数以及一些变量。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
Step4: 增加maxout层的源文件到./src/caffe/layers/mymaxout.cpp SetUp
: 进行check Reshape
: 更改top blob的大小 Forward_cpu
: 实现正向传播 Backward_cpu
: 实现反向传播 REGISTER_LAYER_CLASS
: 最后注册层。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
Step5: 重新编译
- 1
- 2
3. 注意事项
从layer层派生一定得实现四个虚函数
SetUp
,Reshape
,Forward_cpu
,Backward_cpu
;从neuron派生则不需要实现Reshape
函数。虚函数的函数名,函数返回值,函数参数以及参数类型必须得和基类中的定义一致,建议直接从基类中拷贝。否则可能在编译时报如下错误:
- caffe添加层的步骤
- caffe中添加新的网络层的步骤
- caffe 添加新的网络层 (步骤)
- caffe添加自己的层
- caffe添加新的层
- Caffe添加自定义的层
- 【Caffe实践】 添加自己的网络层
- 【Caffe实践】 添加自己的网络层
- 在caffe中添加新的层
- Caffe 中添加自己的网络层
- caffe 添加新的网络层
- 为caffe添加新的层
- caffe-windows添加自己的loss层
- 如何给caffe添加新的层
- caffe中添加新的网络层
- caffe 添加Python层
- caffe添加新层教程
- caffe添加新层教程
- OpenGL矩阵和坐标变换
- Linux进程的组成部分
- attr()和prop()
- Mybatis获取插入记录的自增长ID
- websocket实现简单聊天程序
- caffe添加层的步骤
- 浅谈HTTP协议
- AndroidStudio SVN 使用笔记
- 一步步带你做vue后台管理框架(二)——上手使用
- highcharts 仪表盘样式总结(gauge)
- Spring Boot 微服务集群日志等
- PL/SQL子程序存储过程IN,OUT,IN OUT参数模式详解
- js模板引擎-art-template常用总结
- 设计模式-概述;面向对象基本原则