开发者社区> 问答> 正文

错误“_GeneratorContextManager”对象不可订阅

我得到一个error '_GeneratorContextManager'对象不可订阅'的错误 试图使用contextmanager时,我产生的图像的新功能 这占用了太多的内存,所以我试图在函数调用结束时释放内存

@contextmanager
    def generate_image(self, img, i):
        #print("=================generate image=============================")
        img = Variable(img, requires_grad=True)

        #image_PIL = transforms.ToPILImage()(img[0])
        #image_PIL.save('result/test{}.jpg'.format(i))

        self.net._modules.get(self.finalconv_name).register_forward_hook(self.hook_feature)

        img_tensor = img.to(self.device)
        logit , _ = self.net(img_tensor)
        h_x = F.softmax(logit, dim=1).data.squeeze()

        weight_softmaxtemp = self.weight_softmax
        feature_blobstemp = self.feature_blobs[0]
        probs, idx = h_x.sort(0, True)
        idx_temp = [idx[0]]
        output_cam = self.returnCAM(self.feature_blobs[0],self.weight_softmax,[idx[0].item()])
        height, width = 28,28
        heatmap = cv2.applyColorMap(cv2.resize(output_cam[0], (width, height)), cv2.COLORMAP_JET)
        heatmap2 = cv2.resize(output_cam[0],(width,height))


        img =img.detach().numpy()
        img2 = img[0]
        img2 = np.transpose(img2,axes=(1,2,0))
        img2=cv2.resize(img2,(28,28))

        #TODO erase
        img2 = cv2.applyColorMap(cv2.resize(output_cam[0], (width, height)), cv2.COLORMAP_JET)


        camsresult = np.array(list(map(resize_image, heatmap, img2)))
        del img_tensor,output_cam,heatmap2,img2,img
        gc.collect()
        result = zip(camsresult, heatmap, probs.detach().cpu().numpy(), idx.detach().cpu().numpy())


with  self.generate_image(img,idx) as result:
      sal_maps_b, heat_map_b, probs_b, preds_b = result

我收到一条错误信息 问题来源StackOverflow 地址:/questions/59384137/error-generatorcontextmanager-object-is-not-subscriptable

展开
收起
kun坤 2019-12-26 15:38:56 597 0
1 条回答
写回答
取消 提交回答
  • 不知道为什么这里需要contextmanager装饰器,但是它看起来像

    result = zip(camsresult, heatmap, probs.detach().cpu().numpy(), idx.detach().cpu().numpy())
    

    您应该放弃它,让contextmanager在幕后发挥作用。

    yield zip(camsresult, heatmap, probs.detach().cpu().numpy(), idx.detach().cpu().numpy())
    
    2019-12-26 15:39:05
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

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

相关实验场景

更多