README.md
3.8 KB · 117 lines · markdown Raw
1 ---
2 tags:
3 - image-classification
4 - timm
5 - transformers
6 library_name: timm
7 license: apache-2.0
8 datasets:
9 - imagenet-1k
10 - imagenet-21k
11 ---
12 # Model card for vit_small_patch16_224.augreg_in21k_ft_in1k
13
14 A Vision Transformer (ViT) image classification model. Trained on ImageNet-21k and fine-tuned on ImageNet-1k (with additional augmentation and regularization) in JAX by paper authors, ported to PyTorch by Ross Wightman.
15
16
17 ## Model Details
18 - **Model Type:** Image classification / feature backbone
19 - **Model Stats:**
20 - Params (M): 22.1
21 - GMACs: 4.3
22 - Activations (M): 8.2
23 - Image size: 224 x 224
24 - **Papers:**
25 - How to train your ViT? Data, Augmentation, and Regularization in Vision Transformers: https://arxiv.org/abs/2106.10270
26 - An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale: https://arxiv.org/abs/2010.11929v2
27 - **Dataset:** ImageNet-1k
28 - **Pretrain Dataset:** ImageNet-21k
29 - **Original:** https://github.com/google-research/vision_transformer
30
31 ## Model Usage
32 ### Image Classification
33 ```python
34 from urllib.request import urlopen
35 from PIL import Image
36 import timm
37
38 img = Image.open(urlopen(
39 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
40 ))
41
42 model = timm.create_model('vit_small_patch16_224.augreg_in21k_ft_in1k', pretrained=True)
43 model = model.eval()
44
45 # get model specific transforms (normalization, resize)
46 data_config = timm.data.resolve_model_data_config(model)
47 transforms = timm.data.create_transform(**data_config, is_training=False)
48
49 output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1
50
51 top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)
52 ```
53
54 ### Image Embeddings
55 ```python
56 from urllib.request import urlopen
57 from PIL import Image
58 import timm
59
60 img = Image.open(urlopen(
61 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
62 ))
63
64 model = timm.create_model(
65 'vit_small_patch16_224.augreg_in21k_ft_in1k',
66 pretrained=True,
67 num_classes=0, # remove classifier nn.Linear
68 )
69 model = model.eval()
70
71 # get model specific transforms (normalization, resize)
72 data_config = timm.data.resolve_model_data_config(model)
73 transforms = timm.data.create_transform(**data_config, is_training=False)
74
75 output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor
76
77 # or equivalently (without needing to set num_classes=0)
78
79 output = model.forward_features(transforms(img).unsqueeze(0))
80 # output is unpooled, a (1, 197, 384) shaped tensor
81
82 output = model.forward_head(output, pre_logits=True)
83 # output is a (1, num_features) shaped tensor
84 ```
85
86 ## Model Comparison
87 Explore the dataset and runtime metrics of this model in timm [model results](https://github.com/huggingface/pytorch-image-models/tree/main/results).
88
89 ## Citation
90 ```bibtex
91 @article{steiner2021augreg,
92 title={How to train your ViT? Data, Augmentation, and Regularization in Vision Transformers},
93 author={Steiner, Andreas and Kolesnikov, Alexander and and Zhai, Xiaohua and Wightman, Ross and Uszkoreit, Jakob and Beyer, Lucas},
94 journal={arXiv preprint arXiv:2106.10270},
95 year={2021}
96 }
97 ```
98 ```bibtex
99 @article{dosovitskiy2020vit,
100 title={An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale},
101 author={Dosovitskiy, Alexey and Beyer, Lucas and Kolesnikov, Alexander and Weissenborn, Dirk and Zhai, Xiaohua and Unterthiner, Thomas and Dehghani, Mostafa and Minderer, Matthias and Heigold, Georg and Gelly, Sylvain and Uszkoreit, Jakob and Houlsby, Neil},
102 journal={ICLR},
103 year={2021}
104 }
105 ```
106 ```bibtex
107 @misc{rw2019timm,
108 author = {Ross Wightman},
109 title = {PyTorch Image Models},
110 year = {2019},
111 publisher = {GitHub},
112 journal = {GitHub repository},
113 doi = {10.5281/zenodo.4414861},
114 howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
115 }
116 ```
117