Views
No views yet
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 point1git clone https://github.com/Vishu200672/Breed-Recognizer.git
2cd Breed-Recognizerpip install torch torchvision timm pillow numpy matplotlib seaborn scikit-learn1from classifier import BreedPredictor
2
3# Initialize the predictor
4predictor = BreedPredictor(
5 model_path="best_breed_model.pth",
6 num_classes=26,
7 class_names=["Gir", "Sahiwal", "Kankrej", "Breed4", ...]
8)
9
10# Make a prediction with TTA
11result = predictor.predict(
12 image_path="cattle_image.jpg",
13 use_tta=True,
14 confidence_threshold=0.3
15)
16
17print(f"Breed: {result['breed']}")
18print(f"Confidence: {result['confidence']}")1result = predictor.predict(
2 image_path="test_cattle.jpg",
3 use_tta=True, # Enable test-time augmentation
4 confidence_threshold=0.3
5)
6
7print(f"🐄 Predicted Breed: {result['breed']}")
8print(f"📊 Confidence: {result['confidence']}")
9print(f"🔄 TTA Enabled: {result['tta_enabled']}")1result = predictor.predict(
2 image_path="test_cattle.jpg",
3 use_tta=False # Disable TTA for speed
4)
5
6print(f"⚡ Fast Prediction: {result['breed']}")1top_k_results = predictor.predict_top_k(
2 image_path="test_cattle.jpg",
3 k=3,
4 use_tta=True
5)
6
7for rank, pred in enumerate(top_k_results, 1):
8 print(f"{rank}. {pred['breed']:<15} - {pred['confidence']}")1from pathlib import Path
2
3image_files = list(Path(".").glob("*.jpg"))
4
5for image_path in image_files:
6 result = predictor.predict(
7 image_path=str(image_path),
8 use_tta=True,
9 confidence_threshold=0.5
10 )
11
12 if result['breed'] != "UNCERTAIN":
13 print(f"✅ {image_path.name}: {result['breed']}")
14 else:
15 print(f"⚠️ {image_path.name}: {result['message']}")| Metric | Accuracy |
|---|---|
| Overall Accuracy | 94.2% |
| Single Prediction | High |
| With TTA | Enhanced |
| Confidence Calibration | Excellent |

1python evaluate_confusion_matrix.py \
2 --model-path best_breed_model.pth \
3 --test-dir ./test \
4 --class-names-file class_names.txt \
5 --output confusion_matrix.pngclassifier.py:1use_tta = True # Enable/disable TTA
2confidence_threshold = 0.3 # Minimum confidence (0-1)
3k = 3 # Number of top predictionsnn.py (Training):1BATCH_SIZE = 32 # Adjust based on GPU VRAM
2EPOCHS = 50 # Number of training epochs
3LR = 1e-4 # Learning rate
4LABEL_SMOOTHING = 0.1 # Regularization (0-1)python nn.pybreed_name/ subdirectory.jpg, .png, etc.1# Check top predictions
2results = predictor.predict_top_k(image_path, k=5)
3for pred in results:
4 print(f"{pred['breed']}: {pred['confidence']}")predict_top_k() to see alternativesconfidence_threshold to filter uncertain casesuse_tta=False (10x faster, slightly less accurate)LABEL_SMOOTHING to 0.15-0.2BATCH_SIZE in nn.py