zhiqwang / yolort

yolort is a runtime stack for yolov5 on specialized accelerators such as tensorrt, libtorch, onnxruntime, tvm and ncnn.
https://zhiqwang.com/yolort
GNU General Public License v3.0
708 stars 153 forks source link

Unexpected side effect on matplotlib's backend #467

Closed narugo1992 closed 11 months ago

narugo1992 commented 1 year ago

🐛 Describe the bug

When I import YOLOv5, the backend of matplotlib will be changed to agg unexpectly.

You can run this code, the first output is the correct backend, while the second one will be changed to agg.

import matplotlib
print(matplotlib.get_backend())

from yolort.models import YOLOv5
print(matplotlib.get_backend())

This will cause the plt.show to fail in jupyter notebook with the following warning information

matplotlib is currently using agg, which is a non-gui backend, so cannot show the figure.

Such side effects should be avoided to ensure that the original program will not be affected.

Versions

zhiqwang commented 1 year ago

Hi @narugo1992 , Sorry for missing this message, I guess it's because the dependency that I'm using has set some environment variable that causes the problem, but I can't be sure which dependency is causing this problem in a short time.

But if you want to do visualization within jupyter, you can use the cv2_imshow method below, which is more stable and convenient for me personally.

https://github.com/zhiqwang/yolov5-rt-stack/blob/1043ee3eacd8218c39e324c39693e373daf29a09/yolort/utils/image_utils.py#L52-L65

And see https://zhiqwang.com/yolov5-rt-stack/notebooks/comparison-between-yolort-vs-yolov5.html#Load-model-as-ultralytics-and-inference as usage example.

narugo1992 commented 1 year ago

Hi @narugo1992 , Sorry for missing this message, I guess it's because the dependency that I'm using has set some environment variable that causes the problem, but I can't be sure which dependency is causing this problem in a short time.

But if you want to do visualization within jupyter, you can use the cv2_imshow method below, which is more stable and convenient for me personally.

https://github.com/zhiqwang/yolov5-rt-stack/blob/1043ee3eacd8218c39e324c39693e373daf29a09/yolort/utils/image_utils.py#L52-L65

And see https://zhiqwang.com/yolov5-rt-stack/notebooks/comparison-between-yolort-vs-yolov5.html#Load-model-as-ultralytics-and-inference as usage example.

Thanks for your reply :smile: , using ` cv2_ Imshow ' is indeed a viable alternative. However, for my specific work, what I need to visualize on jupyter is not only original images and yolort detection results, but also various charts must be presented with matplotlib. I believe that I'm not alone, so~~~ looking forward to use the fixed released version.