README.md
| 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 tf_efficientnetv2_s.in21k_ft_in1k |
| 13 | |
| 14 | A EfficientNet-v2 image classification model. Trained on ImageNet-21k and fine-tuned on ImageNet-1k in Tensorflow 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): 21.5 |
| 21 | - GMACs: 5.4 |
| 22 | - Activations (M): 22.7 |
| 23 | - Image size: train = 300 x 300, test = 384 x 384 |
| 24 | - **Papers:** |
| 25 | - EfficientNetV2: Smaller Models and Faster Training: https://arxiv.org/abs/2104.00298 |
| 26 | - **Dataset:** ImageNet-1k |
| 27 | - **Pretrain Dataset:** ImageNet-21k |
| 28 | - **Original:** https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet |
| 29 | |
| 30 | ## Model Usage |
| 31 | ### Image Classification |
| 32 | ```python |
| 33 | from urllib.request import urlopen |
| 34 | from PIL import Image |
| 35 | import timm |
| 36 | |
| 37 | img = Image.open(urlopen( |
| 38 | 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' |
| 39 | )) |
| 40 | |
| 41 | model = timm.create_model('tf_efficientnetv2_s.in21k_ft_in1k', pretrained=True) |
| 42 | model = model.eval() |
| 43 | |
| 44 | # get model specific transforms (normalization, resize) |
| 45 | data_config = timm.data.resolve_model_data_config(model) |
| 46 | transforms = timm.data.create_transform(**data_config, is_training=False) |
| 47 | |
| 48 | output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1 |
| 49 | |
| 50 | top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5) |
| 51 | ``` |
| 52 | |
| 53 | ### Feature Map Extraction |
| 54 | ```python |
| 55 | from urllib.request import urlopen |
| 56 | from PIL import Image |
| 57 | import timm |
| 58 | |
| 59 | img = Image.open(urlopen( |
| 60 | 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' |
| 61 | )) |
| 62 | |
| 63 | model = timm.create_model( |
| 64 | 'tf_efficientnetv2_s.in21k_ft_in1k', |
| 65 | pretrained=True, |
| 66 | features_only=True, |
| 67 | ) |
| 68 | model = model.eval() |
| 69 | |
| 70 | # get model specific transforms (normalization, resize) |
| 71 | data_config = timm.data.resolve_model_data_config(model) |
| 72 | transforms = timm.data.create_transform(**data_config, is_training=False) |
| 73 | |
| 74 | output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1 |
| 75 | |
| 76 | for o in output: |
| 77 | # print shape of each feature map in output |
| 78 | # e.g.: |
| 79 | # torch.Size([1, 24, 150, 150]) |
| 80 | # torch.Size([1, 48, 75, 75]) |
| 81 | # torch.Size([1, 64, 38, 38]) |
| 82 | # torch.Size([1, 160, 19, 19]) |
| 83 | # torch.Size([1, 256, 10, 10]) |
| 84 | |
| 85 | print(o.shape) |
| 86 | ``` |
| 87 | |
| 88 | ### Image Embeddings |
| 89 | ```python |
| 90 | from urllib.request import urlopen |
| 91 | from PIL import Image |
| 92 | import timm |
| 93 | |
| 94 | img = Image.open(urlopen( |
| 95 | 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' |
| 96 | )) |
| 97 | |
| 98 | model = timm.create_model( |
| 99 | 'tf_efficientnetv2_s.in21k_ft_in1k', |
| 100 | pretrained=True, |
| 101 | num_classes=0, # remove classifier nn.Linear |
| 102 | ) |
| 103 | model = model.eval() |
| 104 | |
| 105 | # get model specific transforms (normalization, resize) |
| 106 | data_config = timm.data.resolve_model_data_config(model) |
| 107 | transforms = timm.data.create_transform(**data_config, is_training=False) |
| 108 | |
| 109 | output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor |
| 110 | |
| 111 | # or equivalently (without needing to set num_classes=0) |
| 112 | |
| 113 | output = model.forward_features(transforms(img).unsqueeze(0)) |
| 114 | # output is unpooled, a (1, 1280, 10, 10) shaped tensor |
| 115 | |
| 116 | output = model.forward_head(output, pre_logits=True) |
| 117 | # output is a (1, num_features) shaped tensor |
| 118 | ``` |
| 119 | |
| 120 | ## Model Comparison |
| 121 | Explore the dataset and runtime metrics of this model in timm [model results](https://github.com/huggingface/pytorch-image-models/tree/main/results). |
| 122 | |
| 123 | ## Citation |
| 124 | ```bibtex |
| 125 | @inproceedings{tan2021efficientnetv2, |
| 126 | title={Efficientnetv2: Smaller models and faster training}, |
| 127 | author={Tan, Mingxing and Le, Quoc}, |
| 128 | booktitle={International conference on machine learning}, |
| 129 | pages={10096--10106}, |
| 130 | year={2021}, |
| 131 | organization={PMLR} |
| 132 | } |
| 133 | ``` |
| 134 | ```bibtex |
| 135 | @misc{rw2019timm, |
| 136 | author = {Ross Wightman}, |
| 137 | title = {PyTorch Image Models}, |
| 138 | year = {2019}, |
| 139 | publisher = {GitHub}, |
| 140 | journal = {GitHub repository}, |
| 141 | doi = {10.5281/zenodo.4414861}, |
| 142 | howpublished = {\url{https://github.com/huggingface/pytorch-image-models}} |
| 143 | } |
| 144 | ``` |
| 145 | |