我面临的问题是让matplotlib图形从之前运行的一个python脚本中显示出来.我的意思是,脚本不再运行,但我仍然可以"随机"地显示这个数字.可能是每次我运行不同的python脚本来保存matplotlib图形,但不完全确定.
详细信息:
- 这些脚本是从vscode集成终端执行的.
- 当图形出现时,我可以在我的GPU上看到进程,或者使用
top
命令,但我一关闭图形窗口,进程就消失了,稍后会重新出现.
代码片段:
from pytorch_grad_cam import GradCAM
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image
from torchvision.models import resnet50
import torch
import matplotlib.pyplot as plt
model = resnet50(pretrained=True)
target_layers = [model.layer4[-1]]
# load an image an put it in a pytorch tensor
input_tensor = torch.rand(1, 3, 224, 224)
# Create an input tensor image for your model..
# Note: input_tensor can be a batch tensor with several images!
# Construct the CAM object once, and then re-use it on many images:
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=True)
targets = [ClassifierOutputTarget(281)]
# You can also pass aug_smooth=True and eigen_smooth=True, to apply smoothing.
grayscale_cam = cam(input_tensor=input_tensor, targets=targets)
# In this example grayscale_cam has only one image in the batch:
grayscale_cam = grayscale_cam[0, :]
rgb_img = input_tensor[0, :].permute(1, 2, 0).cpu().numpy()
visualisation = show_cam_on_image(rgb_img, grayscale_cam, use_rgb=True)
plt.figure()
plt.imshow(visualisation)
plt.show()
plt.close('all')
操作系统:Ubuntu 20.04 巨 Python :3.9.16
到目前为止,我try 了以下几点:
- 当它出现时终止进程
- 正在重新启动PC
- 在我的虚拟环境中卸载并重新安装matplotlib
- 查看父进程使用
pstree
并终止它. - 我们正在运行脚本并添加
plt.clf()
.
这可能与试图在有问题的脚本中使用plt.ion()
有关.或者从here开始在pytorch_grad_cam包的后台使用的OpenCV库.
Update:. - It seems that what triggers the figures is saving a file in my vscode editor....
- 在pstree提示中,链接到matplotlib图的python进程有一个代码(即vscode)父进程