美文网首页
目标框损失函数

目标框损失函数

作者: 霜冷长河wzq | 来源:发表于2021-01-25 19:05 被阅读0次

参考代码:https://github.com/AlexeyAB/darknet

MSE损失

/*
计算预测的box
b.x = (i + x[index + 0 * stride]) / lw;
b.y = (j + x[index + 1 * stride]) / lh;
b.w = exp(x[index + 2 * stride]) * biases[2 * n] / w;
b.h = exp(x[index + 3 * stride]) * biases[2 * n + 1] / h;
*/
//lw,lh,输出结果的那层特征图的大小;w,h网络输入的大小;biases指anchor的大小
float tx = (truth.x*lw - i); 
float ty = (truth.y*lh - j);
float tw = log(truth.w*w / biases[2 * n]);
float th = log(truth.h*h / biases[2 * n + 1]);
//计算梯度
delta[index + 0 * stride] += scale * (tx - x[index + 0 * stride]) * iou_normalizer;
delta[index + 1 * stride] += scale * (ty - x[index + 1 * stride]) * iou_normalizer;
delta[index + 2 * stride] += scale * (tw - x[index + 2 * stride]) * iou_normalizer;
delta[index + 3 * stride] += scale * (th - x[index + 3 * stride]) * iou_normalizer;

其他如L1损失,smooth L1损失。

IoU损失系列

预测框准不准,考虑和真值框的3个度量指标。

  • 重叠面积:IoU, GIoU

  • 中心点距离: DIoU

  • 长宽比: CIoU

IoU

//从x, y, w, h 转为 top, left, bottom, right
boxabs pred_tblr = to_tblr(pred);
boxabs truth_tblr = to_tblr(truth);
//计算面积
float X = (pred_b - pred_t) * (pred_r - pred_l);
float Xhat = (truth_tblr.bot - truth_tblr.top) * (truth_tblr.right - truth_tblr.left);
//计算相交区域面积
float Ih = fmin(pred_b, truth_tblr.bot) - fmax(pred_t, truth_tblr.top);
float Iw = fmin(pred_r, truth_tblr.right) - fmax(pred_l, truth_tblr.left);
float I = Iw * Ih;
//计算相并区域面积,则IoU=I/U
float U = X + Xhat - I;

//对X求导
float dX_wrt_t = -1 * (pred_r - pred_l);
float dX_wrt_b = pred_r - pred_l;
float dX_wrt_l = -1 * (pred_b - pred_t);
float dX_wrt_r = pred_b - pred_t;
//对I求导
float dI_wrt_t = pred_t > truth_tblr.top ? (-1 * Iw) : 0;
float dI_wrt_b = pred_b < truth_tblr.bot ? Iw : 0;
float dI_wrt_l = pred_l > truth_tblr.left ? (-1 * Ih) : 0;
float dI_wrt_r = pred_r < truth_tblr.right ? Ih : 0;
//对U求导
float dU_wrt_t = dX_wrt_t - dI_wrt_t;
float dU_wrt_b = dX_wrt_b - dI_wrt_b;
float dU_wrt_l = dX_wrt_l - dI_wrt_l;
float dU_wrt_r = dX_wrt_r - dI_wrt_r;
//链式求导
if (U > 0 ) {
      p_dt = ((U * dI_wrt_t) - (I * dU_wrt_t)) / (U * U);
      p_db = ((U * dI_wrt_b) - (I * dU_wrt_b)) / (U * U);
      p_dl = ((U * dI_wrt_l) - (I * dU_wrt_l)) / (U * U);
      p_dr = ((U * dI_wrt_r) - (I * dU_wrt_r)) / (U * U);
}
//转为x,y,w,h的梯度
p_dx = p_dl + p_dr;           //p_dx, p_dy, p_dw and p_dh are the gradient of IoU.
p_dy = p_dt + p_db;
p_dw = (p_dr - p_dl);         //For dw and dh, we do not divided by 2.
p_dh = (p_db - p_dt);

GIoU

