Skip to content

loading model clobbers existing logging configuration #10132

@rkechols

Description

@rkechols

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

PyTorch Hub

Bug

Loading this repository using torch.hub.load results in existing logger configurations being partially clobbered. Specifically, upon loading this repository, the root logger has a new handler added to it which prints the log message to STDERR with no formatting. Additionally, the root logger level is set to either INFO or ERROR (depending on parameters and environment variables).

Environment

  • YOLOv5 🚀 v6.2-237-g55e9516 Python-3.9.11 torch-1.12.1 CPU
  • OS: macOS 13.0 (22A380)

Minimal Reproducible Example

import logging
import logging.config
import torch

logging.config.dictConfig({
    "version": 1,
    "formatters": {
        "formatter_default": {
            "format": "%(asctime)s - %(levelname)8s - %(name)s - %(message)s"
        },
    },
    "handlers": {
        "handler_stdout": {
            "class": "logging.StreamHandler",
            "formatter": "formatter_default",
            "level": "DEBUG",
            "stream": "ext://sys.stdout",
        },
    },
    "loggers": {
        "my.unrelated.logger": {
            "level": "DEBUG"
        },
        "yolov5": {
            "level": "CRITICAL"
        },
    },
    "root": {
        "level": "INFO",
        "handlers": [
            "handler_stdout",
        ],
    },
})
UNRELATED_LOGGER = logging.getLogger("my.unrelated.logger")


def log_test(message: str):
    UNRELATED_LOGGER.debug(message)
    UNRELATED_LOGGER.info(message)
    UNRELATED_LOGGER.warning(message)
    UNRELATED_LOGGER.error(message)
    UNRELATED_LOGGER.critical(message)
    for handler_ref in logging._handlerList:
        handler_ref().flush()


log_test("before torch.hub.load")

model = torch.hub.load(
    "ultralytics/yolov5",  # commit tested: 55e95168465b094733e3ef1ec36e0a18f200cd94
    "yolov5s",
    trust_repo=True,
    pretrained=False,  # just download and load the repo; no specific weights needed
    device="cpu",
    verbose=False,
)

log_test("after torch.hub.load")

Note that above produces the following to STDOUT and STDERR respectively:

STDOUT:

2022-11-11 10:46:00,781 -    DEBUG - my.unrelated.logger - before torch.hub.load
2022-11-11 10:46:00,782 -     INFO - my.unrelated.logger - before torch.hub.load
2022-11-11 10:46:00,782 -  WARNING - my.unrelated.logger - before torch.hub.load
2022-11-11 10:46:00,782 -    ERROR - my.unrelated.logger - before torch.hub.load
2022-11-11 10:46:00,782 - CRITICAL - my.unrelated.logger - before torch.hub.load
2022-11-11 10:46:02,888 -    DEBUG - my.unrelated.logger - after torch.hub.load
2022-11-11 10:46:02,888 -     INFO - my.unrelated.logger - after torch.hub.load
2022-11-11 10:46:02,888 -  WARNING - my.unrelated.logger - after torch.hub.load
2022-11-11 10:46:02,888 -    ERROR - my.unrelated.logger - after torch.hub.load
2022-11-11 10:46:02,888 - CRITICAL - my.unrelated.logger - after torch.hub.load

STDERR:

after torch.hub.load
after torch.hub.load
after torch.hub.load
after torch.hub.load

According to my configuration for my.unrelated.logger, STDERR should not receive any messages from my.unrelated.logger.

Additional

Separate from this actual bug is the fact that the repo is trying to set logging configurations in imported code. Logging configurations should only be set by the python program's direct entrypoint. Setting logging configurations in packages, libraries, imported modules, etc. removes logging control from top-level code.

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions