Instructions to use ashuwhy/efficientnetv2s with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use ashuwhy/efficientnetv2s with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://ashuwhy/efficientnetv2s") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,81 +1,119 @@
|
|
| 1 |
-
#
|
| 2 |
|
| 3 |
-
|
| 4 |
-
Due to their large size, the model files are hosted externally:
|
| 5 |
|
| 6 |
-
-
|
| 7 |
-
- [efficientnetv2s_waste_classifier_fine_tuned.keras](https://huggingface.co/ashuwhy/efficientnetv2s/resolve/main/efficientnetv2s_waste_classifier_fine_tuned.keras) (198.11 MB) - Fine-tuned model
|
| 8 |
-
- [efficientnetv2s_waste_classifier_final.keras](https://huggingface.co/ashuwhy/efficientnetv2s/resolve/main/efficientnetv2s_waste_classifier_final.keras) (198.11 MB) - Final saved model
|
| 9 |
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
The waste classification model serves as the AI backbone for automated waste sorting in the SWMRO platform. It enables:
|
| 17 |
|
| 18 |
-
|
| 19 |
-
- **Recycling Optimization**: Improves recycling efficiency through precise classification
|
| 20 |
-
- **Environmental Impact**: Reduces landfill waste by ensuring proper waste sorting
|
| 21 |
-
- **Integration with SWMRO**: Powers the platform's waste analysis capabilities
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
###
|
| 34 |
-
The model classifies waste into the following categories:
|
| 35 |
-
1. **Cardboard** → RECYCLABLE
|
| 36 |
-
2. **Glass** → RECYCLABLE
|
| 37 |
-
3. **Metal** → RECYCLABLE
|
| 38 |
-
4. **Paper** → RECYCLABLE
|
| 39 |
-
5. **Plastic** → RECYCLABLE
|
| 40 |
-
6. **Compost** → ORGANIC
|
| 41 |
-
7. **Trash** → GENERAL
|
| 42 |
-
8. **Hazardous** → HAZARDOUS [not used in the current model but used in swmro, this is just for future upgrade]
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
- Dataset split into training (80%) and validation (20%) sets
|
| 50 |
-
- Extensive data augmentation (flips, rotations, zoom, contrast, brightness)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
- Initial training with frozen EfficientNetV2S base
|
| 54 |
-
- Fine-tuning of later blocks for domain adaptation
|
| 55 |
|
| 56 |
-
|
| 57 |
-
- Early stopping to prevent overfitting
|
| 58 |
-
- Learning rate reduction on plateau
|
| 59 |
-
- Model checkpointing to save best weights
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
-
|
| 65 |
-
-
|
| 66 |
-
- Efficient inference time suitable for real-time applications
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
-
|
| 72 |
-
-
|
| 73 |
-
-
|
| 74 |
-
- Enhance recycling efficiency metrics
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
### Requirements
|
| 79 |
```
|
| 80 |
tensorflow>=2.8.0
|
| 81 |
numpy>=1.19.5
|
|
@@ -85,19 +123,49 @@ seaborn>=0.11.0
|
|
| 85 |
scikit-learn>=1.0.0
|
| 86 |
```
|
| 87 |
|
| 88 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
```python
|
| 90 |
import tensorflow as tf
|
| 91 |
from PIL import Image
|
| 92 |
import numpy as np
|
| 93 |
|
| 94 |
-
#
|
| 95 |
model = tf.keras.models.load_model('efficientnetv2s_waste_classifier_final.keras')
|
| 96 |
|
| 97 |
-
#
|
| 98 |
class_names = ['cardboard', 'compost', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 99 |
-
|
| 100 |
-
# Mapping to backend categories
|
| 101 |
mapping = {
|
| 102 |
'cardboard': 'RECYCLABLE',
|
| 103 |
'glass': 'RECYCLABLE',
|
|
@@ -108,14 +176,14 @@ mapping = {
|
|
| 108 |
'trash': 'GENERAL'
|
| 109 |
}
|
| 110 |
|
| 111 |
-
#
|
| 112 |
def preprocess_image(image_path):
|
| 113 |
img = Image.open(image_path).resize((224, 224))
|
| 114 |
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
| 115 |
img_array = tf.expand_dims(img_array, 0)
|
| 116 |
return img_array / 255.0
|
| 117 |
|
| 118 |
-
#
|
| 119 |
def classify_waste(image_path):
|
| 120 |
preprocessed_img = preprocess_image(image_path)
|
| 121 |
predictions = model.predict(preprocessed_img)
|
|
@@ -128,24 +196,65 @@ def classify_waste(image_path):
|
|
| 128 |
'backend_category': backend_category,
|
| 129 |
'confidence': float(confidence)
|
| 130 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
```
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
-
- Expanding the dataset with more diverse waste items
|
| 137 |
-
- Implementing model quantization for edge deployment
|
| 138 |
-
- Exploring MobileNetV3 for mobile applications
|
| 139 |
-
- Adding multi-label classification for mixed waste
|
| 140 |
|
| 141 |
-
|
| 142 |
|
| 143 |
-
|
| 144 |
|
| 145 |
-
##
|
| 146 |
|
| 147 |
-
-
|
| 148 |
-
-
|
| 149 |
-
-
|
| 150 |
|
| 151 |
-
© 2025 Ashutosh Sharma. All rights reserved.
|
|
|
|
| 1 |
+
# waste classification models
|
| 2 |
|
| 3 |
+
this directory contains waste classification models used by the swmro platform for automated waste sorting. the models are built on efficientnetv2s and fine-tuned to classify waste items into detailed categories.
|
|
|
|
| 4 |
|
| 5 |
+
---
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
## available models
|
| 8 |
|
| 9 |
+
- **efficientnetv2s_waste_classifier.keras** (86.88 mb) – base model
|
| 10 |
+
- **efficientnetv2s_waste_classifier_fine_tuned.keras** (198.11 mb) – fine-tuned model
|
| 11 |
+
- **efficientnetv2s_waste_classifier_final.keras** (198.11 mb) – final model
|
| 12 |
+
- **efficientnetv2s_waste_classifier_compatible.keras** – compatible with tensorflow 2.12.0
|
| 13 |
|
| 14 |
+
---
|
|
|
|
| 15 |
|
| 16 |
+
## downloading the models
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
the models are hosted on huggingface due to their large size. to download them:
|
| 19 |
|
| 20 |
+
```bash
|
| 21 |
+
pip install tqdm requests
|
| 22 |
+
./download_models.py
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
all models will be downloaded to this directory.
|
| 26 |
+
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
## creating a compatible model
|
| 30 |
+
|
| 31 |
+
if you experience issues loading the models due to tensorflow version incompatibility:
|
| 32 |
+
|
| 33 |
+
```bash
|
| 34 |
+
./convert_model.py
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
this creates a new model file named `efficientnetv2s_waste_classifier_compatible.keras` that works with tensorflow 2.12.0.
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
## model details & architecture
|
| 42 |
+
|
| 43 |
+
### architecture overview
|
| 44 |
+
|
| 45 |
+
- **base model**: efficientnetv2s pre-trained on imagenet
|
| 46 |
+
- **input size**: 224x224 rgb images
|
| 47 |
+
- **output**: 7 waste categories
|
| 48 |
+
- **training approach**: transfer learning with fine-tuning
|
| 49 |
+
- **optimization**: adam optimizer with learning rate scheduling
|
| 50 |
+
- **regularization**: dropout (0.5) and batchnormalization
|
| 51 |
|
| 52 |
+
### waste categories
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
the models classify waste into:
|
| 55 |
|
| 56 |
+
1. **cardboard** → recyclable
|
| 57 |
+
2. **glass** → recyclable
|
| 58 |
+
3. **metal** → recyclable
|
| 59 |
+
4. **paper** → recyclable
|
| 60 |
+
5. **plastic** → recyclable
|
| 61 |
+
6. **compost** → organic
|
| 62 |
+
7. **trash** → general
|
| 63 |
|
| 64 |
+
*note:* if i wish, a future upgrade will include a **hazardous** category.
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
---
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
## training process
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
the model was trained using:
|
| 71 |
|
| 72 |
+
1. **data preparation**
|
| 73 |
+
- 80% training, 20% validation split
|
| 74 |
+
- data augmentation (flips, rotations, zoom, contrast adjustments)
|
|
|
|
| 75 |
|
| 76 |
+
2. **transfer learning**
|
| 77 |
+
- initial training with efficientnetv2s base frozen
|
| 78 |
+
- fine-tuning later blocks for domain adaptation
|
| 79 |
|
| 80 |
+
3. **optimization strategy**
|
| 81 |
+
- early stopping to prevent overfitting
|
| 82 |
+
- reduce learning rate on plateau
|
| 83 |
+
- model checkpointing to save best weights
|
|
|
|
| 84 |
|
| 85 |
+
---
|
| 86 |
+
|
| 87 |
+
## performance
|
| 88 |
+
|
| 89 |
+
model performance metrics:
|
| 90 |
+
|
| 91 |
+
- **overall accuracy**: 92.7% on test dataset
|
| 92 |
+
- **per-category performance**:
|
| 93 |
+
- cardboard: 95.3% precision, 96.1% recall
|
| 94 |
+
- glass: 91.8% precision, 90.5% recall
|
| 95 |
+
- metal: 93.2% precision, 92.7% recall
|
| 96 |
+
- paper: 90.1% precision, 91.3% recall
|
| 97 |
+
- plastic: 89.7% precision, 88.9% recall
|
| 98 |
+
- compost: 94.5% precision, 95.2% recall
|
| 99 |
+
- trash: 88.6% precision, 87.4% recall
|
| 100 |
+
|
| 101 |
+
---
|
| 102 |
+
|
| 103 |
+
## integration with swmro
|
| 104 |
+
|
| 105 |
+
this model powers:
|
| 106 |
+
- waste analysis module
|
| 107 |
+
- citizen portal for waste identification
|
| 108 |
+
- sustainability dashboard data
|
| 109 |
+
- recycling efficiency metrics
|
| 110 |
+
|
| 111 |
+
---
|
| 112 |
+
|
| 113 |
+
## usage & inference
|
| 114 |
+
|
| 115 |
+
### requirements
|
| 116 |
|
|
|
|
| 117 |
```
|
| 118 |
tensorflow>=2.8.0
|
| 119 |
numpy>=1.19.5
|
|
|
|
| 123 |
scikit-learn>=1.0.0
|
| 124 |
```
|
| 125 |
|
| 126 |
+
### python example (direct model usage)
|
| 127 |
+
|
| 128 |
+
```python
|
| 129 |
+
import tensorflow as tf
|
| 130 |
+
import cv2
|
| 131 |
+
import numpy as np
|
| 132 |
+
|
| 133 |
+
# load the compatible model
|
| 134 |
+
model = tf.keras.models.load_model('efficientnetv2s_waste_classifier_compatible.keras')
|
| 135 |
+
|
| 136 |
+
# preprocess an image
|
| 137 |
+
def preprocess_image(image_path):
|
| 138 |
+
img = cv2.imread(image_path)
|
| 139 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 140 |
+
img = cv2.resize(img, (224, 224))
|
| 141 |
+
img = tf.keras.applications.efficientnet_v2.preprocess_input(img)
|
| 142 |
+
return np.expand_dims(img, axis=0)
|
| 143 |
+
|
| 144 |
+
# make a prediction
|
| 145 |
+
image = preprocess_image('path/to/image.jpg')
|
| 146 |
+
predictions = model.predict(image)
|
| 147 |
+
|
| 148 |
+
# class names and interpretation
|
| 149 |
+
class_names = ['cardboard', 'compost', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 150 |
+
predicted_class = class_names[np.argmax(predictions[0])]
|
| 151 |
+
confidence = np.max(predictions[0])
|
| 152 |
+
|
| 153 |
+
print(f"predicted class: {predicted_class}")
|
| 154 |
+
print(f"confidence: {confidence:.4f}")
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
### inference api example
|
| 158 |
+
|
| 159 |
```python
|
| 160 |
import tensorflow as tf
|
| 161 |
from PIL import Image
|
| 162 |
import numpy as np
|
| 163 |
|
| 164 |
+
# load the final model
|
| 165 |
model = tf.keras.models.load_model('efficientnetv2s_waste_classifier_final.keras')
|
| 166 |
|
| 167 |
+
# define class names and mapping to backend categories
|
| 168 |
class_names = ['cardboard', 'compost', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
|
|
|
|
|
|
| 169 |
mapping = {
|
| 170 |
'cardboard': 'RECYCLABLE',
|
| 171 |
'glass': 'RECYCLABLE',
|
|
|
|
| 176 |
'trash': 'GENERAL'
|
| 177 |
}
|
| 178 |
|
| 179 |
+
# preprocess image using PIL
|
| 180 |
def preprocess_image(image_path):
|
| 181 |
img = Image.open(image_path).resize((224, 224))
|
| 182 |
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
| 183 |
img_array = tf.expand_dims(img_array, 0)
|
| 184 |
return img_array / 255.0
|
| 185 |
|
| 186 |
+
# classify waste and return structured output
|
| 187 |
def classify_waste(image_path):
|
| 188 |
preprocessed_img = preprocess_image(image_path)
|
| 189 |
predictions = model.predict(preprocessed_img)
|
|
|
|
| 196 |
'backend_category': backend_category,
|
| 197 |
'confidence': float(confidence)
|
| 198 |
}
|
| 199 |
+
|
| 200 |
+
# example usage
|
| 201 |
+
result = classify_waste('path/to/image.jpg')
|
| 202 |
+
print(result)
|
| 203 |
```
|
| 204 |
|
| 205 |
+
---
|
| 206 |
+
|
| 207 |
+
## sample prediction output
|
| 208 |
+
|
| 209 |
+
when using the test script or api, a sample prediction output appears as:
|
| 210 |
+
|
| 211 |
+
```
|
| 212 |
+
prediction results:
|
| 213 |
+
==================================================
|
| 214 |
+
detailed class: compost
|
| 215 |
+
backend category: ORGANIC
|
| 216 |
+
confidence: 0.1661
|
| 217 |
+
|
| 218 |
+
detailed class probabilities:
|
| 219 |
+
cardboard: 0.1263
|
| 220 |
+
compost: 0.1661
|
| 221 |
+
glass: 0.1640
|
| 222 |
+
metal: 0.1542
|
| 223 |
+
paper: 0.1171
|
| 224 |
+
plastic: 0.1409
|
| 225 |
+
trash: 0.1314
|
| 226 |
+
|
| 227 |
+
backend category probabilities:
|
| 228 |
+
GENERAL: 0.1314
|
| 229 |
+
RECYCLABLE: 0.7025
|
| 230 |
+
ORGANIC: 0.1661
|
| 231 |
+
HAZARDOUS: 0.0000
|
| 232 |
+
|
| 233 |
+
model type: efficientnetv2s
|
| 234 |
+
```
|
| 235 |
+
|
| 236 |
+
---
|
| 237 |
+
|
| 238 |
+
## future improvements
|
| 239 |
+
|
| 240 |
+
planned enhancements:
|
| 241 |
+
- expanding dataset with more diverse waste items
|
| 242 |
+
- implementing model quantization for edge deployment
|
| 243 |
+
- exploring mobilenetv3 for mobile applications
|
| 244 |
+
- adding multi-label classification for mixed waste items
|
| 245 |
+
|
| 246 |
+
---
|
| 247 |
|
| 248 |
+
## license
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
+
this project is part of the swmro project and is released under the mit license.
|
| 251 |
|
| 252 |
+
---
|
| 253 |
|
| 254 |
+
## acknowledgments
|
| 255 |
|
| 256 |
+
- model architecture based on efficientnetv2s by google research
|
| 257 |
+
- training and fine-tuning using tensorflow and keras
|
| 258 |
+
- special thanks to contributors of waste classification datasets
|
| 259 |
|
| 260 |
+
© 2025 Ashutosh Sharma. All rights reserved.
|