CVPR2018有一篇detection的文章STDN,我之前记录过此文的笔记,文章的上采样方法和DUC基本无异(-!!), 本人仍然坚守在caffe,目前要用到这个层,所以把这个层的代码实现一下。

先不谈源码,等backward写完后在放到我的git上面。因为之前也没怎么写过layer,所以此处记录会犯错的地方。

1. 有一些在setup,reshape,forward等都会用到的变量,要定义在hpp中,如果只在set up 中定义了,那么其他函数无权访问;

2.对于hpp中定义的变量,例如int group_;若在setup中对group进行赋值,其它函数礽需要使用同一个值,那么setup赋值时group_前面什么都不要加,就写group_=...,如果加了int,就表示这个变量仅属于setup了,其他函数就无法访问;

3. setup和reshape中变量无法互通,比如setup中num=bottom[0]->num()获得batchsize,在reshape中一定要继续写上这一条语句,否则无法获得正确的batchsize.forward等函数则不需要;

4.hpp结尾的时候不要忘了#endif;  cpp结尾不要忘了INSTANTIATE_CLASS(EnlargeLayer);REGISTER_LAYER_CLASS(Enlarge);

贴一下源码。github 地址为https://github.com/dlyldxwl/Similar-DUC-Caffe-implement

可能会不一样的地方是,当bottom channels不能整除c^2的时候,top的最后一个channel对应的最后一张map我是用剩下的maps类似与做"mean pool"得到的,backward 类似于mean pool

#ifndef CAFFE_ENLARGE_LAYER_HPP_
#define CAFFE_ENLARGE_LAYER_HPP_#include <vector>#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"namespace caffe {template <typename Dtype>
class EnlargeLayer : public Layer<Dtype> {public:explicit EnlargeLayer(const LayerParameter& param ): Layer<Dtype>(param) {}virtual void LayerSetup(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top);virtual void Reshape(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top);virtual inline const char* type() const { return "Enlarge"; }virtual inline int ExactNumBottomBlobs() const { return 1; }virtual inline int ExactNumTopBlobs() const { return 1; }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);int group_;int img_size_;int batch_;int ch_ori_;int h_ori_;int w_ori_;int scale_;
};}
#endif

cpp文件

