This repository contains an object detection pipeline for weed detection in crops, enhanced with hyperparameter optimization using the Sea Lion Optimizer (SLO). Our approach not only automates the process of detecting weeds among crops but also leverages bio-inspired optimization to fine-tune model hyperparameters, ensuring better accuracy and robustness in real-world agricultural scenarios.
-
Faster R-CNN Backbone
- Built on top of
fasterrcnn_resnet50_fpnfrom PyTorch. - Fine-tuned for multi-class weed detection (e.g., Cheal Weed vs. Sorvu Crop).
- Built on top of
-
Sea Lion Optimizer (SLO)
- Inspired by the collective hunting strategies of sea lions.
- Used to optimize the learning rate and improve training convergence.
-
Data Augmentation (Albumentations)
- Resize (min size 800px, max size 1333px).
- Random brightness/contrast and hue/saturation variations.
- Normalization with ImageNet statistics.
-
Comprehensive Training & Evaluation Pipeline
- Training loop with multi-loss logging (
cls_loss,box_loss,obj_loss,rpn_loss). - Validation with mAP, precision, and recall using
torchmetrics. - Scheduler with multi-step decay for stable learning.
- Training loop with multi-loss logging (
-
Visualization & Testing
- Automatic bounding box visualization on test images.
- Results saved in
detections/for quick inspection.
├── dataset.py # Dataset preparation with augmentations & dataloaders
├── object_detection.py # Model training, SLO optimization, evaluation & testing
├── train_annotations.csv # Example annotations (filename, bbox, label)
├── README.md # Project documentation-
Clone the repository:
git clone https://github.com/Engr-FaizanAli/Demography-Based-Facial-Recognition-Model-Training.git cd Demography-Based-Facial-Recognition-Model-Training -
Install dependencies:
pip install -r requirements.txt
Main Libraries:
- PyTorch & Torchvision
- Albumentations
- Torchmetrics
- Mealpy (for Sea Lion Optimizer)
- Matplotlib, Seaborn, OpenCV
- Images of crops and weeds with bounding box annotations.
- Annotations in CSV format:
filename,xmin,ymin,xmax,ymax,label_id
image1.jpg,34,50,200,300,1
image2.jpg,60,90,180,270,2-
Classes:
0: Background1: Cheal Weed2: Sorvu Crop
Run the Sea Lion Optimizer to find the best learning rate:
from mealpy import SLO
from object_detection import problem
slo_model = SLO.OriginalSLO(epoch=4, pop_size=10)
g_best = slo_model.solve(problem)
print("Best LR:", g_best.solution)from object_detection import train_and_validate
train_and_validate(lr=1e-5, max_epochs=36)- Logs are stored in
aug_data_thirtysix_epoch_training_log.log. - Metrics are stored in
aug_data_thirtysix_epoch_training_results.csv.
from object_detection import test_and_save_detections
test_and_save_detections(save_dir="detections", score_threshold=0.5)Detected images with bounding boxes will be saved in the specified folder.
-
Optimized Faster R-CNN achieves high detection accuracy for weed vs crop classification.
-
Evaluation Metrics:
- mAP (Mean Average Precision)
- Precision @ IoU=0.5
- Recall
Graphs of Training Loss and Validation mAP are automatically saved as:
aug_data_thirtsix_epoch_training_metrics.png
- Precision agriculture: Reduce manual labor by automating weed identification.
- Optimized spraying: Apply herbicides only where needed.
- Yield improvement: Minimize crop damage by targeting weeds accurately.
We welcome contributions!
- Add more crop/weed classes.
- Experiment with other optimizers.
- Extend to instance segmentation (Mask R-CNN).