caffe增长新的layer

---恢复内容开始---html

在caffe中若是想要增长新的功能层,必需要本身在caffe的安装目录下(source code)中增长相应的文件git

大致步骤以下:github

  1. 在caffe/src/caffe/proto/caffe.proto中增长对应layer的parameter message,  有两部分,如今LayerParameter中注册新层名字,注意选取不重复的ID, 而后写上新层的message传递的参数
  2. 在caffe/include/caffe/layers/中添加相应的新层的hpp文件,看其余层的实现,仿照着写
  3. 在caffe/src/caffe/layers/增长相应的.cpp和.cu的文件,进行类的实现, 在文件的末尾仿照着写个注册的语句REGISTE....
  4. 在caffe/src/caffe/gtest中增长新层的测试代码,这样能够保证所添加的层是正确的,不会在以后的运行中报错

参考教程: https://blog.csdn.net/tangwei2014/article/details/46815231api

http://www.javashuo.com/article/p-towekvit-hm.html数组

tensorflow ckpt转 caffemodel遇到的坑函数

 

龙明盛老师的论文基本都是caffe   能够看他是怎么实现的一些层测试

 

一个别人实现的新层例子: spa

https://github.com/luoyetx/OrdinalRegression.net

 

具体实现:3d

  1. 在newlayer.hpp中常定义一些常量或者变量,用来在前传中存储中间计算结果,由于若是定义在forward计算过程当中,那么反传时还要重复计算,因此定义在hpp文件中比较合适
  2. 在newlayer.cpp中,主要有三个功能须要实现: 
    1. LayerSetUp: 主要是作一些CHECK工做,而后根据bottom和top对类中的数据成员初始化    注意通常都是继承基类,若是基类实现了就不要再实现了
    2. Forward_cpu: 前传
    3. backward_cpu: 反传,计算梯度  
  3. 在newlayer.cu中,实现GPU下的前传和反传
  4. 测试代码
  5. 从新编译, make all      make test    make runtest

 

 一些基础知识:

  1. Blob 是一个模板类,至关因而个结构体,主要的成员变量有 data_, diff_, shape_, count_, capacity_,   成员函数主要有Reshape, ReshapeLike, SharedData, Updata
    1. 参考: https://blog.csdn.net/seven_first/article/details/47398613
    2. https://www.jianshu.com/p/59e77efcce83
    3.  

  2. Layer:  卷积层
    1. 参考: https://blog.csdn.net/xizero00/article/details/51049858
    2. 官网api查找:  https://caffe.berkeleyvision.org/doxygen/namespacecaffe.html

 

Kevin 老师帮忙整理的资料

1.  bottom是个blob指针的数组,bottom[0]第一个输入的指针, bottom[1]第二个输入指针, top[0]第一个输出指针

LayerSetUp函数中:

int bottom_batch_size_ = bottom[0]->num();
int bottom_channels_ = bottom[0]->channels();
int bottom_height_ = bottom[0]->height();
int bottom_width_ = bottom[0]->width();

注意,对于指针取数据用->  对于单纯blob取数据 用 .

blob<Dtype>*  表示指向blob的指针,其中blob的数据类型是Dtype

可是若是要取blob对应的数据地址要用 ->cpu_data()  这个相似于取数据的地址操做

Forward_cpu函数中:

Dtype* top_data = top[0]->mutable_cpu_data();  //mutable_cpu_data()表示top[0]数据可修改   

const Dtype* bottom_data = bottom[0]->cpu_data();   //-> cpu_data()表示只可读

 

caffe自带的数学函数 https://blog.csdn.net/seven_first/article/details/47378697

 

2.  shape操做

vector<int> top_shape = bottom[0]->shape();
top[0]->Reshape(top_shape);

若是top[0]的channel是bottom[0]的channel+bottom[1]的channel,其余的都同样,能够这样定义:
vector<int> top_shape = bottom[0]->shape();
top_shape[1] = bottom[0]->shape(1)+bottom[0]->shape(1);
top[0]->Reshape(top_shape);

 

若是这个变量是一个中间变量的话,能够这样定义:
Blob<Dtype> conf_permute_; //通常写在.hpp里
而后在.cpp中的LayerSetUp或者Reshape中定义 conf_permute_.ReshapeLike(*(bottom[1]));

注意成员函数的参数都是类型的,好比是blob指针,就能够直接输bottom[0], 若是要求参数是blob, 

若是有取地址符,那么只须要传入实体,这样也能修改内容

若是是指针的话是 *的形式

 

3. 关于count

const int count = bottom[0]->count();表示 count = bottom[0]的num * channel * height * width
const int count = bottom[0]->count(0, 1); 表示 count = bottom[0]的num * channel
const int count = bottom[0]->count(0, 2); 表示 count = bottom[0]的num * channel* height
const int count = bottom[0]->count(1); 表示 count = bottom[0]的 channel * height * width
const int count = bottom[0]->count(2); 表示 count = bottom[0]的 height * width

const int num = bottom[0]->shape(0); //bottom[0]的batch_size
const int channel = bottom[0]->shape(1); //bottom[0]的channel
const int height = bottom[0]->shape(2); //bottom[0]的height
const int width = bottom[0]->shape(3); //bottom[0]的width


3.printf:
const int count = bottom[0]->count();
printf("count %d\n", count);


4.gdb Debug调试:
https://zhuanlan.zhihu.com/p/28146796

相关文章
相关标签/搜索