timm
/

Image Classification
timm
PyTorch
Safetensors
rwightman HF staff commited on
Commit
e65bed9
1 Parent(s): cc1fcf5
Files changed (4) hide show
  1. README.md +132 -0
  2. config.json +41 -0
  3. model.safetensors +3 -0
  4. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 densenet121.ra_in1k
11
+
12
+ A DenseNet image classification model. Pretrained on ImageNet-1k in `timm` by Ross Wightman using RandAugment `RA` recipe. Related to `B` recipe in [ResNet Strikes Back](https://arxiv.org/abs/2110.00476).
13
+
14
+ ## Model Details
15
+ - **Model Type:** Image classification / feature backbone
16
+ - **Model Stats:**
17
+ - Params (M): 8.0
18
+ - GMACs: 2.9
19
+ - Activations (M): 6.9
20
+ - Image size: train = 224 x 224, test = 288 x 288
21
+ - **Papers:**
22
+ - Densely Connected Convolutional Networks: https://arxiv.org/abs/1608.06993
23
+ - ResNet strikes back: An improved training procedure in timm: https://arxiv.org/abs/2110.00476
24
+ - **Dataset:** ImageNet-1k
25
+ - **Original:** https://github.com/huggingface/pytorch-image-models
26
+
27
+ ## Model Usage
28
+ ### Image Classification
29
+ ```python
30
+ from urllib.request import urlopen
31
+ from PIL import Image
32
+ import timm
33
+
34
+ img = Image.open(urlopen(
35
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
36
+ ))
37
+
38
+ model = timm.create_model('densenet121.ra_in1k', pretrained=True)
39
+ model = model.eval()
40
+
41
+ # get model specific transforms (normalization, resize)
42
+ data_config = timm.data.resolve_model_data_config(model)
43
+ transforms = timm.data.create_transform(**data_config, is_training=False)
44
+
45
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
46
+
47
+ top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)
48
+ ```
49
+
50
+ ### Feature Map Extraction
51
+ ```python
52
+ from urllib.request import urlopen
53
+ from PIL import Image
54
+ import timm
55
+
56
+ img = Image.open(urlopen(
57
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
58
+ ))
59
+
60
+ model = timm.create_model(
61
+ 'densenet121.ra_in1k',
62
+ pretrained=True,
63
+ features_only=True,
64
+ )
65
+ model = model.eval()
66
+
67
+ # get model specific transforms (normalization, resize)
68
+ data_config = timm.data.resolve_model_data_config(model)
69
+ transforms = timm.data.create_transform(**data_config, is_training=False)
70
+
71
+ output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
72
+
73
+ for o in output:
74
+ # print shape of each feature map in output
75
+ # e.g.:
76
+ # torch.Size([1, 64, 112, 112])
77
+ # torch.Size([1, 256, 56, 56])
78
+ # torch.Size([1, 512, 28, 28])
79
+ # torch.Size([1, 1024, 14, 14])
80
+ # torch.Size([1, 1024, 7, 7])
81
+
82
+ print(o.shape)
83
+ ```
84
+
85
+ ### Image Embeddings
86
+ ```python
87
+ from urllib.request import urlopen
88
+ from PIL import Image
89
+ import timm
90
+
91
+ img = Image.open(urlopen(
92
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
93
+ ))
94
+
95
+ model = timm.create_model(
96
+ 'densenet121.ra_in1k',
97
+ pretrained=True,
98
+ num_classes=0, # remove classifier nn.Linear
99
+ )
100
+ model = model.eval()
101
+
102
+ # get model specific transforms (normalization, resize)
103
+ data_config = timm.data.resolve_model_data_config(model)
104
+ transforms = timm.data.create_transform(**data_config, is_training=False)
105
+
106
+ output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
107
+
108
+ # or equivalently (without needing to set num_classes=0)
109
+
110
+ output = model.forward_features(transforms(img).unsqueeze(0))
111
+ # output is unpooled, a (1, 1024, 7, 7) shaped tensor
112
+
113
+ output = model.forward_head(output, pre_logits=True)
114
+ # output is a (1, num_features) shaped tensor
115
+ ```
116
+
117
+ ## Citation
118
+ ```bibtex
119
+ @inproceedings{huang2017densely,
120
+ title={Densely Connected Convolutional Networks},
121
+ author={Huang, Gao and Liu, Zhuang and van der Maaten, Laurens and Weinberger, Kilian Q },
122
+ booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
123
+ year={2017}
124
+ }
125
+ ```
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
+ ```
config.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "densenet121",
3
+ "num_classes": 1000,
4
+ "num_features": 1024,
5
+ "pretrained_cfg": {
6
+ "tag": "ra_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.875,
21
+ "test_crop_pct": 0.95,
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": "features.conv0",
39
+ "classifier": "classifier"
40
+ }
41
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:eae900d8ae5b3716efa7f443fcc692aeb5a7f01f72581835c03c8c495f9eda10
3
+ size 32334434
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e73d2a763b3420ca9fb0ec8e45e094e54d8e1e3add4b2e5073da7d38f0be4613
3
+ size 32522537