Image Classification
Transformers
Safetensors
English
custom_vit_nano
vit
nano
patch16
img224
custom_code
Instructions to use kd13/vit-nano-patch16-224 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use kd13/vit-nano-patch16-224 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-classification", model="kd13/vit-nano-patch16-224", trust_remote_code=True) pipe("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png")# Load model directly from transformers import AutoModelForImageClassification model = AutoModelForImageClassification.from_pretrained("kd13/vit-nano-patch16-224", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -13,4 +13,164 @@ tags:
|
|
| 13 |
- nano
|
| 14 |
- patch16
|
| 15 |
- img224
|
| 16 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
- nano
|
| 14 |
- patch16
|
| 15 |
- img224
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# CustomViT-Nano: 4.24M Parameter Compact Vision Transformer
|
| 19 |
+
|
| 20 |
+
**CustomViT-Nano** is a compact, modernized Vision Transformer architecture designed for efficient ImageNet-1K image classification under a small parameter budget. With only **4.24M trainable parameters**, the model combines a lightweight convolutional stem with modern Transformer components including **2D Rotary Positional Embeddings**, **Pre-RMSNorm**, **SwiGLU feed-forward layers**, and **PyTorch SDPA attention**. The model is designed to deliver strong classification performance while remaining significantly smaller than standard large Vision Transformer baselines.
|
| 21 |
+
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
## Key Architectural Features
|
| 25 |
+
|
| 26 |
+
CustomViT-Nano modernizes a small Vision Transformer design using several efficiency-focused architectural components.
|
| 27 |
+
|
| 28 |
+
| Component | Design in CustomViT-Nano |
|
| 29 |
+
|---|---|
|
| 30 |
+
| **Patch Embedding** | Multi-stage convolutional stem instead of single large patchify projection |
|
| 31 |
+
| **Stem Activation** | GELU |
|
| 32 |
+
| **Token Layout** | 14 × 14 patch tokens + CLS token |
|
| 33 |
+
| **Normalization** | Pre-RMSNorm inside Transformer blocks |
|
| 34 |
+
| **Attention** | Multi-head attention using PyTorch scaled dot-product attention |
|
| 35 |
+
| **Position Encoding** | 2D Rotary Positional Embeddings for image patch grids |
|
| 36 |
+
| **MLP Block** | SwiGLU gated feed-forward network |
|
| 37 |
+
| **Classifier** | CLS-token classification head |
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
## ConvStem Design
|
| 42 |
+
|
| 43 |
+
Instead of directly projecting `16 × 16` image patches with one large-stride convolution, CustomViT-Nano uses a progressive convolutional stem:
|
| 44 |
+
|
| 45 |
+
```text
|
| 46 |
+
224 × 224 × 3
|
| 47 |
+
↓
|
| 48 |
+
112 × 112 × 32
|
| 49 |
+
↓
|
| 50 |
+
56 × 56 × 64
|
| 51 |
+
↓
|
| 52 |
+
28 × 28 × 128
|
| 53 |
+
↓
|
| 54 |
+
14 × 14 × 224
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
This gives the model a stronger local visual inductive bias before global Transformer reasoning.
|
| 58 |
+
|
| 59 |
+
---
|
| 60 |
+
|
| 61 |
+
## Benchmark & Evaluation
|
| 62 |
+
|
| 63 |
+
- **Evaluation Dataset**: ImageNet-1K validation set
|
| 64 |
+
- **Total Evaluation Images**: 50,000
|
| 65 |
+
- **Input Resolution**: 224 × 224
|
| 66 |
+
- **Number of Classes**: 1000
|
| 67 |
+
|
| 68 |
+
| Model | Parameters | Top-1 Accuracy | Top-5 Accuracy |
|
| 69 |
+
|---|---:|---:|---:|
|
| 70 |
+
| **CustomViT-Nano** | **4.24M** | **63.60%** | **84.93%** |
|
| 71 |
+
| **Google ViT-B/16** | **86.6M** | **80.31%** | **95.49%** |
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
## Parameter Efficiency Comparison
|
| 76 |
+
|
| 77 |
+
CustomViT-Nano is approximately **20.4× smaller** than the reference Google ViT-B/16 model.
|
| 78 |
+
|
| 79 |
+
```text
|
| 80 |
+
Google ViT-B/16: 86.6M parameters
|
| 81 |
+
CustomViT-Nano: 4.24M parameters
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
Parameter reduction:
|
| 85 |
+
|
| 86 |
+
```text
|
| 87 |
+
86.6M / 4.24M ≈ 20.4× smaller
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
Despite using only around **4.9%** of the parameters of the 86.6M ViT baseline, CustomViT-Nano achieves:
|
| 91 |
+
|
| 92 |
+
- **63.60% Top-1 Accuracy**
|
| 93 |
+
- **84.93% Top-5 Accuracy**
|
| 94 |
+
|
| 95 |
+
on the ImageNet-1K validation set.
|
| 96 |
+
|
| 97 |
+
---
|
| 98 |
+
|
| 99 |
+
## Target Use Cases & Applications
|
| 100 |
+
|
| 101 |
+
CustomViT-Nano is suitable for scenarios where a compact visual classifier is preferred over a large transformer model.
|
| 102 |
+
|
| 103 |
+
1. **Compact Image Classification**
|
| 104 |
+
Lightweight ImageNet-style classification with a transformer-based architecture.
|
| 105 |
+
|
| 106 |
+
2. **Edge & Resource-Constrained Vision**
|
| 107 |
+
Useful for environments where model size and memory footprint are important constraints.
|
| 108 |
+
|
| 109 |
+
3. **Educational Vision Transformer Research**
|
| 110 |
+
A compact architecture for studying ConvStem patch embeddings, 2D RoPE, SDPA attention, RMSNorm, and SwiGLU inside a full ImageNet-scale classifier.
|
| 111 |
+
|
| 112 |
+
4. **Backbone Experiments**
|
| 113 |
+
Can be used as a small image encoder backbone for downstream classification or transfer-learning experiments.
|
| 114 |
+
|
| 115 |
+
5. **Efficient Model Baselines**
|
| 116 |
+
Useful as a compact baseline for experiments involving distillation, pruning, quantization, or architecture search.
|
| 117 |
+
|
| 118 |
+
---
|
| 119 |
+
|
| 120 |
+
## How to Use
|
| 121 |
+
|
| 122 |
+
### Fast Inference with Hugging Face `pipeline`
|
| 123 |
+
|
| 124 |
+
```python
|
| 125 |
+
from transformers import pipeline
|
| 126 |
+
|
| 127 |
+
classifier = pipeline(
|
| 128 |
+
"image-classification",
|
| 129 |
+
model="kd13/vit-nano-patch16-224",
|
| 130 |
+
trust_remote_code=True
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
results = classifier(
|
| 134 |
+
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
for pred in results:
|
| 138 |
+
print(f"Label: {pred['label']} | Score: {pred['score']:.4f}")
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
---
|
| 142 |
+
|
| 143 |
+
## Inference with PIL Image
|
| 144 |
+
|
| 145 |
+
```python
|
| 146 |
+
from PIL import Image
|
| 147 |
+
from transformers import pipeline
|
| 148 |
+
|
| 149 |
+
classifier = pipeline(
|
| 150 |
+
"image-classification",
|
| 151 |
+
model="kd13/vit-nano-patch16-224",
|
| 152 |
+
trust_remote_code=True
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
image = Image.open("image.jpg").convert("RGB")
|
| 156 |
+
|
| 157 |
+
results = classifier(image)
|
| 158 |
+
|
| 159 |
+
for pred in results:
|
| 160 |
+
print(f"Label: {pred['label']} | Score: {pred['score']:.4f}")
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
---
|
| 164 |
+
|
| 165 |
+
## Limitations
|
| 166 |
+
|
| 167 |
+
- The model is smaller than standard ViT-B models and therefore has lower absolute ImageNet accuracy.
|
| 168 |
+
- It is optimized for image classification, not detection, segmentation, captioning, or multimodal tasks.
|
| 169 |
+
- Performance may vary on images that differ significantly from ImageNet-style natural images.
|
| 170 |
+
- For maximum accuracy, larger models or teacher-distilled variants may perform better.
|
| 171 |
+
|
| 172 |
+
---
|
| 173 |
+
|
| 174 |
+
## Disclaimer
|
| 175 |
+
|
| 176 |
+
This model is intended for research, experimentation, and efficient image-classification use cases. It should be evaluated carefully before use in production or safety-critical applications.
|