SwinIR¶
| Fact | Value |
|---|---|
| Also called | Transformer-based denoising, SwinIR denoising |
| Status | Available through an optional pretrained model |
| Family | Deep learning and learned processing |
| Engine | Science engine |
| Type | Nonlinear, learned, transformer denoising |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | 32 |
| Code | engine/src/pixelnexus_engine/filters/learned.py, swinir |
| Operator code | engine/src/pixelnexus_engine/filters/learned.py, swinir_representation |
| Packages | ONNX Runtime (optional) |
Overview¶
SwinIR, by Liang and colleagues, restores images with a network built from Swin Transformer blocks. PixelNexus uses its model for color photos with Gaussian noise of sigma 25. Unlike DnCNN, whose kernels are the same everywhere, SwinIR's attention weighs the pixels of each small window by how alike they are. So it adapts to edges and textures. 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.
How it works¶
- Scale the pixels to 0 to 1 and mirror the image past its edges.
- Cut it into tiles of 256 pixels that overlap by 32 pixels on each side.
- Run the network on each tile and keep the center.
- Put the centers together.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(y\): The noisy image
- \(H_{SF}, H_{DF}, H_{REC}\): Shallow features, deep features and reconstruction
- \(Q, K, V\): Queries, keys and values of the pixels in one window
- \(d\): The length of each query and key
- \(B\): The learned bias for relative positions in the window
- \(\hat{x}\): The restored image
y is the noisy image. A convolution H_SF gives shallow features. Six residual Swin Transformer groups H_DF give deep features. In each block, attention compares the pixels inside a window of 8 x 8 pixels, with a learned bias B for their relative position. Every second block shifts the windows by 4 pixels, so information crosses window borders. A last convolution H_REC turns the features into a correction, which is added to the image.
Attention compares every pair of pixels in a window. The query of one and the key of the other give a weight. The result is the sum of the values with those weights. Inside a window of 8 x 8 pixels that costs 64 x 64 comparisons, so the cost grows with the number of pixels, not with its square. Shifting the windows by 4 pixels in every second block lets information cross the window borders. After many blocks, each pixel has seen a wide area.
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¶
This filter has no parameters. The model was trained for one noise level, sigma 25.
Implementation¶
swinir 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. SwinIR builds its attention masks from the input size while it runs, so the export is fixed to tiles of 256 x 256 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. The shifted windows reach further than 32 pixels, so the result is close to a run on the whole image, not equal.
Worked example¶
| Noise sigma | Noisy (dB) | DnCNN (dB) | SwinIR, trained for 25 (dB) |
|---|---|---|---|
| 15 | 24.63 | 37.56 | 43.85 |
| 25 | 20.24 | 34.60 | 42.50 |
| 50 | 14.76 | 30.18 | 18.03 |
PSNR against the clean image of the figure, 120 x 120, over the three channels. Gaussian noise in each channel, rounded and clipped to 8 bits.
At sigma 25, its training level, SwinIR scores about 8 dB above DnCNN on this image, and it also wins at sigma 15. At sigma 50 it fails: the result is barely better than the noisy image, since this model never saw noise that strong. DnCNN is blind, trained for a range of levels, so it copes. Use SwinIR only near the noise it was trained for.
Complexity¶
- Time: \(O(N M^2)\)
- Memory: \(O(N)\)
N is the number of pixels and M = 8 the window side. About 11.5 million weights. Much slower than DnCNN, so a GPU helps.
Artifacts and limitations¶
Limitations
- Trained for one noise level. Much weaker or stronger noise is handled less well.
- Slow on a processor without a GPU.
Artifacts
- Over-smooth fine texture where it looks like noise.
Tests¶
Tiny ONNX models made inside the tests check the plumbing in every test run. A model that accepts only one input size proves that every tile has exactly that size. A chain of box filters shows that tiles with enough margin equal a run on the whole image, away from its edges. Color and alpha must be handled as described. Where the real model is downloaded, the real network must raise the PSNR of a noisy image by more than 6 dB.
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.