Add model
Browse files- README.md +171 -0
- config.json +40 -0
- model.safetensors +3 -0
- pytorch_model.bin +3 -0
README.md
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 lambda_resnet50ts.a1h_in1k
|
11 |
+
|
12 |
+
A LambdaNet 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) `A1` recipe
|
18 |
+
* LAMB optimizer
|
19 |
+
* Stronger dropout, stochastic depth, and RandAugment than paper `A1` recipe
|
20 |
+
* Cosine LR schedule with warmup
|
21 |
+
|
22 |
+
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).
|
23 |
+
|
24 |
+
BYOB (with BYOANet attention specific blocks) allows configuration of:
|
25 |
+
* block / stage layout
|
26 |
+
* block-type interleaving
|
27 |
+
* stem layout
|
28 |
+
* output stride (dilation)
|
29 |
+
* activation and norm layers
|
30 |
+
* channel and spatial / self-attention layers
|
31 |
+
|
32 |
+
...and also includes `timm` features common to many other architectures, including:
|
33 |
+
* stochastic depth
|
34 |
+
* gradient checkpointing
|
35 |
+
* layer-wise LR decay
|
36 |
+
* per-stage feature extraction
|
37 |
+
|
38 |
+
|
39 |
+
## Model Details
|
40 |
+
- **Model Type:** Image classification / feature backbone
|
41 |
+
- **Model Stats:**
|
42 |
+
- Params (M): 21.5
|
43 |
+
- GMACs: 5.1
|
44 |
+
- Activations (M): 17.5
|
45 |
+
- Image size: 256 x 256
|
46 |
+
- **Papers:**
|
47 |
+
- LambdaNetworks: Modeling Long-Range Interactions Without Attention: https://arxiv.org/abs/2102.08602
|
48 |
+
- ResNet strikes back: An improved training procedure in timm: https://arxiv.org/abs/2110.00476
|
49 |
+
- **Dataset:** ImageNet-1k
|
50 |
+
|
51 |
+
## Model Usage
|
52 |
+
### Image Classification
|
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('lambda_resnet50ts.a1h_in1k', pretrained=True)
|
63 |
+
model = model.eval()
|
64 |
+
|
65 |
+
# get model specific transforms (normalization, resize)
|
66 |
+
data_config = timm.data.resolve_model_data_config(model)
|
67 |
+
transforms = timm.data.create_transform(**data_config, is_training=False)
|
68 |
+
|
69 |
+
output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
|
70 |
+
|
71 |
+
top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)
|
72 |
+
```
|
73 |
+
|
74 |
+
### Feature Map Extraction
|
75 |
+
```python
|
76 |
+
from urllib.request import urlopen
|
77 |
+
from PIL import Image
|
78 |
+
import timm
|
79 |
+
|
80 |
+
img = Image.open(urlopen(
|
81 |
+
'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
|
82 |
+
))
|
83 |
+
|
84 |
+
model = timm.create_model(
|
85 |
+
'lambda_resnet50ts.a1h_in1k',
|
86 |
+
pretrained=True,
|
87 |
+
features_only=True,
|
88 |
+
)
|
89 |
+
model = model.eval()
|
90 |
+
|
91 |
+
# get model specific transforms (normalization, resize)
|
92 |
+
data_config = timm.data.resolve_model_data_config(model)
|
93 |
+
transforms = timm.data.create_transform(**data_config, is_training=False)
|
94 |
+
|
95 |
+
output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
|
96 |
+
|
97 |
+
for o in output:
|
98 |
+
# print shape of each feature map in output
|
99 |
+
# e.g.:
|
100 |
+
# torch.Size([1, 64, 128, 128])
|
101 |
+
# torch.Size([1, 256, 64, 64])
|
102 |
+
# torch.Size([1, 512, 32, 32])
|
103 |
+
# torch.Size([1, 1024, 16, 16])
|
104 |
+
# torch.Size([1, 2048, 8, 8])
|
105 |
+
|
106 |
+
print(o.shape)
|
107 |
+
```
|
108 |
+
|
109 |
+
### Image Embeddings
|
110 |
+
```python
|
111 |
+
from urllib.request import urlopen
|
112 |
+
from PIL import Image
|
113 |
+
import timm
|
114 |
+
|
115 |
+
img = Image.open(urlopen(
|
116 |
+
'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
|
117 |
+
))
|
118 |
+
|
119 |
+
model = timm.create_model(
|
120 |
+
'lambda_resnet50ts.a1h_in1k',
|
121 |
+
pretrained=True,
|
122 |
+
num_classes=0, # remove classifier nn.Linear
|
123 |
+
)
|
124 |
+
model = model.eval()
|
125 |
+
|
126 |
+
# get model specific transforms (normalization, resize)
|
127 |
+
data_config = timm.data.resolve_model_data_config(model)
|
128 |
+
transforms = timm.data.create_transform(**data_config, is_training=False)
|
129 |
+
|
130 |
+
output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
|
131 |
+
|
132 |
+
# or equivalently (without needing to set num_classes=0)
|
133 |
+
|
134 |
+
output = model.forward_features(transforms(img).unsqueeze(0))
|
135 |
+
# output is unpooled, a (1, 2048, 8, 8) shaped tensor
|
136 |
+
|
137 |
+
output = model.forward_head(output, pre_logits=True)
|
138 |
+
# output is a (1, num_features) shaped tensor
|
139 |
+
```
|
140 |
+
|
141 |
+
## Model Comparison
|
142 |
+
Explore the dataset and runtime metrics of this model in timm [model results](https://github.com/huggingface/pytorch-image-models/tree/main/results).
|
143 |
+
|
144 |
+
## Citation
|
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 |
+
```
|
156 |
+
```bibtex
|
157 |
+
@article{Bello2021LambdaNetworksML,
|
158 |
+
title={LambdaNetworks: Modeling Long-Range Interactions Without Attention},
|
159 |
+
author={Irwan Bello},
|
160 |
+
journal={ArXiv},
|
161 |
+
year={2021},
|
162 |
+
volume={abs/2102.08602}
|
163 |
+
}
|
164 |
+
```
|
165 |
+
```bibtex
|
166 |
+
@inproceedings{wightman2021resnet,
|
167 |
+
title={ResNet strikes back: An improved training procedure in timm},
|
168 |
+
author={Wightman, Ross and Touvron, Hugo and Jegou, Herve},
|
169 |
+
booktitle={NeurIPS 2021 Workshop on ImageNet: Past, Present, and Future}
|
170 |
+
}
|
171 |
+
```
|
config.json
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architecture": "lambda_resnet50ts",
|
3 |
+
"num_classes": 1000,
|
4 |
+
"num_features": 2048,
|
5 |
+
"pretrained_cfg": {
|
6 |
+
"tag": "a1h_in1k",
|
7 |
+
"custom_load": false,
|
8 |
+
"input_size": [
|
9 |
+
3,
|
10 |
+
256,
|
11 |
+
256
|
12 |
+
],
|
13 |
+
"min_input_size": [
|
14 |
+
3,
|
15 |
+
128,
|
16 |
+
128
|
17 |
+
],
|
18 |
+
"fixed_input_size": false,
|
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:bcb80eaa77910c72c9c0ff9e83f99d550270fc23ab0ccac69d01e35cb269cdf8
|
3 |
+
size 86405518
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:aa21c2821d9d6fa9c5af9629e78b54dab1b47a4f61497ed728fc7759e4a0fbc1
|
3 |
+
size 86506541
|