Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌾 Wheat Segmentation Project

A comprehensive computer vision project implementing state-of-the-art segmentation models for wheat head detection and segmentation in field images. This project explores both SAM (Segment Anything Model) fine-tuning and YOLOv8 segmentation approaches to achieve high-precision wheat head identification.

Project Banner Python PyTorch YOLOv8

πŸ“‹ Table of Contents

🎯 Project Overview

This project addresses the challenge of automated wheat head detection and segmentation in agricultural field images. Accurate wheat head counting and segmentation is crucial for:

  • Yield Estimation: Predicting crop yield based on wheat head density
  • Agricultural Research: Analyzing wheat variety performance
  • Precision Agriculture: Optimizing farming practices through computer vision
  • Automated Monitoring: Real-time crop assessment using drones or field cameras

πŸ”§ Features

  • βœ… Dual Model Approach: Implementation of both SAM and YOLOv8 for comprehensive comparison
  • βœ… High-Quality Dataset: 1000 annotated wheat field images with precise segmentation masks
  • βœ… Multiple Model Variants: YOLOv8m and YOLOv8l implementations for different performance requirements
  • βœ… ONNX Export: Optimized models for production deployment
  • βœ… Comprehensive Training: Complete training pipelines with visualization and metrics
  • βœ… Real-time Inference: Fast inference capabilities for practical applications

πŸ“‚ Dataset Structure

data/
β”œβ”€β”€ train/
β”‚   β”œβ”€β”€ images/          # 1000 wheat field images (.jpg)
β”‚   └── masks/           # 1000 corresponding segmentation masks (.png)
└── test/                # 10 test images for evaluation
    β”œβ”€β”€ 2fd875eaa.jpg
    β”œβ”€β”€ 348a992bb.jpg
    └── ... (8 more test images)

Dataset Specifications:

  • Training Images: 1000 high-resolution wheat field photographs
  • Image Format: JPG (RGB)
  • Mask Format: PNG (Binary masks)
  • Image Resolution: Resized to 256Γ—256 for SAM, 640Γ—640 for YOLO
  • Annotation Quality: Pixel-perfect segmentation masks for wheat heads

πŸ—οΈ Project Structure

Wheat-Segmentation-1/
β”œβ”€β”€ data/                          # Dataset directory
β”œβ”€β”€ SAM/                           # SAM model implementation
β”‚   β”œβ”€β”€ fine-tune-sam.ipynb      # SAM fine-tuning notebook
β”‚   └── model.pth                 # Trained SAM model weights
β”œβ”€β”€ YOLO/                          # YOLO model implementations
β”‚   β”œβ”€β”€ yolo8m/                   # YOLOv8 Medium variant
β”‚   β”‚   β”œβ”€β”€ train-yolov8m-seg.ipynb
β”‚   β”‚   β”œβ”€β”€ wheat_segmentation_model.onnx
β”‚   β”‚   └── runs/                 # Training results and metrics
β”‚   └── yolov8l/                  # YOLOv8 Large variant
β”‚       β”œβ”€β”€ train-yolov8l-seg.ipynb
β”‚       └── runs/                 # Training results and metrics
└── README.md                      # This file

πŸš€ Getting Started

Prerequisites

  • Python 3.7+
  • CUDA-compatible GPU (recommended)
  • 8GB+ RAM
  • 10GB+ free disk space

Installation

  1. Clone the repository
git clone https://github.com/your-username/Wheat-Segmentation-1.git
cd Wheat-Segmentation-1
  1. Install dependencies
# Install PyTorch (visit pytorch.org for specific CUDA version)
pip install torch torchvision torchaudio

# Install Ultralytics for YOLOv8
pip install ultralytics==8.0.196

# Install SAM dependencies
pip install git+https://github.com/facebookresearch/segment-anything.git
pip install git+https://github.com/huggingface/transformers.git

# Additional dependencies
pip install datasets opencv-python matplotlib monai roboflow
pip install onnx onnxruntime  # For model export
  1. Verify installation
python -c "import torch; print(f'PyTorch version: {torch.__version__}')"
python -c "import ultralytics; ultralytics.checks()"

🧠 Models & Approaches

1. SAM (Segment Anything Model) Fine-tuning

Approach: Fine-tune Meta's Segment Anything Model on wheat segmentation data using bounding box prompts.

Key Features:

  • Base Model: facebook/sam-vit-large
  • Training Strategy: Freeze vision encoder and prompt encoder, fine-tune mask decoder only
  • Loss Function: Focal Loss for handling class imbalance
  • Input Resolution: 1024Γ—1024 (SAM native resolution)
  • Prompt Strategy: Bounding box prompts with perturbation for robustness

Training Configuration:

  • Epochs: 5
  • Batch Size: 4
  • Optimizer: Adam (lr=1e-5)
  • Device: Multi-GPU training with DataParallel

2. YOLOv8 Segmentation Models

Approach: Train YOLOv8 models specifically for wheat head instance segmentation.

YOLOv8m (Medium)

  • Parameters: ~27M
  • Speed: Balanced performance
  • Use Case: General-purpose applications

YOLOv8l (Large)

  • Parameters: ~46M
  • Speed: Higher accuracy, slower inference
  • Use Case: High-precision applications

