glenn-jocher commited on
Commit
fc7c427
·
unverified ·
1 Parent(s): 597ed4c

Update utils.py

Browse files
Files changed (1) hide show
  1. utils/utils.py +6 -10
utils/utils.py CHANGED
@@ -47,7 +47,7 @@ def check_git_status():
47
 
48
  def check_img_size(img_size, s=32):
49
  # Verify img_size is a multiple of stride s
50
- new_size = make_divisible(img_size, s) # ceil gs-multiple
51
  if new_size != img_size:
52
  print('WARNING: --img-size %g must be multiple of max stride %g, updating to %g' % (img_size, s, new_size))
53
  return new_size
@@ -421,9 +421,7 @@ def compute_loss(p, targets, model): # predictions, targets, model
421
  ft = torch.cuda.FloatTensor if p[0].is_cuda else torch.Tensor
422
  lcls, lbox, lobj = ft([0]), ft([0]), ft([0])
423
  tcls, tbox, indices, anchors = build_targets(p, targets, model) # targets
424
- h = model.module.hyp if hasattr(model, 'module') else model.hyp # hyperparameters
425
- nc = model.module.nc if hasattr(model, 'module') else model.nc
426
- gr = model.module.gr if hasattr(model, 'module') else model.gr
427
  red = 'mean' # Loss reduction (sum or mean)
428
 
429
  # Define criteria
@@ -457,10 +455,10 @@ def compute_loss(p, targets, model): # predictions, targets, model
457
  lbox += (1.0 - giou).sum() if red == 'sum' else (1.0 - giou).mean() # giou loss
458
 
459
  # Obj
460
- tobj[b, a, gj, gi] = (1.0 - gr) + gr * giou.detach().clamp(0).type(tobj.dtype) # giou ratio
461
 
462
  # Class
463
- if nc > 1: # cls loss (only if multiple classes)
464
  t = torch.full_like(ps[:, 5:], cn) # targets
465
  t[range(nb), tcls[i]] = cp
466
  lcls += BCEcls(ps[:, 5:], t) # BCE
@@ -479,7 +477,7 @@ def compute_loss(p, targets, model): # predictions, targets, model
479
  g = 3.0 # loss gain
480
  lobj *= g / bs
481
  if nt:
482
- lcls *= g / nt / nc
483
  lbox *= g / nt
484
 
485
  loss = lbox + lobj + lcls
@@ -490,8 +488,6 @@ def build_targets(p, targets, model):
490
  # Build targets for compute_loss(), input targets(image,class,x,y,w,h)
491
  det = model.module.model[-1] if type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel) \
492
  else model.model[-1] # Detect() module
493
- hyp = model.module.hyp if hasattr(model, 'module') else model.hyp
494
-
495
  na, nt = det.na, targets.shape[0] # number of anchors, targets
496
  tcls, tbox, indices, anch = [], [], [], []
497
  gain = torch.ones(6, device=targets.device) # normalized to gridspace gain
@@ -507,7 +503,7 @@ def build_targets(p, targets, model):
507
  a, t, offsets = [], targets * gain, 0
508
  if nt:
509
  r = t[None, :, 4:6] / anchors[:, None] # wh ratio
510
- j = torch.max(r, 1. / r).max(2)[0] < hyp['anchor_t'] # compare
511
  # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n) = wh_iou(anchors(3,2), gwh(n,2))
512
  a, t = at[j], t.repeat(na, 1, 1)[j] # filter
513
 
 
47
 
48
  def check_img_size(img_size, s=32):
49
  # Verify img_size is a multiple of stride s
50
+ new_size = make_divisible(img_size, int(s)) # ceil gs-multiple
51
  if new_size != img_size:
52
  print('WARNING: --img-size %g must be multiple of max stride %g, updating to %g' % (img_size, s, new_size))
53
  return new_size
 
421
  ft = torch.cuda.FloatTensor if p[0].is_cuda else torch.Tensor
422
  lcls, lbox, lobj = ft([0]), ft([0]), ft([0])
423
  tcls, tbox, indices, anchors = build_targets(p, targets, model) # targets
424
+ h = model.hyp # hyperparameters
 
 
425
  red = 'mean' # Loss reduction (sum or mean)
426
 
427
  # Define criteria
 
455
  lbox += (1.0 - giou).sum() if red == 'sum' else (1.0 - giou).mean() # giou loss
456
 
457
  # Obj
458
+ tobj[b, a, gj, gi] = (1.0 - model.gr) + model.gr * giou.detach().clamp(0).type(tobj.dtype) # giou ratio
459
 
460
  # Class
461
+ if model.nc > 1: # cls loss (only if multiple classes)
462
  t = torch.full_like(ps[:, 5:], cn) # targets
463
  t[range(nb), tcls[i]] = cp
464
  lcls += BCEcls(ps[:, 5:], t) # BCE
 
477
  g = 3.0 # loss gain
478
  lobj *= g / bs
479
  if nt:
480
+ lcls *= g / nt / model.nc
481
  lbox *= g / nt
482
 
483
  loss = lbox + lobj + lcls
 
488
  # Build targets for compute_loss(), input targets(image,class,x,y,w,h)
489
  det = model.module.model[-1] if type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel) \
490
  else model.model[-1] # Detect() module
 
 
491
  na, nt = det.na, targets.shape[0] # number of anchors, targets
492
  tcls, tbox, indices, anch = [], [], [], []
493
  gain = torch.ones(6, device=targets.device) # normalized to gridspace gain
 
503
  a, t, offsets = [], targets * gain, 0
504
  if nt:
505
  r = t[None, :, 4:6] / anchors[:, None] # wh ratio
506
+ j = torch.max(r, 1. / r).max(2)[0] < model.hyp['anchor_t'] # compare
507
  # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n) = wh_iou(anchors(3,2), gwh(n,2))
508
  a, t = at[j], t.repeat(na, 1, 1)[j] # filter
509