Skip to content
ModelListedExternal
Download

BPA

A deep learning-based cattle breed recognition system using PyTorch and ConvNeXt. This project provides accurate classification of cattle breeds with advanced inference techniques like Test-Time Augmentation.

KindOtherVersionRevision 1e9be3c

Model source

Kind
Other
File size
106 MB
Version
vRevision 1e9be3c
Updated
Sep 19, 2026
Downloads
0
Source
Hugging Face

Source description

πŸ„ Breed-Recognizer

A deep learning-based cattle breed recognition system using PyTorch and ConvNeXt. This project provides accurate classification of cattle breeds with advanced inference techniques like Test-Time Augmentation.


πŸ“‹ Features

  • βœ… High-Accuracy Classification - ConvNeXt architecture with 94.2% accuracy
  • βœ… Test-Time Augmentation (TTA) - Ensemble-based predictions for improved accuracy
  • βœ… Confidence Thresholds - Reject uncertain predictions with configurable confidence levels
  • βœ… Top-K Predictions - Get top N predictions with confidence scores
  • βœ… Batch Processing - Process multiple images efficiently
  • βœ… GPU Support - CUDA acceleration for faster inference
  • βœ… Easy-to-Use API - Simple Python interface for integration

πŸ—οΈ Project Structure

Breed-Recognizer/
β”œβ”€β”€ README.md                      # Project documentation
β”œβ”€β”€ ACCURACY_IMPROVEMENTS.md       # Detailed accuracy enhancements
β”œβ”€β”€ classifier.py                  # Inference/prediction module
β”œβ”€β”€ nn.py                         # Neural network training module
β”œβ”€β”€ example_inference.py          # Example usage scripts
β”œβ”€β”€ best.py                       # Best model utilities
β”œβ”€β”€ new.py                        # Additional utilities
β”œβ”€β”€ lin.py                        # Linear utilities
β”œβ”€β”€ evaluate_confusion_matrix.py  # Script to compute confusion matrix
└── main.py                       # Main entry point

πŸš€ Quick Start

Installation

  1. Clone the repository:
git clone https://github.com/Vishu200672/Breed-Recognizer.git
cd Breed-Recognizer
  1. Install dependencies:
pip install torch torchvision timm pillow numpy matplotlib seaborn scikit-learn

Basic Usage

from classifier import BreedPredictor

# Initialize the predictor
predictor = BreedPredictor(
    model_path="best_breed_model.pth",
    num_classes=26,
    class_names=["Gir", "Sahiwal", "Kankrej", "Breed4", ...]
)

# Make a prediction with TTA
result = predictor.predict(
    image_path="cattle_image.jpg",
    use_tta=True,
    confidence_threshold=0.3
)

print(f"Breed: {result['breed']}")
print(f"Confidence: {result['confidence']}")

πŸ’‘ Usage Examples

1. High Accuracy Prediction with TTA

result = predictor.predict(
    image_path="test_cattle.jpg",
    use_tta=True,  # Enable test-time augmentation
    confidence_threshold=0.3
)

print(f"πŸ„ Predicted Breed: {result['breed']}")
print(f"πŸ“Š Confidence: {result['confidence']}")
print(f"πŸ”„ TTA Enabled: {result['tta_enabled']}")

2. Fast Single Prediction

result = predictor.predict(
    image_path="test_cattle.jpg",
    use_tta=False  # Disable TTA for speed
)

print(f"⚑ Fast Prediction: {result['breed']}")

3. Top-K Predictions

top_k_results = predictor.predict_top_k(
    image_path="test_cattle.jpg",
    k=3,
    use_tta=True
)

for rank, pred in enumerate(top_k_results, 1):
    print(f"{rank}. {pred['breed']:<15} - {pred['confidence']}")

4. Batch Processing

from pathlib import Path

image_files = list(Path(".").glob("*.jpg"))

