Add ComputeLoss() class (#1950)
This commit is contained in:
1 parent
f4a78e1b40
commit
ca9babb8e6
3 files changed
+118
-102
No files matched your search
@@ -13,7 +13,6 @@ from models.experimental import attempt_load
|
|||||||
from utils.datasets import create_dataloader
|
from utils.datasets import create_dataloader
|
||||||
from utils.general import coco80_to_coco91_class, check_dataset, check_file, check_img_size, check_requirements, \
|
from utils.general import coco80_to_coco91_class, check_dataset, check_file, check_img_size, check_requirements, \
|
||||||
box_iou, non_max_suppression, scale_coords, xyxy2xywh, xywh2xyxy, set_logging, increment_path, colorstr
|
box_iou, non_max_suppression, scale_coords, xyxy2xywh, xywh2xyxy, set_logging, increment_path, colorstr
|
||||||
from utils.loss import compute_loss
|
|
||||||
from utils.metrics import ap_per_class, ConfusionMatrix
|
from utils.metrics import ap_per_class, ConfusionMatrix
|
||||||
from utils.plots import plot_images, output_to_target, plot_study_txt
|
from utils.plots import plot_images, output_to_target, plot_study_txt
|
||||||
from utils.torch_utils import select_device, time_synchronized
|
from utils.torch_utils import select_device, time_synchronized
|
||||||
@@ -36,7 +35,8 @@ def test(data,
|
|||||||
save_hybrid=False, # for hybrid auto-labelling
|
save_hybrid=False, # for hybrid auto-labelling
|
||||||
save_conf=False, # save auto-label confidences
|
save_conf=False, # save auto-label confidences
|
||||||
plots=True,
|
plots=True,
|
||||||
log_imgs=0): # number of logged images
|
log_imgs=0, # number of logged images
|
||||||
|
compute_loss=None):
|
||||||
|
|
||||||
# Initialize/load model and set device
|
# Initialize/load model and set device
|
||||||
training = model is not None
|
training = model is not None
|
||||||
@@ -111,8 +111,8 @@ def test(data,
|
|||||||
t0 += time_synchronized() - t
|
t0 += time_synchronized() - t
|
||||||
|
|
||||||
# Compute loss
|
# Compute loss
|
||||||
if training:
|
if compute_loss:
|
||||||
loss += compute_loss([x.float() for x in train_out], targets, model)[1][:3] # box, obj, cls
|
loss += compute_loss([x.float() for x in train_out], targets)[1][:3] # box, obj, cls
|
||||||
|
|
||||||
# Run NMS
|
# Run NMS
|
||||||
targets[:, 2:] *= torch.Tensor([width, height, width, height]).to(device) # to pixels
|
targets[:, 2:] *= torch.Tensor([width, height, width, height]).to(device) # to pixels
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ from utils.general import labels_to_class_weights, increment_path, labels_to_ima
|
|||||||
fitness, strip_optimizer, get_latest_run, check_dataset, check_file, check_git_status, check_img_size, \
|
fitness, strip_optimizer, get_latest_run, check_dataset, check_file, check_git_status, check_img_size, \
|
||||||
check_requirements, print_mutation, set_logging, one_cycle, colorstr
|
check_requirements, print_mutation, set_logging, one_cycle, colorstr
|
||||||
from utils.google_utils import attempt_download
|
from utils.google_utils import attempt_download
|
||||||
from utils.loss import compute_loss
|
from utils.loss import ComputeLoss
|
||||||
from utils.plots import plot_images, plot_labels, plot_results, plot_evolution
|
from utils.plots import plot_images, plot_labels, plot_results, plot_evolution
|
||||||
from utils.torch_utils import ModelEMA, select_device, intersect_dicts, torch_distributed_zero_first
|
from utils.torch_utils import ModelEMA, select_device, intersect_dicts, torch_distributed_zero_first
|
||||||
|
|
||||||
@@ -227,6 +227,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
|
|||||||
results = (0, 0, 0, 0, 0, 0, 0) # P, R, mAP@.5, mAP@.5-.95, val_loss(box, obj, cls)
|
results = (0, 0, 0, 0, 0, 0, 0) # P, R, mAP@.5, mAP@.5-.95, val_loss(box, obj, cls)
|
||||||
scheduler.last_epoch = start_epoch - 1 # do not move
|
scheduler.last_epoch = start_epoch - 1 # do not move
|
||||||
scaler = amp.GradScaler(enabled=cuda)
|
scaler = amp.GradScaler(enabled=cuda)
|
||||||
|
compute_loss = ComputeLoss(model) # init loss class
|
||||||
logger.info(f'Image sizes {imgsz} train, {imgsz_test} test\n'
|
logger.info(f'Image sizes {imgsz} train, {imgsz_test} test\n'
|
||||||
f'Using {dataloader.num_workers} dataloader workers\n'
|
f'Using {dataloader.num_workers} dataloader workers\n'
|
||||||
f'Logging results to {save_dir}\n'
|
f'Logging results to {save_dir}\n'
|
||||||
@@ -286,7 +287,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
|
|||||||
# Forward
|
# Forward
|
||||||
with amp.autocast(enabled=cuda):
|
with amp.autocast(enabled=cuda):
|
||||||
pred = model(imgs) # forward
|
pred = model(imgs) # forward
|
||||||
loss, loss_items = compute_loss(pred, targets.to(device), model) # loss scaled by batch_size
|
loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size
|
||||||
if rank != -1:
|
if rank != -1:
|
||||||
loss *= opt.world_size # gradient averaged between devices in DDP mode
|
loss *= opt.world_size # gradient averaged between devices in DDP mode
|
||||||
if opt.quad:
|
if opt.quad:
|
||||||
@@ -344,7 +345,8 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
|
|||||||
dataloader=testloader,
|
dataloader=testloader,
|
||||||
save_dir=save_dir,
|
save_dir=save_dir,
|
||||||
plots=plots and final_epoch,
|
plots=plots and final_epoch,
|
||||||
log_imgs=opt.log_imgs if wandb else 0)
|
log_imgs=opt.log_imgs if wandb else 0,
|
||||||
|
compute_loss=compute_loss)
|
||||||
|
|
||||||
# Write
|
# Write
|
||||||
with open(results_file, 'a') as f:
|
with open(results_file, 'a') as f:
|
||||||
|
|||||||
+109
-95
@@ -85,119 +85,133 @@ class QFocalLoss(nn.Module):
|
|||||||
return loss
|
return loss
|
||||||
|
|
||||||
|
|
||||||
def compute_loss(p, targets, model): # predictions, targets, model
|
class ComputeLoss:
|
||||||
device = targets.device
|
# Compute losses
|
||||||
lcls, lbox, lobj = torch.zeros(1, device=device), torch.zeros(1, device=device), torch.zeros(1, device=device)
|
def __init__(self, model, autobalance=False):
|
||||||
tcls, tbox, indices, anchors = build_targets(p, targets, model) # targets
|
super(ComputeLoss, self).__init__()
|
||||||
h = model.hyp # hyperparameters
|
device = next(model.parameters()).device # get model device
|
||||||
|
h = model.hyp # hyperparameters
|
||||||
|
|
||||||
# Define criteria
|
# Define criteria
|
||||||
BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device)) # weight=model.class_weights)
|
BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device))
|
||||||
BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device))
|
BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device))
|
||||||
|
|
||||||
# Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3
|
# Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3
|
||||||
cp, cn = smooth_BCE(eps=0.0)
|
self.cp, self.cn = smooth_BCE(eps=0.0)
|
||||||
|
|
||||||
# Focal loss
|
# Focal loss
|
||||||
g = h['fl_gamma'] # focal loss gamma
|
g = h['fl_gamma'] # focal loss gamma
|
||||||
if g > 0:
|
if g > 0:
|
||||||
BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g)
|
BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g)
|
||||||
|
|
||||||
# Losses
|
det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module
|
||||||
nt = 0 # number of targets
|
self.balance = {3: [3.67, 1.0, 0.43], 4: [3.78, 1.0, 0.39, 0.22], 5: [3.88, 1.0, 0.37, 0.17, 0.10]}[det.nl]
|
||||||
balance = [4.0, 1.0, 0.3, 0.1, 0.03] # P3-P7
|
# self.balance = [1.0] * det.nl
|
||||||
for i, pi in enumerate(p): # layer index, layer predictions
|
self.ssi = (det.stride == 16).nonzero(as_tuple=False).item() # stride 16 index
|
||||||
b, a, gj, gi = indices[i] # image, anchor, gridy, gridx
|
self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, model.gr, h, autobalance
|
||||||
tobj = torch.zeros_like(pi[..., 0], device=device) # target obj
|
for k in 'na', 'nc', 'nl', 'anchors':
|
||||||
|
setattr(self, k, getattr(det, k))
|
||||||
|
|
||||||
n = b.shape[0] # number of targets
|
def __call__(self, p, targets): # predictions, targets, model
|
||||||
if n:
|
device = targets.device
|
||||||
nt += n # cumulative targets
|
lcls, lbox, lobj = torch.zeros(1, device=device), torch.zeros(1, device=device), torch.zeros(1, device=device)
|
||||||
ps = pi[b, a, gj, gi] # prediction subset corresponding to targets
|
tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets
|
||||||
|
|
||||||
# Regression
|
# Losses
|
||||||
pxy = ps[:, :2].sigmoid() * 2. - 0.5
|
for i, pi in enumerate(p): # layer index, layer predictions
|
||||||
pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i]
|
b, a, gj, gi = indices[i] # image, anchor, gridy, gridx
|
||||||
pbox = torch.cat((pxy, pwh), 1) # predicted box
|
tobj = torch.zeros_like(pi[..., 0], device=device) # target obj
|
||||||
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True) # iou(prediction, target)
|
|
||||||
lbox += (1.0 - iou).mean() # iou loss
|
|
||||||
|
|
||||||
# Objectness
|
n = b.shape[0] # number of targets
|
||||||
tobj[b, a, gj, gi] = (1.0 - model.gr) + model.gr * iou.detach().clamp(0).type(tobj.dtype) # iou ratio
|
if n:
|
||||||
|
ps = pi[b, a, gj, gi] # prediction subset corresponding to targets
|
||||||
|
|
||||||
# Classification
|
# Regression
|
||||||
if model.nc > 1: # cls loss (only if multiple classes)
|
pxy = ps[:, :2].sigmoid() * 2. - 0.5
|
||||||
t = torch.full_like(ps[:, 5:], cn, device=device) # targets
|
pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i]
|
||||||
t[range(n), tcls[i]] = cp
|
pbox = torch.cat((pxy, pwh), 1) # predicted box
|
||||||
lcls += BCEcls(ps[:, 5:], t) # BCE
|
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True) # iou(prediction, target)
|
||||||
|
lbox += (1.0 - iou).mean() # iou loss
|
||||||
|
|
||||||
# Append targets to text file
|
# Objectness
|
||||||
# with open('targets.txt', 'a') as file:
|
tobj[b, a, gj, gi] = (1.0 - self.gr) + self.gr * iou.detach().clamp(0).type(tobj.dtype) # iou ratio
|
||||||
# [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)]
|
|
||||||
|
|
||||||
lobj += BCEobj(pi[..., 4], tobj) * balance[i] # obj loss
|
# Classification
|
||||||
|
if self.nc > 1: # cls loss (only if multiple classes)
|
||||||
|
t = torch.full_like(ps[:, 5:], self.cn, device=device) # targets
|
||||||
|
t[range(n), tcls[i]] = self.cp
|
||||||
|
lcls += self.BCEcls(ps[:, 5:], t) # BCE
|
||||||
|
|
||||||
lbox *= h['box']
|
# Append targets to text file
|
||||||
lobj *= h['obj']
|
# with open('targets.txt', 'a') as file:
|
||||||
lcls *= h['cls']
|
# [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)]
|
||||||
bs = tobj.shape[0] # batch size
|
|
||||||
|
|
||||||
loss = lbox + lobj + lcls
|
obji = self.BCEobj(pi[..., 4], tobj)
|
||||||
return loss * bs, torch.cat((lbox, lobj, lcls, loss)).detach()
|
lobj += obji * self.balance[i] # obj loss
|
||||||
|
if self.autobalance:
|
||||||
|
self.balance[i] = self.balance[i] * 0.9999 + 0.0001 / obji.detach().item()
|
||||||
|
|
||||||
|
if self.autobalance:
|
||||||
|
self.balance = [x / self.balance[self.ssi] for x in self.balance]
|
||||||
|
lbox *= self.hyp['box']
|
||||||
|
lobj *= self.hyp['obj']
|
||||||
|
lcls *= self.hyp['cls']
|
||||||
|
bs = tobj.shape[0] # batch size
|
||||||
|
|
||||||
def build_targets(p, targets, model):
|
loss = lbox + lobj + lcls
|
||||||
# Build targets for compute_loss(), input targets(image,class,x,y,w,h)
|
return loss * bs, torch.cat((lbox, lobj, lcls, loss)).detach()
|
||||||
det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module
|
|
||||||
na, nt = det.na, targets.shape[0] # number of anchors, targets
|
|
||||||
tcls, tbox, indices, anch = [], [], [], []
|
|
||||||
gain = torch.ones(7, device=targets.device) # normalized to gridspace gain
|
|
||||||
ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt)
|
|
||||||
targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices
|
|
||||||
|
|
||||||
g = 0.5 # bias
|
def build_targets(self, p, targets):
|
||||||
off = torch.tensor([[0, 0],
|
# Build targets for compute_loss(), input targets(image,class,x,y,w,h)
|
||||||
[1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m
|
na, nt = self.na, targets.shape[0] # number of anchors, targets
|
||||||
# [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm
|
tcls, tbox, indices, anch = [], [], [], []
|
||||||
], device=targets.device).float() * g # offsets
|
gain = torch.ones(7, device=targets.device) # normalized to gridspace gain
|
||||||
|
ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt)
|
||||||
|
targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices
|
||||||
|
|
||||||
for i in range(det.nl):
|
g = 0.5 # bias
|
||||||
anchors = det.anchors[i]
|
off = torch.tensor([[0, 0],
|
||||||
gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain
|
[1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m
|
||||||
|
# [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm
|
||||||
|
], device=targets.device).float() * g # offsets
|
||||||
|
|
||||||
# Match targets to anchors
|
for i in range(self.nl):
|
||||||
t = targets * gain
|
anchors = self.anchors[i]
|
||||||
if nt:
|
gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain
|
||||||
# Matches
|
|
||||||
r = t[:, :, 4:6] / anchors[:, None] # wh ratio
|
|
||||||
j = torch.max(r, 1. / r).max(2)[0] < model.hyp['anchor_t'] # compare
|
|
||||||
# j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2))
|
|
||||||
t = t[j] # filter
|
|
||||||
|
|
||||||
# Offsets
|
# Match targets to anchors
|
||||||
|
t = targets * gain
|
||||||
|
if nt:
|
||||||
|
# Matches
|
||||||
|
r = t[:, :, 4:6] / anchors[:, None] # wh ratio
|
||||||
|
j = torch.max(r, 1. / r).max(2)[0] < self.hyp['anchor_t'] # compare
|
||||||
|
# j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2))
|
||||||
|
t = t[j] # filter
|
||||||
|
|
||||||
|
# Offsets
|
||||||
|
gxy = t[:, 2:4] # grid xy
|
||||||
|
gxi = gain[[2, 3]] - gxy # inverse
|
||||||
|
j, k = ((gxy % 1. < g) & (gxy > 1.)).T
|
||||||
|
l, m = ((gxi % 1. < g) & (gxi > 1.)).T
|
||||||
|
j = torch.stack((torch.ones_like(j), j, k, l, m))
|
||||||
|
t = t.repeat((5, 1, 1))[j]
|
||||||
|
offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j]
|
||||||
|
else:
|
||||||
|
t = targets[0]
|
||||||
|
offsets = 0
|
||||||
|
|
||||||
|
# Define
|
||||||
|
b, c = t[:, :2].long().T # image, class
|
||||||
gxy = t[:, 2:4] # grid xy
|
gxy = t[:, 2:4] # grid xy
|
||||||
gxi = gain[[2, 3]] - gxy # inverse
|
gwh = t[:, 4:6] # grid wh
|
||||||
j, k = ((gxy % 1. < g) & (gxy > 1.)).T
|
gij = (gxy - offsets).long()
|
||||||
l, m = ((gxi % 1. < g) & (gxi > 1.)).T
|
gi, gj = gij.T # grid xy indices
|
||||||
j = torch.stack((torch.ones_like(j), j, k, l, m))
|
|
||||||
t = t.repeat((5, 1, 1))[j]
|
|
||||||
offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j]
|
|
||||||
else:
|
|
||||||
t = targets[0]
|
|
||||||
offsets = 0
|
|
||||||
|
|
||||||
# Define
|
# Append
|
||||||
b, c = t[:, :2].long().T # image, class
|
a = t[:, 6].long() # anchor indices
|
||||||
gxy = t[:, 2:4] # grid xy
|
indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices
|
||||||
gwh = t[:, 4:6] # grid wh
|
tbox.append(torch.cat((gxy - gij, gwh), 1)) # box
|
||||||
gij = (gxy - offsets).long()
|
anch.append(anchors[a]) # anchors
|
||||||
gi, gj = gij.T # grid xy indices
|
tcls.append(c) # class
|
||||||
|
|
||||||
# Append
|
return tcls, tbox, indices, anch
|
||||||
a = t[:, 6].long() # anchor indices
|
|
||||||
indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices
|
|
||||||
tbox.append(torch.cat((gxy - gij, gwh), 1)) # box
|
|
||||||
anch.append(anchors[a]) # anchors
|
|
||||||
tcls.append(c) # class
|
|
||||||
|
|
||||||
return tcls, tbox, indices, anch
|
|
||||||
Reference in new issue
Block a user