A machine learning project for gender classification using deep learning techniques.
./
├── README.md
├── Dockerfile
├── requirements.txt
├── notebook/
│ └── gender_classifier.ipynb
├── data/
│ ├── train/
│ └── test/
├── models/
│ └── gender_model.h5
├── src/
│ ├── preprocessing.py
│ ├── model.py
│ ├── prediction.py
│ └── retrain.py
├── api/
│ └── app.py
├── ui/
│ └── streamlit_app.py
└── locust/
└── locustfile.py
-
Install dependencies:
pip install -r requirements.txt
-
Run the Streamlit UI:
streamlit run ui/streamlit_app.py
-
Run the FastAPI backend:
cd api uvicorn app:app --reload --port 8080
- Deep Learning Model: MobileNetV2-based gender classification with ~80-85% accuracy
- REST API: FastAPI backend with prediction and retraining endpoints
- Interactive UI: Streamlit web interface for easy image upload and prediction
- Model Retraining: Automated retraining capabilities with timestamped model saving
- Production Ready: Minimal, clean codebase optimized for deployment
-
Start the API server:
cd api uvicorn app:app --reload --port 8080 -
Start the Streamlit UI (in a new terminal):
streamlit run ui/streamlit_app.py
-
Access the application:
- Streamlit UI: http://localhost:8501
- API documentation: http://localhost:8080/docs
- Upload an image using the file uploader in the Streamlit interface
- View the gender prediction with confidence scores
- Use the "Retrain Model" button to trigger model retraining with new data
GET /- Health check and model statusPOST /predict- Predict gender from base64 encoded imagePOST /predict-file- Predict gender from uploaded filePOST /retrain- Trigger model retraining (background task)
- Architecture: MobileNetV2 with custom classification head
- Input Size: 160x160 RGB images
- Accuracy: 80-85% on test data
- Training Time: ~2-3 hours on standard hardware
- Model Size: ~9MB (optimized for deployment)
- Training on gender-labeled facial images
- Automatic preprocessing and augmentation
- Balanced dataset with male/female classes
- Precision: ~0.83
- Recall: ~0.82
- F1-Score: ~0.82
- Inference Time: <100ms per image
The complete development process is documented in notebook/gender_recognition.ipynb.ipynb, including:
- Data exploration and preprocessing
- Model architecture design and training
- Performance evaluation and visualization
- Hyperparameter tuning experiments
- Model comparison and selection
To run the notebook:
jupyter notebook notebook/gender_recognition.ipynbThe application runs successfully in local environments with both API and UI components communicating properly.
Railway Deployment: Attempted deployment to Railway platform encountered several challenges:
-
TensorFlow Version Conflicts:
- Railway's Python environment has compatibility issues with TensorFlow 2.15+
- Model files created with newer TensorFlow versions cannot be loaded
- Downgrading TensorFlow breaks other dependencies
-
Resource Limitations:
- Model loading requires significant memory (>512MB)
- Railway's free tier has strict memory limits
- Cold start times exceed platform timeouts
-
Model File Handling:
- Large model files (9MB+) cause deployment package size issues
- Git LFS integration complications with Railway
-
Dockerization:
FROM python:3.9-slim # Controlled environment with specific TensorFlow version
-
External Model Storage:
- Store models in cloud storage (AWS S3, Google Cloud Storage)
- Download models at runtime or during container initialization
-
Model Optimization:
- Implement model quantization to reduce size
- Use TensorFlow Lite for mobile/edge deployment
- Consider ONNX format for cross-platform compatibility
-
Alternative Platforms:
- Google Cloud Run (better TensorFlow support)
- Heroku with custom buildpacks
- AWS Lambda with container images
Attempted load testing with Locust revealed:
- Local API handles ~50-100 concurrent requests efficiently
- Memory usage scales linearly with concurrent predictions
- Response times average <200ms under normal load
python src/model.pypython src/retrain.pypython -m pytest tests/Key packages:
- TensorFlow 2.15+
- FastAPI
- Streamlit
- Pillow
- NumPy
- Uvicorn
See requirements.txt for complete dependency list.
-
Model Enhancements:
- Experiment with Vision Transformers
- Implement ensemble methods
- Add age classification capability
-
Production Features:
- User authentication system
- Prediction history tracking
- Batch processing capabilities
- Model versioning and A/B testing
-
Deployment Optimization:
- Implement proper Docker containerization
- Set up CI/CD pipeline
- Add monitoring and logging
- Implement model caching strategies