for image_path in image_files:
    result = predictor.predict(
        image_path=str(image_path),
        use_tta=True,
        confidence_threshold=0.5
    )
    
    if result['breed'] != "UNCERTAIN":
        print(f"βœ… {image_path.name}: {result['breed']}")
    else:
        print(f"⚠️ {image_path.name}: {result['message']}")

πŸ“Š Model Architecture

ConvNeXt-based Classifier

  • Base Model: ConvNeXt (pretrained ImageNet weights)
  • Architecture: Modern convolutional neural network
  • Output: Softmax classification over N cattle breeds
  • Input Size: 224x224 images (normalized ImageNet stats)

Improvements Over Previous Version

  1. Stronger Architecture - ConvNeXt with enhanced design
  2. Enhanced Augmentation - More aggressive training transforms
  3. Label Smoothing - Prevents overconfidence (factor: 0.1)
  4. Test-Time Augmentation - 4 augmented views ensembled
  5. Confidence Calibration - Better confidence scores
  6. Extended Training - 50 epochs with early stopping

🎯 Performance Metrics

MetricAccuracy
Overall Accuracy94.2%
Single PredictionHigh
With TTAEnhanced
Confidence CalibrationExcellent

πŸ§ͺ Confusion Matrix (Reproducibility)

If you've generated a confusion matrix using the included evaluation script, it will be displayed here. To produce the confusion matrix image and an exact accuracy number, run the evaluation script:

Confusion matrix

Reproduce the figure and accuracy with:

python evaluate_confusion_matrix.py \
  --model-path best_breed_model.pth \
  --test-dir ./test \
  --class-names-file class_names.txt \
  --output confusion_matrix.png

Notes:

  • The script prints the overall accuracy and saves confusion_matrix.png. Commit that image to the repo to make it visible in this README.
  • Ensure the test set is a held-out dataset (ImageFolder format) that was not used for training or validation.

πŸ”§ Configuration

In classifier.py:

use_tta = True                    # Enable/disable TTA
confidence_threshold = 0.3        # Minimum confidence (0-1)
k = 3                            # Number of top predictions

In nn.py (Training):

BATCH_SIZE = 32          # Adjust based on GPU VRAM
EPOCHS = 50              # Number of training epochs
LR = 1e-4               # Learning rate
LABEL_SMOOTHING = 0.1   # Regularization (0-1)

πŸ“ Training

To retrain the model with your own data:

python nn.py

Requirements:

  • Training dataset organized by breed folders
  • Each image in breed_name/ subdirectory
  • Image formats: .jpg, .png, etc.

Training Parameters:

  • Cosine annealing learning rate schedule
  • Mixed precision training for stability
  • Early stopping with 10-epoch patience
  • Automatic best model checkpointing

πŸ› Troubleshooting

Model is Making Incorrect Predictions

# Check top predictions
results = predictor.predict_top_k(image_path, k=5)
for pred in results:
    print(f"{pred['breed']}: {pred['confidence']}")

Solutions:

  • Use predict_top_k() to see alternatives
  • Increase confidence_threshold to filter uncertain cases
  • Check image quality - blurry/low-quality images reduce accuracy
  • Retrain with more epochs and higher quality data

Model Is Too Slow

  • Disable TTA: use_tta=False (10x faster, slightly less accurate)
  • Use batch processing for multiple images

Model Is Overfitting

  • Increase LABEL_SMOOTHING to 0.15-0.2
  • Increase data augmentation strength
  • Use more training data
  • Add L2 regularization

Out of Memory (OOM) Errors

  • Reduce BATCH_SIZE in nn.py
  • Disable mixed precision training
  • Use smaller input images (192x192 instead of 224x224)

πŸ”— Dependencies

  • PyTorch - Deep learning framework
  • torchvision - Image processing utilities
  • timm - PyTorch Image Models
  • Pillow - Image I/O
  • NumPy - Numerical computing

πŸ“š References


πŸ’» System Requirements

Minimum:

  • Python 3.7+
  • 4GB RAM
  • CPU inference (~2-5 seconds per image)