IoU损失缺陷:两个框不相交时,IoU一直为0。
增加两个框外接矩形中非重合部分所占比重,取值范围为-1到1。
参考论文:<<Generalized Intersection over Union: A Metric and A Loss for Bounding Box
Regression>>


IoU和GIoU损失计算
//计算外接矩形面积
float giou_Cw = fmax(pred_r, truth_tblr.right) - fmin(pred_l, truth_tblr.left);
float giou_Ch = fmax(pred_b, truth_tblr.bot) - fmin(pred_t, truth_tblr.top);
float giou_C = giou_Cw * giou_Ch;
//对C求导
float dC_wrt_t = pred_t < truth_tblr.top ? (-1 * giou_Cw) : 0;
float dC_wrt_b = pred_b > truth_tblr.bot ? giou_Cw : 0;
float dC_wrt_l = pred_l < truth_tblr.left ? (-1 * giou_Ch) : 0;
float dC_wrt_r = pred_r > truth_tblr.right ? giou_Ch : 0;
//GIoU正则项求导,正则项=(C-U)/C
p_dt += ((giou_C * dU_wrt_t) - (U * dC_wrt_t)) / (giou_C * giou_C);
p_db += ((giou_C * dU_wrt_b) - (U * dC_wrt_b)) / (giou_C * giou_C);
p_dl += ((giou_C * dU_wrt_l) - (U * dC_wrt_l)) / (giou_C * giou_C);
p_dr += ((giou_C * dU_wrt_r) - (U * dC_wrt_r)) / (giou_C * giou_C);

DIoU,CIoU

GIoU损失缺陷:出现大框套小框时,GIoU退化成IoU。
参考论文:<<Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression>>

  • DIoU:增加两个框之间的距离度量。


    DIoU中心点距离和对角线
  • CIoU:在DIoU基础上,增加两个框长宽比一致性的度量。
DIoU和CIoU的计算

其中,b表示框的中心点,ρ(·)表示欧式距离,c 表示两个框外接矩形的对角线。
α 是正的调节因子, v 表示长宽比的一致性。

相关文章

  • 目标框损失函数

    参考代码:https://github.com/AlexeyAB/darknet[https://github.c...

  • Giou YOLOv3 | CVPR2019,通用,更优的检测框

    本文提出的GIou损失函数,是一种目标检测领域用于回归目标框损失函数。该Trick适用于任何目标检测算法。本文以Y...

  • 各机器学习模型的目标函数

    目标函数和损失函数的区别 损失函数:模型拟合的越好,损失应该越小; 目标函数:优化的目标,可以是“损失函数”或者“...

  • 机器学习的目标函数、损失函数

    损失函数又叫代价函数。目标函数是一个更加广的概念。对于目标函数来说在有约束条件下的最小化就是损失函数。 用损失函数...

  • RRBOX_2017_电子所

    整体思想:遥感图像目标检测基于ssd框架思想,使用带角度的锚点框回归目标坐标偏移量 ArIOU 损失函数多了角度一...

  • 机器学习随笔

    1 模型的函数表达式,如逻辑回归函数 2 定义损失函数,最小二乘损失,0/1损失,交叉熵。 3 目标函数,损失函数...

  • Bias和Variance

    bias指的是模型训练中保证损失最小 variance指的是模型泛化能力最强 目标函数=损失函数+正则项 损失函数...

  • 深度学习中的损失函数总结已经Center Loss函数笔记

    图片分类里的Center Loss目标函数,损失函数,代价函数损失函数度量的是预测值与真实值之间的差异.损失函数通...

  • 统计机器学习(LR、SVM)-Question

    1. 手写LR的目标函数? 目标函数(objective function)= 损失函数(loss functio...

  • 06.神经网络学习-2

    神经网络的优化目标 数学目标 不论损失函数是什么形式,神经网络的优化目标都是使得损失函数最小化。对于均方误差函数和...

网友评论

      本文标题:目标框损失函数

      本文链接:https://www.haomeiwen.com/subject/ilsbzktx.html