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 test_resnet.r160_in1k |
| 12 | |
| 13 | A very small test ResNet image classification model for testing and sanity checks. Trained on ImageNet-1k by Ross Wightman. |
| 14 | |
| 15 | ## Model Details |
| 16 | - **Model Type:** Image classification / feature backbone |
| 17 | - **Model Stats:** |
| 18 | - Params (M): 0.5 |
| 19 | - GMACs: 0.1 |
| 20 | - Activations (M): 0.6 |
| 21 | - Image size: 160 x 160 |
| 22 | - **Dataset:** ImageNet-1k |
| 23 | - **Papers:** |
| 24 | - PyTorch Image Models: https://github.com/huggingface/pytorch-image-models |
| 25 | - **Original:** https://github.com/huggingface/pytorch-image-models |
| 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('test_resnet.r160_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 | 'test_resnet.r160_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, 32, 80, 80]) |
| 77 | # torch.Size([1, 32, 40, 40]) |
| 78 | # torch.Size([1, 48, 20, 20]) |
| 79 | # torch.Size([1, 192, 10, 10]) |
| 80 | # torch.Size([1, 96, 5, 5]) |
| 81 | |
| 82 | print(o.shape) |
| 83 | ``` |
| 84 | |
| 85 | ### Image Embeddings |
| 86 | ```python |
| 87 | from urllib.request import urlopen |
| 88 | from PIL import Image |
| 89 | import timm |
| 90 | |
| 91 | img = Image.open(urlopen( |
| 92 | 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' |
| 93 | )) |
| 94 | |
| 95 | model = timm.create_model( |
| 96 | 'test_resnet.r160_in1k', |
| 97 | pretrained=True, |
| 98 | num_classes=0, # remove classifier nn.Linear |
| 99 | ) |
| 100 | model = model.eval() |
| 101 | |
| 102 | # get model specific transforms (normalization, resize) |
| 103 | data_config = timm.data.resolve_model_data_config(model) |
| 104 | transforms = timm.data.create_transform(**data_config, is_training=False) |
| 105 | |
| 106 | output = model(transforms(img).unsqueeze(0)) # output is (batch_size, num_features) shaped tensor |
| 107 | |
| 108 | # or equivalently (without needing to set num_classes=0) |
| 109 | |
| 110 | output = model.forward_features(transforms(img).unsqueeze(0)) |
| 111 | # output is unpooled, a (1, 96, 5, 5) shaped tensor |
| 112 | |
| 113 | output = model.forward_head(output, pre_logits=True) |
| 114 | # output is a (1, num_features) shaped tensor |
| 115 | ``` |
| 116 | |
| 117 | ## Model Comparison |
| 118 | ### By Top-1 |
| 119 | |
| 120 | |model |img_size|top1 |top5 |param_count| |
| 121 | |--------------------------------|--------|------|------|-----------| |
| 122 | |test_convnext3.r160_in1k |192 |54.558|79.356|0.47 | |
| 123 | |test_convnext2.r160_in1k |192 |53.62 |78.636|0.48 | |
| 124 | |test_convnext2.r160_in1k |160 |53.51 |78.526|0.48 | |
| 125 | |test_convnext3.r160_in1k |160 |53.328|78.318|0.47 | |
| 126 | |test_convnext.r160_in1k |192 |48.532|74.944|0.27 | |
| 127 | |test_nfnet.r160_in1k |192 |48.298|73.446|0.38 | |
| 128 | |test_convnext.r160_in1k |160 |47.764|74.152|0.27 | |
| 129 | |test_nfnet.r160_in1k |160 |47.616|72.898|0.38 | |
| 130 | |test_efficientnet.r160_in1k |192 |47.164|71.706|0.36 | |
| 131 | |test_efficientnet_evos.r160_in1k|192 |46.924|71.53 |0.36 | |
| 132 | |test_byobnet.r160_in1k |192 |46.688|71.668|0.46 | |
| 133 | |test_efficientnet_evos.r160_in1k|160 |46.498|71.006|0.36 | |
| 134 | |test_efficientnet.r160_in1k |160 |46.454|71.014|0.36 | |
| 135 | |test_byobnet.r160_in1k |160 |45.852|70.996|0.46 | |
| 136 | |test_efficientnet_ln.r160_in1k |192 |44.538|69.974|0.36 | |
| 137 | |test_efficientnet_gn.r160_in1k |192 |44.448|69.75 |0.36 | |
| 138 | |test_efficientnet_ln.r160_in1k |160 |43.916|69.404|0.36 | |
| 139 | |test_efficientnet_gn.r160_in1k |160 |43.88 |69.162|0.36 | |
| 140 | |test_vit2.r160_in1k |192 |43.454|69.798|0.46 | |
| 141 | |test_resnet.r160_in1k |192 |42.376|68.744|0.47 | |
| 142 | |test_vit2.r160_in1k |160 |42.232|68.982|0.46 | |
| 143 | |test_vit.r160_in1k |192 |41.984|68.64 |0.37 | |
| 144 | |test_resnet.r160_in1k |160 |41.578|67.956|0.47 | |
| 145 | |test_vit.r160_in1k |160 |40.946|67.362|0.37 | |
| 146 | |
| 147 | ## Citation |
| 148 | ```bibtex |
| 149 | @misc{rw2019timm, |
| 150 | author = {Ross Wightman}, |
| 151 | title = {PyTorch Image Models}, |
| 152 | year = {2019}, |
| 153 | publisher = {GitHub}, |
| 154 | journal = {GitHub repository}, |
| 155 | doi = {10.5281/zenodo.4414861}, |
| 156 | howpublished = {\url{https://github.com/huggingface/pytorch-image-models}} |
| 157 | } |
| 158 | ``` |
| 159 | |