1、在src\caffe\proto\caffe.proto中搜索message LayerParameter,在optional ReLUParameter relu_param = 123之后添加optional ReLU6Parameter relu6_param = 208; (最后的分号别忘了)

2、搜索message ReLUParameter,在这个ReLUParameter实现结构之后添加

// Message that stores parameters used by ReLU6Layer
message ReLU6Parameter {
  enum Engine {
    DEFAULT = 0;
    CAFFE = 1;
    CUDNN = 2;
  }
  optional Engine engine = 2 [default = DEFAULT];
}

支持proto头文件修改完毕,接下来添加所需的头文件和实现文件。

1.在blob/ssd/include/caffe/layers文件夹下新建relu6_layer.hpp,将

  1.  
  1. #ifndef CAFFE_RELU_LAYER_HPP_
  2. #define CAFFE_RELU_LAYER_HPP_
  3.  
  4. #include <vector>
  5.  
  6. #include "caffe/blob.hpp"
  7. #include "caffe/layer.hpp"
  8. #include "caffe/proto/caffe.pb.h"
  9.  
  10. #include "caffe/layers/neuron_layer.hpp"
  11.  
  12. namespace caffe {
  13.  
  14. /**
  15. * @brief Rectified Linear Unit non-linearity @f$ y = \min(6, \max(0, x)) @f$.
  16. * The simple max is fast to compute, and the function does not saturate.
  17. */
  18. template <typename Dtype>
  19. class ReLU6Layer : public NeuronLayer<Dtype> {
  20. public:
  21. /**
  22. * @param param provides ReLUParameter relu_param,
  23. * with ReLULayer options:
  24. * - negative_slope (\b optional, default 0).
  25. * the value @f$ \nu @f$ by which negative values are multiplied.
  26. */
  27. explicit ReLU6Layer(const LayerParameter& param)
  28. : NeuronLayer<Dtype>(param) {}
  29.  
  30. virtual inline const char* type() const { return "ReLU6"; }
  31.  
  32. protected:
  33. /**
  34. * @param bottom input Blob vector (length 1)
  35. * -# @f$ (N \times C \times H \times W) @f$
  36. * the inputs @f$ x @f$
  37. * @param top output Blob vector (length 1)
  38. * -# @f$ (N \times C \times H \times W) @f$
  39. * the computed outputs @f$
  40. * y = \max(0, x)
  41. * @f$ by default. If a non-zero negative_slope @f$ \nu @f$ is provided,
  42. * the computed outputs are @f$ y = \max(0, x) + \nu \min(0, x) @f$.
  43. */
  44. virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
  45. const vector<Blob<Dtype>*>& top);
  46. virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
  47. const vector<Blob<Dtype>*>& top);
  48.  
  49. /**
  50. * @brief Computes the error gradient w.r.t. the ReLU inputs.
  51. *
  52. * @param top output Blob vector (length 1), providing the error gradient with
  53. * respect to the outputs
  54. * -# @f$ (N \times C \times H \times W) @f$
  55. * containing error gradients @f$ \frac{\partial E}{\partial y} @f$
  56. * with respect to computed outputs @f$ y @f$
  57. * @param propagate_down see Layer::Backward.
  58. * @param bottom input Blob vector (length 1)
  59. * -# @f$ (N \times C \times H \times W) @f$
  60. * the inputs @f$ x @f$; Backward fills their diff with
  61. * gradients @f$
  62. * \frac{\partial E}{\partial x} = \left\{
  63. * \begin{array}{lr}
  64. * 0 & \mathrm{if} \; x \le 0 \\
  65. * \frac{\partial E}{\partial y} & \mathrm{if} \; x > 0
  66. * \end{array} \right.
  67. * @f$ if propagate_down[0], by default.
  68. * If a non-zero negative_slope @f$ \nu @f$ is provided,
  69. * the computed gradients are @f$
  70. * \frac{\partial E}{\partial x} = \left\{
  71. * \begin{array}{lr}
  72. * \nu \frac{\partial E}{\partial y} & \mathrm{if} \; x \le 0 \\
  73. * \frac{\partial E}{\partial y} & \mathrm{if} \; x > 0
  74. * \end{array} \right.
  75. * @f$.
  76. */
  77. virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
  78. const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  79. virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
  80. const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  81. };
  82.  
  83. } // namespace caffe
  84.  
  85. #endif // CAFFE_RELU_LAYER_HPP_
  1.  

