Views
No views yet
1from urllib.request import urlopen
2from PIL import Image
3import timm
4
5img = Image.open(urlopen(
6 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
7))
8
9model = timm.create_model('rexnet_300.nav_in1k', pretrained=True)
10model = model.eval()
11
12# get model specific transforms (normalization, resize)
13data_config = timm.data.resolve_model_data_config(model)
14transforms = timm.data.create_transform(**data_config, is_training=False)
15
16output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
17
18top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)1from urllib.request import urlopen
2from PIL import Image
3import timm
4
5img = Image.open(urlopen(
6 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
7))
8
9model = timm.create_model(
10 'rexnet_300.nav_in1k',
11 pretrained=True,
12 features_only=True,
13)
14model = model.eval()
15
16# get model specific transforms (normalization, resize)
17data_config = timm.data.resolve_model_data_config(model)
18transforms = timm.data.create_transform(**data_config, is_training=False)
19
20output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
21
22for o in output:
23 # print shape of each feature map in output
24 # e.g.:
25 # torch.Size([1, 48, 112, 112])
26 # torch.Size([1, 116, 56, 56])
27 # torch.Size([1, 183, 28, 28])
28 # torch.Size([1, 386, 14, 14])
29 # torch.Size([1, 554, 7, 7])
30
31 print(o.shape)1from urllib.request import urlopen
2from PIL import Image
3import timm
4
5img = Image.open(urlopen(
6 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
7))
8
9model = timm.create_model(
10 'rexnet_300.nav_in1k',
11 pretrained=True,
12 num_classes=0, # remove classifier nn.Linear
13)
14model = model.eval()
15
16# get model specific transforms (normalization, resize)
17data_config = timm.data.resolve_model_data_config(model)
18transforms = timm.data.create_transform(**data_config, is_training=False)
19
20output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
21
22# or equivalently (without needing to set num_classes=0)
23
24output = model.forward_features(transforms(img).unsqueeze(0))
25# output is unpooled, a (1, 3840, 7, 7) shaped tensor
26
27output = model.forward_head(output, pre_logits=True)
28# output is a (1, num_features) shaped tensor| model | top1 | top5 | param_count | img_size | crop_pct |
|---|---|---|---|---|---|
| rexnetr_300.sw_in12k_ft_in1k | 84.53 | 97.252 | 34.81 | 288 | 1.0 |
| rexnetr_200.sw_in12k_ft_in1k | 83.164 | 96.648 | 16.52 | 288 | 1.0 |
| rexnet_300.nav_in1k | 82.772 | 96.232 | 34.71 | 224 | 0.875 |
| rexnet_200.nav_in1k | 81.652 | 95.668 | 16.37 | 224 | 0.875 |
| rexnet_150.nav_in1k | 80.308 | 95.174 | 9.73 | 224 | 0.875 |
| rexnet_130.nav_in1k | 79.478 | 94.68 | 7.56 | 224 | 0.875 |
| rexnet_100.nav_in1k | 77.832 | 93.886 | 4.8 | 224 | 0.875 |
1@misc{han2021rethinking,
2 title={Rethinking Channel Dimensions for Efficient Model Design},
3 author={Dongyoon Han and Sangdoo Yun and Byeongho Heo and YoungJoon Yoo},
4 year={2021},
5 eprint={2007.00992},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV}
8} 1@misc{rw2019timm,
2 author = {Ross Wightman},
3 title = {PyTorch Image Models},
4 year = {2019},
5 publisher = {GitHub},
6 journal = {GitHub repository},
7 doi = {10.5281/zenodo.4414861},
8 howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
9}