# Swin V1 executable examples

The offline mechanics program demonstrates window attention, cyclic masks,
pre-normalized residual blocks and hierarchical patch merging with random weights.
It does not reproduce pretrained semantics or the original ImageNet benchmark.
The original Microsoft source is pinned at
[f82860bfb5225915aca09c3227159ee9e1df874d](https://github.com/microsoft/Swin-Transformer/tree/f82860bfb5225915aca09c3227159ee9e1df874d).
See `swin-original-source-hashes.json` and `LICENSE-swin.txt` for provenance and
MIT attribution. No original timm module is needed or installed.

Download `swin_transformer_training.py` and `requirements-swin.txt` together.
The verified environment is Python 3.13.13, torch 2.12.0+cpu,
torchvision 0.27.0+cpu, NumPy 2.1.2 and Pillow 12.2.0. Run:

```sh
python swin_transformer_training.py --smoke
```

This performs one AdamW step on two random 64×64 RGB images, width 24,
depths [2,2,2,2], window 4 and five classes. Genuine Swin-T instead uses 224×224,
width 96, depths [2,2,6,2], window 7 and 1000 classes. Smoke omits the data recipe,
augmentation, stochastic depth/dropout and full schedule. Import has no seed,
thread, file or network side effects.

Original Microsoft blocks use fixed spatial resolutions and reduce the window
size/disable both shifts if the smallest extent fits a window. Original patch
merging asserts even H/W. Mechanics uses the later TorchVision V1 dynamic port:
right/bottom window padding, per-axis shift disablement and odd-axis merge
padding. Neighbor order is x00,x10,x01,x11; LayerNorm is before reduction.
The cyclic region mask has finite -100 entries, so wrapped weights are negligible
for ordinary logits, not guaranteed exactly zero. Padding is not an independent
validity mask. Relative-position indices have a reversal-complement relation;
learned directional biases need not be symmetric.

## Genuine native classifier export

Download `swin_export.py`, `swin_transformer_training.py`,
`swin-photo-sources.json`, and `swin-original-source-hashes.json` together.
The exporter uses installed native `torchvision.models.swin_t` with a strict
full state dictionary, without any ignored source cache. Supply the explicit
[Swin-T V1 checkpoint](https://download.pytorch.org/models/swin_t-704ceda3.pth),
SHA256 `704ceda373461b0a224fcdddd75cd2a5e9f8064512ed47adbddef7f343fd147b`.
Place the licensed `source-cat.png` and `source-coffee.png` originals referenced
by `swin-photo-sources.json` in `photos/`. These can be downloaded from the
artifact folder; the exporter authenticates their byte hashes.

```sh
python swin_export.py --checkpoint swin_t-704ceda3.pth \
  --images-dir photos --output-dir swin-demo
```

Preprocessing is the exact `Swin_T_Weights.IMAGENET1K_V1.transforms()`:
bicubic shorter-side 232, center crop 224, scale RGB uint8 to [0,1] float32,
then ImageNet mean/std. Saved processed PNGs are authenticated against the
normalized native input; `.npz` files contain that input and every one of 1000
logits and probabilities. `predictions.json` retains all output values, class
names, original/processed/input/array/checkpoint hashes, native source hashes,
TorchVision source revision, producer hashes, runtime and invocation command.
It records the later TorchVision recipe's published 81.474% separately from
original Swin-T 81.3%. The export contains two real-photo inferences and no
ImageNet accuracy measurement. Softmax values are uncalibrated classifier scores.

The saved outputs come directly from the native TorchVision model. There is no
custom backbone, no claim of original Microsoft full-model execution, and no
claim of independently re-executing a benchmark from these two photos.
