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+
- PedroSampaio/fruits-360
|
| 5 |
+
language:
|
| 6 |
+
- en
|
| 7 |
+
base_model:
|
| 8 |
+
- google/efficientnet-b0
|
| 9 |
+
pipeline_tag: image-classification
|
| 10 |
+
tags:
|
| 11 |
+
- pytorch
|
| 12 |
+
- torchvision
|
| 13 |
+
- efficientnet
|
| 14 |
+
- image-classification
|
| 15 |
+
- fruits
|
| 16 |
+
- fruits-360
|
| 17 |
+
- transfer-learning
|
| 18 |
+
- neptune-ai
|
| 19 |
+
widget:
|
| 20 |
+
# Example image URLs from the web - replace if you have better ones
|
| 21 |
+
- src: https://images.unsplash.com/photo-1573246123790-a64e870b8b1a?ixlib=rb-1.2.1&auto=format&fit=crop&w=640 # Example Apple
|
| 22 |
+
example_title: Apple Example
|
| 23 |
+
- src: https://images.unsplash.com/photo-1528825871115-3581a5377919?ixlib=rb-1.2.1&auto=format&fit=crop&w=640 # Example Banana
|
| 24 |
+
example_title: Banana Example
|
| 25 |
+
---
|
| 26 |
+
|
| 27 |
+
# Fruit Classifier - EfficientNet-B0 (Fruits-360 Merged)
|
| 28 |
+
|
| 29 |
+
This repository contains a fruit image classification model based on a fine-tuned **EfficientNet-B0** architecture using PyTorch and torchvision. The model was trained on the **Fruits-360 dataset**, with a modification where specific fruit variants were merged into broader categories (e.g., "Apple Red 1", "Apple 6" merged into "Apple"), resulting in **[76]** distinct classes. <-- Make sure this matches your actual class count
|
| 30 |
+
|
| 31 |
+
Training progress and metrics were tracked using **Neptune.ai**.
|
| 32 |
+
|
| 33 |
+
## Model Description
|
| 34 |
+
|
| 35 |
+
* **Architecture:** EfficientNet-B0 (pre-trained on ImageNet)
|
| 36 |
+
* **Fine-tuning Strategy:** Transfer learning. The pre-trained base model's weights were frozen, and only the final classifier layer was replaced and trained on the target dataset.
|
| 37 |
+
* **Framework:** PyTorch / torchvision
|
| 38 |
+
* **Task:** Image Classification
|
| 39 |
+
* **Dataset:** Fruits-360 (Merged Classes)
|
| 40 |
+
* **Number of Classes:** [76] <-- Make sure this matches your actual class count
|
| 41 |
+
|
| 42 |
+
## Intended Uses & Limitations
|
| 43 |
+
|
| 44 |
+
* **Intended Use:** Classifying images of fruits belonging to one of the [76] merged categories derived from the Fruits-360 dataset. Suitable for educational purposes, demonstrations, or as a baseline for further development.
|
| 45 |
+
* **Limitations:**
|
| 46 |
+
* Trained *only* on the Fruits-360 dataset. Performance on images significantly different from this dataset (e.g., different lighting, backgrounds, occlusions, fruit varieties not present) is not guaranteed.
|
| 47 |
+
* Only recognizes the specific [76] merged classes it was trained on.
|
| 48 |
+
* Performance may vary depending on input image quality.
|
| 49 |
+
* Not intended for safety-critical applications without rigorous testing and validation.
|
| 50 |
+
|
| 51 |
+
## How to Use
|
| 52 |
+
|
| 53 |
+
You can load the model and its configuration directly from the Hugging Face Hub using `torch`, `torchvision`, and `huggingface_hub`.
|
| 54 |
+
|
| 55 |
+
```python
|
| 56 |
+
import torch
|
| 57 |
+
import torchvision.models as models
|
| 58 |
+
from torchvision.models import EfficientNet_B0_Weights # Or the specific version used
|
| 59 |
+
from PIL import Image
|
| 60 |
+
from torchvision import transforms
|
| 61 |
+
import json
|
| 62 |
+
import requests
|
| 63 |
+
from huggingface_hub import hf_hub_download
|
| 64 |
+
import os
|
| 65 |
+
|
| 66 |
+
# --- 1. Define Model Loading Function ---
|
| 67 |
+
def load_model_from_hf(repo_id, model_filename="pytorch_model.bin", config_filename="config.json"):
|
| 68 |
+
"""Loads model state_dict and config from Hugging Face Hub."""
|
| 69 |
+
|
| 70 |
+
# Download config file
|
| 71 |
+
config_path = hf_hub_download(repo_id=repo_id, filename=config_filename)
|
| 72 |
+
with open(config_path, 'r') as f:
|
| 73 |
+
config = json.load(f)
|
| 74 |
+
|
| 75 |
+
num_labels = config['num_labels']
|
| 76 |
+
id2label = config['id2label'] # Load label mapping
|
| 77 |
+
|
| 78 |
+
# Instantiate the correct architecture (EfficientNet-B0)
|
| 79 |
+
# Load architecture without pre-trained weights, as we'll load our fine-tuned ones
|
| 80 |
+
model = models.efficientnet_b0(weights=None)
|
| 81 |
+
|
| 82 |
+
# Modify the classifier head to match the number of classes used during training
|
| 83 |
+
num_ftrs = model.classifier[1].in_features
|
| 84 |
+
model.classifier[1] = torch.nn.Linear(num_ftrs, num_labels)
|
| 85 |
+
|
| 86 |
+
# Download model weights
|
| 87 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
|
| 88 |
+
|
| 89 |
+
# Load the state dict
|
| 90 |
+
# Ensure map_location handles CPU/GPU as needed
|
| 91 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 92 |
+
state_dict = torch.load(model_path, map_location=device)
|
| 93 |
+
model.load_state_dict(state_dict)
|
| 94 |
+
|
| 95 |
+
model.eval() # Set to evaluation mode
|
| 96 |
+
print(f"Model loaded successfully from {repo_id} and set to evaluation mode.")
|
| 97 |
+
return model, config, id2label
|
| 98 |
+
|
| 99 |
+
# --- 2. Define Preprocessing ---
|
| 100 |
+
# Use the same transformations as validation during training
|
| 101 |
+
IMG_SIZE = (224, 224) # Standard EfficientNet input size
|
| 102 |
+
# ImageNet stats often used with EfficientNet pre-training
|
| 103 |
+
mean=[0.485, 0.456, 0.406]
|
| 104 |
+
std=[0.229, 0.224, 0.225]
|
| 105 |
+
|
| 106 |
+
preprocess = transforms.Compose([
|
| 107 |
+
transforms.Resize(IMG_SIZE),
|
| 108 |
+
transforms.ToTensor(),
|
| 109 |
+
transforms.Normalize(mean=mean, std=std),
|
| 110 |
+
])
|
| 111 |
+
|
| 112 |
+
# --- 3. Load Model ---
|
| 113 |
+
repo_id_to_load = "Bhumong/fruit-classifier-efficientnet-b0" # Your repo ID
|
| 114 |
+
model, config, id2label = load_model_from_hf(repo_id_to_load)
|
| 115 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 116 |
+
model.to(device)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# --- 4. Prepare Input Image ---
|
| 120 |
+
# Example: Load an image file (replace with your image path)
|
| 121 |
+
image_path = "path/to/your/fruit_image.jpg" # <-- REPLACE WITH YOUR IMAGE PATH
|
| 122 |
+
|
| 123 |
+
if not os.path.exists(image_path):
|
| 124 |
+
print(f"Warning: Image path not found: {image_path}")
|
| 125 |
+
print("Skipping prediction. Please provide a valid image path.")
|
| 126 |
+
input_batch = None
|
| 127 |
+
else:
|
| 128 |
+
try:
|
| 129 |
+
img = Image.open(image_path).convert("RGB")
|
| 130 |
+
input_tensor = preprocess(img)
|
| 131 |
+
# Add batch dimension (model expects batches)
|
| 132 |
+
input_batch = input_tensor.unsqueeze(0)
|
| 133 |
+
input_batch = input_batch.to(device)
|
| 134 |
+
except Exception as e:
|
| 135 |
+
print(f"Error processing image {image_path}: {e}")
|
| 136 |
+
input_batch = None
|
| 137 |
+
|
| 138 |
+
# --- 5. Make Prediction ---
|
| 139 |
+
if input_batch is not None:
|
| 140 |
+
with torch.no_grad(): # Disable gradient calculations for inference
|
| 141 |
+
output = model(input_batch)
|
| 142 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 143 |
+
top_prob, top_catid = torch.max(probabilities, dim=0)
|
| 144 |
+
|
| 145 |
+
predicted_label_index = top_catid.item()
|
| 146 |
+
# Use the id2label mapping loaded from config
|
| 147 |
+
predicted_label = id2label.get(str(predicted_label_index), "Unknown Label")
|
| 148 |
+
confidence = top_prob.item()
|
| 149 |
+
|
| 150 |
+
print(f"\nPrediction for: {os.path.basename(image_path)}")
|
| 151 |
+
print(f"Predicted Label Index: {predicted_label_index}")
|
| 152 |
+
print(f"Predicted Label: {predicted_label}")
|
| 153 |
+
print(f"Confidence: {confidence:.4f}")
|
| 154 |
+
|
| 155 |
+
|