timm
/

Image Classification
timm
PyTorch
Safetensors
rwightman HF staff commited on
Commit
f5ad926
1 Parent(s): ad14d7a
Files changed (4) hide show
  1. README.md +155 -0
  2. config.json +41 -0
  3. model.safetensors +3 -0
  4. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 nfnet_l0.ra2_in1k
11
+
12
+ A NFNet-Lite (Lightweight NFNet) image classification model. Trained in `timm` by Ross Wightman.
13
+
14
+ Normalization Free Networks are (pre-activation) ResNet-like models without any normalization layers. Instead of Batch Normalization or alternatives, they use Scaled Weight Standardization and specifically placed scalar gains in residual path and at non-linearities based on signal propagation analysis.
15
+
16
+ Lightweight NFNets are `timm` specific variants that reduce the SE and bottleneck ratio from 0.5 -> 0.25 (reducing widths) and use a smaller group size while maintaining the same depth. SiLU activations used instead of GELU.
17
+
18
+
19
+
20
+ ## Model Details
21
+ - **Model Type:** Image classification / feature backbone
22
+ - **Model Stats:**
23
+ - Params (M): 35.1
24
+ - GMACs: 4.4
25
+ - Activations (M): 10.5
26
+ - Image size: train = 224 x 224, test = 288 x 288
27
+ - **Papers:**
28
+ - High-Performance Large-Scale Image Recognition Without Normalization: https://arxiv.org/abs/2102.06171
29
+ - Characterizing signal propagation to close the performance gap in unnormalized ResNets: https://arxiv.org/abs/2101.08692
30
+ - **Original:** https://github.com/huggingface/pytorch-image-models
31
+ - **Dataset:** ImageNet-1k
32
+
33
+ ## Model Usage
34
+ ### Image Classification
35
+ ```python
36
+ from urllib.request import urlopen
37
+ from PIL import Image
38
+ import timm
39
+
40
+ img = Image.open(urlopen(
41
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
42
+ ))
43
+
44
+ model = timm.create_model('nfnet_l0.ra2_in1k', pretrained=True)
45
+ model = model.eval()
46
+
47
+ # get model specific transforms (normalization, resize)
48
+ data_config = timm.data.resolve_model_data_config(model)
49
+ transforms = timm.data.create_transform(**data_config, is_training=False)
50
+
51
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
52
+
53
+ top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)
54
+ ```
55
+
56
+ ### Feature Map Extraction
57
+ ```python
58
+ from urllib.request import urlopen
59
+ from PIL import Image
60
+ import timm
61
+
62
+ img = Image.open(urlopen(
63
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
64
+ ))
65
+
66
+ model = timm.create_model(
67
+ 'nfnet_l0.ra2_in1k',
68
+ pretrained=True,
69
+ features_only=True,
70
+ )
71
+ model = model.eval()
72
+
73
+ # get model specific transforms (normalization, resize)
74
+ data_config = timm.data.resolve_model_data_config(model)
75
+ transforms = timm.data.create_transform(**data_config, is_training=False)
76
+
77
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
78
+
79
+ for o in output:
80
+ # print shape of each feature map in output
81
+ # e.g.:
82
+ # torch.Size([1, 64, 112, 112])
83
+ # torch.Size([1, 256, 56, 56])
84
+ # torch.Size([1, 512, 28, 28])
85
+ # torch.Size([1, 1536, 14, 14])
86
+ # torch.Size([1, 2304, 7, 7])
87
+
88
+ print(o.shape)
89
+ ```
90
+
91
+ ### Image Embeddings
92
+ ```python
93
+ from urllib.request import urlopen
94
+ from PIL import Image
95
+ import timm
96
+
97
+ img = Image.open(urlopen(
98
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
99
+ ))
100
+
101
+ model = timm.create_model(
102
+ 'nfnet_l0.ra2_in1k',
103
+ pretrained=True,
104
+ num_classes=0, # remove classifier nn.Linear
105
+ )
106
+ model = model.eval()
107
+
108
+ # get model specific transforms (normalization, resize)
109
+ data_config = timm.data.resolve_model_data_config(model)
110
+ transforms = timm.data.create_transform(**data_config, is_training=False)
111
+
112
+ output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
113
+
114
+ # or equivalently (without needing to set num_classes=0)
115
+
116
+ output = model.forward_features(transforms(img).unsqueeze(0))
117
+ # output is unpooled, a (1, 2304, 7, 7) shaped tensor
118
+
119
+ output = model.forward_head(output, pre_logits=True)
120
+ # output is a (1, num_features) shaped tensor
121
+ ```
122
+
123
+ ## Model Comparison
124
+ Explore the dataset and runtime metrics of this model in timm [model results](https://github.com/huggingface/pytorch-image-models/tree/main/results).
125
+
126
+
127
+ ## Citation
128
+ ```bibtex
129
+ @article{brock2021high,
130
+ author={Andrew Brock and Soham De and Samuel L. Smith and Karen Simonyan},
131
+ title={High-Performance Large-Scale Image Recognition Without Normalization},
132
+ journal={arXiv preprint arXiv:2102.06171},
133
+ year={2021}
134
+ }
135
+ ```
136
+ ```bibtex
137
+ @inproceedings{brock2021characterizing,
138
+ author={Andrew Brock and Soham De and Samuel L. Smith},
139
+ title={Characterizing signal propagation to close the performance gap in
140
+ unnormalized ResNets},
141
+ booktitle={9th International Conference on Learning Representations, {ICLR}},
142
+ year={2021}
143
+ }
144
+ ```
145
+ ```bibtex
146
+ @misc{rw2019timm,
147
+ author = {Ross Wightman},
148
+ title = {PyTorch Image Models},
149
+ year = {2019},
150
+ publisher = {GitHub},
151
+ journal = {GitHub repository},
152
+ doi = {10.5281/zenodo.4414861},
153
+ howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
154
+ }
155
+ ```
config.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "nfnet_l0",
3
+ "num_classes": 1000,
4
+ "num_features": 2304,
5
+ "pretrained_cfg": {
6
+ "tag": "ra2_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.9,
21
+ "test_crop_pct": 1.0,
22
+ "crop_mode": "center",
23
+ "mean": [
24
+ 0.485,
25
+ 0.456,
26
+ 0.406
27
+ ],
28
+ "std": [
29
+ 0.229,
30
+ 0.224,
31
+ 0.225
32
+ ],
33
+ "num_classes": 1000,
34
+ "pool_size": [
35
+ 7,
36
+ 7
37
+ ],
38
+ "first_conv": "stem.conv1",
39
+ "classifier": "head.fc"
40
+ }
41
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fce06fcc634422a28b462ab884e831e8ecabb24673a6309af4724be74adfeec8
3
+ size 140319350
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7de9a7a7df356fbd65293fa8f9a091056f255e6c6522ebdd8bd169afe37e5d01
3
+ size 140376257