From 2c3221844b604c7e3f26c1f26d0c5ed78f700fd5 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 31 Mar 2022 17:11:43 +0200 Subject: [PATCH] CLI `fire` prep updates (#7229) * CLI fire prep updates * revert unintentional TF export change --- detect.py | 2 +- export.py | 2 +- models/tf.py | 2 +- models/yolo.py | 2 +- train.py | 2 +- utils/benchmarks.py | 2 +- utils/general.py | 15 ++++++++++++--- val.py | 2 +- 8 files changed, 19 insertions(+), 10 deletions(-) diff --git a/detect.py b/detect.py index 2875285ee..14ff9a6ab 100644 --- a/detect.py +++ b/detect.py @@ -238,7 +238,7 @@ def parse_opt(): parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference') opt = parser.parse_args() opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand - print_args(FILE.stem, opt) + print_args(vars(opt)) return opt diff --git a/export.py b/export.py index 78b886fa3..e146dad42 100644 --- a/export.py +++ b/export.py @@ -566,7 +566,7 @@ def parse_opt(): default=['torchscript', 'onnx'], help='torchscript, onnx, openvino, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs') opt = parser.parse_args() - print_args(FILE.stem, opt) + print_args(vars(opt)) return opt diff --git a/models/tf.py b/models/tf.py index c6fb6b82a..1b7653bce 100644 --- a/models/tf.py +++ b/models/tf.py @@ -480,7 +480,7 @@ 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) + print_args(vars(opt)) return opt diff --git a/models/yolo.py b/models/yolo.py index 4cdfea34d..e18614cb3 100644 --- a/models/yolo.py +++ b/models/yolo.py @@ -308,7 +308,7 @@ if __name__ == '__main__': parser.add_argument('--test', action='store_true', help='test all yolo*.yaml') opt = parser.parse_args() opt.cfg = check_yaml(opt.cfg) # check YAML - print_args(FILE.stem, opt) + print_args(vars(opt)) device = select_device(opt.device) # Create model diff --git a/train.py b/train.py index fbaaeb8ef..38c25c053 100644 --- a/train.py +++ b/train.py @@ -515,7 +515,7 @@ def parse_opt(known=False): def main(opt, callbacks=Callbacks()): # Checks if RANK in [-1, 0]: - print_args(FILE.stem, opt) + print_args(vars(opt)) check_git_status() check_requirements(exclude=['thop']) diff --git a/utils/benchmarks.py b/utils/benchmarks.py index 5bfa872cc..69243725b 100644 --- a/utils/benchmarks.py +++ b/utils/benchmarks.py @@ -92,7 +92,7 @@ def parse_opt(): parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference') opt = parser.parse_args() - print_args(FILE.stem, opt) + print_args(vars(opt)) return opt diff --git a/utils/general.py b/utils/general.py index a64680bc0..9622a32c5 100755 --- a/utils/general.py +++ b/utils/general.py @@ -5,6 +5,7 @@ General utils import contextlib import glob +import inspect import logging import math import os @@ -20,6 +21,7 @@ from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output +from typing import Optional from zipfile import ZipFile import cv2 @@ -163,9 +165,15 @@ def methods(instance): return [f for f in dir(instance) if callable(getattr(instance, f)) and not f.startswith("__")] -def print_args(name, opt): - # Print argparser arguments - LOGGER.info(colorstr(f'{name}: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items())) +def print_args(args: Optional[dict] = None, show_file=True, show_fcn=False): + # Print function arguments (optional args dict) + x = inspect.currentframe().f_back # previous frame + file, _, fcn, _, _ = inspect.getframeinfo(x) + if args is None: # get args automatically + args, _, _, frm = inspect.getargvalues(x) + args = {k: v for k, v in frm.items() if k in args} + s = (f'{Path(file).stem}: ' if show_file else '') + (f'{fcn}: ' if show_fcn else '') + LOGGER.info(colorstr(s) + ', '.join(f'{k}={v}' for k, v in args.items())) def init_seeds(seed=0): @@ -346,6 +354,7 @@ def check_img_size(imgsz, s=32, floor=0): if isinstance(imgsz, int): # integer i.e. img_size=640 new_size = max(make_divisible(imgsz, int(s)), floor) else: # list i.e. img_size=[640, 480] + imgsz = list(imgsz) # convert to list if tuple new_size = [max(make_divisible(x, int(s)), floor) for x in imgsz] if new_size != imgsz: LOGGER.warning(f'WARNING: --img-size {imgsz} must be multiple of max stride {s}, updating to {new_size}') diff --git a/val.py b/val.py index bc4abc248..019beedea 100644 --- a/val.py +++ b/val.py @@ -350,7 +350,7 @@ def parse_opt(): opt.data = check_yaml(opt.data) # check YAML opt.save_json |= opt.data.endswith('coco.yaml') opt.save_txt |= opt.save_hybrid - print_args(FILE.stem, opt) + print_args(vars(opt)) return opt