eugenesiow / super-image

Image super resolution models for PyTorch.
https://eugenesiow.github.io/super-image/
Apache License 2.0
162 stars 19 forks source link

How to run inference on GPU #4

Open PodoprikhinMaxim opened 1 year ago

PodoprikhinMaxim commented 1 year ago

Hello! didn't find how to run your models using GPU, can you explain please how i can do it?

kobenein commented 1 year ago

Hello! didn't find how to run your models using GPU, can you explain please how i can do it?

I also want to know

Artucuno commented 1 year ago

Me too

jordanjuras commented 1 year ago

The models (https://github.com/eugenesiow/super-image/tree/main/src/super_image/models) are all classes that inherit from pytorch nn.Modules, via the intermediary base class PretrainedModel (https://github.com/eugenesiow/super-image/blob/main/src/super_image/modeling_utils.py#L160), so you can simply push the model to your GPU like this:

from super_image import EdsrModel, ImageLoader
from PIL import Image
import requests

import torch
device = torch.device("cuda")

url = 'https://paperswithcode.com/media/datasets/Set5-0000002728-07a9793f_zA3bDjj.jpg'
image = Image.open(requests.get(url, stream=True).raw)

model = EdsrModel.from_pretrained('eugenesiow/edsr-base', scale=2).to(device)
inputs = ImageLoader.load_image(torch.from_numpy(image).to(device))
preds = model(inputs).detach().cpu().numpy()

ImageLoader.save_image(preds, './scaled_2x.png')
ImageLoader.save_compare(inputs, preds, './scaled_2x_compare.png')
joefaron commented 1 year ago

@jordanjuras Traceback (most recent call last): File "C:\upscalegpu.py", line 12, in inputs = ImageLoader.load_image(torch.from_numpy(image).to(device)) TypeError: expected np.ndarray (got JpegImageFile)

thvardhan commented 1 year ago
from PIL import Image
import requests
import torch
device = torch.device("cuda")
print(torch.cuda.is_available()) # make sure this is TRUE !

url = './4-o.jpg'
image = Image.open(url)

model = EdsrModel.from_pretrained('eugenesiow/edsr-base', scale=2)
model = model.to(device)
inputs = ImageLoader.load_image(image)

preds = model(inputs.to(device))

ImageLoader.save_image(preds, './scaled_2x.png')
ImageLoader.save_compare(inputs, preds, './scaled_2x_compare.png')

I am using the following code to upscale a local image on GPU which seems to work fine