Skip to content

Engr-FaizanAli/Training-Code-for-Weeds-Detection-In-Crops

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌱 Weed Detection in Crops using Faster R-CNN & Sea Lion Optimizer

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.


🚀 Key Features

  • Faster R-CNN Backbone

    • Built on top of fasterrcnn_resnet50_fpn from PyTorch.
    • Fine-tuned for multi-class weed detection (e.g., Cheal Weed vs. Sorvu Crop).
  • 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.
  • Visualization & Testing

    • Automatic bounding box visualization on test images.
    • Results saved in detections/ for quick inspection.

📂 Project Structure

├── 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

🛠️ Installation

  1. Clone the repository:

    git clone https://github.com/Engr-FaizanAli/Demography-Based-Facial-Recognition-Model-Training.git
    cd Demography-Based-Facial-Recognition-Model-Training
  2. Install dependencies:

    pip install -r requirements.txt

    Main Libraries:

    • PyTorch & Torchvision
    • Albumentations
    • Torchmetrics
    • Mealpy (for Sea Lion Optimizer)
    • Matplotlib, Seaborn, OpenCV

🔬 Dataset

  • 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: Background
    • 1: Cheal Weed
    • 2: Sorvu Crop

⚙️ Training Workflow

1️⃣ Hyperparameter Optimization with SLO

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)

2️⃣ Train the Optimized Model

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.

3️⃣ Test & Save Detections

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.


📊 Results

  • 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

🌍 Applications

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

🤝 Contributing

We welcome contributions!

  • Add more crop/weed classes.
  • Experiment with other optimizers.
  • Extend to instance segmentation (Mask R-CNN).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages