Skip to content

haraksduggal/Microplastics_detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ”ฌ Microplastics Detection

Machine Learning-Based Detection of Marine Microplastics Using Attention-Enhanced Deep Learning

Python PyTorch TensorFlow License

An attention-enhanced deep learning framework for automated microplastic detection
with implications for human health and environmental sustainability


SDG 3 SDG 6 SDG 14

๐Ÿ“– About

Marine microplastic pollution is one of the most pressing environmental and public health challenges of the 21st century. These microscopic plastic fragments (<5mm) have been detected in marine ecosystems, drinking water, food chains, and even human biological tissues.

Traditional identification methods like FTIR and Raman Spectroscopy are expensive, slow, and require specialized expertise. This project presents an automated microplastic detection framework using deep learning and transfer learning, making large-scale environmental monitoring feasible.

โœจ Key Contributions

  • CBAM-Enhanced ResNet50 โ€” Integrates Convolutional Block Attention Module for focused feature extraction on microplastic particles
  • Multi-Architecture Comparison โ€” Comprehensive evaluation across 4 model implementations
  • Cross-Framework Validation โ€” PyTorch and TensorFlow implementations on identical data
  • Grad-CAM Interpretability โ€” Visual explanations confirming attention on particle-relevant regions
  • Sustainability Aligned โ€” Contributes to UN SDGs 3, 6, and 14

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Input   โ”‚โ”€โ”€โ–ถโ”‚  Conv1   โ”‚โ”€โ”€โ–ถโ”‚ Layer1  โ”‚โ”€โ”€โ–ถโ”‚ Layer2  โ”‚โ”€โ”€โ–ถโ”‚ Layer3  โ”‚
โ”‚ 224ร—224  โ”‚   โ”‚ BN+ReLU  โ”‚   โ”‚(frozen) โ”‚   โ”‚(frozen) โ”‚   โ”‚ 1024ch  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜
                                                               โ”‚
                                                          โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”
                                                          โ”‚  CBAM   โ”‚
                                                          โ”‚ 1024ch  โ”‚
                                                          โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜
                                                               โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”
โ”‚ Output  โ”‚โ—€โ”€โ”€โ”‚ FC Head  โ”‚โ—€โ”€โ”€โ”‚ AvgPool โ”‚โ—€โ”€โ”€โ”‚  CBAM   โ”‚โ—€โ”€โ”€โ”‚ Layer4  โ”‚
โ”‚ 2 class โ”‚   โ”‚2048โ†’512โ†’2โ”‚   โ”‚ Global  โ”‚   โ”‚ 2048ch  โ”‚   โ”‚ 2048ch  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

CBAM Module: Feature Map F โ†’ Channel Attention โ†’ Spatial Attention โ†’ Refined F''

๐Ÿ“Š Results

Model Comparison

Model Framework Test Acc. F1-Score AUC-ROC Parameters Final Loss
ResNet50 PyTorch 100.00% 1.00 1.00 25.0M 0.1099
ResNet50 + CBAM PyTorch 100.00% 1.00 1.00 25.2M 0.1014
EfficientNet-B0 PyTorch 100.00% 1.00 1.00 4.7M 0.1159
ResNet50 (TF) TensorFlow 95.25% 0.95 0.97 23.6M 0.2700

Key Findings

๐ŸŽฏ ResNet50 + CBAM achieves the lowest training loss (0.1014) โ€” indicating the most confident, best-calibrated predictions

โšก EfficientNet-B0 matches performance with 5.4ร— fewer parameters โ€” ideal for edge deployment

๐Ÿ”„ Cross-framework validation reveals implementation-level impact on model performance


๐Ÿ“ Project Structure

