yoloV5改进-针对小目标的NWD
短信预约 -IT技能 免费直播动态提醒
论文地址:https://arxiv.org/pdf/2110.13389.pdf
目录
前言:
最近在做目标检测时,图片分辨率为6016 x 2048,遇到一个200*200的小目标问题,这里加入了NWP针对该问题做出优化
wasserstein_loss
utils/metrics.py中加入wasserstein_loss
def wasserstein_loss(pred, target, eps=1e-7, constant=12.8): """Implementation of paper `A Normalized Gaussian Wasserstein Distance for Tiny Object Detection . Args: pred (Tensor): Predicted bboxes of format (cx, cy, w, h), shape (n, 4). target (Tensor): Corresponding gt bboxes, shape (n, 4). eps (float): Eps to avoid log(0). Return: Tensor: Loss tensor. """ center1 = pred[:, :2] center2 = target[:, :2] whs = center1[:, :2] - center2[:, :2] center_distance = whs[:, 0] * whs[:, 0] + whs[:, 1] * whs[:, 1] + eps w1 = pred[:, 2] + eps h1 = pred[:, 3] + eps w2 = target[:, 2] + eps h2 = target[:, 3] + eps wh_distance = ((w1 - w2) ** 2 + (h1 - h2) ** 2) / 4 wasserstein_2 = center_distance + wh_distance return torch.exp(-torch.sqrt(wasserstein_2) / constant)
LOSS.PY
修改loss.py
if n: # pxy, pwh, _, pcls = pi[b, a, gj, gi].tensor_split((2, 4, 5), dim=1) # faster, requires torch 1.8.0 pxy, pwh, _, pcls = pi[b, a, gj, gi].split((2, 2, 1, self.nc), 1) # target-subset of predictions # Regression pxy = pxy.sigmoid() * 2 - 0.5 pwh = (pwh.sigmoid() * 2) ** 2 * anchors[i] pbox = torch.cat((pxy, pwh), 1) # predicted box iou = bbox_iou(pbox, tbox[i], CIoU=True).squeeze() # iou(prediction, target) # ================================================== # lbox += (1.0 - iou).mean() # iou loss 修改 # Objectness # score_iou = iou.detach().clamp(0).type(tobj.dtype) #修改 nwd = wasserstein_loss(pbox, tbox[i]).squeeze() nwd_ratio = 0.5 # 平衡稀疏 nwd和 iou各取0.5 如果数据集全是小目标的换可以设置1或者0.9,,08 意思为只用nwd lbox += (1 - nwd_ratio) * (1.0 - nwd).mean() + nwd_ratio * (1.0 - iou).mean() # Objectness iou = (iou.detach() * nwd_ratio + nwd.detach() * (1 - nwd_ratio)).clamp(0, 1).type(tobj.dtype) #这里clamp(0,1)中设置了最小参数必须大于等于0进行 # =============================================== # Objectness iou = iou.detach().clamp(0).type(tobj.dtype)
结语:
后续更新对小目标改进:有效BiFormer注意力机制
来源地址:https://blog.csdn.net/qq_49627063/article/details/130015203
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341