Logging fix from YOLOv5 (#47)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
pull/48/head
Glenn Jocher 2 years ago committed by GitHub
parent 47f1cb3ef4
commit 8530e3fae0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      ultralytics/yolo/utils/__init__.py

@ -14,6 +14,7 @@ NUM_THREADS = min(8, max(1, os.cpu_count() - 1)) # number of YOLOv5 multiproces
AUTOINSTALL = str(os.getenv('YOLOv5_AUTOINSTALL', True)).lower() == 'true' # global auto-install mode AUTOINSTALL = str(os.getenv('YOLOv5_AUTOINSTALL', True)).lower() == 'true' # global auto-install mode
FONT = 'Arial.ttf' # https://ultralytics.com/assets/Arial.ttf FONT = 'Arial.ttf' # https://ultralytics.com/assets/Arial.ttf
VERBOSE = str(os.getenv('YOLOv5_VERBOSE', True)).lower() == 'true' # global verbose mode VERBOSE = str(os.getenv('YOLOv5_VERBOSE', True)).lower() == 'true' # global verbose mode
LOGGING_NAME = 'yolov5'
def is_colab(): def is_colab():
@ -87,24 +88,31 @@ def colorstr(*input):
return "".join(colors[x] for x in args) + f"{string}" + colors["end"] return "".join(colors[x] for x in args) + f"{string}" + colors["end"]
def set_logging(name=None, verbose=VERBOSE): def set_logging(name=LOGGING_NAME, verbose=True):
# Sets level and returns logger # sets up logging for the given name
if is_colab() or is_kaggle(): rank = int(os.getenv('RANK', -1)) # rank in world for Multi-GPU trainings
for h in logging.root.handlers:
logging.root.removeHandler(h) # remove all handlers associated with the root logger object
rank = int(os.getenv("RANK", -1)) # rank in world for Multi-GPU trainings
level = logging.INFO if verbose and rank in {-1, 0} else logging.ERROR level = logging.INFO if verbose and rank in {-1, 0} else logging.ERROR
log = logging.getLogger(name) logging.config.dictConfig({
log.setLevel(level) "version": 1,
handler = logging.StreamHandler() "disable_existing_loggers": False,
handler.setFormatter(logging.Formatter("%(message)s")) "formatters": {
handler.setLevel(level) name: {
log.addHandler(handler) "format": "%(message)s"}},
"handlers": {
name: {
set_logging() # run before defining LOGGER "class": "logging.StreamHandler",
LOGGER = logging.getLogger("yolov5") # define globally "formatter": name,
if platform.system() == "Windows": "level": level,}},
"loggers": {
name: {
"level": level,
"handlers": [name],
"propagate": False,}}})
set_logging(LOGGING_NAME) # run before defining LOGGER
LOGGER = logging.getLogger(LOGGING_NAME) # define globally (used in train.py, val.py, detect.py, etc.)
if platform.system() == 'Windows':
for fn in LOGGER.info, LOGGER.warning: for fn in LOGGER.info, LOGGER.warning:
setattr(LOGGER, fn.__name__, lambda x: fn(emojis(x))) # emoji safe logging setattr(LOGGER, fn.__name__, lambda x: fn(emojis(x))) # emoji safe logging
@ -119,5 +127,5 @@ class TryExcept(contextlib.ContextDecorator):
def __exit__(self, exc_type, value, traceback): def __exit__(self, exc_type, value, traceback):
if value: if value:
print(emojis(f'{self.msg}{value}')) print(emojis(f"{self.msg}{': ' if self.msg else ''}{value}"))
return True return True

Loading…
Cancel
Save