File size: 6,058 Bytes
c2b0640
 
 
 
 
 
be8824e
 
 
 
1c197c9
 
be8824e
 
 
c2b0640
 
1c197c9
c2b0640
1c197c9
c2b0640
1c197c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2b0640
 
1c197c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2b0640
 
1c197c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
pipeline_tag: image-classification
license: mit
base_model: timm/swin_tiny_patch4_window7_224.ms_in1k
library_name: kerasformers
tags:
- keras
- kerasformers
- image-classification
- swin
- backbone
- arxiv:2103.14030
- pytorch
- jax
- tf
---

## ***See [our collection](https://huggingface.co/collections/kerasformers/swin-transformer-6a6c7c6d86b6537929511843) for all versions of Swin Transformer.***

# Run Swin Transformer with Keras 3: JAX, PyTorch, or TensorFlow

[![GitHub](https://img.shields.io/badge/GitHub-KerasFormers-black?logo=github)](https://github.com/IMvision12/KerasFormers) [![Docs](https://img.shields.io/badge/Docs-Swin-blue)](https://imvision12.github.io/KerasFormers/classification_backbones/) [![Collection](https://img.shields.io/badge/HF-Swin%20collection-yellow)](https://huggingface.co/collections/kerasformers/swin-transformer-6a6c7c6d86b6537929511843)

# kerasformers/swin_tiny_patch4_window7_224_ms_in1k

Paper: [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows (arXiv:2103.14030)](https://arxiv.org/abs/2103.14030) · [HF Papers](https://huggingface.co/papers/2103.14030)

Swin Transformer builds hierarchical feature maps with shifted-window attention. Strong as an ImageNet classifier and as a 4-stage backbone.

For more details on the model, please go to the upstream [model card](https://huggingface.co/timm/swin_tiny_patch4_window7_224.ms_in1k).

Pure-**Keras 3** conversion of [`timm/swin_tiny_patch4_window7_224.ms_in1k`](https://huggingface.co/timm/swin_tiny_patch4_window7_224.ms_in1k) for [kerasformers](https://github.com/IMvision12/KerasFormers). One implementation runs unmodified on **TensorFlow / Torch / JAX**.

This is an **image-classification / backbone** checkpoint (`SwinImageClassify` / `SwinModel`).

## ✨ Quick start

```python
import os
os.environ["KERAS_BACKEND"] = "torch"  # or "jax" / "tensorflow"

from PIL import Image
import numpy as np
from kerasformers.models.swin import SwinImageClassify, SwinModel

model = SwinImageClassify.from_weights("kerasformers/swin_tiny_patch4_window7_224_ms_in1k")
backbone = SwinModel.from_weights(
    "kerasformers/swin_tiny_patch4_window7_224_ms_in1k", as_backbone=True
)

image = Image.open("your_image.jpg").convert("RGB")
image = image.resize((224, 224))
x = np.asarray(image, dtype="float32")[None]  # (1, H, W, 3)
print(model(x).shape)  # (1, num_classes)
feats = backbone(x)
print(len(feats), [tuple(f.shape) for f in feats])
```

Load any Swin Transformer variant the same way with `from_weights("kerasformers/<variant>")`:

| Variant | Hub |
|---|---|
| `swin_base_patch4_window12_384_ms_in1k` | [`kerasformers/swin_base_patch4_window12_384_ms_in1k`](https://huggingface.co/kerasformers/swin_base_patch4_window12_384_ms_in1k) |
| `swin_base_patch4_window12_384_ms_in22k` | [`kerasformers/swin_base_patch4_window12_384_ms_in22k`](https://huggingface.co/kerasformers/swin_base_patch4_window12_384_ms_in22k) |
| `swin_base_patch4_window12_384_ms_in22k_ft_in1k` | [`kerasformers/swin_base_patch4_window12_384_ms_in22k_ft_in1k`](https://huggingface.co/kerasformers/swin_base_patch4_window12_384_ms_in22k_ft_in1k) |
| `swin_base_patch4_window7_224_ms_in1k` | [`kerasformers/swin_base_patch4_window7_224_ms_in1k`](https://huggingface.co/kerasformers/swin_base_patch4_window7_224_ms_in1k) |
| `swin_base_patch4_window7_224_ms_in22k` | [`kerasformers/swin_base_patch4_window7_224_ms_in22k`](https://huggingface.co/kerasformers/swin_base_patch4_window7_224_ms_in22k) |
| `swin_base_patch4_window7_224_ms_in22k_ft_in1k` | [`kerasformers/swin_base_patch4_window7_224_ms_in22k_ft_in1k`](https://huggingface.co/kerasformers/swin_base_patch4_window7_224_ms_in22k_ft_in1k) |
| `swin_large_patch4_window12_384_ms_in22k` | [`kerasformers/swin_large_patch4_window12_384_ms_in22k`](https://huggingface.co/kerasformers/swin_large_patch4_window12_384_ms_in22k) |
| `swin_large_patch4_window12_384_ms_in22k_ft_in1k` | [`kerasformers/swin_large_patch4_window12_384_ms_in22k_ft_in1k`](https://huggingface.co/kerasformers/swin_large_patch4_window12_384_ms_in22k_ft_in1k) |
| `swin_large_patch4_window7_224_ms_in22k` | [`kerasformers/swin_large_patch4_window7_224_ms_in22k`](https://huggingface.co/kerasformers/swin_large_patch4_window7_224_ms_in22k) |
| `swin_large_patch4_window7_224_ms_in22k_ft_in1k` | [`kerasformers/swin_large_patch4_window7_224_ms_in22k_ft_in1k`](https://huggingface.co/kerasformers/swin_large_patch4_window7_224_ms_in22k_ft_in1k) |
| `swin_small_patch4_window7_224_ms_in1k` | [`kerasformers/swin_small_patch4_window7_224_ms_in1k`](https://huggingface.co/kerasformers/swin_small_patch4_window7_224_ms_in1k) |
| `swin_small_patch4_window7_224_ms_in22k` | [`kerasformers/swin_small_patch4_window7_224_ms_in22k`](https://huggingface.co/kerasformers/swin_small_patch4_window7_224_ms_in22k) |
| `swin_small_patch4_window7_224_ms_in22k_ft_in1k` | [`kerasformers/swin_small_patch4_window7_224_ms_in22k_ft_in1k`](https://huggingface.co/kerasformers/swin_small_patch4_window7_224_ms_in22k_ft_in1k) |
| `swin_tiny_patch4_window7_224_ms_in1k` | [`kerasformers/swin_tiny_patch4_window7_224_ms_in1k`](https://huggingface.co/kerasformers/swin_tiny_patch4_window7_224_ms_in1k) |
| `swin_tiny_patch4_window7_224_ms_in22k` | [`kerasformers/swin_tiny_patch4_window7_224_ms_in22k`](https://huggingface.co/kerasformers/swin_tiny_patch4_window7_224_ms_in22k) |

## Tips

- Set `KERAS_BACKEND` **before** importing Keras / kerasformers.
- `SwinImageClassify` returns class logits; `SwinModel` returns features (`as_backbone=True` for multi-scale stages).
- See [docs](https://imvision12.github.io/KerasFormers/classification_backbones/) and [Loading Weights](https://imvision12.github.io/KerasFormers/loading_weights/).
- Upstream / timm checkpoints: `SwinImageClassify.from_weights("hf:timm/swin_tiny_patch4_window7_224.ms_in1k")`.

## Special Thanks

A huge thank you to the Swin Transformer authors and the timm / Hub communities for creating and releasing these models.

License: see YAML `license` (usually matches the upstream checkpoint).