【Caffe实践】 添加自己的网络层

来源:互联网 发布:战舰少女r官方淘宝店 编辑:程序博客网 时间:2024/05/10 00:27

from:http://blog.csdn.net/chenriwei2/article/details/46432727

写在前面:

Caffe 中有众多的网络层,最新版本的代码已经涵盖了很多种类型的网络层,然而,有时候由于各种原因,其给定的网络层不能满足我们的要求,这时候就要对其更改,以使其满足自己的需求,感谢作者开源代码以及众多的代码维护者。

由于Caffe 中的网络层都是直接或者间接地给予Layer 基类,所以,在我们需要添加新的类型时,就需要选择好自己的基类,以使我们能够更好的利用基类已有的一些方法。我们新建的类可以基于 
1. 直接继承于Layer 
2. 继承于DataLayer 
3. 继承于NeuronLayer 
4. 继承于LossLayer 
5. 或者如果是直接想改进某个层的代码,可以直接继承该类,并做相对应的修改即可。

具体步骤:

第一步, 添加头文件.h ,可以自己新建一个头文件,或者追加到一个已有的头文件后面,比如include/DataLayer.hpp 等, 在里面定义一些类变量等。

第二步,添加实现文件,创建类的实现函数,放在src/XXX.cpp 中,如果有cuda实现,则需要添加相对应的.cu文件实现,主要需要写LayerSetup ,reshape ,forward ,backup 函数。

第三步, 注册类,到src/caffe/layer_factory.cpp 中添加对应的类,在其中加入

<code class="hljs scss has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-function" style="box-sizing: border-box;">REGISTER_LAYER_CLASS(MYLAYER, MyLayer)</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

第四步,在src/caffe/proto/caffe.proto 文件中,添加对应的类以及类参数。 
在enum LayerType {}中添加一行

<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">enum</span> <span class="hljs-title" style="box-sizing: border-box;">LayerType</span> {MYLAYER = ID }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//其中MYLAYER 是新添加的类名,ID是类编号,注意不能更之前的重复。</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

再添加类参数

<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">optional MylayerParameter mylayer_param </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> ID2;//其中MylayerParameter 是网络层参数,ID2 是ID</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

最后定义类参数

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">message MylayerParameter {  optional <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">uint32</span> myparam1 = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;   optional <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> myparam2 = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">default</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>]; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

第五步,写测试文件,这个是可选的,不过为了合并到github中,需要测试通过。

0 0
原创粉丝点击