timm
/

Image Classification
timm
PyTorch
Safetensors
rwightman HF staff commited on
Commit
3d04e26
1 Parent(s): b7610cb
Files changed (4) hide show
  1. README.md +168 -0
  2. config.json +33 -0
  3. model.safetensors +3 -0
  4. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - image-classification
4
+ - timm
5
+ library_name: timm
6
+ license: apache-2.0
7
+ datasets:
8
+ - imagenet-1k
9
+ - imagenet-12k
10
+ ---
11
+ # Model card for vit_mediumd_patch16_reg4_gap_256.sbb_in12k_ft_in1k
12
+
13
+ A Vision Transformer (ViT) image classification model. This is a `timm` specific variation of the architecture with registers, global average pooling.
14
+
15
+ There are a number of models in the lower end of model scales that originate in `timm`:
16
+
17
+ | variant | width | mlp width (mult) | heads | depth | timm orig |
18
+ | ------- | ----- | ---------------- | ----- | ----- | ---- |
19
+ | tiny | 192 | 768 (4) | 3 | 12 | n |
20
+ | wee | 256 | 1280 (5) | 4 | 14 | y |
21
+ | pwee | 256 | 1280 (5) | 4 | 16 (parallel) | y |
22
+ | small | 384 | 1536 (4) | 6 | 12 | n |
23
+ | little | 320 | 1792 (5.6) | 5 | 14 | y |
24
+ | medium | 512 | 2048 (4) | 8 | 12 | y |
25
+ | mediumd | 512 | 2048 (4) | 8 | 20 | y |
26
+ | betwixt | 640 | 2560 (4) | 10 | 12 | y |
27
+ | base | 768 | 3072 (4) | 12 | 12 | n |
28
+
29
+ Pretrained on ImageNet-12k and fine-tuned on ImageNet-1k by Ross Wightman in `timm` using recipe template described below.
30
+
31
+ Recipe details:
32
+ * Searching for better baselines. Influced by Swin/DeiT/DeiT-III but w/ increased weight decay, moderate (in12k) to high (in1k) augmentation. Layer-decay used for fine-tune. Some runs used BCE and/or NAdamW instead of AdamW.
33
+ * See [train_hparams.yaml](./train_hparams.yaml) for specifics of each model.
34
+
35
+
36
+ ## Model Details
37
+ - **Model Type:** Image classification / feature backbone
38
+ - **Model Stats:**
39
+ - Params (M): 64.1
40
+ - GMACs: 16.5
41
+ - Activations (M): 24.1
42
+ - Image size: 256 x 256
43
+ - **Papers:**
44
+ - Vision Transformers Need Registers: https://arxiv.org/abs/2309.16588
45
+ - An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale: https://arxiv.org/abs/2010.11929v2
46
+ - **Dataset:** ImageNet-1k
47
+ - **Pretrain Dataset:** ImageNet-12k
48
+ - **Original:** https://github.com/huggingface/pytorch-image-models
49
+
50
+ ## Model Usage
51
+ ### Image Classification
52
+ ```python
53
+ from urllib.request import urlopen
54
+ from PIL import Image
55
+ import timm
56
+
57
+ img = Image.open(urlopen(
58
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
59
+ ))
60
+
61
+ model = timm.create_model('vit_mediumd_patch16_reg4_gap_256.sbb_in12k_ft_in1k', pretrained=True)
62
+ model = model.eval()
63
+
64
+ # get model specific transforms (normalization, resize)
65
+ data_config = timm.data.resolve_model_data_config(model)
66
+ transforms = timm.data.create_transform(**data_config, is_training=False)
67
+
68
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
69
+
70
+ top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)
71
+ ```
72
+
73
+ ### Feature Map Extraction
74
+ ```python
75
+ from urllib.request import urlopen
76
+ from PIL import Image
77
+ import timm
78
+
79
+ img = Image.open(urlopen(
80
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
81
+ ))
82
+
83
+ model = timm.create_model(
84
+ 'vit_mediumd_patch16_reg4_gap_256.sbb_in12k_ft_in1k',
85
+ pretrained=True,
86
+ features_only=True,
87
+ )
88
+ model = model.eval()
89
+
90
+ # get model specific transforms (normalization, resize)
91
+ data_config = timm.data.resolve_model_data_config(model)
92
+ transforms = timm.data.create_transform(**data_config, is_training=False)
93
+
94
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
95
+
96
+ for o in output:
97
+ # print shape of each feature map in output
98
+ # e.g.:
99
+ # torch.Size([1, 512, 16, 16])
100
+ # torch.Size([1, 512, 16, 16])
101
+ # torch.Size([1, 512, 16, 16])
102
+
103
+ print(o.shape)
104
+ ```
105
+
106
+ ### Image Embeddings
107
+ ```python
108
+ from urllib.request import urlopen
109
+ from PIL import Image
110
+ import timm
111
+
112
+ img = Image.open(urlopen(
113
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
114
+ ))
115
+
116
+ model = timm.create_model(
117
+ 'vit_mediumd_patch16_reg4_gap_256.sbb_in12k_ft_in1k',
118
+ pretrained=True,
119
+ num_classes=0, # remove classifier nn.Linear
120
+ )
121
+ model = model.eval()
122
+
123
+ # get model specific transforms (normalization, resize)
124
+ data_config = timm.data.resolve_model_data_config(model)
125
+ transforms = timm.data.create_transform(**data_config, is_training=False)
126
+
127
+ output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
128
+
129
+ # or equivalently (without needing to set num_classes=0)
130
+
131
+ output = model.forward_features(transforms(img).unsqueeze(0))
132
+ # output is unpooled, a (1, 260, 512) shaped tensor
133
+
134
+ output = model.forward_head(output, pre_logits=True)
135
+ # output is a (1, num_features) shaped tensor
136
+ ```
137
+
138
+ ## Model Comparison
139
+ Explore the dataset and runtime metrics of this model in timm [model results](https://github.com/huggingface/pytorch-image-models/tree/main/results).
140
+
141
+ ## Citation
142
+ ```bibtex
143
+ @misc{rw2019timm,
144
+ author = {Ross Wightman},
145
+ title = {PyTorch Image Models},
146
+ year = {2019},
147
+ publisher = {GitHub},
148
+ journal = {GitHub repository},
149
+ doi = {10.5281/zenodo.4414861},
150
+ howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
151
+ }
152
+ ```
153
+ ```bibtex
154
+ @article{darcet2023vision,
155
+ title={Vision Transformers Need Registers},
156
+ author={Darcet, Timoth{'e}e and Oquab, Maxime and Mairal, Julien and Bojanowski, Piotr},
157
+ journal={arXiv preprint arXiv:2309.16588},
158
+ year={2023}
159
+ }
160
+ ```
161
+ ```bibtex
162
+ @article{dosovitskiy2020vit,
163
+ title={An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale},
164
+ author={Dosovitskiy, Alexey and Beyer, Lucas and Kolesnikov, Alexander and Weissenborn, Dirk and Zhai, Xiaohua and Unterthiner, Thomas and Dehghani, Mostafa and Minderer, Matthias and Heigold, Georg and Gelly, Sylvain and Uszkoreit, Jakob and Houlsby, Neil},
165
+ journal={ICLR},
166
+ year={2021}
167
+ }
168
+ ```
config.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "vit_mediumd_patch16_reg4_gap_256",
3
+ "num_classes": 1000,
4
+ "num_features": 512,
5
+ "global_pool": "avg",
6
+ "pretrained_cfg": {
7
+ "tag": "sbb_in12k_ft_in1k",
8
+ "custom_load": false,
9
+ "input_size": [
10
+ 3,
11
+ 256,
12
+ 256
13
+ ],
14
+ "fixed_input_size": true,
15
+ "interpolation": "bicubic",
16
+ "crop_pct": 0.95,
17
+ "crop_mode": "center",
18
+ "mean": [
19
+ 0.5,
20
+ 0.5,
21
+ 0.5
22
+ ],
23
+ "std": [
24
+ 0.5,
25
+ 0.5,
26
+ 0.5
27
+ ],
28
+ "num_classes": 1000,
29
+ "pool_size": null,
30
+ "first_conv": "patch_embed.proj",
31
+ "classifier": "head"
32
+ }
33
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:331016d3a7dee124f87c11711fad53edb60fbe5847e75da01f112c71eda1f3a1
3
+ size 256462704
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c4d3efd8dd8167438d7f65141804af9e49804be1fef385c370daae96f940c02a
3
+ size 256541258