A deep learning project for automated detection and localization of microplastics in images using YOLOv8 object detection models.
This project leverages YOLOv8, a state-of-the-art real-time object detection framework, to identify and classify microplastics in laboratory and environmental samples. The model is trained on a custom-labeled dataset and can be deployed for inference on new images.
- Real-time detection: Processes images efficiently using YOLOv8
- Custom dataset support: Training on custom-labeled microplastics data
- GPU acceleration: Optimized for CUDA-enabled GPUs
- Reproducible pipeline: Jupyter notebooks for training, labeling, and inference
- Configurable inference: Adjustable confidence thresholds and output formats
- Python 3.9 or higher
- CUDA 11.8+ (for GPU acceleration; optional but recommended)
- pip or conda package manager
Clone the repository and install dependencies:
git clone <repository-url>
cd Microplastics_Detection
pip install -r requirements.txtultralytics>=8.0.0— YOLOv8 frameworktorch>=2.0.0— PyTorch deep learning frameworktorchvision>=0.15.0— Computer vision utilitiesopencv-python>=4.7.0— Image processingnumpy>=1.24.0— Numerical operationspandas>=2.0.0— Data manipulation
Install GPU support by specifying the CUDA version:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118Microplastics_Detection/
├── main.ipynb # Training and inference pipeline
├── make-labels.ipynb # Label creation and annotation workflow
├── data.yaml # Dataset configuration
├── Dataset/
│ ├── train/
│ │ ├── images/ # Training images
│ │ ├── labels/ # Training annotations (YOLO format)
│ │ └── _annotations.csv # Training metadata
│ ├── valid/
│ │ ├── images/ # Validation images
│ │ ├── labels/ # Validation annotations
│ │ └── _annotations.csv # Validation metadata
│ └── test/ # Test images for inference
├── runs/
│ └── detect/
│ ├── train/ # Training results and best weights
│ └── predict/ # Inference results
├── requirements.txt
├── README.md
└── .gitignore
Open and run main.ipynb sequentially:
- Cell 1-2: Load YOLOv8 pretrained model
- Cell 3: Verify GPU availability
- Cell 4: Train the model on custom dataset
model = YOLO("yolov8n.pt")
model.train(
data="data.yaml",
epochs=50,
imgsz=640,
batch=32,
device=0,
patience=10,
workers=4
)Training results are saved to runs/detect/train/. The best model weights are automatically saved to runs/detect/train/weights/best.pt.
Run predictions on new images:
from ultralytics import YOLO
# Load the trained model
model = YOLO("runs/detect/train/weights/best.pt")
# Run inference
results = model(
"Dataset/test",
conf=0.25,
save=True,
project="runs/detect",
name="predict",
exist_ok=True
)
# Access predictions
for result in results:
print(result.boxes) # Bounding boxes
print(result.conf) # Confidence scoresUse make-labels.ipynb to create or review training labels for new datasets.
- Base Model: YOLOv8n (nano variant)
- Input Size: 640×640 pixels
- Number of Classes: 1 (Microplastic)
- Training Parameters:
- Epochs: 50 (with early stopping at patience=10)
- Batch Size: 32
- Optimizer: SGD (default)
- Image Size: 640
- Device: GPU (CUDA device 0)
The dataset consists of labeled microplastics images organized as:
- Training:
Dataset/train/images/(~70% of data) with corresponding labels in YOLO format - Validation:
Dataset/valid/images/(~20% of data) used during training for performance monitoring - Test:
Dataset/test/(~10% of data) for final inference evaluation
Annotations follow the YOLO format (one .txt file per image with normalized bounding box coordinates).
This project uses the Microplastic Dataset for Computer Vision from Kaggle:
- Source: Kaggle - Microplastic Dataset for Computer Vision
- License: Check the dataset page for licensing information
To use this dataset, download it from Kaggle and organize it in the Dataset/ directory as shown in the project structure above.
Edit data.yaml to modify dataset paths and class definitions:
train: Dataset/train/images
val: Dataset/valid/images
nc: 1
names:
0: MicroplasticAfter training, evaluation metrics are stored in runs/detect/train/results.csv. Visualizations and prediction outputs are saved in runs/detect/predict/.
- Use best.pt for inference — Always load
best.ptfor predictions, notlast.pt - Avoid retraining — Save and reuse trained weights; retraining is unnecessary for inference
- Batch predictions — Use the same
projectandexist_ok=Trueparameters to avoid creating duplicate output folders - Adjust confidence threshold — Tune the
confparameter based on your precision/recall requirements