Charming bug - torch.load

Use Google Colab ran 50 EDSR generation super-sub neural network, then the network model downloaded to do the test on win10, the result has been an error, the card for a long time

The results Baidu to this article: error when Pytorch load depth model: RuntimeError: cuda runtime error (10 ): invalid device ordinal

 

The cause of this error is because pytorch will save when the model graphics card information is also stored when re-load and found a video card is not the same on the error invalid device ordinal

After know, I hasten to do a test run on the colab, the result has an error

Use the default training GPU, and any course I tested using the CPU, so there are more errors

 

Here is the test under the GPU can be used:

from __future__ import print_function
import argparse
import torch
import torch.backends.cudnn as cudnn
from PIL import Image
from torchvision.transforms import ToTensor

import numpy as np

# ===========================================================
# Argument settings
# ===========================================================
parser = argparse.ArgumentParser(description='PyTorch Super Res Example')
parser.add_argument('--input', type=str, required=False, default='/content/drive/My Drive/app/EDSR_medical/path/rgb512.tif', help='input image to use')
parser.add_argument('--model', type=str, default='/content/drive/My Drive/app/EDSR_medical/path/EDSR_model_50.pth', help='model file to use')
parser.add_argument('--output_filename', type=str, default='/content/drive/My Drive/app/EDSR_medical/path/out_EDSR_50.tif', help='where to save the output image')
args = parser.parse_args()
print(args)


# ===========================================================
# input image setting
# ===========================================================
GPU_IN_USE = torch.cuda.is_available()
img = Image.open(args.input)
red_c, green_c, blue_c = img.split()

red_c.save('/content/drive/My Drive/app/EDSR_medical/path/red_c.tif')
green_c.save('/content/drive/My Drive/app/EDSR_medical/path/green_c.tif')
all_black = Image.open('/content/drive/My Drive/app/EDSR_medical/path/all_black_1024.tif')
# ===========================================================
# model import & setting
# ===========================================================
device = torch.device('cuda' if GPU_IN_USE else 'cpu')
model = torch.load(args.model)#, map_location=lambda storage, loc: storage
model = model.to(device)
data = (ToTensor()(red_c)).view(1, -1, y.size[1], y.size[0])
data = data.to(device)

if GPU_IN_USE:
    cudnn.benchmark = True


# ===========================================================
# output and save image
# ===========================================================
out = model(data)
out = out.cpu()
out_img_r = out.data[0].numpy()
out_img_r *= 255.0
out_img_r = out_img_r.clip(0, 255)
out_img_r = Image.fromarray(np.uint8(out_img_r[0]), mode='L')

out_img_r.save('/content/drive/My Drive/app/EDSR_medical/path/test_r_1024_edsr_50.tif')

out_img_g = all_black
out_img_b = all_black
out_img = Image.merge('RGB', [out_img_r, out_img_g, out_img_b]).convert('RGB')
#out_img = out_img_r
out_img.save(args.output_filename)
print('output image saved to ', args.output_filename)

 

Guess you like

Origin www.cnblogs.com/zgqcn/p/11204351.html