timm
/

Image Classification
timm
PyTorch
Transformers
rwightman HF staff commited on
Commit
30219d9
·
1 Parent(s): d47463a
Files changed (3) hide show
  1. README.md +146 -0
  2. config.json +39 -0
  3. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - image-classification
4
+ - timm
5
+ library_tag: timm
6
+ license: apache-2.0
7
+ datasets:
8
+ - imagenet-1k
9
+ ---
10
+ # Model card for levit_conv_128s.fb_dist_in1k
11
+
12
+ A LeViT image classification model using default linear mode (non-convolutional mode with nn.Linear and nn.BatchNorm1d). Pretrained on ImageNet-1k using distillation by paper authors.
13
+
14
+
15
+ ## Model Details
16
+ - **Model Type:** Image classification / feature backbone
17
+ - **Model Stats:**
18
+ - Params (M): 7.8
19
+ - GMACs: 0.3
20
+ - Activations (M): 1.9
21
+ - Image size: 224 x 224
22
+ - **Papers:**
23
+ - LeViT: a Vision Transformer in ConvNet's Clothing for Faster Inference: https://arxiv.org/abs/2104.01136
24
+ - **Original:** https://github.com/facebookresearch/LeViT
25
+ - **Dataset:** ImageNet-1k
26
+
27
+ ## Model Usage
28
+ ### Image Classification
29
+ ```python
30
+ from urllib.request import urlopen
31
+ from PIL import Image
32
+ import timm
33
+
34
+ img = Image.open(
35
+ urlopen('https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'))
36
+
37
+ model = timm.create_model('levit_conv_128s.fb_dist_in1k', pretrained=True)
38
+ model = model.eval()
39
+
40
+ # get model specific transforms (normalization, resize)
41
+ data_config = timm.data.resolve_model_data_config(model)
42
+ transforms = timm.data.create_transform(**data_config, is_training=False)
43
+
44
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
45
+
46
+ top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)
47
+ ```
48
+
49
+ ### Image Embeddings
50
+ ```python
51
+ from urllib.request import urlopen
52
+ from PIL import Image
53
+ import timm
54
+
55
+ img = Image.open(
56
+ urlopen('https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'))
57
+
58
+ model = timm.create_model(
59
+ 'levit_conv_128s.fb_dist_in1k',
60
+ pretrained=True,
61
+ num_classes=0, # remove classifier nn.Linear
62
+ )
63
+ model = model.eval()
64
+
65
+ # get model specific transforms (normalization, resize)
66
+ data_config = timm.data.resolve_model_data_config(model)
67
+ transforms = timm.data.create_transform(**data_config, is_training=False)
68
+
69
+ output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
70
+
71
+ # or equivalently (without needing to set num_classes=0)
72
+
73
+ output = model.forward_features(transforms(img).unsqueeze(0))
74
+ # output is unpooled (ie.e a (batch_size, num_features, H, W) tensor
75
+
76
+ output = model.forward_head(output, pre_logits=True)
77
+ # output is (batch_size, num_features) tensor
78
+ ```
79
+
80
+ ### Feature Map Extraction
81
+ ```python
82
+ from urllib.request import urlopen
83
+ from PIL import Image
84
+ import timm
85
+
86
+ img = Image.open(
87
+ urlopen('https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'))
88
+
89
+ model = timm.create_model(
90
+ 'levit_conv_128s.fb_dist_in1k',
91
+ pretrained=True,
92
+ features_only=True,
93
+ )
94
+ model = model.eval()
95
+
96
+ # get model specific transforms (normalization, resize)
97
+ data_config = timm.data.resolve_model_data_config(model)
98
+ transforms = timm.data.create_transform(**data_config, is_training=False)
99
+
100
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
101
+
102
+ for o in output:
103
+ # print shape of each feature map in output
104
+ # e.g. for levit_conv_256:
105
+ # torch.Size([2, 256, 14, 14])
106
+ # torch.Size([2, 384, 7, 7])
107
+ # torch.Size([2, 512, 4, 4])
108
+ print(o.shape)
109
+ ```
110
+
111
+ ## Model Comparison
112
+ |model |top1 |top5 |param_count|img_size|
113
+ |-----------------------------------|------|------|-----------|--------|
114
+ |levit_384.fb_dist_in1k |82.596|96.012|39.13 |224 |
115
+ |levit_conv_384.fb_dist_in1k |82.596|96.012|39.13 |224 |
116
+ |levit_256.fb_dist_in1k |81.512|95.48 |18.89 |224 |
117
+ |levit_conv_256.fb_dist_in1k |81.512|95.48 |18.89 |224 |
118
+ |levit_conv_192.fb_dist_in1k |79.86 |94.792|10.95 |224 |
119
+ |levit_192.fb_dist_in1k |79.858|94.792|10.95 |224 |
120
+ |levit_128.fb_dist_in1k |78.474|94.014|9.21 |224 |
121
+ |levit_conv_128.fb_dist_in1k |78.474|94.02 |9.21 |224 |
122
+ |levit_128s.fb_dist_in1k |76.534|92.864|7.78 |224 |
123
+ |levit_conv_128s.fb_dist_in1k |76.532|92.864|7.78 |224 |
124
+
125
+ ## Citation
126
+ ```bibtex
127
+ @InProceedings{Graham_2021_ICCV,
128
+ author = {Graham, Benjamin and El-Nouby, Alaaeldin and Touvron, Hugo and Stock, Pierre and Joulin, Armand and Jegou, Herve and Douze, Matthijs},
129
+ title = {LeViT: A Vision Transformer in ConvNet's Clothing for Faster Inference},
130
+ booktitle = {Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV)},
131
+ month = {October},
132
+ year = {2021},
133
+ pages = {12259-12269}
134
+ }
135
+ ```
136
+ ```bibtex
137
+ @misc{rw2019timm,
138
+ author = {Ross Wightman},
139
+ title = {PyTorch Image Models},
140
+ year = {2019},
141
+ publisher = {GitHub},
142
+ journal = {GitHub repository},
143
+ doi = {10.5281/zenodo.4414861},
144
+ howpublished = {\url{https://github.com/rwightman/pytorch-image-models}}
145
+ }
146
+ ```
config.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "levit_conv_128s",
3
+ "num_classes": 1000,
4
+ "num_features": 384,
5
+ "global_pool": "avg",
6
+ "pretrained_cfg": {
7
+ "tag": "fb_dist_in1k",
8
+ "custom_load": false,
9
+ "input_size": [
10
+ 3,
11
+ 224,
12
+ 224
13
+ ],
14
+ "fixed_input_size": true,
15
+ "interpolation": "bicubic",
16
+ "crop_pct": 0.9,
17
+ "crop_mode": "center",
18
+ "mean": [
19
+ 0.485,
20
+ 0.456,
21
+ 0.406
22
+ ],
23
+ "std": [
24
+ 0.229,
25
+ 0.224,
26
+ 0.225
27
+ ],
28
+ "num_classes": 1000,
29
+ "pool_size": [
30
+ 4,
31
+ 4
32
+ ],
33
+ "first_conv": "stem.conv1.linear",
34
+ "classifier": [
35
+ "head.linear",
36
+ "head_dist.linear"
37
+ ]
38
+ }
39
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:48fdde7c1ff84b3a3a56131c055de981bcca399470469756a3fa83792e1ce73b
3
+ size 31384353