开发者社区> 问答> 正文

引发RuntimeError(“生成器没有停止”)RuntimeError:生成器没有停止

它产生消息 尝试使用上下文管理,因为想要释放内存时 调用genereate_image finish

我必须运行generate_image 500000次在这个过程中,我的ram内存是保持上升 如果你有更好的解决方案,请告诉我 @contextmanager def generate_image(self, img, i): #打印(“= = = = = = = = = = = = = = = = =生成图像= = = = = = = = = = = = = = = = = = = = = = = = = = = = = ") img =变量(img, requires_grad=True)

image_PIL = transforms.ToPILImage () (img [0])

image_PIL.save(“结果/测试{}. jpg”.format(我))

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))

TODO擦掉

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

展开
收起
kun坤 2019-12-26 14:41:14 537 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载