timm
/

Image Classification
timm
PyTorch
Safetensors
rwightman HF staff commited on
Commit
b7c0216
1 Parent(s): e6a3181
Files changed (4) hide show
  1. README.md +138 -0
  2. config.json +35 -0
  3. model.safetensors +3 -0
  4. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - image-classification
4
+ - timm
5
+ library_name: timm
6
+ license: apache-2.0
7
+ ---
8
+ # Model card for tresnet_m.miil_in21k
9
+
10
+ A TResNet image classification model. Trained on ImageNet-21K-P ("ImageNet-21K Pretraining for the Masses", a 11k subset of ImageNet-22k) by paper authors.
11
+
12
+ The weights for this model have been remapped and modified from the originals to work with standard BatchNorm instead of InplaceABN. `inplace_abn` can be problematic to build recently and ends up slower with `memory_format=channels_last`, torch.compile(), etc.
13
+
14
+ ## Model Details
15
+ - **Model Type:** Image classification / feature backbone
16
+ - **Model Stats:**
17
+ - Params (M): 52.3
18
+ - GMACs: 5.8
19
+ - Activations (M): 7.3
20
+ - Image size: 224 x 224
21
+ - **Papers:**
22
+ - TResNet: High Performance GPU-Dedicated Architecture: https://arxiv.org/abs/2003.13630
23
+ - ImageNet-21K Pretraining for the Masses: https://arxiv.org/abs/2104.10972
24
+ - **Pretrain Dataset:** ImageNet-21K-P
25
+ - **Original:**
26
+ - https://github.com/Alibaba-MIIL/TResNet
27
+ - https://github.com/Alibaba-MIIL/ImageNet21K
28
+
29
+ ## Model Usage
30
+ ### Image Classification
31
+ ```python
32
+ from urllib.request import urlopen
33
+ from PIL import Image
34
+ import timm
35
+
36
+ img = Image.open(urlopen(
37
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
38
+ ))
39
+
40
+ model = timm.create_model('tresnet_m.miil_in21k', pretrained=True)
41
+ model = model.eval()
42
+
43
+ # get model specific transforms (normalization, resize)
44
+ data_config = timm.data.resolve_model_data_config(model)
45
+ transforms = timm.data.create_transform(**data_config, is_training=False)
46
+
47
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
48
+
49
+ top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)
50
+ ```
51
+
52
+ ### Feature Map Extraction
53
+ ```python
54
+ from urllib.request import urlopen
55
+ from PIL import Image
56
+ import timm
57
+
58
+ img = Image.open(urlopen(
59
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
60
+ ))
61
+
62
+ model = timm.create_model(
63
+ 'tresnet_m.miil_in21k',
64
+ pretrained=True,
65
+ features_only=True,
66
+ )
67
+ model = model.eval()
68
+
69
+ # get model specific transforms (normalization, resize)
70
+ data_config = timm.data.resolve_model_data_config(model)
71
+ transforms = timm.data.create_transform(**data_config, is_training=False)
72
+
73
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
74
+
75
+ for o in output:
76
+ # print shape of each feature map in output
77
+ # e.g.:
78
+ # torch.Size([1, 64, 56, 56])
79
+ # torch.Size([1, 128, 28, 28])
80
+ # torch.Size([1, 1024, 14, 14])
81
+ # torch.Size([1, 2048, 7, 7])
82
+
83
+ print(o.shape)
84
+ ```
85
+
86
+ ### Image Embeddings
87
+ ```python
88
+ from urllib.request import urlopen
89
+ from PIL import Image
90
+ import timm
91
+
92
+ img = Image.open(urlopen(
93
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
94
+ ))
95
+
96
+ model = timm.create_model(
97
+ 'tresnet_m.miil_in21k',
98
+ pretrained=True,
99
+ num_classes=0, # remove classifier nn.Linear
100
+ )
101
+ model = model.eval()
102
+
103
+ # get model specific transforms (normalization, resize)
104
+ data_config = timm.data.resolve_model_data_config(model)
105
+ transforms = timm.data.create_transform(**data_config, is_training=False)
106
+
107
+ output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
108
+
109
+ # or equivalently (without needing to set num_classes=0)
110
+
111
+ output = model.forward_features(transforms(img).unsqueeze(0))
112
+ # output is unpooled, a (1, 2048, 7, 7) shaped tensor
113
+
114
+ output = model.forward_head(output, pre_logits=True)
115
+ # output is a (1, num_features) shaped tensor
116
+ ```
117
+
118
+ ## Citation
119
+ ```bibtex
120
+ @misc{ridnik2020tresnet,
121
+ title={TResNet: High Performance GPU-Dedicated Architecture},
122
+ author={Tal Ridnik and Hussam Lawen and Asaf Noy and Itamar Friedman},
123
+ year={2020},
124
+ eprint={2003.13630},
125
+ archivePrefix={arXiv},
126
+ primaryClass={cs.CV}
127
+ }
128
+ ```
129
+ ```bibtex
130
+ @misc{ridnik2021imagenet21k,
131
+ title={ImageNet-21K Pretraining for the Masses},
132
+ author={Tal Ridnik and Emanuel Ben-Baruch and Asaf Noy and Lihi Zelnik-Manor},
133
+ year={2021},
134
+ eprint={2104.10972},
135
+ archivePrefix={arXiv},
136
+ primaryClass={cs.CV}
137
+ }
138
+ ```
config.json ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "tresnet_m",
3
+ "num_classes": 11221,
4
+ "num_features": 2048,
5
+ "pretrained_cfg": {
6
+ "tag": "miil_in21k",
7
+ "custom_load": false,
8
+ "input_size": [
9
+ 3,
10
+ 224,
11
+ 224
12
+ ],
13
+ "fixed_input_size": false,
14
+ "interpolation": "bilinear",
15
+ "crop_pct": 0.875,
16
+ "crop_mode": "center",
17
+ "mean": [
18
+ 0.0,
19
+ 0.0,
20
+ 0.0
21
+ ],
22
+ "std": [
23
+ 1.0,
24
+ 1.0,
25
+ 1.0
26
+ ],
27
+ "num_classes": 11221,
28
+ "pool_size": [
29
+ 7,
30
+ 7
31
+ ],
32
+ "first_conv": "body.conv1.conv",
33
+ "classifier": "head.fc"
34
+ }
35
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f0c4dba14875e71f2fbd03f463ad475f0c708a9810be3ec0cc8ec5cfb1e35f24
3
+ size 209617236
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:77e985a1dfb1e646a905df65ff43eaa2be5237ca0a1367cec7013b1f1c19e782
3
+ size 209731669