timm
/

Image Classification
timm
PyTorch
Safetensors
rwightman HF staff commited on
Commit
37762f6
1 Parent(s): b3623d1
Files changed (4) hide show
  1. README.md +138 -0
  2. config.json +36 -0
  3. model.safetensors +3 -0
  4. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - image-classification
4
+ - timm
5
+ library_tag: timm
6
+ license: mit
7
+ datasets:
8
+ - imagenet-1k
9
+ ---
10
+ # Model card for focalnet_base_lrf.ms_in1k
11
+
12
+ A FocalNet image classification model. Pretrained on ImageNet-1k by paper authors.
13
+
14
+
15
+ ## Model Details
16
+ - **Model Type:** Image classification / feature backbone
17
+ - **Model Stats:**
18
+ - Params (M): 88.7
19
+ - GMACs: 15.4
20
+ - Activations (M): 38.1
21
+ - Image size: 224 x 224
22
+ - **Papers:**
23
+ - Focal Modulation Networks: https://arxiv.org/abs/2203.11926
24
+ - **Original:** https://github.com/microsoft/FocalNet
25
+ - **Dataset:** ImageNet-1k
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('focalnet_base_lrf.ms_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
+ 'focalnet_base_lrf.ms_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. for focalnet_base_srf:
76
+ # torch.Size([1, 128, 56, 56])
77
+ # torch.Size([1, 256, 28, 28])
78
+ # torch.Size([1, 512, 14, 14])
79
+ # torch.Size([1, 1024, 7, 7])
80
+ print(o.shape)
81
+ ```
82
+
83
+ ### Image Embeddings
84
+ ```python
85
+ from urllib.request import urlopen
86
+ from PIL import Image
87
+ import timm
88
+
89
+ img = Image.open(urlopen(
90
+ 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
91
+ ))
92
+
93
+ model = timm.create_model(
94
+ 'focalnet_base_lrf.ms_in1k',
95
+ pretrained=True,
96
+ num_classes=0, # remove classifier nn.Linear
97
+ )
98
+ model = model.eval()
99
+
100
+ # get model specific transforms (normalization, resize)
101
+ data_config = timm.data.resolve_model_data_config(model)
102
+ transforms = timm.data.create_transform(**data_config, is_training=False)
103
+
104
+ output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
105
+
106
+ # or equivalently (without needing to set num_classes=0)
107
+
108
+ output = model.forward_features(transforms(img).unsqueeze(0))
109
+ # output is unpooled (ie.e a (batch_size, num_features, H, W) tensor)
110
+
111
+ output = model.forward_head(output, pre_logits=True)
112
+ # output is (batch_size, num_features) tensor
113
+ ```
114
+
115
+ ## Model Comparison
116
+ Explore the dataset and runtime metrics of this model in timm [model results](https://github.com/huggingface/pytorch-image-models/tree/main/results).
117
+
118
+
119
+ ## Citation
120
+ ```bibtex
121
+ @misc{yang2022focal,
122
+ title={Focal Modulation Networks},
123
+ author={Jianwei Yang and Chunyuan Li and Xiyang Dai and Jianfeng Gao},
124
+ journal={Advances in Neural Information Processing Systems (NeurIPS)},
125
+ year={2022}
126
+ }
127
+ ```
128
+ ```bibtex
129
+ @misc{rw2019timm,
130
+ author = {Ross Wightman},
131
+ title = {PyTorch Image Models},
132
+ year = {2019},
133
+ publisher = {GitHub},
134
+ journal = {GitHub repository},
135
+ doi = {10.5281/zenodo.4414861},
136
+ howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
137
+ }
138
+ ```
config.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "focalnet_base_lrf",
3
+ "num_classes": 1000,
4
+ "num_features": 1024,
5
+ "pretrained_cfg": {
6
+ "tag": "ms_in1k",
7
+ "custom_load": false,
8
+ "input_size": [
9
+ 3,
10
+ 224,
11
+ 224
12
+ ],
13
+ "fixed_input_size": false,
14
+ "interpolation": "bicubic",
15
+ "crop_pct": 0.9,
16
+ "crop_mode": "center",
17
+ "mean": [
18
+ 0.485,
19
+ 0.456,
20
+ 0.406
21
+ ],
22
+ "std": [
23
+ 0.229,
24
+ 0.224,
25
+ 0.225
26
+ ],
27
+ "num_classes": 1000,
28
+ "pool_size": [
29
+ 7,
30
+ 7
31
+ ],
32
+ "first_conv": "stem.proj",
33
+ "classifier": "head.fc",
34
+ "license": "mit"
35
+ }
36
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e1c36016eaef14dc7e4eca4bdb1b23720c56278e593cbdb4e2a34a1b23915962
3
+ size 355045572
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:aac909cd416b68fa91aa620a68568bab253e3f16a413dafd4ecea6932e0cc291
3
+ size 355181309