faster rcnn的源码理解(一)SmoothL1LossLayer论文与代码的结合理解

转载自:faster rcnn的源码理解(一)SmoothL1LossLayer论文与代码的结合理解 - 野孩子的专栏 - 博客频道 - CSDN.NET
http://blog.csdn.net/u010668907/article/details/51456928

源码:

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. // ------------------------------------------------------------------  
  2. // Fast R-CNN  
  3. // Copyright (c) 2015 Microsoft  
  4. // Licensed under The MIT License [see fast-rcnn/LICENSE for details]  
  5. // Written by Ross Girshick  
  6. // ------------------------------------------------------------------  
  7.   
  8. #include "caffe/fast_rcnn_layers.hpp"  
  9.   
  10. namespace caffe {  
  11.   
  12. template <typename Dtype>  
  13. __global__ void SmoothL1Forward(const int n, const Dtype* in, Dtype* out,  
  14.     Dtype sigma2) {  
  15.   // f(x) = 0.5 * (sigma * x)^2          if |x| < 1 / sigma / sigma  
  16.   //        |x| - 0.5 / sigma / sigma    otherwise  
  17.   CUDA_KERNEL_LOOP(index, n) {  
  18.     Dtype val = in[index];  
  19.     Dtype abs_val = abs(val);  
  20.     if (abs_val < 1.0 / sigma2) {  
  21.       out[index] = 0.5 * val * val * sigma2;  
  22.     } else {  
  23.       out[index] = abs_val - 0.5 / sigma2;  
  24.     }  
  25.   }  
  26. }  
  27.   
  28. template <typename Dtype>  
  29. void SmoothL1LossLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,  
  30.     const vector<Blob<Dtype>*>& top) {  
  31.   int count = bottom[0]->count();  
  32.   caffe_gpu_sub(  
  33.       count,  
  34.       bottom[0]->gpu_data(),  
  35.       bottom[1]->gpu_data(),  
  36.       diff_.mutable_gpu_data());    // d := b0 - b1  
  37.   if (has_weights_) {  
  38.     // apply "inside" weights  
  39.     caffe_gpu_mul(  
  40.         count,  
  41.         bottom[2]->gpu_data(),  
  42.         diff_.gpu_data(),  
  43.         diff_.mutable_gpu_data());  // d := w_in * (b0 - b1)  
  44.   }  
  45.   SmoothL1Forward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(  
  46.       count, diff_.gpu_data(), errors_.mutable_gpu_data(), sigma2_);  
  47.   CUDA_POST_KERNEL_CHECK;  
  48.   
  49.   if (has_weights_) {  
  50.     // apply "outside" weights  
  51.     caffe_gpu_mul(  
  52.         count,  
  53.         bottom[3]->gpu_data(),  
  54.         errors_.gpu_data(),  
  55.         errors_.mutable_gpu_data());  // d := w_out * SmoothL1(w_in * (b0 - b1))  
  56.   }  
  57.   
  58.   Dtype loss;  
  59.   caffe_gpu_dot(count, ones_.gpu_data(), errors_.gpu_data(), &loss);  
  60.   top[0]->mutable_cpu_data()[0] = loss / bottom[0]->num();  
  61. }  
  62.   
  63. template <typename Dtype>  
  64. __global__ void SmoothL1Backward(const int n, const Dtype* in, Dtype* out,  
  65.     Dtype sigma2) {  
  66.   // f'(x) = sigma * sigma * x         if |x| < 1 / sigma / sigma  
  67.   //       = sign(x)                   otherwise  
  68.   CUDA_KERNEL_LOOP(index, n) {  
  69.     Dtype val = in[index];  
  70.     Dtype abs_val = abs(val);  
  71.     if (abs_val < 1.0 / sigma2) {  
  72.       out[index] = sigma2 * val;  
  73.     } else {  
  74.       out[index] = (Dtype(0) < val) - (val < Dtype(0));  
  75.     }  
  76.   }  
  77. }  
  78.   
  79. template <typename Dtype>  
  80. void SmoothL1LossLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,  
  81.     const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {  
  82.   // after forwards, diff_ holds w_in * (b0 - b1)  
  83.   int count = diff_.count();  
  84.   SmoothL1Backward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(  
  85.       count, diff_.gpu_data(), diff_.mutable_gpu_data(), sigma2_);  
  86.   CUDA_POST_KERNEL_CHECK;  
  87.   for (int i = 0; i < 2; ++i) {  
  88.     if (propagate_down[i]) {  
  89.       const Dtype sign = (i == 0) ? 1 : -1;  
  90.       const Dtype alpha = sign * top[0]->cpu_diff()[0] / bottom[i]->num();  
  91.       caffe_gpu_axpby(  
  92.           count,                           // count  
  93.           alpha,                           // alpha  
  94.           diff_.gpu_data(),                // x  
  95.           Dtype(0),                        // beta  
  96.           bottom[i]->mutable_gpu_diff());  // y  
  97.       if (has_weights_) {  
  98.         // Scale by "inside" weight  
  99.         caffe_gpu_mul(  
  100.             count,  
  101.             bottom[2]->gpu_data(),  
  102.             bottom[i]->gpu_diff(),  
  103.             bottom[i]->mutable_gpu_diff());  
  104.         // Scale by "outside" weight  
  105.         caffe_gpu_mul(  
  106.             count,  
  107.             bottom[3]->gpu_data(),  
  108.             bottom[i]->gpu_diff(),  
  109.             bottom[i]->mutable_gpu_diff());  
  110.       }  
  111.     }  
  112.   }  
  113. }  
  114.   
  115. INSTANTIATE_LAYER_GPU_FUNCS(SmoothL1LossLayer);  
  116.   
  117. }  // namespace caffe  

SmoothL1LossLayer 计算一张图片的损失函数,对应于下图的加号右边部分

 

imini-batchanchor的索引。

Pi是目标的预测概率。

有物体时pi*1,否则为0

ti是一个向量,预测坐标

ti*是一个向量,是gt包围盒的坐标

 

bottom[0]预测坐标,对应于下图的ti

bottom[1]target坐标,对应于下图的ti*

bottom[2]inside,有物体(fg)时为1,否则为0,对应于下图的pi*

bottom[3]outside,没有前景(fg)也没有后景(bg)的为0,其他为1/bg+fg),对应于加号右边的系数部分(但其实这个地方我本人还是不懂,因为论文上说的系数都是一些固定的值,如入=10。初始代码一直在更新,估计又换了别的方法。不论如何,在现在的代码中outside是乘以了后面的结果)

 

Lreg的公式就是下图,另x=ti - ti*

 

 

Pi*Leg(ti, ti*)表明只有有fg20个物体类别)的才有回归损失

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值