> This is the documentation for LibreYOLO 1.6.0. Current version: https://www.libreyolo.com/docs/models/swinir

# SwinIR
A Swin Transformer network for image restoration. LibreYOLO ships inference and validation for its 4x super-resolution checkpoints: the official lightweight, real-world medium and real-world large generators.
Tasks: Restoration. Install: pip install libreyolo.
Verified against LibreYOLO v1.5.0.

## Install

SwinIR needs no optional extra. Everything it imports is in the base install.

```bash
pip install libreyolo
```

## Predict

Weights download from Hugging Face on first use and are cached locally.

**Python**

```python
from libreyolo import LibreYOLO, SAMPLE_IMAGE

model = LibreYOLO("LibreSwinIRm-restore.pt")
result = model(SAMPLE_IMAGE, save=True)

restored = result.restored
print(restored.array.shape, restored.array.dtype)
```

**CLI**

```bash
libreyolo predict model=LibreSwinIRm-restore.pt source=https://raw.githubusercontent.com/LibreYOLO/libreyolo/release/libreyolo/assets/parkour.jpg save=True
```

**Tiled, for large images**

```python
from libreyolo import LibreYOLO

model = LibreYOLO("LibreSwinIRl-restore.pt")

# tile splits the forward pass into overlapping tiles and blends the
# seams back together; tile_pad is the halo added around each tile
# before it is cropped back out. Both are Python-only keyword
# arguments, not CLI flags.
result = model("large-photo.jpg", tile=512, tile_pad=16, save=True)
```

A restore result carries no boxes; `result.restored` is a dense `(H, W, 3)`
uint8 RGB image, on a canvas 4x the input in each dimension. `save=True` writes
that image directly rather than an annotated plot. The input is padded to a
multiple of 8 rather than resized, so predict runs at the photo's own
resolution; a source larger than memory allows can be split with `tile` and
`tile_pad`, which blend the tile seams back together in the output. See
[prediction](/docs/v1.6.0/predict) for sources, streaming and result handling.

## Variants

Three sizes, all fixed at a 4x upscale. `s` is the official lightweight
generator, with four residual Swin Transformer block (RSTB) stages and
pixel-shuffle-direct upsampling. `m` and `l` are the real-world medium and
large generators, with six and nine RSTB stages and a nearest-neighbor-plus-
convolution upsampler built for real-world degradations rather than only
bicubic downscaling.

## Validate

`val()` measures PSNR and SSIM between the restored output and a clean target
image, both computed in RGB on the original canvas with no border crop and no
resizing. SSIM uses an 11x11 Gaussian window with sigma 1.5, averaged over the
three color channels.

**Python**

```python
from libreyolo import LibreYOLO

model = LibreYOLO("LibreSwinIRm-restore.pt")
metrics = model.val(data="my-restore-dataset.yaml")

print(metrics["metrics/PSNR"])
print(metrics["metrics/SSIM"])
```

**CLI**

```bash
libreyolo val model=LibreSwinIRm-restore.pt data=my-restore-dataset.yaml
```

The dataset argument is a YAML pairing a directory of degraded input images
with a directory of clean target images of matching resolution; see
[dataset formats](/docs/v1.6.0/reference/dataset-formats) for the exact keys.

## Export

| Task | ONNX | TorchScript | ExecuTorch | TensorRT | OpenVINO | Paddle | MNN | RKNN | ncnn | TFLite | CoreML | Core AI |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Restoration | yes | yes |  | yes | yes |  |  |  |  | yes |  |  |

A "yes" means the export is supported. An empty cell means the exporter refuses that combination.

An exported artifact loads back through `LibreYOLO()` on its file suffix, so a
`.onnx` or `.engine` file behaves like a checkpoint and returns the same
`Results`. ExecuTorch and every format the matrix marks blocked are not
available for this family; ONNX, TorchScript, TensorRT, OpenVINO and TFLite
are. [Export](/docs/v1.6.0/export) lists the arguments every format accepts and the
extras a few of them add.

**Python**

```python
from libreyolo import LibreYOLO

model = LibreYOLO("LibreSwinIRm-restore.pt")

# imgsz defaults to a small internal patch size when omitted, not
# your working resolution, so pass the size your deployment actually
# feeds the model.
model.export(format="onnx", imgsz=512)
model.export(format="tensorrt", imgsz=512, half=True)
```

**CLI**

```bash
libreyolo export model=LibreSwinIRm-restore.pt format=onnx imgsz=512
```

**Use the exported file**

```python
from libreyolo import LibreYOLO, SAMPLE_IMAGE

# The factory routes on the file suffix, so an exported artifact loads
# like any checkpoint and returns the same Results object.
model = LibreYOLO("LibreSwinIRm-restore.onnx")
result = model(SAMPLE_IMAGE)

print(result.restored.array.shape)
```

## Checkpoints

Every published weight file for this family.

| File | Input (px) | Task | Weights license |
| --- | --- | --- | --- |
| `LibreSwinIRs-restore.pt` |  | Restoration | apache-2.0 |
| `LibreSwinIRm-restore.pt` |  | Restoration | apache-2.0 |
| `LibreSwinIRl-restore.pt` |  | Restoration | apache-2.0 |

## Licensing

Check the license on the Hugging Face repository of the specific weights you download. That repository is authoritative and licenses are not always uniform across a family. This is a description of the licenses involved, not legal advice.

- Original work: SwinIR, Computer Vision Lab, ETH Zurich
- Upstream license: Apache-2.0
- Upstream source: https://github.com/JingyunLiang/SwinIR
- LibreYOLO code: Apache-2.0
- Weights: Apache-2.0, republished at https://huggingface.co/LibreYOLO
- Interpretation: Apache-2.0 is a permissive license, so these weights can be used in commercial and closed-source products. It asks you to keep its license text and attribution notices with any copy of the weights you redistribute, and it grants a patent license. It places no obligation on your own application code. LibreYOLO's checkpoints are format conversions of the official pretrained generators, with the learned parameters unchanged; SwinIR's real-world variants need a degradation and GAN training pipeline that is not wired into this library, so there is no LibreYOLO-trained variant to license separately.

## Citation

```bibtex
@article{liang2021swinir,
  title={SwinIR: Image Restoration Using Swin Transformer},
  author={Liang, Jingyun and Cao, Jiezhang and Sun, Guolei and Zhang, Kai and Van Gool, Luc and Timofte, Radu},
  journal={arXiv preprint arXiv:2108.10257},
  year={2021}
}
```

Copied from https://github.com/JingyunLiang/SwinIR#citation


## SwinIR architecture

- s: [SVG](/diagrams/models/swinir/s-restore.svg), [interactive diagram](/diagrams/models/swinir/s-restore.html)
- m: [SVG](/diagrams/models/swinir/m-restore.svg), [interactive diagram](/diagrams/models/swinir/m-restore.html)
- l: [SVG](/diagrams/models/swinir/l-restore.svg), [interactive diagram](/diagrams/models/swinir/l-restore.html)
- Family with explicit head/residual alternatives: [SVG](/diagrams/models/swinir/family.svg), [interactive diagram](/diagrams/models/swinir/family.html)
