A comprehensive end-to-end machine learning system for identifying and classifying animal diseases from images using deep learning techniques.
- Multiple Model Architectures: Custom CNNs, ResNet, EfficientNet, VGG, Inception, and DenseNet
- Data Preprocessing: Advanced image preprocessing and augmentation pipelines
- Transfer Learning: Pre-trained models fine-tuned for animal disease classification
- Web Interface: User-friendly web application for image upload and prediction
- REST API: FastAPI-based API for integration with other systems
- Batch Processing: Support for multiple image analysis
- Comprehensive Evaluation: Detailed metrics, confusion matrices, and visualization
- Model Deployment: Easy model serialization and deployment utilities
animal-disease-classifier/
βββ src/ # Source code
β βββ config.py # Configuration settings
β βββ data_preprocessing.py # Data handling and preprocessing
β βββ models.py # Model architectures
β βββ train.py # Training pipeline
β βββ evaluation.py # Model evaluation and metrics
β βββ inference.py # Prediction and inference
β βββ app.py # FastAPI web application
βββ data/ # Dataset storage
β βββ raw/ # Raw dataset
β βββ processed/ # Processed images
β βββ train/ # Training set
β βββ validation/ # Validation set
β βββ test/ # Test set
βββ models/ # Saved models
βββ templates/ # HTML templates
βββ static/ # Static web assets
βββ notebooks/ # Jupyter notebooks
βββ tests/ # Unit tests
βββ docs/ # Documentation
βββ requirements.txt # Dependencies
βββ README.md # This file
# Clone the repository
git clone <repository-url>
cd animal-disease-classifier
# Create virtual environment (Windows)
python -m venv venv
venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtCreate your dataset structure:
data/raw/
βββ healthy/
β βββ image1.jpg
β βββ image2.jpg
β βββ ...
βββ bacterial_infection/
β βββ image1.jpg
β βββ image2.jpg
β βββ ...
βββ viral_infection/
β βββ ...
βββ fungal_infection/
βββ ...from src.data_preprocessing import DataPreprocessor
from src.config import Config
config = Config()
preprocessor = DataPreprocessor(config)
# Organize dataset
preprocessor.organize_dataset_from_folder('data/raw', 'data/processed')
# Create train/validation/test splits
preprocessor.create_train_val_test_split('data/processed')from src.train import train
# Train with EfficientNet-B0 (recommended)
history = train('efficientnet_b0')
# Or try other models
# history = train('resnet_50')
# history = train('simple_cnn')from src.evaluation import evaluate_trained_model
from src.config import Config
config = Config()
results = evaluate_trained_model(config.BEST_MODEL_PATH, config.TEST_DATA_DIR)
print(f"Accuracy: {results['accuracy']:.4f}")from src.inference import DiseasePredictor
from src.config import Config
config = Config()
predictor = DiseasePredictor(config.BEST_MODEL_PATH, config)
# Single image prediction
result = predictor.get_prediction_explanation('path/to/image.jpg')
print(f"Prediction: {result['top_prediction']['class']}")
print(f"Confidence: {result['top_prediction']['confidence']:.4f}")cd src
python app.pyVisit http://localhost:5000 to access the web interface.
The system supports classification of the following animal diseases:
- Healthy: Normal, disease-free animals
- Bacterial Infection: Bacterial-caused diseases
- Viral Infection: Viral-caused diseases
- Fungal Infection: Fungal-caused diseases
- Parasitic Infection: Parasite-caused diseases
- Nutritional Deficiency: Nutrition-related health issues
- Genetic Disorder: Hereditary conditions
Note: Modify DISEASE_CATEGORIES in config.py based on your specific dataset.
| Model Type | Description | Use Case |
|---|---|---|
simple_cnn |
Basic CNN for baseline | Quick prototyping |
advanced_cnn |
Custom CNN with residual connections | Balanced performance |
resnet_50/101/152 |
ResNet variants | High accuracy |
efficientnet_b0/b1/b2/b3 |
EfficientNet variants | Best efficiency |
vgg16/vgg19 |
VGG architectures | Transfer learning |
inception |
InceptionV3 | Feature diversity |
densenet |
DenseNet121 | Parameter efficiency |
from src.models import ModelFactory
factory = ModelFactory()
model = factory.create_model(
model_type='efficientnet_b0',
num_classes=7,
input_shape=(224, 224, 3)
)Modify training parameters in config.py:
class Config:
BATCH_SIZE = 32
EPOCHS = 100
LEARNING_RATE = 0.001
EARLY_STOPPING_PATIENCE = 10
REDUCE_LR_PATIENCE = 5from src.models import ModelFactory
from src.data_preprocessing import DataPreprocessor
from src.config import Config
config = Config()
preprocessor = DataPreprocessor(config)
# Create datasets
train_ds = preprocessor.create_tensorflow_dataset(config.TRAIN_DATA_DIR, augment=True)
val_ds = preprocessor.create_tensorflow_dataset(config.VAL_DATA_DIR, augment=False)
# Create and compile model
factory = ModelFactory(config)
model = factory.create_model('efficientnet_b0', len(train_ds.class_names))
# Train model
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=config.EPOCHS,
callbacks=get_callbacks(config.MODELS_DIR)
)The system provides comprehensive evaluation metrics:
- Accuracy: Overall classification accuracy
- Precision/Recall/F1: Per-class and weighted metrics
- Confusion Matrix: Detailed classification breakdown
- Classification Report: Comprehensive performance summary
- Visualizations: Training curves, confusion matrices, class distributions
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Web interface home page |
| GET | /health |
Health check |
| POST | /predict |
Single image prediction |
| POST | /predict/batch |
Batch image prediction |
| GET | /models/info |
Model information |
| GET | /models/classes |
Supported classes |
| POST | /models/reload |
Reload model |
import requests
# Single image prediction
files = {'file': open('image.jpg', 'rb')}
data = {'top_k': 3}
response = requests.post('http://localhost:5000/predict', files=files, data=data)
result = response.json()
print(f"Top prediction: {result['top_prediction']['class']}")
print(f"Confidence: {result['top_prediction']['confidence']:.4f}")# Health check
curl -X GET http://localhost:5000/health
# Single image prediction
curl -X POST -F "file=@image.jpg" -F "top_k=3" http://localhost:5000/predict
# Get model information
curl -X GET http://localhost:5000/models/info# config.py
class Config:
# Image settings
IMAGE_SIZE = (224, 224)
CHANNELS = 3
# Training parameters
BATCH_SIZE = 32
EPOCHS = 100
LEARNING_RATE = 0.001
# Data augmentation
ROTATION_RANGE = 20
HORIZONTAL_FLIP = True
ZOOM_RANGE = 0.2
# API settings
API_HOST = '0.0.0.0'
API_PORT = 5000- Use Transfer Learning: EfficientNet or ResNet models typically perform better
- Data Augmentation: Enable augmentation for training data
- Balanced Dataset: Ensure roughly equal samples per class
- High-Quality Images: Use clear, well-lit images
- Proper Preprocessing: Follow the preprocessing pipeline
- Start with EfficientNet-B0: Good balance of speed and accuracy
- Use Mixed Precision: Enable if you have compatible GPU
- Optimize Batch Size: Increase if you have sufficient GPU memory
- Early Stopping: Prevent overfitting and save time
-
Model Not Loading
# Check if model file exists import os print(os.path.exists('models/best_model.h5'))
-
Out of Memory Error
# Reduce batch size in config.py BATCH_SIZE = 16 # or smaller
-
Low Accuracy
- Check data quality and balance
- Try different model architectures
- Increase training epochs
- Verify data preprocessing
-
API Not Starting
# Check if port is available netstat -an | findstr :5000
from tensorflow.keras import layers, Model
def create_custom_model(input_shape, num_classes):
inputs = layers.Input(shape=input_shape)
x = layers.Conv2D(64, 3, activation='relu')(inputs)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(128, activation='relu')(x)
outputs = layers.Dense(num_classes, activation='softmax')(x)
model = Model(inputs, outputs)
return modelfrom src.models import create_ensemble_model
# Load multiple trained models
models = [
keras.models.load_model('models/efficientnet_b0.h5'),
keras.models.load_model('models/resnet_50.h5'),
keras.models.load_model('models/densenet.h5')
]
# Create ensemble
ensemble = create_ensemble_model(models, num_classes=7)from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Custom augmentation
datagen = ImageDataGenerator(
rotation_range=30,
width_shift_range=0.3,
height_shift_range=0.3,
horizontal_flip=True,
vertical_flip=False,
zoom_range=0.2,
brightness_range=[0.8, 1.2]
)- File Upload Validation: Only allow image files
- File Size Limits: Prevent large file uploads
- Input Sanitization: Validate all user inputs
- Rate Limiting: Implement API rate limiting for production
- Authentication: Add authentication for production APIs
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "src/app.py"]- AWS: Use EC2 with GPU instances for training, ECS for serving
- Google Cloud: Use AI Platform for training and deployment
- Azure: Use Machine Learning service for end-to-end ML lifecycle
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
This system is designed for educational and research purposes. While it can assist in preliminary disease identification, it should never replace professional veterinary diagnosis and treatment. Always consult qualified veterinarians for animal health issues.
For questions, issues, or contributions:
- Create an issue on GitHub
- Check the troubleshooting section
- Review the documentation in
/docs
- TensorFlow and Keras teams for the deep learning framework
- FastAPI team for the excellent web framework
- The open-source community for various libraries and tools
- Veterinary professionals who provided domain expertise
Happy Coding! πΎ