Prediction#
Use the super-image library to quickly upscale an image.
Setting up the Environment#
Install the library#
We will install the super-image using pip install.
pip install -qq super-image
Load a Pretrained Model for Inference#
Next we run a few lines of code to:
Image.openandrequests.get- Download an image from a URL (website) and store this as theimagevariable.EdsrModel.from_pretrained- Download and load a small, pre-trained deep-learning model to themodelvariable.ImageLoader.load_image- Load the image into themodelusing theImageLoaderhelper.- Use the model to run inference on the image (
inputs). ImageLoader.save_image- Save the upscaled image output as a.pngfile using theImageLoaderhelper.ImageLoader.save_compare- Save a.pngthat compares our upscaled image from the model with a baseline image usingBicubicupscaling.
from super_image import EdsrModel, ImageLoader
from PIL import Image
import requests
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)
inputs = ImageLoader.load_image(image)
preds = model(inputs)
ImageLoader.save_image(preds, './scaled_2x.png')
ImageLoader.save_compare(inputs, preds, './scaled_2x_compare.png')
View the comparison image to see, visually, how our model performed (on the right) against the baseline bicubic method (left).
import cv2
img = cv2.imread('./scaled_2x_compare.png')
cv2.imshow(img)
We can view the original image that we pulled from the URL/website using cv2.imshow.
import numpy as np
cv2.imshow(cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB))
Try Other Models#
- You can replace the
EdsrModelwith other pretrained models. - You can try different scales:
bicubic_x2,bicubic_x3orbicubic_x4 - Compare the performance via the leaderboard.