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 | --- |
| 11 | # Model card for efficientvit_l2.r288_in1k |
| 12 | |
| 13 | An EfficientViT (MIT) image classification model. Trained on ImageNet-1k by paper authors. |
| 14 | |
| 15 | ## Model Details |
| 16 | - **Model Type:** Image classification / feature backbone |
| 17 | - **Model Stats:** |
| 18 | - Params (M): 63.7 |
| 19 | - GMACs: 11.5 |
| 20 | - Activations (M): 32.2 |
| 21 | - Image size: 288 x 288 |
| 22 | - **Papers:** |
| 23 | - EfficientViT: Multi-Scale Linear Attention for High-Resolution Dense Prediction: https://arxiv.org/abs/2205.14756 |
| 24 | - **Original:** https://github.com/mit-han-lab/efficientvit |
| 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('efficientvit_l2.r288_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 | 'efficientvit_l2.r288_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, 72, 72]) |
| 77 | # torch.Size([1, 128, 36, 36]) |
| 78 | # torch.Size([1, 256, 18, 18]) |
| 79 | # torch.Size([1, 512, 9, 9]) |
| 80 | |
| 81 | print(o.shape) |
| 82 | ``` |
| 83 | |
| 84 | ### Image Embeddings |
| 85 | ```python |
| 86 | from urllib.request import urlopen |
| 87 | from PIL import Image |
| 88 | import timm |
| 89 | |
| 90 | img = Image.open(urlopen( |
| 91 | 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' |
| 92 | )) |
| 93 | |
| 94 | model = timm.create_model( |
| 95 | 'efficientvit_l2.r288_in1k', |
| 96 | pretrained=True, |
| 97 | num_classes=0, # remove classifier nn.Linear |
| 98 | ) |
| 99 | model = model.eval() |
| 100 | |
| 101 | # get model specific transforms (normalization, resize) |
| 102 | data_config = timm.data.resolve_model_data_config(model) |
| 103 | transforms = timm.data.create_transform(**data_config, is_training=False) |
| 104 | |
| 105 | output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor |
| 106 | |
| 107 | # or equivalently (without needing to set num_classes=0) |
| 108 | |
| 109 | output = model.forward_features(transforms(img).unsqueeze(0)) |
| 110 | # output is unpooled, a (1, 512, 9, 9) shaped tensor |
| 111 | |
| 112 | output = model.forward_head(output, pre_logits=True) |
| 113 | # output is a (1, num_features) shaped tensor |
| 114 | ``` |
| 115 | |
| 116 | ## Citation |
| 117 | ```bibtex |
| 118 | @article{cai2022efficientvit, |
| 119 | title={EfficientViT: Enhanced linear attention for high-resolution low-computation visual recognition}, |
| 120 | author={Cai, Han and Gan, Chuang and Han, Song}, |
| 121 | journal={arXiv preprint arXiv:2205.14756}, |
| 122 | year={2022} |
| 123 | } |
| 124 | ``` |
| 125 | |