【程序】yolo

编程入门 行业动态 更新时间:2024-10-10 10:28:03

【<a href=https://www.elefans.com/category/jswz/34/1771429.html style=程序】yolo"/>

【程序】yolo

nooobj_loss = F.mse_loss(noo_pred_c, noo_target_c, size_average=False)

说明:nooobj_loss是在输出图片的方格里面,将预测结果的最后30维的4和9,也就是第5和第10个元素显示出来,这两个位置的元素在target_tensor里是置信度。通过对照,选出GT没有标注的表格,对这些表格中预测值的置信度做回归。

目的:是为了通过回归将相应表格的预测值中的置信度学习为0。

contain_loss = F.mse_loss(box_pred_response[:, 4], box_target_response_iou[:, 4], size_average=False)

说明:contain_loss是在输出图片上面,拿预测的坐标信息和GT计算出来实际的IOU(选最大的),然后将它和预测的IOU做回归。

目的:通过回归将预测的IOU值与计算出来的差值接近0。

loc_loss = F.mse_loss(box_pred_response[:, :2], box_target_response[:, :2], size_average=False) + F.mse_loss(torch.sqrt(box_pred_response[:, 2:4]), torch.sqrt(box_target_response[:, 2:4]), size_average=False)

说明:loc_loss是在输出图上,将预测的框的中心点和比例信息与GT的框的信息做回归。

目的:通过回归将预测框信息与GT框信息的差值接近0。

not_contain_loss = F.mse_loss(box_pred_not_response[:, 4], box_target_not_response[:, 4], size_average=False)

说明:not_contain_loss是在输出图上(由于每个表格预测两个框),将IOU值较低的那个预测框信息的置信度设置为0,与相对应的预测框的置信度做回归。

目的:通过回归将相应预测框的置信度接近0.

class_loss = F.mse_loss(class_pred, class_target, size_average=False)

说明:class_loss是对分类值做回归,对预测框预测出来的类别和GT框的分类值做回归,使预测类别更准确。

目的:通过回归将预测框的分类值与GT一致。