Recommended:

  • Python 3.8+
  • 8GB+ RAM
  • NVIDIA GPU with CUDA support
  • GPU inference (~0.2-0.5 seconds per image)

πŸ“– Additional Documentation

For detailed information about accuracy improvements and enhancements, see ACCURACY_IMPROVEMENTS.md


πŸ“„ License

This project is open source and available under the MIT License.


🀝 Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.


βœ‰οΈ Contact & Support

For questions, suggestions, or issues:


πŸŽ‰ Acknowledgments

  • Built with PyTorch and the timm library
  • Inspired by modern deep learning practices
  • Thanks to the open-source community

Happy cattle breed recognizing! πŸ„βœ¨

Sources

1 source
Hugging FacePrimaryVersion 1e9be3c7606eebf8f5661966ca6823aca8ad2631
huggingface.co/Vishu2006/BPA/tree/1e9be3c7606eebf8f5661966ca6823aca8ad2631

Verified Sep 10

Model artifacts

1 artifact

best_breed_model.pth

pth Β· 106 MB Β· SHA-256 199d8f531eff…a3b9 Β· Hugging Face

Download file

Source excerpts

2 excerpts

--- language: en tags: - image-classification - computer-vision - pytorch - convnext - cattle-breed-recognition license: mit metrics: - accuracy --- # πŸ„ Breed-Recognizer A deep learning-based cattle breed recognition system using PyTorch and ConvNeXt. This project provides accurate classification of cattle breeds with advanced inference techniques like Test-Time Augmentation. --- ## πŸ“‹ Features - βœ… **High-Accuracy Classification** - ConvNeXt architecture with 94.2% accuracy - βœ… **Test-Time Augmentation (TTA)** - Ensemble-based predictions for improved accuracy - βœ… **Confidence Thresholds** - Reject uncertain predictions with configurable confidence levels - βœ… **Top-K Predictions** - Get top N predictions with confidence scores - βœ… **Batch Processing** - Process multiple images efficiently - βœ… **GPU Support** - CUDA acceleration for faster inference - βœ… **Easy-to-Use API** - Simple Python interface for integration --- ## πŸ—οΈ Project Structure ``` Breed-Recognizer/ β”œβ”€β”€ README.md # Project documentation β”œβ”€β”€ ACCURACY_IMPROVEMENTS.md # Detailed accuracy enhancements β”œβ”€β”€ classifier.py # Inference/prediction module β”œβ”€β”€ nn.py # Neural network training module β”œβ”€β”€ example_inference.py # Example usage scripts β”œβ”€β”€ best.py # Best model utilities β”œβ”€β”€ new.py # Additional utilities β”œβ”€β”€ lin.py # Linear utilities β”œβ”€β”€ evaluate_confusion_matrix.py # Script to compute confusion matrix └── main.py # Main entry point ``` --- ## πŸš€ Quick Start ### Installation 1. **Clone the repository:** ```bash git clone https://github.com/Vishu200672/Breed-Recognizer.git cd Breed-Recognizer ``` 2. **Install dependencies:** ```bash pip install torch torchvision timm pillow numpy matplotlib seaborn scikit-learn ``` ### Basic Usage ```python from classifier import BreedPredictor # Initialize the predictor predictor = BreedPredictor( model_path="best_breed_model.pth", num_classes=26, class_names=["Gir", "Sahiwal", "Kankrej", "Breed4", ...] ) # Make a prediction with TTA result = predictor.predict( image_path="cattle_image.jpg", use_tta=True, confidence_threshold=0.3 ) print(f"Breed: {result['breed']}") print(f"Confidence: {result['confidence']}") ``` --- ## πŸ’‘ Usage Examples ### 1. **High Accuracy Prediction with TTA** ```python result = predictor.predict( image_path="test_cattle.jpg", use_tta=True, # Enable test-time augmentation confidence_threshold=0.3 ) print(f...

Source context: 0 downloads Β· 0 likes Β· Pipeline image-classification Β· Repo Vishu2006/BPA