Skip to content

theblag/Microplastic-detection-yolo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microplastics Detection using YOLOv8

A deep learning project for automated detection and localization of microplastics in images using YOLOv8 object detection models.

Overview

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.

Features

  • 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

Requirements

  • Python 3.9 or higher
  • CUDA 11.8+ (for GPU acceleration; optional but recommended)
  • pip or conda package manager

Installation

Clone the repository and install dependencies:

git clone <repository-url>
cd Microplastics_Detection
pip install -r requirements.txt

Dependencies

  • ultralytics>=8.0.0 — YOLOv8 framework
  • torch>=2.0.0 — PyTorch deep learning framework
  • torchvision>=0.15.0 — Computer vision utilities
  • opencv-python>=4.7.0 — Image processing
  • numpy>=1.24.0 — Numerical operations
  • pandas>=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/cu118

Project Structure

Microplastics_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

Usage

Training

Open and run main.ipynb sequentially:

  1. Cell 1-2: Load YOLOv8 pretrained model
  2. Cell 3: Verify GPU availability
  3. 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.

Inference

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 scores

Label Creation

Use make-labels.ipynb to create or review training labels for new datasets.

Model Information

  • 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)

Dataset

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).

Data Source

This project uses the Microplastic Dataset for Computer Vision from Kaggle:

To use this dataset, download it from Kaggle and organize it in the Dataset/ directory as shown in the project structure above.

Configuration

Edit data.yaml to modify dataset paths and class definitions:

train: Dataset/train/images
val: Dataset/valid/images
nc: 1
names:
  0: Microplastic

Results

After training, evaluation metrics are stored in runs/detect/train/results.csv. Visualizations and prediction outputs are saved in runs/detect/predict/.

Best Practices

  1. Use best.pt for inference — Always load best.pt for predictions, not last.pt
  2. Avoid retraining — Save and reuse trained weights; retraining is unnecessary for inference
  3. Batch predictions — Use the same project and exist_ok=True parameters to avoid creating duplicate output folders
  4. Adjust confidence threshold — Tune the conf parameter based on your precision/recall requirements

References

About

A deep learning project that uses YOLOv8 for detecting and classifying microplastics in images. The model is trained on a custom dataset with labeled images and can perform inference on test data with configurable confidence thresholds. Includes training pipeline with GPU support, dataset management, and evaluation capabilities.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors