nailarais1/image-classifier-efficientnet
137
1---2library_name: pytorch3tags:4- pytorch5- image-classification6- flowers7- computer-vision8pipeline_tag: image-classification9language:10- en11---12 13# ๐ธ 102-Flower Image Classifier โ EfficientNet-B014 15A PyTorch EfficientNet-B0 image classification model trained to recognize **102 flower categories** from the Oxford 102 Category Flower Dataset.16 17## Model Performance18 19| Metric | Result |20| ------------------------ | --------------- |21| Architecture | EfficientNet-B0 |22| Number of classes | 102 |23| Input size | 224 ร 224 |24| Best validation accuracy | **94.38%** |25| Training epochs | 3 |26| Optimizer | AdamW |27| Learning rate | 0.001 |28 29The model was trained using transfer learning with an ImageNet-pretrained EfficientNet-B0 backbone.30 31## Dataset32 33This model was trained using the **Oxford 102 Category Flower Dataset**, created by **Maria-Elena Nilsback and Andrew Zisserman**.34 35The dataset contains 102 flower categories with variations in scale, pose, lighting, and appearance.36 37Official dataset page:38 39https://www.robots.ox.ac.uk/~vgg/data/flowers/102/40 41Please review the original dataset documentation and terms before using or redistributing dataset-derived material.42 43## Files44 45* `checkpoint.pth` โ trained PyTorch checkpoint46* `model_config.json` โ model architecture information47* `training_config.json` โ training configuration48* `class_config.json` โ exact class/index mappings49* `labels.txt` โ flower labels50* `requirements.txt` โ Python dependencies51 52## Checkpoint Contents53 54The `checkpoint.pth` file contains:55 56* `epoch`57* `model_state_dict`58* `optimizer_state_dict`59* `class_to_idx`60 61## Use the Model62 63Install the dependencies:64 65```bash66pip install torch torchvision pillow67```68 69Load the model:70 71```python72import json73import torch74import torch.nn as nn75from torchvision import models, transforms76from PIL import Image77 78checkpoint = torch.load(79 "checkpoint.pth",80 map_location="cpu",81 weights_only=False82)83 84model = models.efficientnet_b0(weights=None)85 86model.classifier[1] = nn.Linear(87 model.classifier[1].in_features,88 10289)90 91model.load_state_dict(92 checkpoint["model_state_dict"]93)94 95model.eval()96 97with open(98 "class_config.json",99 "r",100 encoding="utf-8"101) as f:102 class_config = json.load(f)103 104idx_to_class = {105 int(k): v106 for k, v in class_config["idx_to_class"].items()107}108 109transform = transforms.Compose([110 transforms.Resize(256),111 transforms.CenterCrop(224),112 transforms.ToTensor(),113 transforms.Normalize(114 [0.485, 0.456, 0.406],115 [0.229, 0.224, 0.225]116 )117])118 119image = Image.open(120 "flower.jpg"121).convert("RGB")122 123x = transform(124 image125).unsqueeze(0)126 127with torch.inference_mode():128 probabilities = torch.softmax(129 model(x),130 dim=1131 )132 133 confidence, prediction = probabilities.max(134 dim=1135 )136 137idx = prediction.item()138 139print(140 "Prediction:",141 idx_to_class[idx]142)143 144print(145 "Confidence:",146 f"{confidence.item() * 100:.2f}%"147)148```149 150## Top-5 Predictions151 152You can also get the five most likely flower categories:153 154```python155with torch.inference_mode():156 probabilities = torch.softmax(157 model(x),158 dim=1159 )160 161 values, indices = torch.topk(162 probabilities,163 k=5164 )165 166for probability, index in zip(167 values[0],168 indices[0]169):170 flower = idx_to_class[index.item()]171 confidence = probability.item() * 100172 173 print(174 f"{flower}: {confidence:.2f}%"175 )176```177 178## Training Configuration179 180The model was trained using transfer learning.181 182* Architecture: EfficientNet-B0183* Classes: 102184* Image size: 224 ร 224185* Batch size: 32186* Epochs: 3187* Optimizer: AdamW188* Learning rate: 0.001189* Loss: CrossEntropyLoss190* Scheduler: StepLR191* Mixed precision: CUDA when available192 193### Training Augmentation194 195* Random resized crop196* Random horizontal flip197* Color jitter198* ImageNet normalization199 200### Validation Preprocessing201 202* Resize to 256203* Center crop to 224204* ImageNet normalization205 206## Evaluation207 208The best validation accuracy achieved during training was:209 210**94.38%**211 212This result corresponds to the validation split used during training.213 214Performance may vary on images that differ substantially from the training data.215 216## Interactive Demo217 218An interactive Gradio application can be deployed using this model so that users can upload flower images directly through a web browser.219 220The demo can provide:221 222* Image upload223* Flower prediction224* Confidence score225* Top-5 predictions226 227## Limitations228 229This model is designed to classify images into the 102 flower categories represented in the training dataset.230 231Predictions may be less reliable when:232 233* The image does not contain a supported flower category.234* The flower is heavily obscured.235* The image is blurry or poorly illuminated.236* Multiple flowers appear in the image.237* The image differs substantially from the training distribution.238 239This model should be considered an image-classification research/demo model and not a definitive botanical identification system.240 241## Citation242 243If you use this model or the underlying dataset, please provide attribution to the original dataset authors.244 245**Maria-Elena Nilsback and Andrew Zisserman**246 247*"Automated Flower Classification over a Large Number of Classes."*248 249Proceedings of the Indian Conference on Computer Vision, Graphics and Image Processing (ICVGIP), 2008.250 251## Dataset Reference252 253Oxford 102 Category Flower Dataset:254 255https://www.robots.ox.ac.uk/~vgg/data/flowers/102/256 257## Author258 259**Naila Rais**260 261Hugging Face:262 263`nailarais1`264 265Model:266 267`nailarais1/image-classifier-efficientnet`268 269Architecture:270 271**EfficientNet-B0**272 273Best validation accuracy:274 275**94.38%**