Skip to content

Suppress torch.hub.load() Output #13237

@DylDevs

Description

@DylDevs

Search before asking

Question

Goody day all!

I am working on a project using YOLOv5 to detect objects. In this project, I would like the console output to have a specific format and be clean. However, when loading a YOLOv5 model with torch.hub.load, it outputs this to the console:

YOLOv5  2024-7-31 Python-3.11.9 torch-2.3.1+cpu CPU

Fusing layers... 
Model summary: 157 layers, 1788931 parameters, 0 gradients, 4.2 GFLOPs
Adding AutoShape...

Ive been searching for a way to suppress this output to replace with my own, but I cant seem to find a way that works.
I came across this issue: #2862, which hinted at 3 solutions:

  1. logging.getLogger("utils.general").setLevel(logging.WARNING)
  2. logging.getLogger("yolov5").setLevel(logging.WARNING)
  3. torch.hub.load(verbose=False)

None of these solutions worked for me, I also saw this online:

class SuppressPrint:
    def __enter__(self):
        self._original_stdout = sys.stdout
        sys.stdout = open(os.devnull, 'w')
    
    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout.close()
        sys.stdout = self._original_stdout
        
with SuppressPrint():
    torch.hub.load()

However, this had no effect either.
If anyone had ideas as to how I could go about solving this, it would be appreciated!

Thanks!

  • DylDev

Additional

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions