Restormer¶
| Fact | Value |
|---|---|
| Also called | Attention-based restoration, Restormer denoising, Restormer deblurring |
| Status | Available through an optional pretrained model |
| Family | Deep learning and learned processing |
| Engine | Science engine |
| Type | Nonlinear, learned, transformer restoration |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | the whole image |
| Code | engine/src/pixelnexus_engine/filters/learned.py, restormer |
| Operator code | engine/src/pixelnexus_engine/filters/learned.py, restormer_representation |
| Packages | ONNX Runtime (optional) |
Overview¶
Restormer, by Zamir and colleagues, restores photos with a network of transformer blocks. PixelNexus offers two of its models. One removes the noise of real cameras, trained on smartphone photos. The other removes motion blur, trained on frames of handheld video. The filter runs in the PixelNexus engine.
It needs an optional model. Each 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¶
- Mirror the image at the bottom and right to sides that are multiples of 8.
- Run the network on the whole image, or on overlapping tiles of 720 pixels, averaging the overlaps.
- Crop to the image size and clamp to 0 to 1.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(y\): The degraded image
- \(f_\theta\): The network with its trained weights
- \(Q, K, V\): Queries, keys and values, one row per pixel
- \(\alpha\): A learned temperature
- \(\hat{x}\): The restored image
y is the degraded image and f the network, which predicts a correction. Q, K and V come from the features by 1 x 1 and depthwise 3 x 3 convolutions, and have one row per pixel and one column per channel. K transposed times Q is a channel by channel matrix. So the attention compares channels across the whole image, and its cost grows linearly with the number of pixels. alpha is a learned temperature. The network has four levels, each halving the size, and a gated feed-forward part in every block.
Attention between pixels compares every pixel with every other one, so its cost grows with the square of the number of pixels. That is too much for a photo. Restormer turns the attention around. It compares the feature channels with each other, each summed over all pixels. That matrix has one row and one column per channel, whatever the image size. So the attention sees the whole image and still costs time in proportion to its size. Depthwise 3 x 3 convolutions before the attention bring in the neighbors of each pixel.
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 |
|---|---|---|---|
Task task |
Remove real camera noise | Remove real camera noise, Remove motion blur | Which model runs. Each downloads on its own, about 105 MB. |
Implementation¶
restormer in engine/src/pixelnexus_engine/filters/learned.py runs an ONNX file. tools/export_models.py made it from the official weights, with the settings of the option files of the Restormer repository. Like the repository's demo.py, it mirrors the image at the bottom and right to sides that are multiples of 8. Images up to 720 pixels on each side run whole. Larger ones run in tiles of 720 pixels, and where tiles overlap, their results are averaged. The attention spans the whole input, so tiles are close to a whole run, not equal.
Worked example¶
Gaussian noise in each color channel, on the image of the DnCNN figure:
| Noise sigma | Noisy (dB) | DnCNN (dB) | Restormer, real noise (dB) |
|---|---|---|---|
| 15 | 24.63 | 37.56 | 25.97 |
| 25 | 20.24 | 34.60 | 24.47 |
| 50 | 14.76 | 30.18 | 19.71 |
Motion blur along the rows, on the image of the figure and on the texture image of the edge pages:
| Image | Blur length | Blurred (dB) | Restormer, motion blur (dB) |
|---|---|---|---|
| Step and lines | 5 | 24.61 | 26.14 |
| Step and lines | 9 | 23.35 | 24.27 |
| Texture | 9 | 17.78 | 17.44 |
PSNR against the clean images, over the three channels for the noise and on gray for the blur. Noise and blur are rounded and clipped to 8 bits.
The real noise model gains only 1.3 to 5 dB on this synthetic noise, far less than DnCNN. Real camera noise differs from it: neighbors share part of it, and it grows with the brightness. A model learns the noise it was trained on. The motion blur model sharpens the step, and gains 0.9 to 1.5 dB there. On the texture image it gains nothing, since stripes and fine noise lost to the blur cannot be told apart from plausible new ones.
Complexity¶
- Time: \(O(N C^2)\)
- Memory: \(O(N C)\)
N is the number of pixels and C the number of channels inside the network. About 26 million weights. Slow without a GPU.
Artifacts and limitations¶
Limitations
- Each model handles the degradation it was trained for. Out of focus blur needs another model.
- Large and slow on a processor.
Artifacts
- Smooth, painted fine texture where noise was strong.
Tests¶
Tiny ONNX models made inside the tests check the plumbing in every test run. One refuses sides that are not multiples of 8, so the test proves the padding, and the crop back to the image size. Another checks that overlapping tiles are averaged and that the result is clamped. Where the real models are downloaded, the noise model must raise the PSNR of a noisy image by more than 6 dB. The blur model must raise the PSNR of an image with a horizontal motion blur.
References¶
- Zamir, S. W., Arora, A., Khan, S., Hayat, M., Khan, F. S., and Yang, M.-H. Restormer. Efficient transformer for high-resolution image restoration. IEEE/CVF Conference on Computer Vision and Pattern Recognition, 5718 to 5729, 2022. DOI 10.1109/CVPR52688.2022.00564.