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 F
class yoloLoss(nn.Module):
def __init__(self, S, B, l_coord, l_noobj):
super(yoloLoss, self).__init__()
self.S = S
self.B = B
self.l_coord = l_coord
self.l_noobj = l_noobj
def 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 iou
def 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 loss
noo_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 和 False
noo_pred_mask.zero_() # 全部改成False
noo_pred_mask[:, 4] = 1
noo_pred_mask[:, 9] = 1
noo_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 loss
coo_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 box
box1 = box_pred[i:i + 2] # 获取当前格点预测的b个box
box1_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 = self.compute_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] = 1
coo_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 loss
box_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 loss
box_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 typo
not_contain_loss = F.mse_loss(box_pred_not_response[:, 4], box_target_not_response[:, 4], size_average=False)
# 3.class loss
class_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
|