r-cnn学习(五):SmoothL1LossLayer论文与代码的结合理解

A Loss Function for Learning Region Proposals

训练RPN时,只对两种anchor给予正标签:和gt_box有着最高的IoU && IoU超过0.7。如果对于

所有的gt_box,其IoU都小于0.3,则标记为负。损失函数定义如下:

其中i为一个mini-batch中某anchor的索引,pi表示其为目标的预测概率,pi*表示gt_box(正为1,否则为0)。

ti和ti*分别表示预测框的位置和gt_box框的位置。Lreg如下:

bound-box regression中各参数的计算方式为:

   (4)

其对应的SmoothL1LossLayer代码如下:

// ------------------------------------------------------------------
// Fast R-CNN
// Copyright (c) 2015 Microsoft
// Licensed under The MIT License [see fast-rcnn/LICENSE for details]
// Written by Ross Girshick
// ------------------------------------------------------------------  

#include "caffe/fast_rcnn_layers.hpp"  

namespace caffe {
 //SmoothL1的前向计算,即(3)式
template <typename Dtype>
__global__ void SmoothL1Forward(const int n, const Dtype* in, Dtype* out,
    Dtype sigma2) {
  // f(x) = 0.5 * (sigma * x)^2          if |x| < 1 / sigma / sigma
  //        |x| - 0.5 / sigma / sigma    otherwise
  CUDA_KERNEL_LOOP(index, n) {
    Dtype val = in[index];
    Dtype abs_val = abs(val);
    if (abs_val < 1.0 / sigma2) {
      out[index] = 0.5 * val * val * sigma2;
    } else {
      out[index] = abs_val - 0.5 / sigma2;
    }
  }
}
 //
template <typename Dtype>
void SmoothL1LossLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
    const vector<Blob<Dtype>*>& top) {
  int count = bottom[0]->count();
  caffe_gpu_sub(
      count,
      bottom[0]->gpu_data(),  //ti
      bottom[1]->gpu_data(),  //ti*
      diff_.mutable_gpu_data());    // d := ti - ti*
  if (has_weights_) {
    // apply "inside" weights  //乘上相关的权重
    caffe_gpu_mul(
        count,
        bottom[2]->gpu_data(),
        diff_.gpu_data(),
        diff_.mutable_gpu_data());  // d := w_in * (b0 - b1)
  }    //
SmoothL1Forward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>( count, diff_.gpu_data(), errors_.mutable_gpu_data(), sigma2_); CUDA_POST_KERNEL_CHECK; if (has_weights_) { // apply "outside" weights caffe_gpu_mul( count, bottom[3]->gpu_data(), errors_.gpu_data(), errors_.mutable_gpu_data()); // d := w_out * SmoothL1(w_in * (b0 - b1)) } Dtype loss; caffe_gpu_dot(count, ones_.gpu_data(), errors_.gpu_data(), &loss); top[0]->mutable_cpu_data()[0] = loss / bottom[0]->num(); } template <typename Dtype> __global__ void SmoothL1Backward(const int n, const Dtype* in, Dtype* out, Dtype sigma2) { // f‘(x) = sigma * sigma * x if |x| < 1 / sigma / sigma  // = sign(x) otherwise CUDA_KERNEL_LOOP(index, n) { Dtype val = in[index]; Dtype abs_val = abs(val); if (abs_val < 1.0 / sigma2) { out[index] = sigma2 * val; } else { out[index] = (Dtype(0) < val) - (val < Dtype(0)); } } } template <typename Dtype> void SmoothL1LossLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { // after forwards, diff_ holds w_in * (b0 - b1) int count = diff_.count(); SmoothL1Backward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>( count, diff_.gpu_data(), diff_.mutable_gpu_data(), sigma2_); CUDA_POST_KERNEL_CHECK; for (int i = 0; i < 2; ++i) { if (propagate_down[i]) { const Dtype sign = (i == 0) ? 1 : -1; const Dtype alpha = sign * top[0]->cpu_diff()[0] / bottom[i]->num(); caffe_gpu_axpby( count, // count alpha, // alpha diff_.gpu_data(), // x Dtype(0), // beta bottom[i]->mutable_gpu_diff()); // y if (has_weights_) { // Scale by "inside" weight caffe_gpu_mul( count, bottom[2]->gpu_data(), bottom[i]->gpu_diff(), bottom[i]->mutable_gpu_diff()); // Scale by "outside" weight caffe_gpu_mul( count, bottom[3]->gpu_data(), bottom[i]->gpu_diff(), bottom[i]->mutable_gpu_diff()); } } } } INSTANTIATE_LAYER_GPU_FUNCS(SmoothL1LossLayer); } // namespace caffe 
时间: 2024-08-07 08:23:50

r-cnn学习(五):SmoothL1LossLayer论文与代码的结合理解的相关文章

深度学习用于文本分类的论文及代码集锦

深度学习用于文本分类的论文及代码集锦 原创: FrankLearningMachine 机器学习blog 4天前 [1] Convolutional Neural Networks for Sentence Classification Yoon Kim New York University EMNLP 2014 http://www.aclweb.org/anthology/D14-1181 这篇文章主要利用CNN基于预训练好的词向量中对句子进行分类.作者发现利用微调来学习任务相关的词向量可

[转]计算机视觉、机器学习相关领域论文和源代码大集合

计算机视觉.机器学习相关领域论文和源代码大集合--持续更新…… [email protected] http://blog.csdn.net/zouxy09 注:下面有project网站的大部分都有paper和相应的code.Code一般是C/C++或者Matlab代码. 最近一次更新:2013-3-17 一.特征提取Feature Extraction: ·         SIFT [1] [Demo program][SIFT Library] [VLFeat] ·         PCA

R语言学习笔记

參考:W.N. Venables, D.M. Smith and the R DCT: Introduction to R -- Notes on R: A Programming Environment for Data Analysis and Graphics,2003. http://bayes.math.montana.edu/Rweb/Rnotes/R.html 前言:关于R 在R的官方教程里是这么给R下注解的:一个数据分析和图形显示的程序设计环境(A system for data

计算机视觉、机器学习相关领域论文和源代码大集合[转]

注:下面有project网站的大部分都有paper和相应的code.Code一般是C/C++或者Matlab代码. 最近一次更新:2013-3-17 一.特征提取Feature Extraction: ·         SIFT [1] [Demo program][SIFT Library] [VLFeat] ·         PCA-SIFT [2] [Project] ·         Affine-SIFT [3] [Project] ·         SURF [4] [Ope

[转载][资料].计算机视觉、机器学习相关领域论文和源代码大集合

注:下面有project网站的大部分都有paper和相应的code.Code一般是C/C++或者Matlab代码. 最近一次更新:2013-3-17 目录(注:未添加索引,仅用于方便浏览) 一.特征提取Feature Extraction 二.图像分割Image Segmentation 三.目标检测Object Detection 四.显著性检测Saliency Detection 五.图像分类.聚类Image Classification, Clustering 六.抠图Image Matt

Objective C 快速入门学习五

<一>继承和多态 @class Complex 声明类(同C++) 子类函数成员 super 访问父类 同C++类似 1.通过继承 在子类中添加新方法 2.通过继承 在子类中添加新成员 3.通过继承 实现多态(实现比较简单,通过Id通用类型作为父类) 4.重载 5.抽象类abstract作用:创建子类更容易:提供了处理所有派生子类的公共接口:抽象方法制定了标准协议,规范子类必须实现. 6.通用类型id,编译时不会做类型检查,在运行时才会动态绑定具体类型,指出错误. 静态类型在编译阶段就会指出错

qt学习 (五) 登陆界面之连接按钮

登陆步骤是比对输入的账号密码与数据库中的表项目是否一致 一样,  跳出mainwidget对话框 不一样,跳出消息错误框 今天就是要进去, 因为进去以后是widget的窗口,所以把用来核对消息的数据库放在MAINwidget.cpp中, 再拿一个用户点击连接的子类 login.cpp, 就在这里面画用户登录账号密码textEdit 1 除了textEdit 基本都是button 慢慢加. 登录界面可以学的东西很多. 2 在widget.h文件中加入数据库所需要的头文件, #include <Qt

R语言学习.first

Question: 1.R语言与C语言语法上有什么不同,有什么相同? 所有R代码都用于操作对象,和C++一样是面向对象的语言,C是面向过程的语言,R中有每一种数据结构相当于C++中的类,每个数据相当于一个对象: 有数值向量形式的对象:c(1,2,3,4,5) 有字符型对象:“This is the world” 有各种元素形成的列表样式的对象:list(c(1,2,3),"This is the world") 数值本身也是一个对象:1  2 3 函数也是一个对象:f = functi

(转) AI突破性论文及代码实现汇总

本文转自:https://zhuanlan.zhihu.com/p/25191377 AI突破性论文及代码实现汇总 极视角 · 2 天前 What Can AI Do For You? "The business plans of the next 10,000 startups are easy to forecast: Take X and add AI." - Kevin Kelly "A hundred years ago electricity transforme