# -*- coding: utf-8 -*-
"""
@Time          : 2020/08/12 18:30
@Author        : FelixFu / Bryce
@File          : yoloLoss.py
@Noice         :
@Modificattion :@Detail    : a little dufficult in builting yoloLoss funcion
"""import torch
import torch.nn as nn
import torch.nn.functional as Fclass yoloLoss(nn.Module):def __init__(self, S, B, l_coord, l_noobj):super(yoloLoss, self).__init__()self.S = Sself.B = Bself.l_coord = l_coordself.l_noobj = l_noobjdef compute_iou(self, box1, box2):"""Compute the intersection over union of two set of boxes, each box is [x1,y1,x2,y2].Args:box1: (tensor) bounding boxes, sized [N,4].box2: (tensor) bounding boxes, sized [M,4].Return:(tensor) iou, sized [N,M]."""# 首先计算两个box左上角点坐标的最大值和右下角坐标的最小值,然后计算交集面积,最后把交集面积除以对应的并集面积N = box1.size(0)M = box2.size(0)lt = torch.max(  # 左上角的点box1[:, :2].unsqueeze(1).expand(N, M, 2),  # [N,2] -> [N,1,2] -> [N,M,2]box2[:, :2].unsqueeze(0).expand(N, M, 2),  # [M,2] -> [1,M,2] -> [N,M,2])rb = torch.min(  # 右下角的点box1[:, 2:].unsqueeze(1).expand(N, M, 2),  # [N,2] -> [N,1,2] -> [N,M,2]box2[:, 2:].unsqueeze(0).expand(N, M, 2),  # [M,2] -> [1,M,2] -> [N,M,2])wh = rb - lt  # [N,M,2]wh[wh < 0] = 0  # clip at 指两个box没有重叠区域inter = wh[:, :, 0] * wh[:, :, 1]  # [N,M]area1 = (box1[:, 2] - box1[:, 0]) * (box1[:, 3] - box1[:, 1])  # [N,]area2 = (box2[:, 2] - box2[:, 0]) * (box2[:, 3] - box2[:, 1])  # [M,]area1 = area1.unsqueeze(1).expand_as(inter)  # [N,] -> [N,1] -> [N,M]area2 = area2.unsqueeze(0).expand_as(inter)  # [M,] -> [1,M] -> [N,M]iou = inter / (area1 + area2 - inter)return ioudef forward(self, pred_tensor, target_tensor):"""pred_tensor: (tensor) size(batchsize,S,S,Bx5+20=30) [x,y,w,h,c]target_tensor: (tensor) size(batchsize,S,S,30)"""N = pred_tensor.size()[0]# 具有目标标签的索引(bs, 7, 7, 30)中7*7方格中的哪个方格包含目标coo_mask = target_tensor[:, :, :, 4] > 0  # coo_mask.shape = (bs, 7, 7)noo_mask = target_tensor[:, :, :, 4] == 0  # 不具有目标的标签索引# 得到含物体的坐标等信息(coo_mask扩充到与target_tensor一样形状, 沿最后一维扩充)coo_mask = coo_mask.unsqueeze(-1).expand_as(target_tensor)noo_mask = noo_mask.unsqueeze(-1).expand_as(target_tensor)#  coo_pred:tensor[, 30](所有batch数据都压缩在一起)coo_pred = pred_tensor[coo_mask].view(-1, 30)box_pred = coo_pred[:, :10].reshape(-1, 5)  # box[x1,y1,w1,h1,c1], [x2,y2,w2,h2,c2]class_pred = coo_pred[:, 10:]coo_target = target_tensor[coo_mask].view(-1, 30)box_target = coo_target[:, :10].contiguous().view(-1, 5)class_target = coo_target[:, 10:]# compute not contain obj lossnoo_pred = pred_tensor[noo_mask].view(-1, 30)noo_target = target_tensor[noo_mask].view(-1, 30)noo_pred_mask = torch.cuda.ByteTensor(noo_pred.size()).bool()  # 随机的True 和 Falsenoo_pred_mask.zero_()  # 全部改成Falsenoo_pred_mask[:, 4] = 1noo_pred_mask[:, 9] = 1noo_pred_c = noo_pred[noo_pred_mask]  # noo pred只需要计算 c 的损失 size[-1,2]noo_target_c = noo_target[noo_pred_mask]nooobj_loss = F.mse_loss(noo_pred_c, noo_target_c, size_average=False)# compute contain obj losscoo_response_mask = torch.cuda.ByteTensor(box_target.size()).bool()coo_response_mask.zero_()coo_not_response_mask = torch.cuda.ByteTensor(box_target.size()).bool()coo_not_response_mask.zero_()box_target_iou = torch.zeros(box_target.size()).cuda()for i in range(0, box_target.size()[0], 2):  # choose the best iou boxbox1 = box_pred[i:i + 2]  # 获取当前格点预测的b个boxbox1_xyxy = torch.FloatTensor(box1.size())# (x,y,w,h)box1_xyxy[:, :2] = box1[:, :2] / 14. - 0.5 * box1[:, 2:4]box1_xyxy[:, 2:4] = box1[:, :2] / 14. + 0.5 * box1[:, 2:4]box2 = box_target[i].view(-1, 5)box2_xyxy = torch.FloatTensor(box2.size())box2_xyxy[:, :2] = box2[:, :2] / 14. - 0.5 * box2[:, 2:4]box2_xyxy[:, 2:4] = box2[:, :2] / 14. + 0.5 * box2[:, 2:4]  # 存在负值,但比较的应该是比例层面的,以物体为中心点iou = selfpute_iou(box1_xyxy[:, :4], box2_xyxy[:, :4])  # [2,1]max_iou, max_index = iou.max(0)max_index = max_index.data.cuda()coo_response_mask[i + max_index] = 1coo_not_response_mask[i + 1 - max_index] = 1###### we want the confidence score to equal the# intersection over union (IOU) between the predicted box# and the ground truth###### iou value 作为box包含目标的confidence(赋值在向量的第五个位置)box_target_iou[i + max_index, torch.LongTensor([4]).cuda()] = (max_iou).data.cuda()box_target_iou = box_target_iou.cuda()# 1.response lossbox_pred_response = box_pred[coo_response_mask].view(-1, 5)box_target_response_iou = box_target_iou[coo_response_mask].view(-1, 5)box_target_response = box_target[coo_response_mask].view(-1, 5)contain_loss = F.mse_loss(box_pred_response[:, 4], box_target_response_iou[:, 4], size_average=False)loc_loss = F.mse_loss(box_pred_response[:, :2], box_target_response[:, :2], size_average=False) + F.mse_loss(torch.sqrt(box_pred_response[:, 2:4]), torch.sqrt(box_target_response[:, 2:4]), size_average=False)# 2.not response lossbox_pred_not_response = box_pred[coo_not_response_mask].view(-1, 5)box_target_not_response = box_target[coo_not_response_mask].view(-1, 5)box_target_not_response[:, 4] = 0# not_contain_loss = F.mse_loss(box_pred_response[:,4],box_target_response[:,4],size_average=False)# I believe this bug is simply a typonot_contain_loss = F.mse_loss(box_pred_not_response[:, 4], box_target_not_response[:, 4], size_average=False)# 3.class lossclass_loss = F.mse_loss(class_pred, class_target, size_average=False)return (self.l_coord * loc_loss + self.B * contain_loss + not_contain_loss + self.l_noobj * nooobj_loss + class_loss) / N

更多推荐

【程序】yolo

本文发布于:2024-02-13 22:50:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760950.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   yolo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!