From e5b6d4a3a0b42e6ec7ed8f5ab7baecac7ff6908a Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sat, 18 Sep 2021 14:16:19 +0200 Subject: [PATCH] Refactor argparser printing to `print_args()` (#4850) * Refactor argparser printing to `print_args()` * Cleanup --- detect.py | 12 ++++++------ export.py | 5 +++-- models/tf.py | 6 +++--- train.py | 7 +++---- utils/general.py | 5 +++++ val.py | 6 +++--- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/detect.py b/detect.py index ef7458d52db3..57bd6eea9ec4 100644 --- a/detect.py +++ b/detect.py @@ -19,12 +19,12 @@ sys.path.append(FILE.parents[0].as_posix()) # add yolov5/ to path from models.experimental import attempt_load -from utils.datasets import LoadStreams, LoadImages -from utils.general import check_img_size, check_imshow, check_requirements, check_suffix, colorstr, is_ascii, \ - non_max_suppression, apply_classifier, scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path, \ - save_one_box +from utils.datasets import LoadImages, LoadStreams +from utils.general import apply_classifier, check_img_size, check_imshow, check_requirements, check_suffix, colorstr, \ + increment_path, is_ascii, non_max_suppression, print_args, save_one_box, scale_coords, set_logging, \ + strip_optimizer, xyxy2xywh from utils.plots import Annotator, colors -from utils.torch_utils import select_device, load_classifier, time_sync +from utils.torch_utils import load_classifier, select_device, time_sync @torch.no_grad() @@ -279,11 +279,11 @@ def parse_opt(): parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference') opt = parser.parse_args() opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand + print_args(FILE.stem, opt) return opt def main(opt): - print(colorstr('detect: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items())) check_requirements(exclude=('tensorboard', 'thop')) run(**vars(opt)) diff --git a/export.py b/export.py index dd7eefc51702..9d7576bf383a 100644 --- a/export.py +++ b/export.py @@ -41,7 +41,8 @@ from models.yolo import Detect from utils.activations import SiLU from utils.datasets import LoadImages -from utils.general import colorstr, check_dataset, check_img_size, check_requirements, file_size, set_logging, url2file +from utils.general import colorstr, check_dataset, check_img_size, check_requirements, file_size, print_args, \ + set_logging, url2file from utils.torch_utils import select_device @@ -322,12 +323,12 @@ def parse_opt(): default=['torchscript', 'onnx'], help='available formats are (torchscript, onnx, coreml, saved_model, pb, tflite, tfjs)') opt = parser.parse_args() + print_args(FILE.stem, opt) return opt def main(opt): set_logging() - print(colorstr('export: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items())) run(**vars(opt)) diff --git a/models/tf.py b/models/tf.py index 5d7153f246eb..5b918ee3c34a 100644 --- a/models/tf.py +++ b/models/tf.py @@ -27,9 +27,9 @@ from tensorflow import keras from models.common import Conv, Bottleneck, SPP, DWConv, Focus, BottleneckCSP, Concat, autopad, C3 -from models.experimental import MixConv2d, CrossConv, attempt_load +from models.experimental import CrossConv, MixConv2d, attempt_load from models.yolo import Detect -from utils.general import colorstr, make_divisible, set_logging +from utils.general import make_divisible, print_args, set_logging from utils.activations import SiLU LOGGER = logging.getLogger(__name__) @@ -434,12 +434,12 @@ def parse_opt(): parser.add_argument('--dynamic', action='store_true', help='dynamic batch size') opt = parser.parse_args() opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand + print_args(FILE.stem, opt) return opt def main(opt): set_logging() - print(colorstr('tf.py: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items())) run(**vars(opt)) diff --git a/train.py b/train.py index 1d0c2c608878..281a3c0bad7a 100644 --- a/train.py +++ b/train.py @@ -36,7 +36,7 @@ from utils.datasets import create_dataloader from utils.general import labels_to_class_weights, increment_path, labels_to_image_weights, init_seeds, \ strip_optimizer, get_latest_run, check_dataset, check_git_status, check_img_size, check_requirements, \ - check_file, check_yaml, check_suffix, print_mutation, set_logging, one_cycle, colorstr, methods + check_file, check_yaml, check_suffix, print_args, print_mutation, set_logging, one_cycle, colorstr, methods from utils.downloads import attempt_download from utils.loss import ComputeLoss from utils.plots import plot_labels, plot_evolve @@ -470,9 +470,8 @@ def parse_opt(known=False): def main(opt, callbacks=Callbacks()): # Checks - set_logging(RANK) if RANK in [-1, 0]: - print(colorstr('train: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items())) + print_args(FILE.stem, opt) check_git_status() check_requirements(requirements=FILE.parent / 'requirements.txt', exclude=['thop']) @@ -508,7 +507,7 @@ def main(opt, callbacks=Callbacks()): if not opt.evolve: train(opt.hyp, opt, device, callbacks) if WORLD_SIZE > 1 and RANK == 0: - _ = [print('Destroying process group... ', end=''), dist.destroy_process_group(), print('Done.')] + _ = LOGGER.info('Destroying process group... ', end=''), dist.destroy_process_group(), LOGGER.info('Done.') # Evolve hyperparameters (optional) else: diff --git a/utils/general.py b/utils/general.py index 561602323ab2..d4d8e2064d08 100755 --- a/utils/general.py +++ b/utils/general.py @@ -89,6 +89,11 @@ def set_logging(rank=-1, verbose=True): level=logging.INFO if (verbose and rank in [-1, 0]) else logging.WARN) +def print_args(name, opt): + # Print argparser arguments + print(colorstr(f'{name}: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items())) + + def init_seeds(seed=0): # Initialize random number generator (RNG) seeds https://pytorch.org/docs/stable/notes/randomness.html # cudnn seed 0 settings are slower and more reproducible, else faster and less reproducible diff --git a/val.py b/val.py index 16dd76d680f7..3574fb085c07 100644 --- a/val.py +++ b/val.py @@ -24,7 +24,7 @@ from utils.datasets import create_dataloader from utils.general import coco80_to_coco91_class, check_dataset, check_img_size, check_requirements, \ check_suffix, check_yaml, box_iou, non_max_suppression, scale_coords, xyxy2xywh, xywh2xyxy, set_logging, \ - increment_path, colorstr + increment_path, colorstr, print_args from utils.metrics import ap_per_class, ConfusionMatrix from utils.plots import output_to_target, plot_images, plot_val_study from utils.torch_utils import select_device, time_sync @@ -295,7 +295,7 @@ def run(data, def parse_opt(): - parser = argparse.ArgumentParser(prog='val.py') + parser = argparse.ArgumentParser() parser.add_argument('--data', type=str, default='data/coco128.yaml', help='dataset.yaml path') parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)') parser.add_argument('--batch-size', type=int, default=32, help='batch size') @@ -319,12 +319,12 @@ def parse_opt(): opt.save_json |= opt.data.endswith('coco.yaml') opt.save_txt |= opt.save_hybrid opt.data = check_yaml(opt.data) # check YAML + print_args(FILE.stem, opt) return opt def main(opt): set_logging() - print(colorstr('val: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items())) check_requirements(requirements=FILE.parent / 'requirements.txt', exclude=('tensorboard', 'thop')) if opt.task in ('train', 'val', 'test'): # run normally