This project implements wheat head detection using computer vision and deep learning techniques. The goal is to detect and localize individual wheat heads in field images, which is crucial for agricultural analysis, yield estimation, and crop monitoring.
Objective: Detect wheat heads in field images using bounding box detection.
Input: RGB images of wheat fields (1024Γ1024 pixels)
Output: Bounding boxes around individual wheat heads with confidence scores
Data Format:
- Images:
.jpgfiles containing wheat field photographs - Annotations: CSV file with columns
[image_id, width, height, bbox, source] - Bounding boxes: Format
[x, y, width, height]in pixel coordinates
Example annotation:
image_id,width,height,bbox,source
b6ab77fd7,1024,1024,"[834.0, 222.0, 56.0, 36.0]",usask_1
b6ab77fd7,1024,1024,"[226.0, 548.0, 130.0, 58.0]",usask_1- DETR (Detection Transformer): State-of-the-art object detection using transformers
- U-Net: Semantic segmentation approach for wheat head detection
This project now automatically downloads and sets up the data for you! No manual downloads needed.
-
Clone and Navigate to Project:
git clone https://github.com/Roy-Ayalon/final_IP_project.git cd final_IP_project -
Activate Virtual Environment:
# Activate the existing wheat-env virtual environment source wheat-env/bin/activate # Verify activation (you should see (wheat-env) in your prompt) which python
-
Install Dependencies:
# Upgrade pip and install requirements python -m pip install --upgrade pip pip install -r requirements.txt # Install additional dependencies for training pip install pytorch-lightning wandb torchmetrics pip install segmentation-models-pytorch # Verify installation pip list | grep torch
-
Kaggle API Setup (Choose one option):
Option A: Provide credentials via command line
# Get your credentials from https://www.kaggle.com/account python src/generate_data.py --model unet --kaggle_username YOUR_USERNAME --kaggle_key YOUR_API_KEYOption B: Use kaggle.json file
# 1. Go to https://www.kaggle.com/account # 2. Click "Create New API Token" # 3. Move downloaded kaggle.json to ~/.kaggle/kaggle.json # 4. Set permissions: chmod 600 ~/.kaggle/kaggle.json python src/generate_data.py --model unet
Option C: Use environment variables
export KAGGLE_USERNAME=your_username export KAGGLE_KEY=your_api_key python src/generate_data.py --model unet
-
Accept Competition Rules:
- Visit https://www.kaggle.com/c/global-wheat-detection/rules
- Click "I Understand and Accept" to join the competition
Option 1: Interactive Setup (Recommended)
Simply run the interactive setup script:
python setup_project.pyThis will:
- Guide you through choosing U-Net or DETR (or both)
- Automatically download the Global Wheat Detection dataset from Kaggle
- Set up all necessary folders and files
- Give you the exact commands to start training
Option 2: Direct Command Line
For more control, use the generate_data.py script directly:
# Setup for U-Net training with API credentials
python src/generate_data.py --model unet --kaggle_username YOUR_USERNAME --kaggle_key YOUR_API_KEY
# Setup for DETR training with existing kaggle.json
python src/generate_data.py --model detr
# Custom validation split (e.g., 15% validation)
python src/generate_data.py --model unet --val_ratio 0.15 --kaggle_username YOUR_USERNAME --kaggle_key YOUR_API_KEYFor U-Net Training:
data_unet/
βββ train/
β βββ images/ # Training images
β βββ masks/ # Training masks (binary)
βββ val/
β βββ images/ # Validation images
β βββ masks/ # Validation masks
βββ masks/ # Generated masks from CSV
βββ unannotated/ # Images without annotations
For DETR Training:
data_detr/
βββ train/ # All training images
βββ train.csv # Annotations file
Your data should be organized as follows:
data/
βββ train.csv # Annotations with bounding boxes
βββ train/ # Training images
β βββ b6ab77fd7.jpg # Individual image files
β βββ 51f1be19e.jpg
β βββ ...
βββ test/ # Test images (for inference)
βββ 2fd875eaa.jpg
βββ ...
If you prefer to set up data manually, use the data generation script to create masks and split your dataset:
# Generate masks from bounding boxes and split into train/val
python src/generate_data.py \
--csv data/train.csv \
--images_dir data/train \
--output_masks_dir data/train_masks \
--train_images_dir data/processed/train \
--train_masks_dir data/processed/train_masks \
--val_images_dir data/processed/val \
--val_masks_dir data/processed/val_masks \
--unannotated_dir data/unannotated \
--val_ratio 0.2 \
--seed 42 \
--prefix imgThis will:
- β Generate binary masks from bounding box annotations
- β Split dataset into training (80%) and validation (20%) sets
- β
Move images without annotations to
unannotated/folder - β
Rename files with consistent naming (
img_0001.jpg, etc.)
After running the setup, you'll get the exact training commands. For example:
Train the DETR model with command line arguments:
# Basic training with default parameters (from automatic setup)
python src/train_detr.py --csv_path data_detr/train.csv --images_dir data_detr/train
# Basic training with manual data
python src/train_detr.py \
--csv_path data/train.csv \
--images_dir data/train \
--epochs 50 \
--batch_size 16 \
--learning_rate 1e-4 \
--num_workers 4
# Advanced training with custom parameters
python src/train_detr.py \
--csv_path data/train.csv \
--images_dir data/train \
--epochs 100 \
--batch_size 32 \
--learning_rate 1e-4 \
--num_workers 8 \
--val_ratio 0.1 \
--num_queries 100 \
--hidden_dim 256 \
--warmup_epochs 5 \
--project_name "wheat-detection-detr"# U-Net training (from automatic setup)
python src/train_unet.py --images_dir data_unet/train/images --masks_dir data_unet/train/masks --val_images_dir data_unet/val/images --val_masks_dir data_unet/val/masks
# Train U-Net for segmentation-based detection (manual data)
python src/train_unet.py \
--images_dir data/processed/train \
--masks_dir data/processed/train_masks \
--val_images_dir data/processed/val \
--val_masks_dir data/processed/val_masks \
--epochs 50 \
--batch_size 8 \
--learning_rate 1e-4| Argument | Description | Default |
|---|---|---|
--csv_path |
Path to CSV annotations file | data/train.csv |
--images_dir |
Directory containing training images | data/train |
--epochs |
Number of training epochs | 50 |
--batch_size |
Batch size for training | 16 |
--learning_rate |
Learning rate for optimizer | 1e-4 |
--num_workers |
Number of data loading workers | 4 |
--val_ratio |
Validation split ratio | 0.1 |
--num_queries |
Number of object queries (DETR) | 100 |
--hidden_dim |
Hidden dimension size | 256 |
--project_name |
W&B project name | wheat-detection |
The training uses Weights & Biases (W&B) for experiment tracking:
-
Login to W&B (first time only):
wandb login
-
View training progress:
- Training loss and validation mAP
- Sample predictions with bounding boxes
- Model performance metrics
-
Access your experiments: Visit wandb.ai to view detailed logs
Evaluation metrics used:
- mAP (mean Average Precision): Primary metric for object detection
- IoU thresholds: 0.50 to 0.75 (Kaggle competition standard)
- Loss components: Classification loss + Bounding box regression + GIoU loss
This section presents comprehensive results from our wheat head detection experiments using both DETR and U-Net architectures.
Sample images from the Global Wheat Detection dataset showing:
- Various wheat field conditions and growth stages
- Different lighting conditions and image quality
- Diverse wheat head densities and orientations
Dataset Statistics Analysis:
- Distribution of wheat heads per image
- Bounding box size variations
- Image quality and annotation consistency metrics
DETR Architecture Overview:
- Backbone: ResNet-50 feature extractor
- Transformer Encoder: Self-attention mechanisms for global context
- Transformer Decoder: Object queries for direct set prediction
- Detection Heads: Classification and bounding box regression
U-Net Architecture Features:
- Encoder Path: Convolutional layers with max-pooling for feature extraction
- Decoder Path: Up-sampling with skip connections for precise localization
- Skip Connections: Preserve spatial information across resolution levels
- Output: Binary masks converted to bounding boxes via post-processing
Baseline DETR Results:
- Training Loss: Shows convergence but potential overfitting
- Validation mAP: Peak performance around 0.45-0.50
- Key Observation: Model struggles with generalization without augmentation
Prediction Examples (No Augmentation):
- Good detection of prominent wheat heads
- Struggles with smaller or overlapping instances
- Some false positives in complex backgrounds
Test Set Performance (No Augmentation):
- Demonstrates overfitting to training distribution
- Reduced performance on unseen test images
Batch Size 32 Results:
- mAP Performance: ~0.55-0.60 (significant improvement)
- Loss Convergence: More stable training with augmentation
- Visual Quality: Better detection of small wheat heads
Batch Size 256 Results:
- Training Dynamics: Different convergence pattern with larger batches
- Memory Requirements: Higher GPU memory usage
- Performance Trade-offs: Comparison with smaller batch sizes
Batch Size 1024 Results:
- Large Batch Training: Effects on gradient estimation and convergence
- Hardware Requirements: Multi-GPU training considerations
- Performance Analysis: Optimal batch size determination
U-Net Training Results:
- Segmentation Loss: Binary cross-entropy with Dice loss
- Validation AP: Average Precision after mask-to-box conversion
- Convergence: Stable training with consistent improvement
U-Net Prediction Visualizations:
- Segmentation Maps: High-quality binary masks for wheat heads
- Boundary Detection: Precise wheat head boundaries
- Post-processing: Conversion from masks to bounding boxes
Data Augmentation Techniques Applied:
- Geometric Transformations: Rotation, scaling, flipping
- Color Augmentations: Brightness, contrast, saturation adjustments
- Noise Addition: Gaussian noise for robustness
- Crop Variations: Random crops and aspect ratio changes
Impact Analysis:
- Improved Generalization: Better performance on test sets
- Reduced Overfitting: More stable validation curves
- Robustness: Better handling of diverse field conditions
High-Quality Predictions:
- Accurate bounding box localization
- High confidence scores for clear wheat heads
- Good performance in optimal lighting conditions
Failure Case Analysis:
- Dense Overlapping: Difficulty separating closely packed wheat heads
- Lighting Conditions: Poor performance in shadows or overexposure
- Image Quality: Blurry or low-resolution regions cause missed detections
- Background Complexity: False positives in complex backgrounds
| Model | Best mAP | Strengths | Weaknesses |
|---|---|---|---|
| DETR (w/ Aug) | ~0.60 | Direct object detection, end-to-end training | Requires large datasets, slower inference |
| U-Net | ~0.55 | Precise boundaries, efficient training | Post-processing needed, struggles with overlapping objects |
- Data Augmentation is Critical: 15-20% improvement in mAP scores
- Batch Size Effects: Optimal batch size around 32-256 for this dataset
- Architecture Trade-offs: DETR excels in direct detection, U-Net in precise segmentation
- Challenging Scenarios: Dense wheat fields and poor lighting remain difficult
final_IP_project/
βββ src/
β βββ train_detr.py # DETR training script with CLI
β βββ train_unet.py # U-Net training script
β βββ Detr.py # DETR model implementation
β βββ unet.py # U-Net model implementation
β βββ dataset.py # Dataset classes and data loading
β βββ loss.py # Loss functions (Hungarian matching)
β βββ generate_data.py # Data preprocessing utilities
β βββ definitions.py # Configuration constants
βββ data/ # Your wheat detection dataset
βββ wheat-env/ # Python virtual environment
βββ requirements.txt # Project dependencies
βββ README.md # This file
-
Virtual environment not found:
# Recreate the environment if needed python -m venv wheat-env source wheat-env/bin/activate
-
CUDA not available:
- The code will automatically use MPS (Apple Silicon) or CPU
- No action needed for M1/M2 Macs
-
Memory issues:
- Reduce
--batch_sizeto 8 or 4 - Reduce
--num_workersto 2 or 0
- Reduce
-
Permission errors:
chmod +x src/train_detr.py
- "Kaggle API credentials not found": Follow the Kaggle API setup steps above
- "Failed to download dataset": Make sure you've accepted the competition rules
- Permission errors: Ensure kaggle.json has correct permissions (600)
That's it! The automatic setup handles everything else. π
- DETR Paper: End-to-End Object Detection with Transformers
- Dataset: Global Wheat Detection Challenge
- PyTorch Lightning: Documentation






.png)
.png)


.png)
.png)


.png)
.png)


.png)
.png)








