A comprehensive AI-powered system for automated cervical cytology image analysis and pap smear classification with explainable AI capabilities.
This application provides a complete pipeline for:
- Automated Pap Smear Classification: Multi-class cell classification using deep learning
- Explainable AI: Grad-CAM heatmaps for model interpretability
- Multiple Instance Learning: Slide-level analysis from patch-level predictions
- REST API: Production-ready FastAPI service with comprehensive endpoints
- Real-time Inference: Fast prediction with confidence scoring and uncertainty estimation
The system classifies cervical cytology images into 8 categories:
- Superficial Squamous - Normal superficial cells
- Intermediate Squamous - Normal intermediate cells
- Columnar Epithelial - Normal columnar cells
- Mild Squamous Non-keratinizing Dysplasia - Low-grade abnormality
- Moderate Squamous Non-keratinizing Dysplasia - Moderate abnormality
- Severe Squamous Non-keratinizing Dysplasia - High-grade abnormality
- Squamous Cell Carcinoma Keratinizing Type - Malignant keratinizing
- Squamous Cell Carcinoma Non-keratinizing Type - Malignant non-keratinizing
- Python 3.10+
- CUDA-capable GPU (optional, for training and faster inference)
- Docker and Docker Compose (for containerized deployment)
- 8GB+ RAM recommended
- Clone the repository
git clone https://github.com/yourusername/pap-smear-processing.git
cd pap-smear-processing- Create virtual environment
python -m venv venv
# On Windows
venv\Scripts\activate
# On macOS/Linux
source venv/bin/activate- Install dependencies
pip install -r requirements.txt- Download SIPaKMeD dataset (only needed for training new models)
python -m data.downloadNote: End users who just want to analyze images do NOT need to download the dataset. The dataset is only required if you want to:
- Train new models from scratch
- Retrain existing models
- Experiment with different architectures
For image analysis using pre-trained models, skip this step.
CPU-only deployment:
docker-compose up pap-smear-apiGPU-enabled deployment:
docker-compose --profile gpu up pap-smear-api-gpuDevelopment mode:
docker-compose --profile development up pap-smear-devFull production stack with monitoring:
docker-compose --profile production --profile monitoring up# Launch the user-friendly GUI
python launch_gui.pyThis opens a desktop application with:
- Simple drag-and-drop image upload
- Real-time processing with progress indicators
- Clear visualization of results
- No technical knowledge required
python main.py serveThen open your browser to http://localhost:8000 for the interactive web interface.
python -m uvicorn api.app:app --reload --host 0.0.0.0 --port 8000Access the API:
- API Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/api/v1/health
- Model Info: http://localhost:8000/api/v1/model/info
import requests
from pathlib import Path
# Single image prediction
with open('path/to/image.jpg', 'rb') as f:
response = requests.post(
'http://localhost:8000/api/v1/predict',
files={'file': f},
json={
'return_probabilities': True,
'return_heatmap': True,
'confidence_threshold': 0.7
}
)
result = response.json()
print(f"Prediction: {result['prediction']['class_name']}")
print(f"Confidence: {result['prediction']['confidence']:.3f}")Health check:
curl -X GET "http://localhost:8000/api/v1/health"Single prediction:
curl -X POST "http://localhost:8000/api/v1/predict" \
-H "Content-Type: multipart/form-data" \
-F "file=@image.jpg" \
-F 'request={"return_probabilities": true, "return_heatmap": true}'Batch prediction:
curl -X POST "http://localhost:8000/api/v1/predict/batch" \
-H "Content-Type: multipart/form-data" \
-F "files=@image1.jpg" \
-F "files=@image2.jpg" \
-F 'request={"prediction_params": {"return_probabilities": true}}'Pap Smeet Processing app/
βββ api/ # FastAPI application
β βββ app.py # Main FastAPI app
β βββ routes.py # API endpoints
β βββ models.py # Pydantic models
β βββ utils.py # API utilities
βββ data/ # Data processing
β βββ download.py # Dataset downloader
β βββ preprocessing.py # Image preprocessing
β βββ dataset.py # PyTorch datasets
βββ models/ # Model architectures
β βββ classifier.py # Patch classifier
β βββ mil.py # MIL models
β βββ trainer.py # Training utilities
β βββ utils.py # Model utilities
βββ inference/ # Inference and explainability
β βββ predictor.py # Prediction classes
β βββ explainability.py # Grad-CAM implementation
β βββ utils.py # Inference utilities
βββ utils/ # Shared utilities
β βββ logging.py # Logging setup
β βββ metrics.py # Evaluation metrics
β βββ visualization.py # Plotting utilities
β βββ io.py # File I/O utilities
βββ config.py # Configuration
βββ requirements.txt # Dependencies
βββ Dockerfile # Docker configuration
βββ docker-compose.yml # Docker Compose setup
- Data Pipeline: Automated SIPaKMeD dataset download, preprocessing, and augmentation
- Model Training: Transfer learning with ResNet/EfficientNet backbones
- Inference Engine: Fast prediction with batch processing support
- Explainability: Grad-CAM heatmaps for model interpretability
- MIL Pipeline: Multiple Instance Learning for slide-level classification
- REST API: Production-ready FastAPI service
- Monitoring: Comprehensive logging and experiment tracking
- Download SIPaKMeD dataset:
python -m data.download --force- Create data splits:
python -c "
from data.dataset import create_data_splits
from config import data_config
create_data_splits(
data_config.raw_data_path / 'sipakmed',
data_config.processed_data_path / 'splits'
)"from models.trainer import ModelTrainer
from models.classifier import create_model
from data.dataset import create_dataloaders
# Create model
model = create_model("patch_classifier")
# Create data loaders
dataloaders = create_dataloaders(
data_config.processed_data_path / 'splits',
batch_size=32
)
# Train model
trainer = ModelTrainer(
model=model,
train_loader=dataloaders['train'],
val_loader=dataloaders['val']
)
history = trainer.train()# GPU training
docker-compose --profile training up pap-smear-trainer| Endpoint | Method | Description |
|---|---|---|
/api/v1/health |
GET | Health check and system status |
/api/v1/predict |
POST | Single image prediction |
/api/v1/predict/batch |
POST | Batch image prediction |
/api/v1/model/info |
GET | Model information |
/api/v1/statistics |
GET | API usage statistics |
/api/v1/feedback |
POST | Submit prediction feedback |
| Endpoint | Method | Description |
|---|---|---|
/api/v1/admin/load_model |
POST | Load new model |
/api/v1/admin/models |
GET | List loaded models |
{
"success": true,
"prediction": {
"class_index": 0,
"class_name": "superficial_squamous",
"probability": 0.85,
"confidence": 0.85
},
"probabilities": {
"superficial_squamous": 0.85,
"intermediate_squamous": 0.10,
"columnar_epithelial": 0.05
},
"metrics": {
"confidence": 0.85,
"uncertainty": 0.15,
"confidence_level": "high",
"reliable_prediction": true
},
"processing_info": {
"inference_time_ms": 150.5,
"image_size": [512, 512]
}
}The application uses a centralized configuration system in config.py:
# Model configuration
model_config.backbone = "resnet50"
model_config.num_classes = 8
model_config.batch_size = 32
model_config.learning_rate = 1e-4
# API configuration
api_config.host = "0.0.0.0"
api_config.port = 8000
api_config.max_file_size = 50 * 1024 * 1024 # 50MB
# Logging configuration
logging_config.use_wandb = True
logging_config.wandb_project = "pap-smear-classification"# API Configuration
API_PORT=8000
API_HOST=0.0.0.0
# Weights & Biases
WANDB_PROJECT=pap-smear-classification
WANDB_ENTITY=your-username
# Logging
LOG_LEVEL=INFOThe application integrates with Weights & Biases for experiment tracking:
from utils.logging import create_experiment_logger
# Create experiment logger
exp_logger = create_experiment_logger(
"training_experiment",
config={"learning_rate": 0.001, "batch_size": 32}
)
# Log metrics
exp_logger.log_metrics({"accuracy": 0.95, "loss": 0.05}, step=1)
# Log artifacts
exp_logger.log_artifact(model_path, "model")When using the monitoring profile, Prometheus metrics are available at:
- Prometheus: http://localhost:9090
- Grafana: http://localhost:3000 (admin/admin123)
pytest tests/ -v --cov=.# Test health endpoint
curl http://localhost:8000/api/v1/health
# Test prediction with sample image
python scripts/test_api.py# Install locust
pip install locust
# Run load test
locust -f tests/load_test.py --host=http://localhost:8000- Build production image:
docker build --target production -t pap-smear-api:latest .- Deploy with Docker Compose:
docker-compose --profile production up -d- Deploy to Kubernetes:
kubectl apply -f k8s/- Model Optimization: Convert to ONNX or TorchScript for faster inference
- Caching: Redis integration for response caching
- Load Balancing: Nginx reverse proxy with multiple API instances
- GPU Acceleration: CUDA support for faster inference
- Input Validation: Comprehensive file type and size validation
- Rate Limiting: Built-in request rate limiting
- Authentication: JWT token support (configurable)
- HTTPS: SSL/TLS support via Nginx
- Container Security: Non-root user in production containers
- CPU: 4+ cores recommended
- RAM: 8GB minimum, 16GB recommended
- GPU: CUDA-capable GPU for training (optional for inference)
- Storage: 10GB+ for datasets and models
See requirements.txt for complete list. Key dependencies:
- PyTorch: Deep learning framework
- FastAPI: Web framework
- OpenCV: Image processing
- Albumentations: Data augmentation
- Weights & Biases: Experiment tracking
- Grad-CAM: Model explainability
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
# Install development dependencies
pip install -r requirements-dev.txt
# Install pre-commit hooks
pre-commit install
# Run tests
pytest
# Run linting
flake8 .
black .
isort .This project is licensed under the MIT License - see the LICENSE file for details.
- SIPaKMeD Dataset: Plissiti, M.E., Dimitrakopoulos, P., Sfikas, G., Nikou, C., Krikoni, O., Charchanti, A.: SIPAKMED: A New Database for Identification of Classes and Segmentation of Nuclei in Pap Smear Images. Computational and Mathematical Methods in Medicine 2018, 8797906:1-8797906:13 (2018)
- PyTorch Team: For the excellent deep learning framework
- FastAPI Team: For the modern web framework
- Grad-CAM Authors: For explainable AI techniques
For questions, issues, or contributions:
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: support@papsmear-ai.com
This software is for research and educational purposes only. It should not be used for clinical diagnosis without proper validation and expert oversight. Always consult qualified medical professionals for medical decisions.
Built with β€οΈ for advancing medical AI research