timm
/

Image Classification
timm
PyTorch
Safetensors
rwightman HF staff commited on
Commit
78c4b93
1 Parent(s): cdb3402
Files changed (4) hide show
  1. README.md +170 -0
  2. config.json +40 -0
  3. model.safetensors +3 -0
  4. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - image-classification
4
+ - timm
5
+ library_name: timm
6
+ license: apache-2.0
7
+ datasets:
8
+ - imagenet-1k
9
+ ---
10
+ # Model card for botnet26t_256.c1_in1k
11
+
12
+ A BotNet image classification model (, based on ResNet architecture). Trained on ImageNet-1k in `timm` by Ross Wightman.
13
+
14
+ NOTE: this model did not adhere to any specific paper configuration, it was tuned for reasonable training times and reduced frequency of self-attention blocks.
15
+
16
+ Recipe details:
17
+ * Based on [ResNet Strikes Back](https://arxiv.org/abs/2110.00476) `C` recipes
18
+ * SGD (w/ Nesterov) optimizer and AGC (adaptive gradient clipping).
19
+ * Cosine LR schedule with warmup
20
+
21
+ This model architecture is implemented using `timm`'s flexible [BYOBNet (Bring-Your-Own-Blocks Network)](https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/byobnet.py).
22
+
23
+ BYOB (with BYOANet attention specific blocks) allows configuration of:
24
+ * block / stage layout
25
+ * block-type interleaving
26
+ * stem layout
27
+ * output stride (dilation)
28
+ * activation and norm layers
29
+ * channel and spatial / self-attention layers
30
+
31
+ ...and also includes `timm` features common to many other architectures, including:
32
+ * stochastic depth
33
+ * gradient checkpointing
34
+ * layer-wise LR decay
35
+ * per-stage feature extraction
36
+
37
+
38
+ ## Model Details
39
+ - **Model Type:** Image classification / feature backbone
40
+ - **Model Stats:**
41
+ - Params (M): 12.5
42
+ - GMACs: 3.3
43
+ - Activations (M): 12.0
44
+ - Image size: 256 x 256
45
+ - **Papers:**
46
+ - Bottleneck Transformers for Visual Recognition: https://arxiv.org/abs/2101.11605
47
+ - ResNet strikes back: An improved training procedure in timm: https://arxiv.org/abs/2110.00476
48
+ - **Dataset:** ImageNet-1k
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('botnet26t_256.c1_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
+ 'botnet26t_256.c1_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, 64, 128, 128])
100
+ # torch.Size([1, 256, 64, 64])
101
+ # torch.Size([1, 512, 32, 32])
102
+ # torch.Size([1, 1024, 16, 16])
103
+ # torch.Size([1, 2048, 8, 8])
104
+
105
+ print(o.shape)
106
+ ```
107
+
108
+ ### Image Embeddings
109
+ ```python
110
+ from urllib.request import urlopen
111
+ from PIL import Image
112
+ import timm
113
+
114
+ img = Image.open(urlopen(
115
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
116
+ ))
117
+
118
+ model = timm.create_model(
119
+ 'botnet26t_256.c1_in1k',
120
+ pretrained=True,
121
+ num_classes=0, # remove classifier nn.Linear
122
+ )
123
+ model = model.eval()
124
+
125
+ # get model specific transforms (normalization, resize)
126
+ data_config = timm.data.resolve_model_data_config(model)
127
+ transforms = timm.data.create_transform(**data_config, is_training=False)
128
+
129
+ output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
130
+
131
+ # or equivalently (without needing to set num_classes=0)
132
+
133
+ output = model.forward_features(transforms(img).unsqueeze(0))
134
+ # output is unpooled, a (1, 2048, 8, 8) shaped tensor
135
+
136
+ output = model.forward_head(output, pre_logits=True)
137
+ # output is a (1, num_features) shaped tensor
138
+ ```
139
+
140
+ ## Model Comparison
141
+ Explore the dataset and runtime metrics of this model in timm [model results](https://github.com/huggingface/pytorch-image-models/tree/main/results).
142
+
143
+ ## Citation
144
+ ```bibtex
145
+ @misc{rw2019timm,
146
+ author = {Ross Wightman},
147
+ title = {PyTorch Image Models},
148
+ year = {2019},
149
+ publisher = {GitHub},
150
+ journal = {GitHub repository},
151
+ doi = {10.5281/zenodo.4414861},
152
+ howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
153
+ }
154
+ ```
155
+ ```bibtex
156
+ @article{Srinivas2021BottleneckTF,
157
+ title={Bottleneck Transformers for Visual Recognition},
158
+ author={A. Srinivas and Tsung-Yi Lin and Niki Parmar and Jonathon Shlens and P. Abbeel and Ashish Vaswani},
159
+ journal={2021 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
160
+ year={2021},
161
+ pages={16514-16524}
162
+ }
163
+ ```
164
+ ```bibtex
165
+ @inproceedings{wightman2021resnet,
166
+ title={ResNet strikes back: An improved training procedure in timm},
167
+ author={Wightman, Ross and Touvron, Hugo and Jegou, Herve},
168
+ booktitle={NeurIPS 2021 Workshop on ImageNet: Past, Present, and Future}
169
+ }
170
+ ```
config.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "botnet26t_256",
3
+ "num_classes": 1000,
4
+ "num_features": 2048,
5
+ "pretrained_cfg": {
6
+ "tag": "c1_in1k",
7
+ "custom_load": false,
8
+ "input_size": [
9
+ 3,
10
+ 256,
11
+ 256
12
+ ],
13
+ "min_input_size": [
14
+ 3,
15
+ 224,
16
+ 224
17
+ ],
18
+ "fixed_input_size": true,
19
+ "interpolation": "bicubic",
20
+ "crop_pct": 0.95,
21
+ "crop_mode": "center",
22
+ "mean": [
23
+ 0.485,
24
+ 0.456,
25
+ 0.406
26
+ ],
27
+ "std": [
28
+ 0.229,
29
+ 0.224,
30
+ 0.225
31
+ ],
32
+ "num_classes": 1000,
33
+ "pool_size": [
34
+ 8,
35
+ 8
36
+ ],
37
+ "first_conv": "stem.conv1.conv",
38
+ "classifier": "head.fc"
39
+ }
40
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4a67ec477bf2c9589ece019ae101ade9f3c0176708f1cd55995a05c86f33df23
3
+ size 50098028
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:74c5d40061bfb29e692bc96f5ea45fa623b08bf4881d7e11e7bc8069426c17b2
3
+ size 50148629