Training Configuration:

  • Epochs: 25 (YOLOv8m), 15 (YOLOv8l)
  • Image Size: 640Γ—640
  • Batch Size: 16
  • Augmentations: Mosaic, mixup, rotation, scaling

πŸ“Š Model Performance

YOLOv8 Results

Model mAP50 (Box) mAP50-95 (Box) mAP50 (Mask) mAP50-95 (Mask) Speed (ms)
YOLOv8m 96.9% 84.6% 96.4% 71.4% ~1400
YOLOv8l 96.9% 84.3% 96.5% 70.8% ~76

SAM Results

  • Training Loss Reduction: From 0.45 to 0.16 over 5 epochs
  • Convergence: Stable training with consistent improvement
  • Segmentation Quality: High-quality masks with precise boundaries

πŸ’» Usage

Training SAM Model

# Navigate to SAM directory
cd SAM/

# Open and run the Jupyter notebook
jupyter notebook fine-tune-sam.ipynb

The SAM training process includes:

  1. Data loading and preprocessing
  2. Bounding box prompt generation
  3. Model fine-tuning with Focal Loss
  4. Visualization of results

Training YOLOv8 Models

# For YOLOv8m
cd YOLO/yolo8m/
yolo task=segment mode=train model=yolov8m-seg.pt data=path/to/data.yaml epochs=25 imgsz=640

# For YOLOv8l
cd YOLO/yolov8l/
yolo task=segment mode=train model=yolov8l-seg.pt data=path/to/data.yaml epochs=15 imgsz=640

Running Inference

YOLO Inference

# Using PyTorch model
yolo task=segment mode=predict model=path/to/best.pt source=path/to/images conf=0.25

# Using ONNX model
yolo task=segment mode=predict model=path/to/best.onnx source=path/to/images conf=0.25

SAM Inference

from transformers import SamModel, SamProcessor
import torch

# Load the fine-tuned model
model = SamModel.from_pretrained("facebook/sam-vit-large")
model.load_state_dict(torch.load("SAM/model.pth"))
processor = SamProcessor.from_pretrained("facebook/sam-vit-large")

# Run inference with bounding box prompt
inputs = processor(image, input_boxes=[[bbox]], return_tensors="pt")
outputs = model(**inputs)

πŸ“ Pre-trained Models

Available Models

Model Type Size Location Performance
SAM Fine-tuned PyTorch ~2.4GB SAM/model.pth High-quality masks
YOLOv8m PyTorch ~52MB YOLO/yolo8m/runs/segment/train/weights/best.pt mAP50: 96.9%
YOLOv8l PyTorch ~88MB YOLO/yolov8l/runs/segment/train3/weights/best.pt mAP50: 96.9%
YOLOv8m ONNX ~105MB YOLO/yolo8m/wheat_segmentation_model.onnx Production-ready
YOLOv8l ONNX ~175MB YOLO/yolov8l/runs/segment/train3/weights/best.onnx Production-ready

Model Selection Guide

  • For Research/Experimentation: Use SAM fine-tuned model for highest quality masks
  • For Real-time Applications: Use YOLOv8m ONNX model for balanced speed/accuracy
  • For Maximum Accuracy: Use YOLOv8l PyTorch model
  • For Production Deployment: Use ONNX versions for cross-platform compatibility

πŸ”„ Model Export & Deployment

ONNX Export

# Export YOLOv8 to ONNX
from ultralytics import YOLO
model = YOLO("path/to/best.pt")
model.export(format="onnx", dynamic=False)

Deployment Options

  1. Local Inference: Direct PyTorch/ONNX inference
  2. Roboflow Deploy: Cloud-based API deployment
  3. Edge Deployment: ONNX Runtime for edge devices
  4. Docker Containers: Containerized inference services

πŸ“ˆ Results

Qualitative Results

  • Precision: Models accurately identify individual wheat heads
  • Boundary Quality: Sharp, precise segmentation boundaries
  • Robustness: Consistent performance across different lighting conditions
  • Scalability: Efficient processing of multiple wheat heads per image

Quantitative Metrics

  • Detection Accuracy: >96% mAP50 for both YOLO variants
  • Segmentation Quality: >96% mask mAP50
  • Speed: Real-time inference capability with optimized models
  • Generalization: Strong performance on test set

πŸ› οΈ Technical Details

Data Preprocessing

  • Image Normalization: 0-1 scaling for neural networks
  • Augmentation: Geometric and photometric augmentations
  • Mask Processing: Binary mask conversion and validation
  • Quality Control: Filtering of empty masks and corrupted data

Training Optimizations

  • Mixed Precision: Automatic Mixed Precision for faster training
  • Multi-GPU: DataParallel training support
  • Learning Rate: Cosine annealing and warmup strategies
  • Early Stopping: Patience-based training termination

Inference Optimizations

  • Model Fusion: Layer fusion for faster inference
  • Batch Processing: Efficient batch inference
  • Memory Management: Optimized memory usage
  • Post-processing: Efficient NMS and mask generation

πŸ“š References

πŸ‘₯ Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


🌾 Happy Wheat Segmentation!

For questions or support, please open an issue or contact the maintainers.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages