我正在对50000张图像进行图像处理(摄像头)。在此过程中,内存不断增加。我正在使用python,pycharm尝试找出使代码的哪一部分增加了内存有没有办法找出导致内存使用的哪一行?我想在此过程中释放内存。
def returnCAM(self, feature_conv, weight_softmax, class_idx):
size_upsample = (128, 128)
bz, nc, h, w = feature_conv.shape
output_cam = []
cam = weight_softmax[class_idx].dot(feature_conv.reshape((nc, h * w)))
cam = cam.reshape(h, w)
cam = cam - np.min(cam)
cam_img = cam / np.max(cam)
cam_img = np.uint8(255 * cam_img)
output_cam.append(cv2.resize(cam_img, size_upsample))
return output_cam
以及生成凸轮图像
def generate_image(self, img, i):
img = Variable(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_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)))
return camsresult,heatmap,output_cam,probs.detach().cpu().numpy(), idx.detach().cpu().numpy()
通过使用此行output_cam.append(cv2.resize(cam_img, size_upsample))
很明显内存会增加...
所以不要追加仅返回一个return cv2.resize(cam_img, size_upsample)