This project implements an animal weight classification system using deep learning and traditional machine learning approaches. The system classifies animals into three weight categories: Underweight, Healthy, and Overweight based on their body proportions(Body length) extracted from the AP-10K animal pose dataset.
- Project Overview
- Features
- Prerequisites
- Installation & Setup
- Dataset Download
- Project Structure
- Usage Instructions
- Model Architecture
- Results
- Troubleshooting
- Contributing
This project uses two complementary approaches for animal weight classification:
- Traditional ML Approach: Histogram of Oriented Gradients (HOG) features + Random Forest
- Deep Learning Approach: Custom CNN with data augmentation
The classification is based on body length measurements calculated from animal keypoint annotations, creating proxy labels for weight categories.
- Multi-approach Classification: Both traditional ML and deep learning methods
- Robust Data Processing: Handles missing data and various image formats
- Comprehensive Evaluation: Detailed metrics, confusion matrices, and visualizations
- Custom CNN Architecture: Lightweight CNN model optimized for animal classification
- HOG Feature Extraction: Traditional computer vision features for comparison
- Data Augmentation: Improves model generalization with rotation, shifts, and flips
- Model Persistence: Save and load trained models with weights and architecture
- Python 3.8+
- Anaconda or Miniconda
- Jupyter Notebook
- 8GB+ RAM recommended
- GPU support optional but recommended for faster training
conda create -n animal_classification python=3.9
conda activate animal_classification
conda install jupyter notebook
Download this project and navigate to the project directory:
git clone https://github.com/Dkplucas/model.git
cd model
pip install -r requirements_ml.txt
jupyter notebook
- Visit the official repository: AP-10K Dataset
- Download the dataset following the instructions in the repository
- Extract the dataset to a directory outside your git repository (to avoid large file issues) with the following structure:
data/
βββ ap-10K/
βββ annotations/
β βββ ap10k-train-split1.json
β βββ ap10k-train-split2.json
β βββ ap10k-train-split3.json
β βββ ap10k-val-split1.json
β βββ ap10k-val-split2.json
β βββ ap10k-val-split3.json
β βββ ap10k-test-split1.json
β βββ ap10k-test-split2.json
β βββ ap10k-test-split3.json
βββ data/
βββ 000000000001.jpg
βββ 000000000002.jpg
βββ ... (all image files)
Important: Ensure the dataset is placed in the data/ap-10K/
directory relative to your Jupyter notebook working directory.
animal_classification/
βββ dataprocess.py # Data preprocessing and label creation
βββ hogfeatures.py # HOG feature extraction
βββ model.py # Deep learning model training
βββ projection.py # Additional analysis and projections
βββ requirements_ml.txt # Project dependencies
βββ .gitignore # Git ignore file for dataset exclusion
βββ README.md # This file
βββ data/ # Dataset directory (to be created outside repo)
βββ ap-10K/
βββ annotations/ # JSON annotation files
βββ data/ # Image files
Open and run dataprocess.py
in Jupyter Notebook:
%run dataprocess.py
What this does:
- Loads AP-10K annotations from JSON files
- Calculates body length from keypoint coordinates (nose to tail distance)
- Creates weight classification labels (Underweight/Healthy/Overweight)
- Splits data into train/validation/test sets
- Saves processed data as CSV files
Expected outputs:
data/train_split.csv
data/val_split.csv
data/test_split.csv
Run hogfeatures.py
in Jupyter Notebook:
%run hogfeatures.py
What this does:
- Extracts Histogram of Oriented Gradients (HOG) features from images
- Processes images in batches for memory efficiency
- Handles various image path structures in the dataset
- Prepares traditional ML features for classification
Expected outputs:
- HOG feature arrays for train/validation/test sets
- Progress logs showing successful feature extractions
Run model.py
in Jupyter Notebook:
%run model.py
What this does:
- Builds and trains custom CNN model
- Implements data augmentation and callbacks
- Evaluates model performance on test set
- Generates confusion matrices and classification reports
- Saves trained model and training history
Expected outputs:
best_weights.weights.h5
- Best model weights during traininganimal_weight_classifier_weights.h5
- Final model weightsanimal_weight_classifier_architecture.json
- Model architecturetraining_log.csv
- Training historyconfusion_matrix.png
- Confusion matrix visualizationtraining_history.png
- Training/validation curves
- Base Model: Custom CNN from scratch
- Input: 224Γ224Γ3 RGB images
- Architecture:
- Conv2D(32 filters, 3Γ3 kernel, ReLU activation)
- MaxPooling2D(2Γ2)
- Conv2D(64 filters, 3Γ3 kernel, ReLU activation)
- MaxPooling2D(2Γ2)
- Conv2D(64 filters, 3Γ3 kernel, ReLU activation)
- GlobalAveragePooling2D()
- Dropout(0.5)
- Dense(64, ReLU activation)
- Dropout(0.3)
- Dense(3) # 3 classes
- Optimizer: Adam (learning_rate=1e-4)
- Loss: Sparse Categorical Crossentropy
- Feature Extraction: HOG (Histogram of Oriented Gradients)
- Parameters:
- 9 orientations
- 16Γ16 pixels per cell
- 2Γ2 cells per block
- L2-Hys normalization
- Classifier: Random Forest (can be extended)
The model provides:
- Classification metrics: Precision, Recall, F1-score for each class
- Confusion matrix: Visual representation of classification performance
- Training curves: Loss and accuracy over training epochs
- Class distribution analysis: Understanding of dataset balance
-
"Image not found" errors:
- Ensure dataset is extracted to correct
data/ap-10K/
directory - Check that both annotation files and images are present
- Ensure dataset is extracted to correct
-
Memory errors during training:
- Reduce batch size in
model.py
(default is 8-16) - Close other applications to free up RAM
- Reduce batch size in
-
Slow training:
- Consider using GPU acceleration with tensorflow-gpu
- Reduce image resolution if necessary
-
Import errors:
- Ensure all dependencies are installed:
pip install -r requirements_ml.txt
- Activate the correct conda environment
- Ensure all dependencies are installed:
-
TensorFlow/Keras errors:
- Update TensorFlow:
pip install tensorflow --upgrade
- Check CUDA compatibility for GPU usage
- Update TensorFlow:
# If sklearn package fails, install scikit-learn instead:
pip install scikit-learn
# If skimage package fails, install scikit-image instead:
pip install scikit-image
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is for educational purposes. Please refer to the AP-10K dataset license for data usage terms.
- AP-10K Dataset for providing the animal pose dataset
- TensorFlow and scikit-learn communities for the frameworks
Note: This project is designed for research and educational purposes. For production use, consider additional validation and testing on diverse datasets.