修改caffe源代码--添加loss(层)函数--CPU
来源:互联网 发布:网络连接受限制有叹号 编辑:程序博客网 时间:2024/06/06 10:08
本文主体参考:http://blog.csdn.net/sihailongwang/article/details/72783944
因其在windows上面操作,本文提供在Linux上操作的尝试。另外部分命名不规则,所以修正了一下。
修改caffe源代码,loss层是一个比较独立的一个层,而且可以仿照caffe给的样例进行添加,难度会稍微小点。
caffe自带了十种loss层(contrastive、euclidean、hinge、multinomial_logistic、
sigmoid_cross_entropy、smooth_L1、smooth_L1_ohem、
softmax、softmax_ohem、infogain)
详见:http://blog.csdn.NET/sihailongwang/article/details/72657637
公式含义推荐:http://blog.csdn.net/u012177034/article/details/52144325
接下来,就是自己添加一个新的loss(层)函数了,我打算添加:Absolute loss
第一步:在caffe.proto增加对应的LayerParameter 和 message:
在caffe.proto中的message LayerParameter 中添加下面这行:
在caffe.proto中的某个与message LayerParameter 平级的位置添加下面这段代码:
第二步:打开./include/caffe/下的文件loss_layers.hpp,在caffe命名空间最下面添加:
template <typename Dtype> class AbsoluteLossLayer : public LossLayer<Dtype> { public: explicit AbsoluteLossLayer(const LayerParameter& param) : LossLayer<Dtype>(param), dis_() {} virtual void Reshape(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual inline const char* type() const { return "AbsoluteLoss"; } virtual inline bool AllowForceBackward(const int bottom_index) const { return true; } protected: /// @copydoc AbsoluteLossLayer virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual void Backward_cpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); Blob<Dtype> dis_; };
第三步:在./src/caffe/util/math_functions.cpp中增加“绝对值求和”模板函数,在caffe命名空间最下面添加:
template <typename Dtype> Dtype caffe_cpu_abs_sum(const int n, const Dtype* x) { return caffe_cpu_asum(n, x); } template float caffe_cpu_asum<float>(const int n, const float* x); template double caffe_cpu_asum<double>(const int n, const double* x);
第四步:在./src/caffe/layers/下增加相应layer的CPU/GPU实现文件:
CPU版本(absolute_loss_layer.cpp):
#include <vector> #include "caffe/loss_layers.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { template <typename Dtype> void AbsoluteLossLayer<Dtype>::Reshape( const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { LossLayer<Dtype>::Reshape(bottom, top); //在LossLayer 中定义 CHECK_EQ(bottom[0]->count(1), bottom[1]->count(1)) //保证输入维度相同 << "Inputs must have the same dimension."; dis_.ReshapeLike(*bottom[0]); //Blob 类型的diff_用来存放两个bottom的差,和bottom具有相同的 } template <typename Dtype> void AbsoluteLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { int count = bottom[0]->count(); //总共有count个featuremap caffe_sub( count, bottom[0]->cpu_data(), bottom[1]->cpu_data(), dis_.mutable_cpu_data()); //diff_ = bottom[0] - bottom[1] Dtype loss_param = this->layer_param_.absolute_loss_param().dis(); Dtype abs_sum = caffe_cpu_abs_sum(count,dis_.cpu_data()); //Dtype dot = caffe_cpu_abs_sum()(count, diff_.cpu_data(), dis_.cpu_data()); Dtype loss = loss_param * abs_sum / bottom[0]->num(); top[0]->mutable_cpu_data()[0] = loss; } template <typename Dtype> void AbsoluteLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { for (int i = 0; i < 2; ++i) { if (propagate_down[i]) { //对于输入的label bottom propagate_dowm为0 const Dtype sign = (i == 0) ? 1 : -1; const Dtype alpha = sign * top[0]->cpu_diff()[0] / bottom[i]->num(); caffe_cpu_axpby( bottom[i]->count(), // count alpha, // alpha dis_.cpu_data(), // a Dtype(0), // beta bottom[i]->mutable_cpu_diff()); // b } //bottom[i]->mutable_cpu_diff()) = alpha*dis_.cpu_data() } } INSTANTIATE_CLASS(AbsoluteLossLayer); REGISTER_LAYER_CLASS(AbsoluteLoss); } // namespace caffe
第五步:打开caffe,进行编译,编译成功即可、
- 修改caffe源代码--添加loss(层)函数--CPU
- 修改caffe源代码--添加loss(层)函数--GPU
- 修改caffe源代码从添加loss(层)函数开始
- DeepLearning(基于caffe)实战项目(8)--修改caffe源代码从添加loss(层)函数开始
- caffe-windows添加自己的loss层
- Caffe添加自定义层-自定义loss
- Caffe Loss层
- Caffe Loss层
- Caffe Loss 层
- center loss代码注释(caffe新添加层)
- caffe中的loss函数
- caffe之(五)loss层
- caffe: 新建一个loss层
- caffe层笔记系列Loss层
- caffe中各种loss函数
- 【caffe学习笔记】loss layer 损失层
- 新版caffe添加自己的层(目前只学会添加,我想要添加的loss还没能实现)
- 在caffe 中添加Scale-invariant loss
- BlueNRG How-to
- Linux之文件及目录常用操作
- 面向对象概述、this、private
- jQuery Mobile中按钮button的data-*选项
- 淘淘商城系列(x)Active整合spring实现商品同步索引
- 修改caffe源代码--添加loss(层)函数--CPU
- Python学习笔记——20170830
- 构造方法、static
- 列表页面查询的内容来自多个表的处理方法
- 浅谈IT行业加班有没有价值
- Logback+ELK+SpringMVC搭建日志收集服务器
- 未来计划,助力成长
- jQuery Mobile中复选框checkbox的data-*选项
- 代码块、继承、重写、final