Caffe学习系列(15):添加新层
如何在Caffe中增加一层新的Layer呢?主要分为四步:
(1)在./src/caffe/proto/caffe.proto 中增加对应layer的paramter message;
(2)在./include/caffe/***layers.hpp中增加该layer的类的声明,***表示有common_layers.hpp,
data_layers.hpp, neuron_layers.hpp, vision_layers.hpp 和loss_layers.hpp等;
(3)在./src/caffe/layers/目录下新建.cpp和.cu(GPU)文件,进行类实现。
(4)在./src/caffe/gtest/中增加layer的测试代码,对所写的layer前传和反传进行测试,测试还包括速度。(可省略,但建议加上)
这位博主添加了一个计算梯度的网络层,简介明了:
http://blog.csdn.net/shuzfan/article/details/51322976
这几位博主增加了自定义的loss层,可供参考:
http://blog.csdn.net/langb2014/article/details/50489305
http://blog.csdn.net/tangwei2014/article/details/46815231
我以添加precision_recall_loss层来学习代码,主要是precision_recall_loss_layer.cpp的实现
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <vector>
#include <opencv2/opencv.hpp> #include "caffe/layer.hpp"
#include "caffe/util/io.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/vision_layers.hpp" namespace caffe { //初始化,调用父类进行相应的初始化
template <typename Dtype>
void PrecisionRecallLossLayer<Dtype>::LayerSetUp(
const vector<Blob<Dtype>*> &bottom, const vector<Blob<Dtype>*> &top) {
LossLayer<Dtype>::LayerSetUp(bottom, top);
}
//进行维度变换
template <typename Dtype>
void PrecisionRecallLossLayer<Dtype>::Reshape(
const vector<Blob<Dtype>*> &bottom,
const vector<Blob<Dtype>*> &top) {
//同样先调用父类的Reshape,通过成员变量loss_来改变输入维度
LossLayer<Dtype>::Reshape(bottom, top);
loss_.Reshape(bottom[]->num(), bottom[]->channels(),
bottom[]->height(), bottom[]->width()); // Check the shapes of data and label 检查两个输入的维度是否想等
CHECK_EQ(bottom[]->num(), bottom[]->num())
<< "The number of num of data and label should be same.";
CHECK_EQ(bottom[]->channels(), bottom[]->channels())
<< "The number of channels of data and label should be same.";
CHECK_EQ(bottom[]->height(), bottom[]->height())
<< "The heights of data and label should be same.";
CHECK_EQ(bottom[]->width(), bottom[]->width())
<< "The width of data and label should be same.";
}
//前向传导
template <typename Dtype>
void PrecisionRecallLossLayer<Dtype>::Forward_cpu(
const vector<Blob<Dtype>*> &bottom, const vector<Blob<Dtype>*> &top) {
const Dtype *data = bottom[]->cpu_data();
const Dtype *label = bottom[]->cpu_data();
const int num = bottom[]->num(); //num和count什么区别
const int dim = bottom[]->count() / num;
const int channels = bottom[]->channels();
const int spatial_dim = bottom[]->height() * bottom[]->width();
//存疑?
const int pnum =
this->layer_param_.precision_recall_loss_param().point_num();
top[]->mutable_cpu_data()[] = ;
//对于每个通道
for (int c = ; c < channels; ++c) {
Dtype breakeven = 0.0;
Dtype prec_diff = 1.0;
for (int p = ; p <= pnum; ++p) {
int true_positive = ; //统计每类的个数
int false_positive = ;
int false_negative = ;
int true_negative = ;
for (int i = ; i < num; ++i) {
const Dtype thresh = 1.0 / pnum * p; //计算阈值?
for (int j = ; j < spatial_dim; ++j) {
//取得相应的值和标签
const Dtype data_value = data[i * dim + c * spatial_dim + j];
const int label_value = (int)label[i * dim + c * spatial_dim + j];
//统计
if (label_value == && data_value >= thresh) {
++true_positive;
}
if (label_value == && data_value >= thresh) {
++false_positive;
}
if (label_value == && data_value < thresh) {
++false_negative;
}
if (label_value == && data_value < thresh) {
++true_negative;
}
}
}
//计算precision和recall
Dtype precision = 0.0;
Dtype recall = 0.0;
if (true_positive + false_positive > ) {
precision =
(Dtype)true_positive / (Dtype)(true_positive + false_positive);
} else if (true_positive == ) { //都是负类?
precision = 1.0;
}
if (true_positive + false_negative > ) {
recall =
(Dtype)true_positive / (Dtype)(true_positive + false_negative);
} else if (true_positive == ) {
recall = 1.0;
}
if (prec_diff > fabs(precision - recall) //如果二c者相差小
&& precision > && precision <
&& recall > && recall < ) {
breakeven = precision; //保留
prec_diff = fabs(precision - recall);
}
}
top[]->mutable_cpu_data()[] += 1.0 - breakeven; //计算误差
}
top[]->mutable_cpu_data()[] /= channels; //???
}
//反向
template <typename Dtype>
void PrecisionRecallLossLayer<Dtype>::Backward_cpu(
const vector<Blob<Dtype>*> &top,
const vector<bool> &propagate_down,
const vector<Blob<Dtype>*> &bottom) {
for (int i = ; i < propagate_down.size(); ++i) {
if (propagate_down[i]) { NOT_IMPLEMENTED; }
}
}
#ifdef CPU_ONLY
STUB_GPU(PrecisionRecallLossLayer);
#endif //注册该层
INSTANTIATE_CLASS(PrecisionRecallLossLayer);
REGISTER_LAYER_CLASS(PrecisionRecallLoss); } // namespace caffe
- template <typename Dtype>
- void PrecisionRecallLossLayer<Dtype>::Forward_cpu(
- const vector<Blob<Dtype>*> &bottom, const vector<Blob<Dtype>*> &top) {
- const Dtype *data = bottom[0]->cpu_data();
- const Dtype *label = bottom[1]->cpu_data();
- const int num = bottom[0]->num();
- const int dim = bottom[0]->count() / num;
- const int channels = bottom[0]->channels();
- const int spatial_dim = bottom[0]->height() * bottom[0]->width();
- const int pnum =
- this->layer_param_.precision_recall_loss_param().point_num();
- top[0]->mutable_cpu_data()[0] = 0;
- for (int c = 0; c < channels; ++c) {
- Dtype breakeven = 0.0;
- Dtype prec_diff = 1.0;
- for (int p = 0; p <= pnum; ++p) {
- int true_positive = 0;
- int false_positive = 0;
- int false_negative = 0;
- int true_negative = 0;
- for (int i = 0; i < num; ++i) {
- const Dtype thresh = 1.0 / pnum * p;
- for (int j = 0; j < spatial_dim; ++j) {
- const Dtype data_value = data[i * dim + c * spatial_dim + j];
- const int label_value = (int)label[i * dim + c * spatial_dim + j];
- if (label_value == 1 && data_value >= thresh) {
- ++true_positive;
- }
- if (label_value == 0 && data_value >= thresh) {
- ++false_positive;
- }
- if (label_value == 1 && data_value < thresh) {
- ++false_negative;
- }
- if (label_value == 0 && data_value < thresh) {
- ++true_negative;
- }
- }
- }
- Dtype precision = 0.0;
Caffe学习系列(15):添加新层的更多相关文章
- Caffe学习系列(3):视觉层(Vision Layers)及参数
所有的层都具有的参数,如name, type, bottom, top和transform_param请参看我的前一篇文章:Caffe学习系列(2):数据层及参数 本文只讲解视觉层(Vision La ...
- 转 Caffe学习系列(3):视觉层(Vision Layers)及参数
所有的层都具有的参数,如name, type, bottom, top和transform_param请参看我的前一篇文章:Caffe学习系列(2):数据层及参数 本文只讲解视觉层(Vision La ...
- Caffe学习系列(2):数据层及参数
要运行caffe,需要先创建一个模型(model),如比较常用的Lenet,Alex等, 而一个模型由多个屋(layer)构成,每一屋又由许多参数组成.所有的参数都定义在caffe.proto这个文件 ...
- 转 Caffe学习系列(2):数据层及参数
http://www.cnblogs.com/denny402/p/5070928.html 要运行caffe,需要先创建一个模型(model),如比较常用的Lenet,Alex等, 而一个模型由多个 ...
- Caffe学习系列(15):计算图片数据的均值
图片减去均值后,再进行训练和测试,会提高速度和精度.因此,一般在各种模型中都会有这个操作. 那么这个均值怎么来的呢,实际上就是计算所有训练样本的平均值,计算出来后,保存为一个均值文件,在以后的测试中, ...
- Caffe学习系列(4):激活层(Activiation Layers)及参数
在激活层中,对输入数据进行激活操作(实际上就是一种函数变换),是逐元素进行运算的.从bottom得到一个blob数据输入,运算后,从top输入一个blob数据.在运算过程中,没有改变数据的大小,即输入 ...
- 转 Caffe学习系列(4):激活层(Activiation Layers)及参数
在激活层中,对输入数据进行激活操作(实际上就是一种函数变换),是逐元素进行运算的.从bottom得到一个blob数据输入,运算后,从top输入一个blob数据.在运算过程中,没有改变数据的大小,即输入 ...
- Caffe 学习系列
学习列表: Google protocol buffer在windows下的编译 caffe windows 学习第一步:编译和安装(vs2012+win 64) caffe windows学习:第一 ...
- Caffe学习系列(23):如何将别人训练好的model用到自己的数据上
caffe团队用imagenet图片进行训练,迭代30多万次,训练出来一个model.这个model将图片分为1000类,应该是目前为止最好的图片分类model了. 假设我现在有一些自己的图片想进行分 ...
随机推荐
- DataReader和DataSet的异同
DataReader:使用时始终占用SqlConnection,在线操作数据库:每次只在内存中加载一条数据,所以占用的内存是很小的:是只进的. 只读的. DataSet:则是将数据一次性加载在内存中. ...
- Wget下载终极用法和15个详细的例子
Wget下载终极用法和15个详细的例子 备注:wget 不支持https 下载,也没有相关https参数,当下载https的时候或以改用 axelWget是一种很好用的因特网下载工具,他具有的很多特 ...
- C++中引用与指针的区别(详细介绍)
C++中引用与指针的区别(详细介绍) C++中的引用与指针的区别 指向不同类型的指针的区别在于指针类型可以知道编译器解释某个特定地址(指针指向的地址)中的内存内容及大小,而void*指针则只表示一 ...
- fileinput模块
刚才练习的时候,报如下错误: AttributeError: module 'fileinput' has no attribute 'input',后来Google参考这篇文章https://mai ...
- Unity3D热更全书
http://www.cnblogs.com/crazylights/p/3897742.html
- 最近这么火的iOS视频直播
快速集成iOS基于RTMP的视频推流 http://www.jianshu.com/p/8ea016b2720e iOS视频直播初窥:高仿<喵播APP> http://www.jiansh ...
- 简单CSS布局留用
1.导航栏固定显示代码,文字居中,z-index header{ position: fixed; top: 0px; left: 10%; width: 80%; height: 80px; bor ...
- 快递api网接口快递调用方法
----------------实体类 [DataContract] public class SyncResponseEntity { public SyncResponseEntity() { } ...
- jQuery 鼠标拖拽移动窗口
拖拽移动需要注意的是:拖拽移动的窗口是如何定位的,如果"left"属性为"%" ,以"margin-left"来计算定位,如下实例,如果&q ...
- Python画图笔记
matplotlib的官方网址:http://matplotlib.org/ 问题 Python Matplotlib画图,在坐标轴.标题显示这五个字符 ⊥ + - ⊺ ⨁,并且保存后也能显示 h ...