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 efficientnet_b0.ra_in1k |
| 12 | |
| 13 | A EfficientNet image classification model. Trained on ImageNet-1k in `timm` using recipe template described below. |
| 14 | |
| 15 | Recipe details: |
| 16 | * RandAugment `RA` recipe. Inspired by and evolved from EfficientNet RandAugment recipes. Published as `B` recipe in [ResNet Strikes Back](https://arxiv.org/abs/2110.00476). |
| 17 | * RMSProp (TF 1.0 behaviour) optimizer, EMA weight averaging |
| 18 | * Step (exponential decay w/ staircase) LR schedule with warmup |
| 19 | |
| 20 | |
| 21 | ## Model Details |
| 22 | - **Model Type:** Image classification / feature backbone |
| 23 | - **Model Stats:** |
| 24 | - Params (M): 5.3 |
| 25 | - GMACs: 0.4 |
| 26 | - Activations (M): 6.7 |
| 27 | - Image size: 224 x 224 |
| 28 | - **Papers:** |
| 29 | - EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks: https://arxiv.org/abs/1905.11946 |
| 30 | - ResNet strikes back: An improved training procedure in timm: https://arxiv.org/abs/2110.00476 |
| 31 | - **Dataset:** ImageNet-1k |
| 32 | - **Original:** https://github.com/huggingface/pytorch-image-models |
| 33 | |
| 34 | ## Model Usage |
| 35 | ### Image Classification |
| 36 | ```python |
| 37 | from urllib.request import urlopen |
| 38 | from PIL import Image |
| 39 | import timm |
| 40 | |
| 41 | img = Image.open(urlopen( |
| 42 | 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' |
| 43 | )) |
| 44 | |
| 45 | model = timm.create_model('efficientnet_b0.ra_in1k', pretrained=True) |
| 46 | model = model.eval() |
| 47 | |
| 48 | # get model specific transforms (normalization, resize) |
| 49 | data_config = timm.data.resolve_model_data_config(model) |
| 50 | transforms = timm.data.create_transform(**data_config, is_training=False) |
| 51 | |
| 52 | output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1 |
| 53 | |
| 54 | top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5) |
| 55 | ``` |
| 56 | |
| 57 | ### Feature Map Extraction |
| 58 | ```python |
| 59 | from urllib.request import urlopen |
| 60 | from PIL import Image |
| 61 | import timm |
| 62 | |
| 63 | img = Image.open(urlopen( |
| 64 | 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' |
| 65 | )) |
| 66 | |
| 67 | model = timm.create_model( |
| 68 | 'efficientnet_b0.ra_in1k', |
| 69 | pretrained=True, |
| 70 | features_only=True, |
| 71 | ) |
| 72 | model = model.eval() |
| 73 | |
| 74 | # get model specific transforms (normalization, resize) |
| 75 | data_config = timm.data.resolve_model_data_config(model) |
| 76 | transforms = timm.data.create_transform(**data_config, is_training=False) |
| 77 | |
| 78 | output = model(transforms(img).unsqueeze(0)) # unsqueeze single image into batch of 1 |
| 79 | |
| 80 | for o in output: |
| 81 | # print shape of each feature map in output |
| 82 | # e.g.: |
| 83 | # torch.Size([1, 16, 112, 112]) |
| 84 | # torch.Size([1, 24, 56, 56]) |
| 85 | # torch.Size([1, 40, 28, 28]) |
| 86 | # torch.Size([1, 112, 14, 14]) |
| 87 | # torch.Size([1, 320, 7, 7]) |
| 88 | |
| 89 | print(o.shape) |
| 90 | ``` |
| 91 | |
| 92 | ### Image Embeddings |
| 93 | ```python |
| 94 | from urllib.request import urlopen |
| 95 | from PIL import Image |
| 96 | import timm |
| 97 | |
| 98 | img = Image.open(urlopen( |
| 99 | 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' |
| 100 | )) |
| 101 | |
| 102 | model = timm.create_model( |
| 103 | 'efficientnet_b0.ra_in1k', |
| 104 | pretrained=True, |
| 105 | num_classes=0, # remove classifier nn.Linear |
| 106 | ) |
| 107 | model = model.eval() |
| 108 | |
| 109 | # get model specific transforms (normalization, resize) |
| 110 | data_config = timm.data.resolve_model_data_config(model) |
| 111 | transforms = timm.data.create_transform(**data_config, is_training=False) |
| 112 | |
| 113 | output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor |
| 114 | |
| 115 | # or equivalently (without needing to set num_classes=0) |
| 116 | |
| 117 | output = model.forward_features(transforms(img).unsqueeze(0)) |
| 118 | # output is unpooled, a (1, 1280, 7, 7) shaped tensor |
| 119 | |
| 120 | output = model.forward_head(output, pre_logits=True) |
| 121 | # output is a (1, num_features) shaped tensor |
| 122 | ``` |
| 123 | |
| 124 | ## Model Comparison |
| 125 | Explore the dataset and runtime metrics of this model in timm [model results](https://github.com/huggingface/pytorch-image-models/tree/main/results). |
| 126 | |
| 127 | ## Citation |
| 128 | ```bibtex |
| 129 | @inproceedings{tan2019efficientnet, |
| 130 | title={Efficientnet: Rethinking model scaling for convolutional neural networks}, |
| 131 | author={Tan, Mingxing and Le, Quoc}, |
| 132 | booktitle={International conference on machine learning}, |
| 133 | pages={6105--6114}, |
| 134 | year={2019}, |
| 135 | organization={PMLR} |
| 136 | } |
| 137 | ``` |
| 138 | ```bibtex |
| 139 | @misc{rw2019timm, |
| 140 | author = {Ross Wightman}, |
| 141 | title = {PyTorch Image Models}, |
| 142 | year = {2019}, |
| 143 | publisher = {GitHub}, |
| 144 | journal = {GitHub repository}, |
| 145 | doi = {10.5281/zenodo.4414861}, |
| 146 | howpublished = {\url{https://github.com/huggingface/pytorch-image-models}} |
| 147 | } |
| 148 | ``` |
| 149 | ```bibtex |
| 150 | @inproceedings{wightman2021resnet, |
| 151 | title={ResNet strikes back: An improved training procedure in timm}, |
| 152 | author={Wightman, Ross and Touvron, Hugo and Jegou, Herve}, |
| 153 | booktitle={NeurIPS 2021 Workshop on ImageNet: Past, Present, and Future} |
| 154 | } |
| 155 | ``` |
| 156 | |