From 53cdaf6bf5fcdf28b140d3898d59876ceee5fac0 Mon Sep 17 00:00:00 2001 From: yxNONG <62932917+yxNONG@users.noreply.github.com> Date: Thu, 2 Jul 2020 13:48:19 +0800 Subject: [PATCH] Update utils.py --- utils/utils.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/utils/utils.py b/utils/utils.py index c33f41f..2205999 100755 --- a/utils/utils.py +++ b/utils/utils.py @@ -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