Skip to content

Real-ESRGAN

Fact Value
Also called Real-ESRGAN general, realesr-general-x4v3
Status Available through an optional pretrained model
Family Deep learning and learned processing
Engine Science engine
Type Nonlinear, learned, GAN-trained super-resolution
Output Image
Operator shown as algorithm (exact)
Support radius 34
Code engine/src/pixelnexus_engine/filters/learned.py, real_esrgan
Operator code engine/src/pixelnexus_engine/filters/learned.py, real_esrgan
Packages ONNX Runtime (optional)

Overview

Real-ESRGAN, by Wang and colleagues, enlarges an image four times. It was trained on photos degraded on purpose by blur, noise, resizing and JPEG compression together, so it copes with real photos. Its training used a GAN, which rewards detail that looks real rather than detail that is exact. So it invents plausible texture. PixelNexus uses its compact general model. The filter runs in the PixelNexus engine.

It needs an optional model. The model downloads only when you click Download model in the Properties panel, which first shows its size and license, and the engine checks its SHA-256 before it keeps it. Models published as PyTorch weights are converted to ONNX once on your computer with tools/export_models.py. The engine itself never uses PyTorch. Results carry the label: AI result. Detail may not exist in the original scene.

Four panels in four quarters each: stripes, fine noise, soft blobs and flat gray. The original is sharp. Bicubic is soft, with the stripes blurred and the noise smoothed away. Real-ESRGAN gives crisp stripes but turns the soft blobs into a busy, painted texture that was not there. EDSR is close to bicubic, a little sharper. Four panels in four quarters each: stripes, fine noise, soft blobs and flat gray. The original is sharp. Bicubic is soft, with the stripes blurred and the noise smoothed away. Real-ESRGAN gives crisp stripes but turns the soft blobs into a busy, painted texture that was not there. EDSR is close to bicubic, a little sharper.
The texture image of the edge pages: stripes, fine noise, soft blobs and a flat gray. Shrunk four times with bicubic, then enlarged four times with bicubic, Real-ESRGAN and EDSR.

How it works

  1. Premultiply the colors by alpha.
  2. Run the network and rearrange its channels into a four times larger image.
  3. Add the nearest-neighbor enlargement.
  4. Enlarge alpha with bicubic.

Mathematical foundation

\[ \hat{x} = \text{PixelShuffle}_4\big(f_\theta(y)\big) + \text{nearest}_4(y) \]

The formula shows the default parameter values.

where

  • \(y\): The small image
  • \(f_\theta\): The network

The compact network runs 34 convolutions with PReLU at the small size, and a pixel shuffle turns its 48 channels into a picture four times larger. The nearest-neighbor enlargement of the input is added, so the network learns only the missing detail. Training with a GAN loss makes the detail look real, not exact.

The compact network works at the small size and enlarges only in its last layer, where a pixel shuffle turns 48 channels into a four times larger picture. The nearest-neighbor enlargement of the input is added, so the network learns only the missing detail.

Kernel or operator

A trained network has many learned kernels, not one. The Inspector shows the stages and the model with its license.

Parameters

Parameter Default Values Meaning
Scale scale 4 4 to 4 x How many times larger the image becomes in each direction. The model was trained for 4 times only.

Implementation

real_esrgan in engine/src/pixelnexus_engine/filters/learned.py runs the ONNX file that tools/export_models.py made from the official weights, with the 32 body convolutions of the general model. Colors are premultiplied by alpha first, as Resize does, and alpha is enlarged with bicubic. Images run in tiles of 256 pixels with 40 pixels of margin, wider than the reach of the 34 convolutions.

Worked example

Method Whole (dB) Stripes (dB) Noise (dB) Blobs (dB) Flat (dB)
Bicubic 20.34 18.54 16.54 31.41 40.20
Real-ESRGAN 18.34 15.52 16.36 21.28 40.99
EDSR 20.34 18.68 16.44 31.89 40.44

PSNR against the original, 160 x 160, gray. The small copy was made with Pillow's bicubic resize, which filters before it samples.

Real-ESRGAN scores below bicubic on this image, 18.3 dB against 20.3, although its stripes look crisper. On the soft blobs it loses 10 dB, since it paints texture that was not there. PSNR rewards caution, while Real-ESRGAN aims at a convincing look. Never use it where the detail must be true.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(16 N)\)

N is the number of input pixels. The output has 16 times as many.

Artifacts and limitations

Limitations

  • Invents texture. Never use it where detail must be true, such as evidence or measurement.

Artifacts

  • Painted, over-smooth skin and hair.
  • False texture on fine patterns.

Tests

Tiny ONNX models made inside the tests check the plumbing in every test run. Tiles with enough margin must equal a run on the whole image. Color, alpha and padding must be handled as described. Where the real model is downloaded, as on the development machine, the real network is checked too. The result must be four times larger, with alpha enlarged too.

References

  • Wang, X., Xie, L., Dong, C., and Shan, Y. Real-ESRGAN. Training real-world blind super-resolution with pure synthetic data. IEEE/CVF International Conference on Computer Vision Workshops, 1905 to 1914, 2021. DOI 10.1109/ICCVW54120.2021.00217.