ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
51.08k stars 16.42k forks source link

Cannot use matplotlib #2779

Closed mfoglio closed 3 years ago

mfoglio commented 3 years ago

Every time I load a yolo v5model using pytorch hub I cannot use matplotlib anymore. More precisely, matplotlib does not throw any error, but no plot is displayed. I am working with PyCharm connected to an SSH interpreter. matplotlib works with no issue if I don't load a yolo v5 model, so I suppose that yolo messes with some settings. I tried to change different matplotlib parameters but with no success. Is there anyone who can give me a hint? Thank you very much

glenn-jocher commented 3 years ago

@mfoglio ah, thanks for the bug report!

Yes I think I know exactly what's causing this. In YOLOv5 we disabled live previews of matlab plots as we only generate plots to save directly to jpg/png in all cases. This results in faster plotting and better compatibility across environments.

When you load a YOLOv5 Hub model then it is also executing the setting lines as it imports submodules. I'm not immediately sure of the best fix, but I'll add a TODO here and think about the best way to address this.

The relevant code section is here FYI: https://github.com/ultralytics/yolov5/blob/1487bc84ff3babfb502dffb5ffbdc7e02fcb1879/utils/plots.py#L24-L27

glenn-jocher commented 3 years ago

@mfoglio actually I do have one idea. Can you try to import your model first before any other custom imports per https://github.com/ultralytics/yolov5/issues/2414?

# Model
import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# Imports
# place added imports after model is loaded to avoid conflicts

# Images
dir = 'https://github.com/ultralytics/yolov5/raw/master/data/images/'
imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')]  # batch of images

# Inference
results = model(imgs)
results.print()  # or .show(), .save()
mfoglio commented 3 years ago

@glenn-jocher thank you for getting back to me. I tried to the model before, as well as your example, but unluckily both did not print anything

glenn-jocher commented 3 years ago

@mfoglio then you would have to explicitly define a matplotlib backend in your code: https://matplotlib.org/2.0.2/faq/usage_faq.html#what-is-a-backend

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

gunesevitan commented 2 years ago

I solved it with a workaround like this.

import torch
import matplotlib

model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Load model first
matplotlib.use('TkAgg') # Change backend after loading model
glenn-jocher commented 2 years ago

Dwight?

mfoglio commented 2 years ago

@gunesevitan what OS are you using? I am getting the following error when trying to use your solution: ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

gunesevitan commented 2 years ago

My OS is Ubuntu 20.04.4 LTS.

rexayyy commented 2 years ago
results = model(imgs)
results.print() 

how can i use it in windows 10. i am getting a kernel dead error on my jupyter notebook?

glenn-jocher commented 2 years ago

@rexayyy it appears you may have environment problems. Please ensure you meet all dependency requirements if you are attempting to run YOLOv5 locally. If in doubt, create a new virtual Python 3.9 environment, clone the latest repo (code changes daily), and pip install requirements.txt again from scratch.

💡 ProTip! Try one of our verified environments below if you are having trouble with your local environment.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Models and datasets download automatically from the latest YOLOv5 release when first requested.

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

izhako commented 1 year ago

I solved it with a workaround like this.

import torch
import matplotlib

model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Load model first
matplotlib.use('TkAgg') # Change backend after loading model

This works indeed iff the virtual environment is properly configured using package requirements.txt. Thanks

jvanmelckebeke commented 1 year ago

I use following workaround, which isn't really elegant but gets the job done.

import matplotlib

class IsolatedYOLO:
    def __init__(self, model_path: str):
        self.model_path = model_path

    def __enter__(self):
        # as a workaround we will save the current backend, and restore it when we exit
        self._current_backend = matplotlib.get_backend()

        from ultralytics import YOLO
        self._yolo = YOLO(self.model_path)
        return self._yolo

    def __exit__(self, exc_type, exc_value, traceback):
        # reset the original backend
        matplotlib.use(self._current_backend)

which can be used as:

with IsolatedYOLO('models/yolov8s.pt') as model:
    image_results = model.predict(....)
# plot functions here
glenn-jocher commented 1 year ago

@jvanmelckebeke glad to hear you found a workaround! Your implementation of IsolatedYOLO using a context manager is a creative way to address the issue. Please feel free to reach out if you have any further questions or need assistance. Happy coding!

traiand-bolt commented 9 months ago

If working in a Jupyter NB, running the magic %matplotlib inline after loading the models worked for me.

glenn-jocher commented 9 months ago

@traiand-bolt great to hear that %matplotlib inline resolved the issue for you in a Jupyter environment! This magic command is indeed a common solution for ensuring that plots are displayed inline within Jupyter notebooks. If you encounter any more issues or have questions, don't hesitate to ask. Happy coding! 🚀

traiand-bolt commented 9 months ago

@traiand-bolt great to hear that %matplotlib inline resolved the issue for you in a Jupyter environment! This magic command is indeed a common solution for ensuring that plots are displayed inline within Jupyter notebooks. If you encounter any more issues or have questions, don't hesitate to ask. Happy coding! 🚀

Thanks for the clarification! I had forgotten to mention I was working in Jupyter. I updated the comment now.

glenn-jocher commented 9 months ago

No problem at all, @traiand-bolt! It's always good to have the full context. If you have any more insights to share or need further assistance, feel free to reach out. Enjoy your work with YOLOv5 and Jupyter notebooks! 😊📊

jitesh3023 commented 5 months ago

Is it possible to use matplotlib directly in detect.py of yoloV5 model?

glenn-jocher commented 5 months ago

@jitesh3023 yes, you can use matplotlib directly in the detect.py of the YOLOv5 model for visualizing results. You'll need to import matplotlib.pyplot and add your plotting code after detections are made. Make sure to handle the plot display correctly, especially if you're running the script in a non-interactive environment. If you encounter any issues or need further customization tips, feel free to ask! Happy coding! 🚀

jitesh3023 commented 2 months ago

@glenn-jocher Thanks!!

glenn-jocher commented 2 months ago

@jitesh3023 you're welcome! If you have any further questions or need assistance, feel free to ask.