它产生消息 尝试使用上下文管理,因为想要释放内存时 调用genereate_image finish
我必须运行generate_image 500000次在这个过程中,我的ram内存是保持上升 如果你有更好的解决方案,请告诉我 @contextmanager def generate_image(self, img, i): #打印(“= = = = = = = = = = = = = = = = =生成图像= = = = = = = = = = = = = = = = = = = = = = = = = = = = = ") img =变量(img, requires_grad=True)
self.net._modules.get (self.finalconv_name) .register_forward_hook (self.hook_feature)
img_tensor = img.to (self.device) logit, _ = self.net(img_张量) h_x = F。softmax(分对数,昏暗的= 1).data.squeeze ()
weight_softmaxtemp = self.weight_softmax feature_blobstemp = self.feature_blobs [0] idx = h_x。排序(0,真的) idx_temp = [idx [0]] output_cam = self.returnCAM (self.feature_blobs [0], self.weight_softmax, [idx [0] .item ())) 高,宽= 28,28 热图= cv2.applyColorMap (cv2。调整大小(output_cam[0],(宽度,高度)),cv2.COLORMAP_JET) heatmap2 = cv2.resize (output_cam[0],(宽度、高度))
.numpy img = img.detach () () img2 = img [0] img2 = np.transpose (img2轴= (1 2 0)) img2 = cv2.resize (img2(28日28))
img2 = cv2.applyColorMap (cv2。调整大小(output_cam[0],(宽度,高度)),cv2.COLORMAP_JET)
camsresult = np。阵列(列表(地图(resize_image热图,img2))) del img_tensor output_cam、heatmap2 img2, img gc.collect () 结果= zip(camsresult, heatmap, probs.detach().cpu().numpy(), idx.detach().cpu().numpy()) 返回结果
使用self.generate_image(img,idx)作为结果: sal_maps_b, heat_map_b, probs_b, preds_b =结果 问题来源StackOverflow 地址:/questions/59384300/raise-runtimeerrorgenerator-didnt-stop-runtimeerror-generator-didnt-stop
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。