Super Resolving Images using SRGAN

Arsh Anwar
5 min readNov 28, 2022

The technique of recovering high-resolution (HR) pictures from low-resolution (LR) photos is known as image super-resolution (SR). It is a significant class of image processing algorithms in computer vision and image processing, with several real-world applications including medical imaging, satellite imaging, surveillance and security, and astronomical imaging.

Deep learning-based SR models have been intensively researched in recent years as deep learning techniques have advanced, and they frequently reach state-of-the-art performance on several SR benchmarks. Deep learning methods ranging from the early Convolutional Neural Networks (CNN)-based method to recent promising Generative Adversarial Nets-based SR approaches have been used to address SR challenges.

An Inference from SRGAN. Left: Low-Resolution Image Right: Super-Resolved Image

Why do we actually need a Super-Resolved Image?

Instead of going through all of this technological nonsense, why don’t we just take a high-resolution image? The explanation is the same as why we don’t buy a DSLR when it’s plainly superior to a phone camera. Yes, getting a high-resolution photograph is costly and often impossible. I’ll mention a few examples here.

1) Medical Images

Medical pictures (such as MRI, CT, PET, and so on) are particularly useful in an illness diagnosis. Due to resolution restrictions, these scans aren’t always useful. Better photos have been created using SR methods.

left — Original LR Knee scan; right — SR image. Image Source: Image super-resolution

2) Enhancement of video and optical image stabilization

Super-resolution methods have been used on low-resolution pictures in videos (Standard definition — SDTV) to make videos that are close to High Definition Videos (HDTV) and can be played on HD televisions. Super-resolution can also be utilized to eliminate blurring caused by the camera’s motion. To address such concerns, SR techniques are expected to be used in phones and tablets in the near future.

3) Surveillance

Surveillance cameras are now widely used for traffic and security monitoring. It goes without saying that installing high-resolution cameras everywhere is unfeasible. Because videos include more data than photos and are vulnerable to camera motion errors, super-resolution in this domain becomes much more challenging. The majority of video super-resolution techniques

4) Astronomical Images

The resolution of astronomical equipment is restricted by their technology. Super-resolution is a boon to researchers exploring and studying outer space

left — LR astronomical image; right — SR image. Image Source: Image super-resolution

In today’s blog, we are going to learn about SRGAN and use it for super-resolving, Low-Resolution Images.

So let’s get started.

Importing Libraries

import os

import cv2
import torch
from torch import nn

import SRGAN.imgproc as imgproc
import SRGAN.model as model
from SRGAN.utils import load_state_dict

We are using SRGAN-Pytorch, a PyTorch implementation of Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network.

Loading our Model

Here, we are going to load the pre-trained srresnet_x4 SRGAN Model and start it in eval mode.

# super_res_gan.py
sr_model = build_model('srresnet_x4', device)
# Load model weights
sr_model = load_state_dict(
sr_model, 'SRGAN/results/pretrained_models/SRGAN_x4-ImageNet-8c4a7569.pth.tar')
# Start the verification mode of the model.
sr_model.eval()

Building Driver Function

For Super Resolving the images we need to create a driver function that will take image_path, preprocess it and do the inferencing using the Loaded model.

# super_res_gan.py
def main(image):
image = cv2.imread(image)
lr_tensor = imgproc.preprocess_one_image(image, device)
# Use the model to generate super-resolved images
with torch.no_grad():
sr_tensor = sr_model(lr_tensor)
# Post-Process Image
sr_image = imgproc.tensor_to_image(sr_tensor, False, False)
sr_image = cv2.cvtColor(sr_image, cv2.COLOR_RGB2BGR)
sr_image = cv2.cvtColor(sr_image, cv2.COLOR_BGR2RGB)
return sr_image

Creating Gradio App

We will create our gradio which will enable us to use this driver function interactively

demo = gr.Blocks(title='Super Resolution GAN Demo')  # Create a gradio block

with demo:
gr.Markdown("# Super Resolution GAN Demo")
with gr.Tabs():
with gr.TabItem("Examples"): # If the user wants to use the examples
with gr.Row():
rad1 = gr.components.Radio(
['Image 1', 'Image 2'], label='Select Low Resolution Image and wait till it appears!') # Radio button to select the image
img1 = gr.Image(label="Low Resolution Image Example", shape=(300, 300))
submit1 = gr.Button('Submit')
with gr.TabItem("Do it yourself!"): # If the user wants to add their own image
with gr.Row():
img3 = gr.Image(
label="Add any Low resolution image", shape=(300, 300))
submit2 = gr.Button('Submit')

def action1(choice): # Function to show the article when the user selects the article
global filepath
if choice == 'Image 1':
filepath = './SRGAN/samples/comic_lr.png'
return './SRGAN/samples/comic_lr.png'
elif choice == 'Image 2':
filepath = './SRGAN/samples/pika.jpeg'
return './SRGAN/samples/pika.jpeg'

# Change the image when the user selects the image name
rad1.change(action1, rad1, img1)

# Output for the Highlighted text
op = gr.Image(label="High Res Image", shape=(300, 300))

gr.Markdown(
"### Made with ❤️ by Arsh using TrueFoundry's Gradio Deployment")
gr.Markdown(
"### [Github Repo](https://github.com/d4rk-lucif3r/Super-Resolution-GAN-Demo)")
gr.Markdown(
'### [Blog]()')

def fn(img1): # Main function
global filepath
result = main(filepath)
return result

try:
submit1.click(fn=fn, outputs=[
op], inputs=[img1]) # Submit button for the examples
except Exception as e:
logging.info('Error in submit1 ', e)
pass
# Submit button for the user input
submit2.click(fn=fn, outputs=[op], inputs=[img1])

demo.queue() # Queue the block
demo.launch(server_port=8080, server_name='0.0.0.0',
show_error=True) # Launch the gradio block

Test Drive

Running our App by using:

python app.py

Voila! it runs, Now we will work on deploying this.

Here we can see a small low res image has been upscaled and super-resolved.

Deployment

We are going to use TrueFoundry for our deployment

Logging into TrueFoundry

Heading to Deployment Section

1) Creating a new deployment

2) Select the Service option and Workspace name

3) Fill out properties and submit

4) Deploying

5) Successful Deployment

6) Final Thoughts

After the deployment, you will be able to use the Gradio App.

The app is deployed here: https://super-res-demo-demo-projects.tfy-ctl-euwe1-production.production.truefoundry.com

Video

Code

The above code is also present in my Repository

References:

  1. TrueFoundry: https://truefoundry.com/
  2. TrueFoundry App: https://app.truefoundry.com/
  3. TrueFoundry Docs: http://docs.truefoundry.com/
  4. Code: https://github.com/d4rk-lucif3r/Super-Resolution-GAN-Demo
  5. SRGAN-PyTorch: https://github.com/Lornatang/SRGAN-PyTorch
  6. SRGAN Paper: https://arxiv.org/abs/1609.04802v5
  7. My Fork of SRGAN-PyTorch: https://github.com/d4rk-lucif3r/SRGAN-PyTorch
  8. My Fork of DeOldify: https://github.com/d4rk-lucif3r/DeOldify

--

--

Arsh Anwar

AI/ML expert. Built LuciferML (100k+ downloads). Co-founder @Revca, building smart solutions for a sustainable future.