#include <algorithm>
#include <vector>
#include "caffe/layers/enlarge_layer.hpp"namespace caffe {template <typename Dtype>
void EnlargeLayer<Dtype>::LayerSetup(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top)
{img_size_ = this->layer_param_.enlarge_param().size();CHECK_GT(img_size_,0)<<"feature map size must be greater than 0";ch_ori_ = bottom[0]->channels();h_ori_ = bottom[0]->height();w_ori_ = bottom[0]->width();scale_ = int(img_size_ / h_ori_);group_ = int(ch_ori_ / (scale_*scale_)); //channels after enlargeCHECK_EQ(h_ori_,w_ori_)<<"the width and height of the feature map to be sampled are equal";CHECK_GT(img_size_,h_ori_)<<"size param need be greater than feature map size";}template <typename Dtype>
void EnlargeLayer<Dtype>:: Reshape(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top)
{img_size_ = this->layer_param_.enlarge_param().size();batch_ = bottom[0]->num();ch_ori_ = bottom[0]->channels();h_ori_ = bottom[0]->height();w_ori_ = bottom[0]->width();scale_ = int(img_size_ / h_ori_);group_ = int(ch_ori_ / (scale_*scale_));top[0]->Reshape(batch_,group_,img_size_,img_size_);
}template <typename Dtype>
void EnlargeLayer<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 sp_os = bottom[0]->count(2);const int sp_ns = top[0]->count(2); const int extra_maps_ = int(ch_ori_%(scale_*scale_));for (int m = 0; m < batch_; ++m){for (int n = 0; n < group_; ++n){if ((n!=group_-1)||(extra_maps_==0)) {for (int h = 0; h < img_size_; ++h){for (int w = 0; w < img_size_; ++w){int index_n = h*img_size_ + w; //index of top(new) feature mapint index_o = (h%scale_*scale_ + w%scale_)*sp_os + (h / scale_*w_ori_ + w / scale_);// index of bottom(old) feature maptop_data[index_n] = bottom_data[index_o];}}bottom_data += scale_*scale_*sp_os;top_data += sp_ns;}else{for (int h = 0; h < img_size_; ++h){for (int w = 0; w < img_size_; ++w){int index_n = h*img_size_ + w; //index of top(new) feature mapint map_ind_o = h%scale_*scale_ + w%scale_;if (map_ind_o != scale_*scale_-1) {int index_o = map_ind_o*sp_os + (h / scale_*w_ori_ + w / scale_);// index of bottom(old) feature maptop_data[index_n] = bottom_data[index_o];}else{Dtype sum=0.0;for (int i=0;i<=extra_maps_;++i){int index_extra = (map_ind_o+i)*sp_os + (h / scale_*w_ori_ + w / scale_);sum+=bottom_data[index_extra];}Dtype ave=sum/(extra_maps_+1);top_data[index_n] = ave;}}}}}}
}template <typename Dtype>
void EnlargeLayer<Dtype>:: Backward_cpu(const vector<Blob<Dtype>*>& top,const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom)
{if(propagate_down[0]){const Dtype* top_diff = top[0]->cpu_diff();Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();const int sp_ns = top[0]->count(2);const int sp_os = bottom[0]->count(2);const int extra_maps_ = int(ch_ori_%(scale_*scale_));for (int m = 0; m < batch_; ++m){for (int n = 0; n < group_; ++n){if ((n!=group_-1)||(extra_maps_==0)){for (int h = 0; h < img_size_; ++h){for (int w = 0; w < img_size_; ++w){int index_n = h*img_size_ + w; //index of top feature mapint index_o = (h%scale_*scale_ + w%scale_)*sp_os + (h / scale_*w_ori_ + w / scale_);// index of bottom feature mapbottom_diff[index_o] = top_diff[index_n];}}bottom_diff += scale_*scale_*sp_os;top_diff += sp_ns;}else{for (int h = 0; h < img_size_; ++h){for (int w = 0; w < img_size_; ++w){int index_n = h*img_size_ + w; //index of top(new) feature mapint map_ind_o = h%scale_*scale_ + w%scale_;if (map_ind_o != scale_*scale_-1){int index_o = map_ind_o*sp_os + (h / scale_*w_ori_ + w / scale_);bottom_diff[index_o] = top_diff[index_n];}else{Dtype ave_diff = top_diff[index_n]/(extra_maps_+1);for (int i=0;i<=extra_maps_;++i){int index_extra = (map_ind_o+i)*sp_os + (h / scale_*w_ori_ + w / scale_);bottom_diff[index_extra] = ave_diff;}} }}}}}}
}#ifdef CPU_ONLY
STUB_GPU(EnlargeLayer);
#endifINSTANTIATE_CLASS(EnlargeLayer);
REGISTER_LAYER_CLASS(Enlarge);}

DUC/STDN的caffe源码实现相关推荐

  1. Caffe源码中Solver文件分析

    Caffe源码(caffe version commit: 09868ac , date: 2015.08.15)中有一些重要的头文件,这里介绍下include/caffe/solver.hpp文件的 ...

  2. Caffe源码中Net文件分析

    Caffe源码(caffe version commit: 09868ac , date: 2015.08.15)中有一些重要的头文件,这里介绍下include/caffe/net.hpp文件的内容: ...

  3. Caffe源码中Pooling Layer文件分析

    Caffe源码(caffe version commit: 09868ac , date: 2015.08.15)中有一些重要的头文件,这里介绍下include/caffe/vision_layers ...

  4. Caffe源码中layer文件分析

    Caffe源码(caffe version commit: 09868ac , date: 2015.08.15)中有一些重要的头文件,这里介绍下include/caffe/layer.hpp文件的内 ...

  5. Caffe源码中io文件分析

    Caffe源码(caffe version commit: 09868ac , date: 2015.08.15)中有一些重要的头文件,这里介绍下include/caffe/util/io.hpp文件 ...

  6. Caffe源码中blob文件分析

    Caffe源码(caffe version commit: 09868ac , date: 2015.08.15)中有一些重要的头文件,这里介绍下include/caffe/blob.hpp文件的内容 ...

  7. Caffe源码中syncedmem文件分析

    Caffe源码(caffe version:09868ac , date: 2015.08.15)中有一些重要文件,这里介绍下syncedmem文件. 1.      include文件: (1).& ...

  8. Caffe源码中math_functions文件分析

    Caffe源码(caffe version:09868ac , date: 2015.08.15)中有一些重要文件,这里介绍下math_functions文件. 1.      include文件: ...

  9. Caffe源码中caffe.proto文件分析

    Caffe源码(caffe version:09868ac , date: 2015.08.15)中有一些重要文件,这里介绍下caffe.proto文件. 在src/caffe/proto目录下有一个 ...

最新文章

  1. OSI中端到端与点到点区别。
  2. java发送加密报文_RSA加密---从后台到客户端实现报文加解密
  3. 利用注解 + 反射消除重复代码,妙!
  4. 《密码与安全新技术专题》第11周作业
  5. 当字符串为空但不为空时
  6. Spring MVC会话教程
  7. android 设置drawable大小,在Android中调整Drawable大小
  8. 为什么要用 SpringMVC 的 SessionStatus
  9. 华为路由器配置Telnet登录
  10. android 按键流程及映射
  11. 计算机网络中出现异常流量,计算机网络下网络流量异常的检测算法
  12. idea编辑窗口显示outdated version解决方案
  13. Typora简单传图(Typora+PicGo-Core+SMMS/阿里云OSS 实现图床)
  14. 03-stable diffusion国风小姐姐
  15. 《C语言程序设计》江宝钏主编-习题5-4-素数表!!!!!
  16. 【Error】西部数据磁盘插上不显示盘符
  17. [VBA]EXCEL表格,运行VBA报错:运行时错误‘9’ 下标越界
  18. PT100三线制恒流源接法
  19. 1w+大学生在线学习,弘玑Cyclone与伯禹教育开展大学生RPA认证集训营
  20. StringBuffer的使用

热门文章

  1. 「强烈收藏」Python第三方库资源大全,1000+工具包
  2. 博奥智源科技,发布阅卷系统开发性能及技术功能分享
  3. 有道云笔记修改背景图片
  4. 根据后端接口文档写前端参数
  5. JS判断IE,FF等浏览器类型
  6. 优炫数据库出席用友商业创新大会,携手伙伴赋能数智化生态
  7. Topsis算法(优劣解距离法)——综合评价方法
  8. xmind序列码激活亲测可用版本
  9. java实现批量插入数据
  10. 在Linux VSCode中编写调试C++解决ipch文件过大问题