Caffe-Windows添加新层
来源:互联网 发布:淘宝上架宝贝数量限制 编辑:程序博客网 时间:2024/06/06 01:50
Caffe-Windows添加新层
Caffe自身提供了很多Layer,可以实现一些基础的功能。然而,很多情况下我们需要自己定义一些函数(Layer)嵌入到CNN中。下面是我的实现过程。
Caffe-Windows+CPU+VS2013
首先给出我添加的层(头文件和源文件)
all_pass_layer.hpp
#ifndef CAFFE_ALL_PASS_LAYER_HPP_ #define CAFFE_ALL_PASS_LAYER_HPP_ #include <vector> #include "caffe/blob.hpp" #include "caffe/layer.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/layers/neuron_layer.hpp" namespace caffe { template <typename Dtype> class AllPassLayer : public NeuronLayer<Dtype> { public: explicit AllPassLayer(const LayerParameter& param) : NeuronLayer<Dtype>(param) {} virtual inline const char* type() const { return "AllPass"; } protected: virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual void Forward_gpu(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); virtual void Backward_gpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); };} // namespace caffe #endif // CAFFE_ALL_PASS_LAYER_HPP_
all_pass_layer.cpp
#include <algorithm> #include <vector> #include "caffe/layers/all_pass_layer.hpp" #include <iostream> using namespace std;#define DEBUG_AP(str) cout<<str<<endl namespace caffe { template <typename Dtype> void AllPassLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const Dtype* bottom_data = bottom[0]->cpu_data(); Dtype* top_data = top[0]->mutable_cpu_data(); const int count = bottom[0]->count(); for (int i = 0; i < count; ++i) { top_data[i] = bottom_data[i]; } DEBUG_AP("Here is All Pass Layer, forwarding."); DEBUG_AP(this->layer_param_.all_pass_param().key()); } template <typename Dtype> void AllPassLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { if (propagate_down[0]) { const Dtype* bottom_data = bottom[0]->cpu_data(); const Dtype* top_diff = top[0]->cpu_diff(); Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); const int count = bottom[0]->count(); for (int i = 0; i < count; ++i) { bottom_diff[i] = top_diff[i]; } } DEBUG_AP("Here is All Pass Layer, backwarding."); DEBUG_AP(this->layer_param_.all_pass_param().key()); }#ifdef CPU_ONLY STUB_GPU(AllPassLayer);#endif INSTANTIATE_CLASS(AllPassLayer); REGISTER_LAYER_CLASS(AllPass);} // namespace caffe
以上定义的层实现了一种全通功能。这边的层比较简单,只作为一个例子方便实验。
将.hpp文件放到路径caffe-windows\caffe-master\include\caffe\layers下
将.cpp文件放到路径caffe-windows\caffe-master\src\caffe\layers下
修改caffe-windows\caffe-master\src\caffe\proto,共需要修改两处(关键)!
1.找到 LayerParameter 描述,增加一项:optional AllPassParameter all_pass_param =145;
注意,这里的参数设置不能与LayerParameter中其他参数相同!
2.仍然在 caffe.proto 中,增加 AllPassParameter 声明,位置任意。其功能是可以用于从 prototxt 中读取预设值。message AllPassParameter {
optional float key = 1 [default = 0];
}
以上两步完成后,我们需要的层的内容和参数都已经设置好了。接下来就是将我们定义的层加载到Caffe中去。
打开Caffe工程
添加头文件
添加源文件
万事具备,编译下caffe工程
应该是没有问题的,编译完后就可以使用啦- -!
下面简单测试一下
deploy.prototxt
name: “AllPassTest”
layer {
name: “data”
type: “Input”
top: “data”
input_param { shape: { dim: 10 dim: 3 dim: 227 dim: 227 } }
}
layer {
name: “ap”
type: “AllPass”
bottom: “data”
top: “conv1”
all_pass_param {
key: 12.88
}
}
.\Build\x64\Release\caffe.exe time -model examples/deploy.prototxt
pause
好啦,后面需要做的就是根据自己的需要修改层的内容。祝大家学习愉快!
———-参考资料http://blog.csdn.net/zb1165048017/article/details/59112034
http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/kkk584520/article/details/52721838
http://blog.csdn.net/u014565333/article/details/54017025
- windows-caffe添加新层
- caffe添加新层windows
- Caffe-Windows添加新层
- caffe添加新层windows
- caffe添加新层windows
- 【caffe-Windows】新层添加——LSTM
- caffe添加新层教程
- caffe添加新层教程
- caffe 添加一个新层
- caffe添加新层教程
- caffe中添加新层
- caffe添加一个新层
- caffe添加新的层
- caffe添加新层教程
- caffe添加新层教程
- 在caffe中添加新的层
- caffe 添加新的网络层
- 为caffe添加新的层
- 内部div居中显示
- iOS 解决首次push到含有UITextView的界面,会卡顿的问题
- hdu 3518-后缀数组(无重叠子串)
- Excel排名
- Ajax 原理过程 同步与异步区别 优缺点
- Caffe-Windows添加新层
- 告别拖延症
- OSI模型、TCP/IP模型及常用网络协议
- IntelliJ IDEA 常用设置讲解
- AAR 库怎么来配置
- 递归经典整数划分问题
- 1.10.ARM裸机第十部分-SD卡启动详解
- 伪氏排名规则
- Android Studio导入工程报错[Plugin with id 'com.android.application' not found]