timm
/

Image Classification
timm
PyTorch
Safetensors
rwightman HF staff commited on
Commit
187d952
1 Parent(s): a6f0e1e
Files changed (4) hide show
  1. README.md +157 -0
  2. config.json +41 -0
  3. model.safetensors +3 -0
  4. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 eca_nfnet_l1.ra2_in1k
11
+
12
+ A ECA-NFNet-Lite (Lightweight NFNet w/ ECA attention) 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
+ This NFNet variant also uses ECA (Efficient Channel Attention) instead of SE (Squeeze-and-Excitation).
19
+
20
+
21
+
22
+ ## Model Details
23
+ - **Model Type:** Image classification / feature backbone
24
+ - **Model Stats:**
25
+ - Params (M): 41.4
26
+ - GMACs: 9.6
27
+ - Activations (M): 22.0
28
+ - Image size: train = 256 x 256, test = 320 x 320
29
+ - **Papers:**
30
+ - High-Performance Large-Scale Image Recognition Without Normalization: https://arxiv.org/abs/2102.06171
31
+ - Characterizing signal propagation to close the performance gap in unnormalized ResNets: https://arxiv.org/abs/2101.08692
32
+ - **Original:** https://github.com/huggingface/pytorch-image-models
33
+ - **Dataset:** ImageNet-1k
34
+
35
+ ## Model Usage
36
+ ### Image Classification
37
+ ```python
38
+ from urllib.request import urlopen
39
+ from PIL import Image
40
+ import timm
41
+
42
+ img = Image.open(urlopen(
43
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
44
+ ))
45
+
46
+ model = timm.create_model('eca_nfnet_l1.ra2_in1k', pretrained=True)
47
+ model = model.eval()
48
+
49
+ # get model specific transforms (normalization, resize)
50
+ data_config = timm.data.resolve_model_data_config(model)
51
+ transforms = timm.data.create_transform(**data_config, is_training=False)
52
+
53
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
54
+
55
+ top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)
56
+ ```
57
+
58
+ ### Feature Map Extraction
59
+ ```python
60
+ from urllib.request import urlopen
61
+ from PIL import Image
62
+ import timm
63
+
64
+ img = Image.open(urlopen(
65
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
66
+ ))
67
+
68
+ model = timm.create_model(
69
+ 'eca_nfnet_l1.ra2_in1k',
70
+ pretrained=True,
71
+ features_only=True,
72
+ )
73
+ model = model.eval()
74
+
75
+ # get model specific transforms (normalization, resize)
76
+ data_config = timm.data.resolve_model_data_config(model)
77
+ transforms = timm.data.create_transform(**data_config, is_training=False)
78
+
79
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
80
+
81
+ for o in output:
82
+ # print shape of each feature map in output
83
+ # e.g.:
84
+ # torch.Size([1, 64, 128, 128])
85
+ # torch.Size([1, 256, 64, 64])
86
+ # torch.Size([1, 512, 32, 32])
87
+ # torch.Size([1, 1536, 16, 16])
88
+ # torch.Size([1, 3072, 8, 8])
89
+
90
+ print(o.shape)
91
+ ```
92
+
93
+ ### Image Embeddings
94
+ ```python
95
+ from urllib.request import urlopen
96
+ from PIL import Image
97
+ import timm
98
+
99
+ img = Image.open(urlopen(
100
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
101
+ ))
102
+
103
+ model = timm.create_model(
104
+ 'eca_nfnet_l1.ra2_in1k',
105
+ pretrained=True,
106
+ num_classes=0, # remove classifier nn.Linear
107
+ )
108
+ model = model.eval()
109
+
110
+ # get model specific transforms (normalization, resize)
111
+ data_config = timm.data.resolve_model_data_config(model)
112
+ transforms = timm.data.create_transform(**data_config, is_training=False)
113
+
114
+ output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
115
+
116
+ # or equivalently (without needing to set num_classes=0)
117
+
118
+ output = model.forward_features(transforms(img).unsqueeze(0))
119
+ # output is unpooled, a (1, 3072, 8, 8) shaped tensor
120
+
121
+ output = model.forward_head(output, pre_logits=True)
122
+ # output is a (1, num_features) shaped tensor
123
+ ```
124
+
125
+ ## Model Comparison
126
+ Explore the dataset and runtime metrics of this model in timm [model results](https://github.com/huggingface/pytorch-image-models/tree/main/results).
127
+
128
+
129
+ ## Citation
130
+ ```bibtex
131
+ @article{brock2021high,
132
+ author={Andrew Brock and Soham De and Samuel L. Smith and Karen Simonyan},
133
+ title={High-Performance Large-Scale Image Recognition Without Normalization},
134
+ journal={arXiv preprint arXiv:2102.06171},
135
+ year={2021}
136
+ }
137
+ ```
138
+ ```bibtex
139
+ @inproceedings{brock2021characterizing,
140
+ author={Andrew Brock and Soham De and Samuel L. Smith},
141
+ title={Characterizing signal propagation to close the performance gap in
142
+ unnormalized ResNets},
143
+ booktitle={9th International Conference on Learning Representations, {ICLR}},
144
+ year={2021}
145
+ }
146
+ ```
147
+ ```bibtex
148
+ @misc{rw2019timm,
149
+ author = {Ross Wightman},
150
+ title = {PyTorch Image Models},
151
+ year = {2019},
152
+ publisher = {GitHub},
153
+ journal = {GitHub repository},
154
+ doi = {10.5281/zenodo.4414861},
155
+ howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
156
+ }
157
+ ```
config.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "eca_nfnet_l1",
3
+ "num_classes": 1000,
4
+ "num_features": 3072,
5
+ "pretrained_cfg": {
6
+ "tag": "ra2_in1k",
7
+ "custom_load": false,
8
+ "input_size": [
9
+ 3,
10
+ 256,
11
+ 256
12
+ ],
13
+ "test_input_size": [
14
+ 3,
15
+ 320,
16
+ 320
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
+ 8,
36
+ 8
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:1bf1165c240d2c71ffd7d3d66ed2ecb5b4e55ab307a4befd11c94bd711f8df61
3
+ size 165663618
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:314c58dfdc27038b9aac56c2087bc777d207335444854d7474c9ebbe346dc92c
3
+ size 165750817