Microplastics_detection/
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ dataset/
โ”‚   โ”œโ”€โ”€ train/
โ”‚   โ”‚   โ”œโ”€โ”€ microplastic/          # 1,600 images
โ”‚   โ”‚   โ””โ”€โ”€ non_microplastic/      # 1,600 images
โ”‚   โ”œโ”€โ”€ val/
โ”‚   โ”‚   โ”œโ”€โ”€ microplastic/          # 200 images
โ”‚   โ”‚   โ””โ”€โ”€ non_microplastic/      # 200 images
โ”‚   โ””โ”€โ”€ test/
โ”‚       โ”œโ”€โ”€ microplastic/          # 200 images
โ”‚       โ””โ”€โ”€ non_microplastic/      # 200 images
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ results/
โ”‚   โ”œโ”€โ”€ resnet50_cbam_best.pth     # Best model weights
โ”‚   โ”œโ”€โ”€ training_curves.png        # Loss & accuracy plots
โ”‚   โ”œโ”€โ”€ confusion_matrix.png       # Test set confusion matrix
โ”‚   โ”œโ”€โ”€ roc_curve.png              # ROC curve with AUC
โ”‚   โ”œโ”€โ”€ classification_report.txt  # Precision, recall, F1
โ”‚   โ””โ”€โ”€ gradcam_*.png              # Grad-CAM heatmaps
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ results_vanilla/            # Vanilla ResNet50 results
โ”œโ”€โ”€ ๐Ÿ“‚ results_efficientnet/       # EfficientNet-B0 results
โ”‚
โ”œโ”€โ”€ ๐Ÿ augment_mp.py               # Image augmentation (781 โ†’ 2000)
โ”œโ”€โ”€ ๐Ÿ split_dataset.py            # Train/val/test split
โ”œโ”€โ”€ ๐Ÿ train_resnet50_cbam.py      # ResNet50 + CBAM training
โ”œโ”€โ”€ ๐Ÿ train_resnet50_vanilla.py   # Vanilla ResNet50 training
โ”œโ”€โ”€ ๐Ÿ train_efficientnet.py       # EfficientNet-B0 training
โ”‚
โ”œโ”€โ”€ ๐Ÿ“„ microplastic_ieee_paper.tex # IEEE format research paper
โ””โ”€โ”€ ๐Ÿ“„ README.md

๐Ÿš€ Getting Started

Prerequisites

pip install torch torchvision matplotlib scikit-learn seaborn tqdm pillow numpy

Step 1 โ€” Augment Microplastic Images

Expand 781 original microplastic images to 2,000 using microscopy-appropriate augmentations:

# Update INPUT_DIR in augment_mp.py to your image folder
python augment_mp.py

Augmentations applied: rotation, flipping, brightness/contrast jitter, Gaussian blur, noise injection, random crop-resize, sharpening (2โ€“4 per image).

Step 2 โ€” Split Dataset

Create balanced train/val/test splits (80/10/10):

# Update paths in split_dataset.py
python split_dataset.py

Step 3 โ€” Train Models

# Train ResNet50 + CBAM (primary model)
python train_resnet50_cbam.py

# Train vanilla ResNet50 (baseline)
python train_resnet50_vanilla.py

# Train EfficientNet-B0 (efficiency comparison)
python train_efficientnet.py

Training time: ~25 min per model on Apple M-series (MPS) | ~15 min on NVIDIA GPU

Step 4 โ€” View Results

All results are automatically saved to their respective results/ directories:

  • Training curves (loss & accuracy)
  • Confusion matrices
  • ROC curves with AUC scores
  • Classification reports
  • Grad-CAM heatmaps (ResNet50 models)

๐Ÿง  CBAM: How It Works

The Convolutional Block Attention Module applies two sequential attention mechanisms:

Channel Attention โ€” "What features to focus on"

M_c(F) = ฯƒ(MLP(AvgPool(F)) + MLP(MaxPool(F)))

Spatial Attention โ€” "Where to focus"

