glenn-jocher pre-commit-ci[bot] commited on
Commit
5f941a8
1 Parent(s): 676e10c

Print dataset scan only `if RANK in (-1, 0)` (#7337)

Browse files

* Print dataset scan only `if RANK in (-1, 0)`

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

Files changed (2) hide show
  1. train.py +5 -5
  2. utils/datasets.py +2 -1
train.py CHANGED
@@ -316,7 +316,7 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
316
  train_loader.sampler.set_epoch(epoch)
317
  pbar = enumerate(train_loader)
318
  LOGGER.info(('\n' + '%10s' * 7) % ('Epoch', 'gpu_mem', 'box', 'obj', 'cls', 'labels', 'img_size'))
319
- if RANK in [-1, 0]:
320
  pbar = tqdm(pbar, total=nb, bar_format='{l_bar}{bar:10}{r_bar}{bar:-10b}') # progress bar
321
  optimizer.zero_grad()
322
  for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
@@ -365,7 +365,7 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
365
  last_opt_step = ni
366
 
367
  # Log
368
- if RANK in [-1, 0]:
369
  mloss = (mloss * i + loss_items) / (i + 1) # update mean losses
370
  mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB)
371
  pbar.set_description(('%10s' * 2 + '%10.4g' * 5) %
@@ -379,7 +379,7 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
379
  lr = [x['lr'] for x in optimizer.param_groups] # for loggers
380
  scheduler.step()
381
 
382
- if RANK in [-1, 0]:
383
  # mAP
384
  callbacks.run('on_train_epoch_end', epoch=epoch)
385
  ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'names', 'stride', 'class_weights'])
@@ -440,7 +440,7 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
440
 
441
  # end epoch ----------------------------------------------------------------------------------------------------
442
  # end training -----------------------------------------------------------------------------------------------------
443
- if RANK in [-1, 0]:
444
  LOGGER.info(f'\n{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.')
445
  for f in last, best:
446
  if f.exists():
@@ -518,7 +518,7 @@ def parse_opt(known=False):
518
 
519
  def main(opt, callbacks=Callbacks()):
520
  # Checks
521
- if RANK in [-1, 0]:
522
  print_args(vars(opt))
523
  check_git_status()
524
  check_requirements(exclude=['thop'])
 
316
  train_loader.sampler.set_epoch(epoch)
317
  pbar = enumerate(train_loader)
318
  LOGGER.info(('\n' + '%10s' * 7) % ('Epoch', 'gpu_mem', 'box', 'obj', 'cls', 'labels', 'img_size'))
319
+ if RANK in (-1, 0):
320
  pbar = tqdm(pbar, total=nb, bar_format='{l_bar}{bar:10}{r_bar}{bar:-10b}') # progress bar
321
  optimizer.zero_grad()
322
  for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
 
365
  last_opt_step = ni
366
 
367
  # Log
368
+ if RANK in (-1, 0):
369
  mloss = (mloss * i + loss_items) / (i + 1) # update mean losses
370
  mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB)
371
  pbar.set_description(('%10s' * 2 + '%10.4g' * 5) %
 
379
  lr = [x['lr'] for x in optimizer.param_groups] # for loggers
380
  scheduler.step()
381
 
382
+ if RANK in (-1, 0):
383
  # mAP
384
  callbacks.run('on_train_epoch_end', epoch=epoch)
385
  ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'names', 'stride', 'class_weights'])
 
440
 
441
  # end epoch ----------------------------------------------------------------------------------------------------
442
  # end training -----------------------------------------------------------------------------------------------------
443
+ if RANK in (-1, 0):
444
  LOGGER.info(f'\n{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.')
445
  for f in last, best:
446
  if f.exists():
 
518
 
519
  def main(opt, callbacks=Callbacks()):
520
  # Checks
521
+ if RANK in (-1, 0):
522
  print_args(vars(opt))
523
  check_git_status()
524
  check_requirements(exclude=['thop'])
utils/datasets.py CHANGED
@@ -36,6 +36,7 @@ HELP_URL = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
36
  IMG_FORMATS = 'bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp' # include image suffixes
37
  VID_FORMATS = 'asf', 'avi', 'gif', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ts', 'wmv' # include video suffixes
38
  BAR_FORMAT = '{l_bar}{bar:10}{r_bar}{bar:-10b}' # tqdm bar format
 
39
 
40
  # Get orientation exif tag
41
  for orientation in ExifTags.TAGS.keys():
@@ -454,7 +455,7 @@ class LoadImagesAndLabels(Dataset):
454
 
455
  # Display cache
456
  nf, nm, ne, nc, n = cache.pop('results') # found, missing, empty, corrupt, total
457
- if exists:
458
  d = f"Scanning '{cache_path}' images and labels... {nf} found, {nm} missing, {ne} empty, {nc} corrupt"
459
  tqdm(None, desc=prefix + d, total=n, initial=n, bar_format=BAR_FORMAT) # display cache results
460
  if cache['msgs']:
 
36
  IMG_FORMATS = 'bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp' # include image suffixes
37
  VID_FORMATS = 'asf', 'avi', 'gif', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ts', 'wmv' # include video suffixes
38
  BAR_FORMAT = '{l_bar}{bar:10}{r_bar}{bar:-10b}' # tqdm bar format
39
+ LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
40
 
41
  # Get orientation exif tag
42
  for orientation in ExifTags.TAGS.keys():
 
455
 
456
  # Display cache
457
  nf, nm, ne, nc, n = cache.pop('results') # found, missing, empty, corrupt, total
458
+ if exists and LOCAL_RANK in (-1, 0):
459
  d = f"Scanning '{cache_path}' images and labels... {nf} found, {nm} missing, {ne} empty, {nc} corrupt"
460
  tqdm(None, desc=prefix + d, total=n, initial=n, bar_format=BAR_FORMAT) # display cache results
461
  if cache['msgs']: