Image 运行时错误:CUDA使用预先训练的模型内存不足
我使用一个预先训练好的模型来改善图像 [https://github.com/swz30/MIRNet.][1] 我创建了一个demo.py(下面的代码)文件,以测试我的图像集是否符合预先培训的模板。在我的第一组图像中,所有的图像都具有很高的分辨率,我总是得到相同的错误:Image 运行时错误:CUDA使用预先训练的模型内存不足,image,machine-learning,pytorch,conv-neural-network,artificial-intelligence,Image,Machine Learning,Pytorch,Conv Neural Network,Artificial Intelligence,我使用一个预先训练好的模型来改善图像 [https://github.com/swz30/MIRNet.][1] 我创建了一个demo.py(下面的代码)文件,以测试我的图像集是否符合预先培训的模板。在我的第一组图像中,所有的图像都具有很高的分辨率,我总是得到相同的错误: RuntimeError: CUDA out of memory. Tried to allocate 5.38 GiB (GPU 0; 3.95 GiB total capacity; 379.90 MiB already
RuntimeError: CUDA out of memory. Tried to allocate 5.38 GiB (GPU 0; 3.95 GiB total capacity; 379.90 MiB already allocated; 2.89 GiB free; 16.10 MiB cached)
当我只测试一张分辨率较低的图像时,错误依然存在,但方式很奇怪:
RuntimeError: CUDA out of memory. Tried to allocate 1014.00 MiB (GPU 0; 3.95 GiB total capacity; 2.61 GiB already allocated; 527.44 MiB free; 23.25 MiB cached)
我对另一个存储库中的demo.py文件进行了必要的更改,以便在我的图像集上测试MIRNet。在这个过程中,我不得不做出一些与图形兼容性相关的配置,但一切都解决了
你对解决我的问题有什么建议吗?我在linux环境中使用的是预先培训过的模型,具有anaconda和graphics->NVIDIA GEFORCE GTX 960m 4gb的所有正确规范
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms.functional as TF
from PIL import Image
import os
from runpy import run_path
from skimage import img_as_ubyte
from collections import OrderedDict
from natsort import natsorted
from glob import glob
import cv2
import argparse
parser = argparse.ArgumentParser(description='Demo MIRNet')
parser.add_argument('--input_dir', default='./samples/', type=str, help='Input images')
parser.add_argument('--result_dir', default='./samples/output/', type=str, help='Directory for results')
parser.add_argument('--task', required=True, type=str, help='Task to run',
choices=['fivek', 'Denoising', 'SR_x3'])
args = parser.parse_args()
def save_img(filepath, img):
cv2.imwrite(filepath, cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
def load_checkpoint(model, weights):
checkpoint = torch.load(weights)
try:
model.load_state_dict(checkpoint["state_dict"])
except:
state_dict = checkpoint["state_dict"]
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
task = args.task
inp_dir = args.input_dir
out_dir = args.result_dir
os.makedirs(out_dir, exist_ok=True)
files = natsorted(glob(os.path.join(inp_dir, '*.jpg'))
+ glob(os.path.join(inp_dir, '*.JPG'))
+ glob(os.path.join(inp_dir, '*.png'))
+ glob(os.path.join(inp_dir, '*.PNG')))
if len(files) == 0:
raise Exception(f"No files found at {inp_dir}")
# Load corresponding model architecture and weights
load_file = run_path(os.path.join("networks", "MIRNet_model.py"))
model = load_file['MIRNet']()
model.cuda()
weights = os.path.join("pretrained_models/denoising", "model_" + task.lower() + ".pth")
load_checkpoint(model, weights)
model.eval()
img_multiple_of = 8
for file_ in files:
img = Image.open(file_).convert('RGB')
input_ = TF.to_tensor(img).unsqueeze(0).cuda()
# Pad the input if not_multiple_of 8
h, w = input_.shape[2], input_.shape[3]
H, W = ((h + img_multiple_of) // img_multiple_of) * img_multiple_of, (
(w + img_multiple_of) // img_multiple_of) * img_multiple_of
padh = H - h if h % img_multiple_of != 0 else 0
padw = W - w if w % img_multiple_of != 0 else 0
input_ = F.pad(input_, (0, padw, 0, padh), 'reflect')
with torch.no_grad():
restored = model(input_)
restored = restored[0]
restored = torch.clamp(restored, 0, 1)
# Unpad the output
restored = restored[:, :, :h, :w]
restored = restored.permute(0, 2, 3, 1).cpu().detach().numpy()
restored = img_as_ubyte(restored[0])
f = os.path.splitext(os.path.split(file_)[-1])[0]
save_img((os.path.join(out_dir, f + '.png')), restored)
print(f"Files saved at {out_dir}")
[1]: https://github.com/swz30/MIRNet.
它可能听起来很哑,但请尝试在终端中执行此命令:
pkill -9 python
注意,这个命令会杀死所有python进程。
也许这个过程中有一个在您尝试代码时卡住了,占用了GPU内存。如果这个命令不能解决您的问题,请尝试在Google Colab上运行代码,看看问题是否仍然存在:Colab应该为您提供具有10-12Gb RAM的GPU。
保持我们的最新状态您可以在Colab上测试它,它将为您提供11或12 GB的GPU(通常),以便您了解需要多少GPU内存。您还可以尝试在代码中使用
torch.cuda.empty_cache
。