# Swin Transformer
Swin Transformer V1: a hierarchical vision transformer that computes attention inside shifted local windows instead of over the whole image. LibreYOLO ships four sizes for image classification.
Tasks: Classification. Install: pip install libreyolo.
Verified against LibreYOLO v1.6.0.

## Install

Swin 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("LibreSwint-cls.pt")
result = model(SAMPLE_IMAGE, save=True)

probs = result.probs
print(probs.top1, probs.top1conf)
print(probs.top5, probs.top5conf)
```

**CLI**

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

A classifier returns `result.probs` instead of `result.boxes`: `top1`
and `top5` give class indices, `top1conf` and `top5conf` give their
confidences. Every size is fixed to a 224px input, because the final attention
stage is built for that resolution; predict, validate and export all raise if
you pass a different `imgsz`. See [prediction](/docs/predict) for sources,
streaming and result handling.

## Variants

Four sizes, tiny through large, built from the same shifted-window tower and
differing in embedding width and stage depth. Large is pretrained on
ImageNet-22k and fine-tuned on ImageNet-1k; the other three are trained on
ImageNet-1k directly. LibreYOLO ships this family inference-only: prediction,
ImageNet-style top-1/top-5 validation and export are supported, and the
upstream ImageNet training recipe is not implemented.

## Validate

`val()` runs against an ImageFolder-style split (a directory with `train/` and
`val/` subfolders, one folder per class) and returns top-1 and top-5 accuracy.

**Python**

```python
from libreyolo import LibreYOLO

model = LibreYOLO("LibreSwint-cls.pt")

# data is a directory root with train/ and val/ class-folder splits
# (ImageFolder layout), not a dataset YAML.
metrics = model.val(data="imagenet-1k/")

print(metrics["metrics/accuracy_top1"])
print(metrics["metrics/accuracy_top5"])
```

**CLI**

```bash
libreyolo val model=LibreSwint-cls.pt data=imagenet-1k/
```

Validation and INT8 calibration use the family evaluation transform. Export metadata records `norm_mean`, `norm_std` and `resize_mode`; older artifacts fall back to family values. Calibration preprocessors return the required CHW array and ratio.

## Export

| Task | ONNX | TorchScript | ExecuTorch | TensorRT | OpenVINO | Paddle | MNN | RKNN | ncnn | TFLite | CoreML | Core AI |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Classification | yes | 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`. [Export](/docs/export) lists the arguments every format accepts and
the extras a few of them add.

**Python**

```python
from libreyolo import LibreYOLO

model = LibreYOLO("LibreSwint-cls.pt")
model.export(format="onnx")
model.export(format="tensorrt", half=True)
```

**CLI**

```bash
libreyolo export model=LibreSwint-cls.pt format=onnx
libreyolo export model=LibreSwint-cls.pt format=tensorrt half=True
```

**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("LibreSwint-cls.onnx")
result = model(SAMPLE_IMAGE)

print(result.probs.top1)
```

## Checkpoints

Every published weight file for this family.

| File | Input (px) | Task | Weights license |
| --- | --- | --- | --- |
| `LibreSwint-cls.pt` | 224 | Classification | mit |
| `LibreSwins-cls.pt` | 224 | Classification | mit |
| `LibreSwinb-cls.pt` | 224 | Classification | mit |
| `LibreSwinl-cls.pt` | 224 | Classification | mit |

## 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: Swin Transformer, Microsoft Research
- Upstream license: MIT
- Upstream source: https://github.com/microsoft/Swin-Transformer
- LibreYOLO code: Apache-2.0
- Weights: MIT, republished at https://huggingface.co/LibreYOLO
- Interpretation: MIT is a permissive license, so these weights can be used in commercial and closed-source products. It asks only that you keep the license text and copyright notice with any copy you redistribute, and it carries no explicit patent grant. LibreYOLO's runtime code for this family is a derived port of the Apache-2.0 timm Swin implementation (Ross Wightman, huggingface/pytorch-image-models), kept parameter-name compatible so the tensors load unchanged; the four released Tiny/Small/Base/Large checkpoints are Microsoft's own MIT-licensed patch-4/window-7 classifiers. Code and weights therefore sit under two different permissive licenses, both of which allow commercial use.

## Citation

```bibtex
@inproceedings{liu2021Swin,
  title={Swin Transformer: Hierarchical Vision Transformer using Shifted Windows},
  author={Liu, Ze and Lin, Yutong and Cao, Yue and Hu, Han and Wei, Yixuan and Zhang, Zheng and Lin, Stephen and Guo, Baining},
  booktitle={Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV)},
  year={2021}
}
```

Copied from https://github.com/microsoft/Swin-Transformer#citing-swin-transformer


## Swin architecture

- Swin t: [SVG](/diagrams/models/swin/t-classify.svg), [interactive diagram](/diagrams/models/swin/t-classify.html)
- Swin s: [SVG](/diagrams/models/swin/s-classify.svg), [interactive diagram](/diagrams/models/swin/s-classify.html)
- Swin b: [SVG](/diagrams/models/swin/b-classify.svg), [interactive diagram](/diagrams/models/swin/b-classify.html)
- Swin l: [SVG](/diagrams/models/swin/l-classify.svg), [interactive diagram](/diagrams/models/swin/l-classify.html)
- Swin family: [SVG](/diagrams/models/swin/family-classify.svg), [interactive diagram](/diagrams/models/swin/family-classify.html)