2.在blob/ssd/src/caffe/layers文件夹下新建relu6_layer.cpp,将

  1. #include <algorithm>
  2. #include <vector>
  3.  
  4. #include "caffe/layers/relu6_layer.hpp"
  5.  
  6. namespace caffe {
  7.  
  8. template <typename Dtype>
  9. void ReLU6Layer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
  10. const vector<Blob<Dtype>*>& top) {
  11. const Dtype* bottom_data = bottom[0]->cpu_data();
  12. Dtype* top_data = top[0]->mutable_cpu_data();
  13. const int count = bottom[0]->count();
  14. for (int i = 0; i < count; ++i) {
  15. top_data[i] = std::min(std::max(bottom_data[i], Dtype(0)), Dtype(6));
  16. }
  17. }
  18.  
  19. template <typename Dtype>
  20. void ReLU6Layer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
  21. const vector<bool>& propagate_down,
  22. const vector<Blob<Dtype>*>& bottom) {
  23. if (propagate_down[0]) {
  24. const Dtype* bottom_data = bottom[0]->cpu_data();
  25. const Dtype* top_diff = top[0]->cpu_diff();
  26. Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
  27. const int count = bottom[0]->count();
  28. for (int i = 0; i < count; ++i) {
  29. bottom_diff[i] = top_diff[i] *
  30. ((bottom_data[i] > 0 && bottom_data[i] < 6));
  31. }
  32. }
  33. }
  34.  
  35. #ifdef CPU_ONLY
  36. STUB_GPU(ReLU6Layer);
  37. #endif
  38.  
  39. INSTANTIATE_CLASS(ReLU6Layer);
  40. REGISTER_LAYER_CLASS(ReLU6);
  41.  
  42. } // namespace caffe

3.在blob/ssd/src/caffe/layers文件夹下新建relu6_layer.cu,将

  1. #include <algorithm>
  2. #include <vector>
  3.  
  4. #include "caffe/layers/relu6_layer.hpp"
  5.  
  6. namespace caffe {
  7.  
  8. template <typename Dtype>
  9. __global__ void ReLU6Forward(const int n, const Dtype* in, Dtype* out) {
  10. CUDA_KERNEL_LOOP(index, n) {
  11. out[index] = in[index] < 0 ? 0: (in[index] > 6 ? 6 : in[index]);
  12. }
  13. }
  14.  
  15. template <typename Dtype>
  16. void ReLU6Layer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
  17. const vector<Blob<Dtype>*>& top) {
  18. const Dtype* bottom_data = bottom[0]->gpu_data();
  19. Dtype* top_data = top[0]->mutable_gpu_data();
  20. const int count = bottom[0]->count();
  21. // NOLINT_NEXT_LINE(whitespace/operators)
  22. ReLU6Forward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(
  23. count, bottom_data, top_data);
  24. CUDA_POST_KERNEL_CHECK;
  25. // << " count: " << count << " bottom_data: "
  26. // << (unsigned long)bottom_data
  27. // << " top_data: " << (unsigned long)top_data
  28. // << " blocks: " << CAFFE_GET_BLOCKS(count)
  29. // << " threads: " << CAFFE_CUDA_NUM_THREADS;
  30. }
  31.  
  32. template <typename Dtype>
  33. __global__ void ReLU6Backward(const int n, const Dtype* in_diff,
  34. const Dtype* in_data, Dtype* out_diff) {
  35. CUDA_KERNEL_LOOP(index, n) {
  36. out_diff[index] = in_diff[index] * ((in_data[index] > 0)
  37. && (in_data[index] < 6));
  38. }
  39. }
  40.  
  41. template <typename Dtype>
  42. void ReLU6Layer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
  43. const vector<bool>& propagate_down,
  44. const vector<Blob<Dtype>*>& bottom) {
  45. if (propagate_down[0]) {
  46. const Dtype* bottom_data = bottom[0]->gpu_data();
  47. const Dtype* top_diff = top[0]->gpu_diff();
  48. Dtype* bottom_diff = bottom[0]->mutable_gpu_diff();
  49. const int count = bottom[0]->count();
  50. // NOLINT_NEXT_LINE(whitespace/operators)
  51. ReLU6Backward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(
  52. count, top_diff, bottom_data, bottom_diff);
  53. CUDA_POST_KERNEL_CHECK;
  54. }
  55. }
  56.  
  57. INSTANTIATE_LAYER_GPU_FUNCS(ReLU6Layer);
  58.  
  59. } // namespace caffe

重新编译ssd。

