Update utils.py

pull/1/head
yxNONG 5 years ago committed by GitHub
parent cdb9bde181
commit 53cdaf6bf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -421,7 +421,9 @@ def compute_loss(p, targets, model): # predictions, targets, model
ft = torch.cuda.FloatTensor if p[0].is_cuda else torch.Tensor
lcls, lbox, lobj = ft([0]), ft([0]), ft([0])
tcls, tbox, indices, anchors = build_targets(p, targets, model) # targets
h = model.hyp # hyperparameters
h = model.module.hyp if hasattr(model, 'module') else model.hyp # hyperparameters
nc = model.module.nc if hasattr(model, 'module') else model.nc
gr = model.module.gr if hasattr(model, 'module') else model.gr
red = 'mean' # Loss reduction (sum or mean)
# Define criteria
@ -455,10 +457,10 @@ def compute_loss(p, targets, model): # predictions, targets, model
lbox += (1.0 - giou).sum() if red == 'sum' else (1.0 - giou).mean() # giou loss
# Obj
tobj[b, a, gj, gi] = (1.0 - model.gr) + model.gr * giou.detach().clamp(0).type(tobj.dtype) # giou ratio
tobj[b, a, gj, gi] = (1.0 - gr) + gr * giou.detach().clamp(0).type(tobj.dtype) # giou ratio
# Class
if model.nc > 1: # cls loss (only if multiple classes)
if nc > 1: # cls loss (only if multiple classes)
t = torch.full_like(ps[:, 5:], cn) # targets
t[range(nb), tcls[i]] = cp
lcls += BCEcls(ps[:, 5:], t) # BCE
@ -477,7 +479,7 @@ def compute_loss(p, targets, model): # predictions, targets, model
g = 3.0 # loss gain
lobj *= g / bs
if nt:
lcls *= g / nt / model.nc
lcls *= g / nt / nc
lbox *= g / nt
loss = lbox + lobj + lcls
@ -488,6 +490,8 @@ def build_targets(p, targets, model):
# Build targets for compute_loss(), input targets(image,class,x,y,w,h)
det = model.module.model[-1] if type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel) \
else model.model[-1] # Detect() module
hyp = model.module.hyp if hasattr(model, 'module') else model.hyp
na, nt = det.na, targets.shape[0] # number of anchors, targets
tcls, tbox, indices, anch = [], [], [], []
gain = torch.ones(6, device=targets.device) # normalized to gridspace gain
@ -503,7 +507,7 @@ def build_targets(p, targets, model):
a, t, offsets = [], targets * gain, 0
if nt:
r = t[None, :, 4:6] / anchors[:, None] # wh ratio
j = torch.max(r, 1. / r).max(2)[0] < model.hyp['anchor_t'] # compare
j = torch.max(r, 1. / r).max(2)[0] < 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))
a, t = at[j], t.repeat(na, 1, 1)[j] # filter

Loading…
Cancel
Save