Pytorch 和 Matplotlib 干扰

问题描述 投票:0回答:1

我在 Matplotlib 和 torch 方面遇到了一个奇怪的错误。如果我使用 torch.hub.load 行运行,plt.imshow 将不会显示任何内容(即使框架是正确的图像)。如果我评论这一行 plt.imshow 就可以工作。

此 torch.hub.load 行是否被注释 cv2.imshow 是否有效。

onnx_path = "my_weights.onnx"
yolo_path = "lib/yolov5/"


torch.hub.load(yolo_path, 'custom', path=onnx_path, source='local') 

video_reader = VideoReader(str(src_file))

# wait for thread to read
while not video_reader.is_ready():
    waiting += 1
    time.sleep(1)

while(video_reader.is_ready()):
   frame = video_reader.frame

   #cv2.imshow('image',frame)
   #cv2.waitKey(0)


   plt.imshow(frame)
   plt.axis('off')
   plt.show()

我似乎错过了一些东西,但我没有看到它。感谢任何帮助:)

python matplotlib pytorch
1个回答
0
投票

这与 yolo 更改 matplotlib 后端有关,请参阅 this github issues。解决方案应该是加载后重置matpltolib后端:

onnx_path = "my_weights.onnx"
yolo_path = "lib/yolov5/"


# Save backend
b = plt.get_backend()
    
torch.hub.load(yolo_path, 'custom', path=onnx_path, source='local') 

#Reset backend
matplotlib.use(b)

video_reader = VideoReader(str(src_file))

# wait for thread to read
while not video_reader.is_ready():
    waiting += 1
    time.sleep(1)

while(video_reader.is_ready()):
   frame = video_reader.frame

   #cv2.imshow('image',frame)
   #cv2.waitKey(0)


   plt.imshow(frame)
   plt.axis('off')
   plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.