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.open
andrequests.get
- Download an image from a URL (website) and store this as theimage
variable.EdsrModel.from_pretrained
- Download and load a small, pre-trained deep-learning model to themodel
variable.ImageLoader.load_image
- Load the image into themodel
using theImageLoader
helper.- Use the model to run inference on the image (
inputs
). ImageLoader.save_image
- Save the upscaled image output as a.png
file using theImageLoader
helper.ImageLoader.save_compare
- Save a.png
that compares our upscaled image from the model with a baseline image usingBicubic
upscaling.
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
EdsrModel
with other pretrained models. - You can try different scales:
bicubic_x2
,bicubic_x3
orbicubic_x4
- Compare the performance via the leaderboard.