timm
/

Image Classification
timm
PyTorch
Safetensors
Transformers
rwightman HF staff commited on
Commit
4f64854
·
1 Parent(s): 5b56c37
Files changed (4) hide show
  1. README.md +151 -0
  2. config.json +40 -0
  3. model.safetensors +3 -0
  4. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 resnetv2_50d_gn.ah_in1k
11
+
12
+ A ResNet-V2 (pre-activation ResNet) image classification model. Trained on ImageNet-1k by Ross Wightman in `timm` using ResNet strikes back (RSB) `A1` based recipe.
13
+
14
+ This model uses:
15
+ * A 3x3 3-layer stem, avg-pool in shortcut downsample.
16
+ * Group Normalization (GN) instead of Batch Normalization (BN).
17
+
18
+
19
+ ## Model Details
20
+ - **Model Type:** Image classification / feature backbone
21
+ - **Model Stats:**
22
+ - Params (M): 25.6
23
+ - GMACs: 4.4
24
+ - Activations (M): 11.9
25
+ - Image size: train = 224 x 224, test = 288 x 288
26
+ - **Papers:**
27
+ - ResNet strikes back: An improved training procedure in timm: https://arxiv.org/abs/2110.00476
28
+ - Identity Mappings in Deep Residual Networks: https://arxiv.org/abs/1603.05027
29
+ - **Dataset:** ImageNet-1k
30
+ - **Original:** https://github.com/huggingface/pytorch-image-models
31
+
32
+ ## Model Usage
33
+ ### Image Classification
34
+ ```python
35
+ from urllib.request import urlopen
36
+ from PIL import Image
37
+ import timm
38
+
39
+ img = Image.open(urlopen(
40
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
41
+ ))
42
+
43
+ model = timm.create_model('resnetv2_50d_gn.ah_in1k', pretrained=True)
44
+ model = model.eval()
45
+
46
+ # get model specific transforms (normalization, resize)
47
+ data_config = timm.data.resolve_model_data_config(model)
48
+ transforms = timm.data.create_transform(**data_config, is_training=False)
49
+
50
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
51
+
52
+ top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)
53
+ ```
54
+
55
+ ### Feature Map Extraction
56
+ ```python
57
+ from urllib.request import urlopen
58
+ from PIL import Image
59
+ import timm
60
+
61
+ img = Image.open(urlopen(
62
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
63
+ ))
64
+
65
+ model = timm.create_model(
66
+ 'resnetv2_50d_gn.ah_in1k',
67
+ pretrained=True,
68
+ features_only=True,
69
+ )
70
+ model = model.eval()
71
+
72
+ # get model specific transforms (normalization, resize)
73
+ data_config = timm.data.resolve_model_data_config(model)
74
+ transforms = timm.data.create_transform(**data_config, is_training=False)
75
+
76
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
77
+
78
+ for o in output:
79
+ # print shape of each feature map in output
80
+ # e.g.:
81
+ # torch.Size([1, 64, 112, 112])
82
+ # torch.Size([1, 256, 56, 56])
83
+ # torch.Size([1, 512, 28, 28])
84
+ # torch.Size([1, 1024, 14, 14])
85
+ # torch.Size([1, 2048, 7, 7])
86
+
87
+ print(o.shape)
88
+ ```
89
+
90
+ ### Image Embeddings
91
+ ```python
92
+ from urllib.request import urlopen
93
+ from PIL import Image
94
+ import timm
95
+
96
+ img = Image.open(urlopen(
97
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
98
+ ))
99
+
100
+ model = timm.create_model(
101
+ 'resnetv2_50d_gn.ah_in1k',
102
+ pretrained=True,
103
+ num_classes=0, # remove classifier nn.Linear
104
+ )
105
+ model = model.eval()
106
+
107
+ # get model specific transforms (normalization, resize)
108
+ data_config = timm.data.resolve_model_data_config(model)
109
+ transforms = timm.data.create_transform(**data_config, is_training=False)
110
+
111
+ output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
112
+
113
+ # or equivalently (without needing to set num_classes=0)
114
+
115
+ output = model.forward_features(transforms(img).unsqueeze(0))
116
+ # output is unpooled, a (1, 2048, 7, 7) shaped tensor
117
+
118
+ output = model.forward_head(output, pre_logits=True)
119
+ # output is a (1, num_features) shaped tensor
120
+ ```
121
+
122
+ ## Model Comparison
123
+ Explore the dataset and runtime metrics of this model in timm [model results](https://github.com/huggingface/pytorch-image-models/tree/main/results).
124
+
125
+ ## Citation
126
+ ```bibtex
127
+ @inproceedings{wightman2021resnet,
128
+ title={ResNet strikes back: An improved training procedure in timm},
129
+ author={Wightman, Ross and Touvron, Hugo and Jegou, Herve},
130
+ booktitle={NeurIPS 2021 Workshop on ImageNet: Past, Present, and Future}
131
+ }
132
+ ```
133
+ ```bibtex
134
+ @article{He2016,
135
+ author = {Kaiming He and Xiangyu Zhang and Shaoqing Ren and Jian Sun},
136
+ title = {Identity Mappings in Deep Residual Networks},
137
+ journal = {arXiv preprint arXiv:1603.05027},
138
+ year = {2016}
139
+ }
140
+ ```
141
+ ```bibtex
142
+ @misc{rw2019timm,
143
+ author = {Ross Wightman},
144
+ title = {PyTorch Image Models},
145
+ year = {2019},
146
+ publisher = {GitHub},
147
+ journal = {GitHub repository},
148
+ doi = {10.5281/zenodo.4414861},
149
+ howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
150
+ }
151
+ ```
config.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "resnetv2_50d_gn",
3
+ "num_classes": 1000,
4
+ "num_features": 2048,
5
+ "pretrained_cfg": {
6
+ "tag": "ah_in1k",
7
+ "custom_load": false,
8
+ "input_size": [
9
+ 3,
10
+ 224,
11
+ 224
12
+ ],
13
+ "test_input_size": [
14
+ 3,
15
+ 288,
16
+ 288
17
+ ],
18
+ "fixed_input_size": false,
19
+ "interpolation": "bicubic",
20
+ "crop_pct": 0.95,
21
+ "crop_mode": "center",
22
+ "mean": [
23
+ 0.5,
24
+ 0.5,
25
+ 0.5
26
+ ],
27
+ "std": [
28
+ 0.5,
29
+ 0.5,
30
+ 0.5
31
+ ],
32
+ "num_classes": 1000,
33
+ "pool_size": [
34
+ 7,
35
+ 7
36
+ ],
37
+ "first_conv": "stem.conv1",
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:0f0ef7e255842fcd35c1c8d3db5c5e950a1bf70d1b4e8bb00ddcf44bfe47fabf
3
+ size 102290100
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:09356be3051621d6d89c12e6b03e10ff81e3cdf8787689426a93aa051f69a3fb
3
+ size 102336713