SmoothL1LossLayer论文与代码的结合理解

本文详细解析了用于训练区域提议网络(RPN)的损失函数,包括正负样本选择标准及Smooth L1 Loss的GPU实现代码。该损失函数适用于处理边界框回归任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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代码如下,整个过程分为两部分:前向计算以及后向计算(1)式的后半部分:

 

复制代码
// ------------------------------------------------------------------  
// 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_) {  //乘上相关的权重,对应于(1)式中的pi*,有目标时为1
    // apply "inside" weights  
    caffe_gpu_mul(  
        count,  
        bottom[2]->gpu_data(), //pi* 
        diff_.gpu_data(),  
        diff_.mutable_gpu_data());  // d := w_in * (b0 - b1)  
  }  
  //代入计算SmoothL1
  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(), // 1/Nreg 
        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();  
}  
//反向计算,对smoothLoss求导  
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();  
  //调用反向smoothloss,diff_.gpu_data()表示x,diff_.mutable_gpu_data()表示smoothloss的导数
  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  
### Smooth L1 Loss 的概念及用法 Smooth L1 Loss 是一种结合了 L1 Loss 和 L2 Loss 特性的损失函数,在深度学习的目标检测任务中被广泛使用。它通过平滑处理解决了 L1 Loss 对异常值不敏感以及 L2 Loss 在接近零时梯度过大的问题[^1]。 其定义如下: 当 \(|x| < 1\) 时, \[ \text{Smooth L1}(x) = 0.5 \cdot (x)^2, \] 当 \(|x| \geq 1\) 时, \[ \text{Smooth L1}(x) = |x| - 0.5. \] 这种分段设计使得该损失函数在误差较小时具有二次特性,从而提供更稳定的收敛;而在误差较大时表现为线性增长,减少对异常值的影响[^4]。 以下是 Python 实现的一个简单例子: ```python import torch import torch.nn.functional as F def smooth_l1_loss(predictions, targets): diff = predictions - targets abs_diff = torch.abs(diff) cond = torch.le(abs_diff, 1.0) # 条件判断:abs(x) 是否小于等于 1 loss_smooth_part = 0.5 * (diff ** 2) loss_linear_part = abs_diff - 0.5 loss = torch.where(cond, loss_smooth_part, loss_linear_part) return loss.mean() ``` --- ### CIoU 的概念及用法 CIoU(Complete Intersection over Union)是一种改进版的边界框回归损失函数,旨在解决传统 IoU 及其变种 GIoU 和 DIoU 存在的一些局限性。相比其他方法,CIoU 不仅考虑重叠区域的比例关系,还引入中心点距离和宽高比例因子来进一步优化定位精度[^3]。 具体而言,CIoU 定义为三个部分的加权组合: \[ \text{CIoU} = \text{IoU} - \frac{\rho^2(b,b^{gt})}{c^2} - \alpha v, \] 其中: - \(\text{IoU}\): 表示两个矩形框交集并集面积之比; - \(\rho^2(b,b^{gt}) / c^2\): 中心点间欧几里得距离平方除以覆盖最远两点形成的最小闭包直径平方; - \(\alpha v\): 调整参数乘以宽高中偏差角度项\(v=\left(1-\exp(-\beta(w/w_{gt}-h/h_{gt})^2)\right)\),这里\(\beta>0\)是一个超参控制权重大小。 Python 实现可以参考以下代码片段: ```python def ciou_loss(pred_boxes, target_boxes, eps=1e-7): """ 计算 CIoU 损失 参数: pred_boxes: 预测框张量形状 [N, 4], 坐标格式为中心坐标+宽度高度形式(cx,cy,w,h). target_boxes: 真实标签框同上[N, 4]. 返回: 平均 CIoU 损失标量值. """ cx_pred, cy_pred, w_pred, h_pred = pred_boxes.unbind(dim=-1) cx_target, cy_target, w_target, h_target = target_boxes.unbind(dim=-1) wh_pred = torch.stack([w_pred, h_pred], dim=-1).clamp(min=eps) wh_target = torch.stack([w_target, h_target], dim=-1).clamp(min=eps) center_distance_squared = ((cx_pred - cx_target)**2 + (cy_pred - cy_target)**2) diagonal_length_bounding_box = ( (torch.max(cx_pred+w_pred/2., cx_target+w_target/2.) - torch.min(cx_pred-w_pred/2., cx_target-w_target/2.))**2 + (torch.max(cy_pred+h_pred/2., cy_target+h_target/2.) - torch.min(cy_pred-h_pred/2., cy_target-h_target/2.))**2 ) ious = calculate_iou(pred_boxes, target_boxes) # 自定义iou计算逻辑省略... v = (4 / math.pi**2) * torch.pow(torch.atan(w_target / h_target) - torch.atan(w_pred / h_pred), 2) with torch.no_grad(): alpha = v / (1 - ious + v + eps) ciou_term_2 = center_distance_squared / diagonal_length_bounding_box ciou_term_3 = alpha * v ciou_losses = 1 - ious + ciou_term_2 + ciou_term_3 return ciou_losses.mean() # 辅助函数 IOU... def calculate_iou(box_a, box_b): ... ``` --- ### Smooth L1 Loss CIoU 的对比分析 两者的主要区别在于适用场景和技术特点的不同。 - **Smooth L1 Loss**: 更适合于一般化的数值回归任务,尤其是对于目标位置偏移较小的情况表现良好。 - **CIoU**: 主要针对边界框回归问题进行了专门的设计,能够更好地捕捉到物体尺度变化带来的影响,并且综合考量几何属性提升性能。 因此,在实际应用过程中可以根据具体的业务需求选择合适的损失函数或者尝试联合使用多种类型的损失函数来进行实验验证效果最佳方案。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值