SSD: ReLU6的更多相关文章

  1. 移动端目标识别(3)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之Running on mobile with TensorFlow Lite (写的很乱,回头更新一个简洁的版本)

    承接移动端目标识别(2) 使用TensorFlow Lite在移动设备上运行         在本节中,我们将向您展示如何使用TensorFlow Lite获得更小的模型,并允许您利用针对移动设备优化 ...

  2. SSD框架训练自己的数据集

    SSD demo中详细介绍了如何在VOC数据集上使用SSD进行物体检测的训练和验证.本文介绍如何使用SSD实现对自己数据集的训练和验证过程,内容包括: 1 数据集的标注2 数据集的转换3 使用SSD如 ...

  3. 光驱SSD安装Win7+ubuntu系统双系统

    准备条件: U盘,32GB,三星品牌 SSD,120GB,三星品牌 win7旗舰版,Ghost系统(安装简单嘛),Ylmf_Ghost_Win7_SP1_x64_2016_1011.iso ubunt ...

  4. 创建Azure DS 虚拟机并附加SSD硬盘

    $subscriptionName = "Windows Azure Enterprise Trial" #订阅名称 $location = "China East&qu ...

  5. 关闭电脑SSD的磁盘碎片整理

    小白往往会把机械硬盘时代的习惯带进固态硬盘时代,比如碎片整理.机械硬盘时代砖家最喜欢告诉小白:“系统慢了吧?赶紧碎片整理撒.”小白屁颠屁颠地整理去了.殊不知碎片整理对于SSD来说完全就是种折磨.这种“ ...

  6. SQL Server 2014新特性探秘(2)-SSD Buffer Pool Extension

    简介     SQL Server 2014中另一个非常好的功能是,可以将SSD虚拟成内存的一部分,来供SQL Server数据页缓冲区使用.通过使用SSD来扩展Buffer-Pool,可以使得大量随 ...

  7. Macbook SSD硬盘空间不够用了?来个Xcode大瘦身吧!

    原文转自:http://www.jianshu.com/p/03fed9a5fc63    日期:2016-04-22 最近突然发现我的128G SSD硬盘只剩下可怜的8G多,剩下这么少的一点空间连X ...

  8. 搭把手教美工妹妹如何通过升级SSD提升电脑性能

    -----by LinHan 不单单适用于妹子,我这名的意思的妹子也能看懂. 以下教程依据实践和部分互联网资料总结得出,向博客园, CSDN的前辈们致谢:同时,如有说的不正确或有不到位的地方,麻烦指出 ...

  9. [archlinux][hardware] ThankPad T450自带SSD做bcache之后的使用寿命分析

    这个分析的起因,是由于我之前干了这两个事: [troubleshoot][archlinux][bcache] 修改linux文件系统 / 分区方案 / 做混合硬盘 / 系统转生大!手!术!(调整底层 ...

随机推荐

  1. npm的安装和使用?

    参考: http://www.cnblogs.com/chyingp/p/npm.html 在css中使用变量, 采用less或sass来编译css 改变网页网站开发和构建的方式, 除了用emmet( ...

  2. cannot open window service on computer '.' in window application

    1.配置错误,需要检查对应的windows service的exe文件所在文件夹下的log 2.在命令行通过Start-Service启动,需要有管理员权限.

  3. Print a file's last modified date in Bash

    date -r <filename> #!/usr/bin/env bash for i in /var/log/*.out; do stat -f "%Sm" -t ...

  4. P3301 [SDOI2013]方程

    思路 容斥的挺好的练习题 对于第二个条件,可以直接使m减去suma2,使得第二个条件舍去,然后m再减去n,使得问题转化成有n1个变量要满足小于等于某个数的条件,其他的随便取,求整数解的个数 对n1,以 ...

  5. 【ASP.Net】publish asp.net to local IIS

    做web项目难免要将项目部署, 要么部署在azure上,要么部署在本地, 使用IIS去host. 部署步骤很简单, 1. vs打开你的web项目, 项目名上面右键选择publish 2. 在弹出的pu ...

  6. Kubernetes简介

    Kubernetes is an open-source platform designed to automate deploying, scaling, and operating applica ...

  7. spring-tool-suite使用教程,并创建spring配置文件

    本文为博主原创,未经允许不得转载: 在应用springMVC框架的时候,每次创建spring的xml配置文件时,需要很多步骤,非常麻烦. 所以spring提供了spring-tool-suite插件, ...

  8. HDU 3401 Trade(斜率优化dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...

  9. 中国地区免费注册bitcointalk论坛教程

    bitcointalk论坛是著名的老牌比特币论坛,中本聪当年也在这里和各路大神探讨.但现在国家的高墙禁止网民访问. 你可能会用一个国外的代理工具来看贴,看贴确实可以,但是如果想注册,注册完后就会发现帐 ...

  10. 《spring boot 实战》读书笔记

    前言:虽然已经用spring boot开发过一套系统,但是之前都是拿来主义,没有系统的,全面的了解过这套框架.现在通过学习<spring boot实战>这本书,希望温故知新.顺便实现自己的 ...