avatarNuno Bispo

Summary

This article provides a step-by-step guide on how to create a Python script for AI image upscaling using the Real-ESRGAN model, which can be run on a CPU without requiring a GPU.

Abstract

The article begins by introducing the concept of AI image upscaling and the Real-ESRGAN model, which can be used to upscale images to resolutions of up to 4096x4096. The author then proceeds to explain how to create a new PyCharm project, install the necessary external libraries, and write the script for AI image upscaling. The script uses the Real-ESRGAN model to upscale an input image and save the resulting image to disk. The article concludes by demonstrating the results of the script on an example image and providing links to the source code and other relevant resources.

Bullet points

  • The article provides a step-by-step guide on how to create a Python script for AI image upscaling using the Real-ESRGAN model.
  • The Real-ESRGAN model can be used to upscale images to resolutions of up to 4096x4096.
  • The article explains how to create a new PyCharm project, install the necessary external libraries, and write the script for AI image upscaling.
  • The script uses the Real-ESRGAN model to upscale an input image and save the resulting image to disk.
  • The article demonstrates the results of the script on an example image and provides links to the source code and other relevant resources.
  • The script can be run on a CPU without requiring a GPU.
  • The article assumes that the reader already has Python and PyCharm installed on their computer.

AI Image upscale made easy, no GPU needed

In this post I am going to show how easy and quick is to create a Python script that allows you to upscale an image using AI.

This is the kind of AI image that deserves an upscale resolution

This article is a follow-up to my previous article about generating your own AI images locally on your computer:

With this article’s script, you can upscale the AI-generated images to resolutions of up to 4096x4096 (using a scale factor of 8x).

This article assumes that you already have Python and PyCharm installed on your computer.

Creating a new PyCharm project

Let’s start by creating a new PyCharm project that we can call ‘AIUpscale’, as seen in the example below:

Creating a new PyCharm project

We can leave the ‘Create a main.py welcome script’ option on, as this will also create a proper Run configuration automatically for us on PyCharm.

The created project should look like this:

The autogenerated main.py file

Installing External Libraries

We will use an external library to make our AI upscale work using an ESRGAN AI model. The following GitHub project projects that resource for us:

Let’s install it using PIP:

pip install git+https://github.com/sberbank-ai/Real-ESRGAN.git

We can do this using the terminal tab in PyCharm:

Installing our external library with PIP

Creating the script

We can now create the script that will receive as an input a low-resolution image (or any image), perform an upscale with the AI model, and save the resulting image to disk.

Let’s examine the code:

import torch
from PIL import Image
from RealESRGAN import RealESRGAN

# Device CPU
device = torch.device('cpu')

# Load model and scale factor
model = RealESRGAN(device, scale=4)
model.load_weights('weights/RealESRGAN_x4.pth', download=True)

# Load image
path_to_image = 'inputs/lr_image.png'
image = Image.open(path_to_image).convert('RGB')

# Upscale image
sr_image = model.predict(image)

# Save image
sr_image.save('results/sr_image.png')
  • We select as a device the CPU, as we intend to see how easy it is to upscale images with AI with only the CPU;
  • The next step is to load the model weights, which we set to automatically download on the first run. Here we are using a scale of 4, meaning the original image is going to upscale with a factor of 4x. Other possible values are 2 and 8;
  • Load the input image and convert it to RGB, this is required for the AI model to properly upscale the image;
  • Run the image upscale with the ‘predict’ method from our model, passing the image
  • Save the resulting image into the disk

Let’s see how this simple script looks on our PyCharm project:

The script code on PyCharm

Running the script

We can now run the script by pressing the big play button on the PyCharm interface.

The script will start by downloading the weights:

Downloading the weights to run our model

After that, the script will run and upscale the image. While the script is running there will be no output, but you might also get this warning message:

Warning message about using GPU when a GPU is not installed

The reason for this warning message, which shows despite we explicitly defined our script to run on CPU only is due to a bug on the external library.

I have already submitted a pull request with a fix for this issue:

Pull Request to fix the warning message

Despite the warning message, the script will run fine and after 1 to 2 minutes (depending on your CPU), the output image will be generated and saved.

Checking the results

As an input, the following image was used in my example:

Input image with a resolution of 512x512

The resulting image, with an upscale factor of 4x, is:

Output image with a resolution of 2048x2048

As you can see, the AI model not only upscale the image resolution but also made the image much sharper.

Conclusion

In this article, we saw how easy it is to create a script to upscale images using AI.

The external library/model that we used not only upscales the image but also makes it sharper and more visually appealing.

The source code of this script, as well as the images, can be found at:

Thank you for reading and I will see you on the Internet.

Follow me on Twitter: https://twitter.com/DevAsService

Check out my website at: https://developer-service.io/

Check out other articles that might interest you:

If you enjoyed reading this article and found it useful, you can support me by signing up for a Medium membership (if you are not a member). It will only cost you $5 a month, giving you access to all stories on Medium! (and I will receive a small commission)

Besides that, if you want to stay updated when I post a new article, you can signup for my free newsletter!

Technology
Programming
AI
Python
Image Processing
Recommended from ReadMedium