M_s(F') = ฯƒ(Conv7ร—7([AvgPool(F'); MaxPool(F')]))

CBAM adds only ~213K parameters (+0.9%) to ResNet50 while providing:

  • More focused attention on particle regions
  • Faster convergence during training
  • Better-calibrated prediction confidence

๐Ÿ” Grad-CAM Visualization

Grad-CAM heatmaps reveal where the model looks when making predictions:

CBAM Model Vanilla Model
Attention Pattern Concentrated on particles Diffuse across image
Background Suppression Strong Weak
Scientific Validity Focuses on morphology Relies on context

๐Ÿ“‹ Dataset Details

Property Value
Total Images 4,000 (2,000 per class)
Original Microplastic 781 images (augmented to 2,000)
Original Non-Microplastic 5,000 images (subsampled to 2,000)
Image Size 224 ร— 224 pixels
Microplastic Source Laboratory petri dish captures
Non-Microplastic Source IFCB flow cytometry imaging
Split Ratio 80% train / 10% val / 10% test

โš™๏ธ Training Configuration

Hyperparameter PyTorch Models TensorFlow Model
Epochs 30 20
Optimizer Adam Adam
Learning Rate 1ร—10โปโด 1ร—10โปโด
Weight Decay 1ร—10โปโด โ€”
Scheduler Cosine Annealing ReduceLROnPlateau
Mixup ฮฑ 0.2 โ€”
Loss Weighted CE Weighted CE
Hardware Apple MPS NVIDIA T4 (Colab)

๐Ÿ“ Research Paper

The complete IEEE-format research paper is included as microplastic_ieee_paper.tex. It covers:

  • Comprehensive literature review (31 references, Chicago style)
  • Full methodology with CBAM mathematical formulation
  • Four-model comparative analysis
  • Training convergence and parameter efficiency study
  • Grad-CAM interpretability analysis
  • Cross-framework (PyTorch vs TensorFlow) validation
  • Implications for human health and UN SDGs
  • Honest limitations and 8 future work directions

๐Ÿ”ฎ Future Work

  • ๐Ÿ”ฌ Same-domain validation with unified imaging protocols
  • ๐Ÿท๏ธ Multi-class morphotype classification (fiber, fragment, film, pellet, foam)
  • ๐Ÿ“ก Spectral-visual data fusion combining CNN features with FTIR/Raman data
  • ๐Ÿ“ฑ Edge deployment via quantization and pruning for portable devices
  • ๐ŸŽฏ Object detection using YOLOv8 for particle-level localization and counting
  • ๐ŸŽจ Generative augmentation using GANs/diffusion models for synthetic training data
  • ๐Ÿงช Polymer identification through multi-task learning
  • ๐Ÿ“ˆ Longitudinal monitoring integration with automated sampling stations

๐Ÿ›ก๏ธ Limitations

The microplastic and non-microplastic images originate from distinct imaging modalities (petri dish photography vs. flow cytometry), which may allow models to leverage imaging-domain features rather than particle morphology alone. This is transparently documented in the research paper. The framework is validated as a laboratory pre-screening tool, with same-domain evaluation identified as a priority for future work.


๐Ÿ“š Key References

  1. Woo et al. (2018) โ€” CBAM: Convolutional Block Attention Module โ€” ECCV
  2. He et al. (2016) โ€” Deep Residual Learning โ€” CVPR
  3. Tan & Le (2019) โ€” EfficientNet โ€” ICML
  4. Selvaraju et al. (2017) โ€” Grad-CAM โ€” ICCV
  5. Jambeck et al. (2015) โ€” Plastic waste inputs into the ocean โ€” Science

Full bibliography with 31 Chicago-style references available in the research paper.


๐Ÿ‘ฉโ€๐Ÿ’ป Author

Haraks Duggal, Harpreet Singh, Jovan Kooner

Built with ๐Ÿงช science and ๐Ÿ’ป deep learning for a cleaner planet

If this project helped you, consider giving it a โญ

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages