BioWasteNet is a lightweight deep learning architecture designed to classify materials into biodegradable and non-biodegradable categories. The model leverages a compact parameter space (169,690 trainable parameters) with a deep structure (119 layers) to achieve high accuracy while maintaining computational efficiency.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""Cloned from Git ultralytics""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Open your PyCharm IDE and click on the top left Hamburger. Look at Git, click on 'Clone'. You will see the URL bar and then paste the link in it, and then clone. https://github.com/bebedovis/ultralytics After this, you will see it open the IDEs with the Ultralytics folder in your specific path.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""How to activate?""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
- Activate the virtual environment, it would be 3.11.
- You will see at the bottom right, add Python interpreter. And install 3.11(ultralytics)
- Ensure you have installed.
- In the terminal, run this script to verify that the check .venv\script\activate python interpreter is activated.
- You will see in the terminal (.venv) PS C:\Users\path\ultralytics>
- Install libraries in the terminal. pip install ultralytics onnx onnx-tf tensorflow seaborn
- Also install. pip install torch torchaudio torchvision--index-url https://download/pytorch.org/whl/cu118
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""* Create a file yolov8n.yaml*"""""""""""""""""""""""""""""""""""""""
- Create a new file where you clone the folder. \ultralytics\cfg\models\v8\
- The file name should be yolov8n.yaml
- Paste the code in.
nc: 2 # number of classes (1 for human) depth_multiple: 0.33 # model depth multiplier width_multiple: 0.25 # layer channel multiplier ch: 3 # input channels (3 for RGB)
backbone:
- [-1, 1, Conv, [16, 3, 1]] # 0-P1/2
- [-1, 1, Conv, [32, 3, 2]] # 1-P2/4
- [-1, 1, C2f, [32, True]] # 2
- [-1, 1, Conv, [64, 3, 2]] # 3-P3/8
- [-1, 1, C2f, [64, True]] # 4
- [-1, 1, Conv, [128, 3, 2]] # 5-P4/16
- [-1, 1, C2f, [128, True]] # 6
- [-1, 1, Conv, [256, 3, 2]] # 7-P5/32
- [-1, 1, SPPF, [256, 5]] # 8
head:
- [-1, 1, nn.Upsample, [None, 2, 'nearest']] # upsample
- [[-1, 6], 1, Concat, [1]] # concat
- [-1, 1, C2f, [128, True]] # 11
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
- [[-1, 4], 1, Concat, [1]]
- [-1, 1, C2f, [64, True]] # 14
- [[-1, 11], 1, Detect, [nc]] # P3,P4,P5 detection #end
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""* Create a datasets folder*"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
- Paste a datasets folder in ultralytics folder.
- Folder name should be datasets.
- Replace accurate location of your data file. (test), (train), (data.yaml).
- structure of datasets.
datasets/ │ ├── train/ │ ├── images/ │ └── val/ │ ├── test/ # optional │ ├── images/ │ └── val/ │── val/ │ ├── images/ │ └── val/ ├──data.yaml
- Paste the code in data.yaml file.
test: C:\Users\path\Desktop\ultralytics\datasets\test\images #replace path with your path train: C:\Users\path\Desktop\ultralytics\datasets\train\images #optaional replace path with your path val: C:\Users\path\Desktop\ultralytics\datasets\val\images #replace path with your path
nc: 2 names: ['biodegradable', 'non-biodegradable']
""""""""""""""""""""""* Create a new file train_grayscale.py *"""""""""""""""""""""""""""""""""
- Open this new file and paste code.
- Edit Line 8 use you Location ......\ultralytics\ultralytics\cfg\models\v8\yolov8n.yaml
- Copy your datasets folder and paste in your clone folder. with Accurate structure.
- Edit Line 9 use you Location ......ultralytics\datasets\data.yaml
""" Train YOLOv8n on grayscale images with 2 classes. Make sure your dataset and config files are properly defined. """ from ultralytics import YOLO
model_arch = r"C:\Users\path\ultralytics\ultralytics\cfg\models\v8\yolov8n.yaml" # paste your yolov8n.yaml file location. data_yaml = r"C:\Users\path\ultralytics\datasets\data.yaml" # paste your data.yaml file location. imgsz = 64 # image size should be 640, 128, or 94 for good result epochs = 500 # epochs is must be 200 to 500 range batch_size = 16 # ch = 1 experiment_name = "yolov8n_grayscale" # replace name what you want. device = "CPU" # GPU (or 'cpu' if needed) if name == "main": model = YOLO(model_arch) model.train( data=data_yaml, imgsz=imgsz, epochs=epochs, batch=batch_size, device=device, ch=ch, name=experiment_name, verbose=True ) print(f"\n✅ Training complete. Model saved to: runs/detect/{experiment_name}/weights/best.pt\n") #end
This will create a C:\Users\your_path\ultralytics\runs\detect\yolov8n_grayscale\weights\best.pt in 449 KB file. Copy this file best.pt and paste in main folder where you had cloned.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""" Create a new file export_to_onnx.py """""""""""""""""""""""""""""""""""""""""""""""""""""""
- Paste code given blow.
- run this code.
from ultralytics import YOLO import onnx
model = YOLO(r"C:\Users....path....\runs\detect\Person_detect\weights\best.pt")
onnx_path = model.export(format="onnx", opset=12, imgsz=64)
onnx_model = onnx.load(onnx_path) onnx_model.ir_version = 9 onnx.save(onnx_model, onnx_path)
print(f"✅ ONNX model exported and fixed at: {onnx_path}")
::This will create a new file with name best.onnx
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" Create a new file onnx_to_tf.py """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
- Create new file in same directory name it onnx_to_tf
- Paste the code given blow.
- Ensure have you created onnx file in previuos step!
from onnx_tf.backend import prepare import onnx
onnx_model = onnx.load(r"C:\Users...path..\runs\detect\Person_detect\weights\best.onnx")
tf_rep = prepare(onnx_model)
tf_rep.export_graph("Bio_non_model") # change model name as you prefered name
This will creat a new folder in your path with name tf_model
""""""""""""""""""""""""""""""""""""""""""""""""""""""""" Create a new file tflite_to_INT8.py """""""""""""""""""""""""""""""""""""""""""""""""""""""""""
- Create a new file in same directory and name it tflite_to_INT8
- Ensure have your created tf_model folder using previous step!
- Paste the code given blow.
- This will create and new file with name model_int8.tflite in 257 KB
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model("Bio_non_model") converter.optimizations = [tf.lite.Optimize.DEFAULT] tflite_model = converter.convert()
with open("Bio_non_model_quant.tflite", "wb") as f: # change name as your prefered f.write(tflite_model)
print("Quantized TFLite model saved as Bio_non_model_quant.tflite")
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Now you have got model_int8.tflite in 173 KB
- Final step! Copy your model_int8.tflite file and paste in C volume for ease way to get location path.
- Open Git bash run as administrator.
- Run this command cd /c
- Now you have entered in C volume.
- Run this command. xxd -i model_int8.tflite > best.cc
- Now success fully created best.cc file in 1.1 MB
- Congratulation!
""""""""""NOW you can able to deploy in ESP32 model """"""""""""""""""""" If you want to increase size of images then where 64 replace by you preferred size.