Closed mfoglio closed 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
@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()
@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
@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
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.
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
Dwight?
@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
My OS is Ubuntu 20.04.4 LTS.
results = model(imgs) results.print()
how can i use it in windows 10. i am getting a kernel dead error on my jupyter notebook?
@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.
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.
YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
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.
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
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
@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!
If working in a Jupyter NB, running the magic %matplotlib inline
after loading the models worked for me.
@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 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.
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! 😊📊
Is it possible to use matplotlib directly in detect.py of yoloV5 model?
@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! 🚀
@glenn-jocher Thanks!!
@jitesh3023 you're welcome! If you have any further questions or need assistance, feel free to ask.
Every time I load a
yolo v5
model using pytorch hub I cannot usematplotlib
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 ayolo v5
model, so I suppose that yolo messes with some settings. I tried to change differentmatplotlib
parameters but with no success. Is there anyone who can give me a hint? Thank you very much