Skip to content

SwinIR super-resolution

Fact Value
Also called SwinIR 2x, Transformer super-resolution
Status Available through an optional pretrained model
Family Deep learning and learned processing
Engine Science engine
Type Nonlinear, learned, transformer super-resolution
Output Image
Operator shown as algorithm (exact)
Support radius 32
Code engine/src/pixelnexus_engine/filters/learned.py, swinir_sr
Operator code engine/src/pixelnexus_engine/filters/learned.py, swinir_sr
Packages ONNX Runtime (optional)

Overview

This filter enlarges an image two times with the lightweight SwinIR model of Liang and colleagues. It was trained on clean images that were shrunk with bicubic, so it rebuilds the detail that bicubic shrinking removed. It cannot undo blur, noise or JPEG artifacts, which Real-ESRGAN was trained for. 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. The 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, then bicubic, FSRCNN and SwinIR after shrinking two times. All keep the stripes. In the noise quarter, FSRCNN and SwinIR draw a wormy texture that the original does not have. Four panels in four quarters each: stripes, fine noise, soft blobs and flat gray. The original, then bicubic, FSRCNN and SwinIR after shrinking two times. All keep the stripes. In the noise quarter, FSRCNN and SwinIR draw a wormy texture that the original does not have.
The texture image, shrunk two times with bicubic, then enlarged two times with bicubic, FSRCNN and SwinIR.

How it works

  1. Premultiply the colors by alpha and mirror the image past its edges.
  2. Cut it into tiles of 128 pixels that overlap by 32 pixels on each side.
  3. Run the network on each tile and keep the center, now two times larger.
  4. Enlarge alpha with bicubic.

Mathematical foundation

\[ F_0 = H_{SF}(y),\quad F_{DF} = H_{DF}(F_0),\quad \hat{x} = \text{PixelShuffle}_2\big(H_{REC}(F_{DF} + F_0)\big) \]

The formula shows the default parameter values.

where

  • \(y\): The small image
  • \(H_{SF}, H_{DF}, H_{REC}\): Shallow features, deep features and reconstruction
  • \(\hat{x}\): The enlarged image

y is the small image. A convolution H_SF gives shallow features. Four residual Swin Transformer groups H_DF give deep features, with attention inside windows of 8 x 8 pixels. The windows shift by 4 pixels in every second block. A last convolution H_REC gives 12 channels, and the pixel shuffle turns them into a picture two times larger.

The network works at the small size. Only its last layer enlarges: a convolution makes 12 channels, and the pixel shuffle moves each group of 4 channels into a block of 2 x 2 pixels. The attention of the Swin Transformer blocks is the same as in the SwinIR denoiser.

Kernel or operator

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

Parameters

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

Implementation

swinir_sr in engine/src/pixelnexus_engine/filters/learned.py runs an ONNX file. tools/export_models.py made it from the official weights. The settings are those of SwinIR's own test script. The export is fixed to tiles of 128 x 128 pixels. fixed_tiles mirrors the image past its edges. It cuts the image into tiles that overlap by 32 pixels on each side and keeps the center of each, now two times larger. Colors are premultiplied by alpha first, as Resize does, and alpha is enlarged with bicubic.

Worked example

Method PSNR (dB)
Bicubic 23.08
FSRCNN 22.99
SwinIR 23.00

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

On this image SwinIR scores the same as bicubic, within 0.1 dB. The fine noise and the stripes are lost in the small copy, so no method can bring them back, and SwinIR draws plausible texture in their place. On smooth images that were shrunk with bicubic, the tests show SwinIR above bicubic.

Complexity

  • Time: \(O(N M^2)\)
  • Memory: \(O(4 N)\)

N is the number of input pixels and M = 8 the window side. The output has 4 times as many pixels. About 0.91 million weights.

Artifacts and limitations

Limitations

  • Trained on clean images shrunk with bicubic. Noise, blur and JPEG artifacts in the input stay or get stronger.
  • Two times only.

Artifacts

  • Over-sharp, slightly wavy edges on text and fine lines.

Tests

Tiny ONNX models made inside the tests check the plumbing in every test run, as for the SwinIR denoiser. The result must be two times larger, with alpha enlarged too. Where the real model is downloaded, the real network must score above bicubic on an image that was shrunk with bicubic.

References

  • Liang, J., Cao, J., Sun, G., Zhang, K., Van Gool, L., and Timofte, R. SwinIR. Image restoration using Swin Transformer. IEEE/CVF International Conference on Computer Vision Workshops, 1833 to 1844, 2021. DOI 10.1109/ICCVW54120.2021.00210.