Skip to content

NicolasNoya/CardiacPathologyPrediction

Repository files navigation

Cardiac Pathology Prediction πŸ«€

Python PyTorch License Hugging Face

Deep Learning-based cardiac MRI segmentation and pathology classification using a custom-implemented DenseNet architecture for automated cardiovascular disease diagnosis.

πŸš€ Quick Access

🎯 Project Overview

This project implements an end-to-end deep learning pipeline for cardiac pathology prediction from short-axis cardiac cine MR images. The system performs semantic segmentation of cardiac structures (left ventricle, right ventricle, and myocardium) using a fully convolutional DenseNet, followed by feature extraction and pathology classification.

Key Features:

  • πŸ—οΈ Custom DenseNet implementation built from scratch in PyTorch
  • πŸ”¬ Medical image processing pipeline for NIfTI (.nii) format MRI data
  • 🎯 Multi-class segmentation of cardiac structures (4 classes)
  • πŸ“Š Automated feature extraction from segmented cardiac regions
  • 🧠 Random Forest classifier for pathology prediction
  • πŸ“ˆ Comprehensive training pipeline with TensorBoard profiling
  • πŸ”„ Data augmentation and Region of Interest (ROI) extraction

Architecture

The project is based on the paper: "Densely Connected Fully Convolutional Network for Short-Axis Cardiac Cine MR Image Segmentation and Heart Diagnosis Using Random Forest"

πŸ—οΈ Project Structure

CardiacPathologyPrediction/
β”œβ”€β”€ densenet/                    # Custom DenseNet implementation
β”‚   β”œβ”€β”€ densenet.py             # Main network architecture
β”‚   β”œβ”€β”€ dense_block.py          # Dense block components
β”‚   β”œβ”€β”€ layer.py                # Individual layer definitions
β”‚   β”œβ”€β”€ transitions.py          # Transition layers (up/down sampling)
β”‚   └── custom_loss.py          # Combined loss function (CE + Dice)
β”œβ”€β”€ research/                    # Experimental scripts and prototypes
β”œβ”€β”€ data/                        # Dataset directory (not included in repo)
β”‚   β”œβ”€β”€ Train/                  # Training MRI images
β”‚   β”œβ”€β”€ Test/                   # Test MRI images
β”‚   β”œβ”€β”€ metaDataTrain.csv       # Patient metadata
β”‚   └── metaDataTest.csv        # Test metadata
β”œβ”€β”€ runs/                        # TensorBoard logs
β”œβ”€β”€ densnet_trainer.py          # Training pipeline orchestration
β”œβ”€β”€ feature_extractor.py        # Cardiac feature computation
β”œβ”€β”€ niidataloader.py            # Custom PyTorch dataset for NIfTI files
β”œβ”€β”€ roi.py                      # Region of Interest extraction
β”œβ”€β”€ profiler.py                 # TensorBoard profiling utilities
β”œβ”€β”€ model_weights.pth           # Trained model checkpoint
β”œβ”€β”€ presentation.ipynb          # Results and analysis notebook
└── requirements.txt            # Python dependencies

πŸ”§ Installation

Prerequisites

  • Python 3.10+
  • CUDA-compatible GPU (recommended for training)
  • 8GB+ RAM

Setup Instructions

  1. Clone the repository
git clone https://github.com/NicolasNoya/CardiacPathologyPrediction.git
cd CardiacPathologyPrediction
  1. Create a virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install PyTorch (select the appropriate version for your system)

    Visit PyTorch Get Started and choose your configuration.

    Example for CUDA 11.8:

    pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
  2. Install dependencies

pip install -r requirements.txt

πŸš€ Usage

Training the Model

from densnet_trainer import DenseNetTrainer

# Initialize trainer
trainer = DenseNetTrainer(
    path_to_images="data/Train",
    epochs=100,
    alpha=0.25,  # Combined loss weight
    train_fraction=0.8,
    check_val_every=10
)

# Train the model
trainer.train()

Loading Pre-trained Weights

Option 1: From Hugging Face (Recommended)

from huggingface_hub import hf_hub_download
from densenet.densenet import DenseNet
import torch

# Download weights from Hugging Face
model_path = hf_hub_download(
    repo_id="NicolasNoya/Heart_Segmentation",
    filename="model_weights.pth"
)

# Load model
model = DenseNet()
model.load_state_dict(torch.load(model_path, map_location='cpu'))
model.eval()

Option 2: From Local File

from densenet.densenet import DenseNet
import torch

model = DenseNet()
model.load_model("model_weights.pth")
model.eval()

Feature Extraction

from feature_extractor import FeatureExtractor

extractor = FeatureExtractor()
features = extractor.extract_features(
    diastole_image, 
    systole_image,
    height, 
    weight, 
    voxel_spacing
)

Visualization with TensorBoard

tensorboard --logdir=runs

Then navigate to http://localhost:6006 in your browser.

πŸ“Š Model Architecture

The DenseNet architecture consists of:

  • Inception-X initial layer (1 β†’ 24 channels)
  • Encoder path: 3 dense blocks (3, 4, 5 layers) with transition down
  • Bottleneck: Dense block with 8 layers
  • Decoder path: 3 dense blocks (5, 4, 3 layers) with transition up and skip connections
  • Output: 1Γ—1 convolution + softmax (4 classes)
  • Growth rate: 8 (bottleneck: 7)

Loss Function: Combined Cross-Entropy and Dice Loss

L = Ξ± Β· L_CE + (1 - Ξ±) Β· L_Dice

πŸ“ˆ Results

The model achieves competitive performance on cardiac MRI segmentation:

  • Effective segmentation of left ventricle, right ventricle, and myocardium
  • Robust to variations in cardiac anatomy
  • Efficient feature extraction for downstream pathology classification

Detailed results and visualizations are available in presentation.ipynb.

οΏ½ Uploading to Hugging Face

To make your model publicly accessible and showcase it to potential employers, you can upload it to Hugging Face Hub:

Step 1: Get Your Hugging Face Token

  1. Create an account at huggingface.co
  2. Go to Settings β†’ Access Tokens
  3. Create a new token with write permissions
  4. Copy the token

Step 2: Upload Your Model

# Set your token as environment variable (recommended)
export HF_TOKEN=your_token_here

# Run the upload script
python upload_to_huggingface.py --repo-name your-username/cardiac-densenet

Or pass the token directly:

python upload_to_huggingface.py --token your_token_here --repo-name your-username/cardiac-densenet

Step 3: Use Your Model from Hugging Face

Once uploaded, anyone can download and use your model:

from huggingface_hub import hf_hub_download
import torch

# Download weights
model_path = hf_hub_download(
    repo_id="your-username/cardiac-densenet",
    filename="model_weights.pth"
)

# Load and use
model = DenseNet()
model.load_state_dict(torch.load(model_path))
model.eval()

Benefits:

  • 🌐 Publicly accessible model weights
  • πŸ“Š Automatic model card generation
  • πŸ” Discoverable in Hugging Face search
  • πŸ’Ό Great for portfolio and job applications
  • πŸ“ˆ Track downloads and usage

�🧩 Key Components

Core Modules

Module Description
densnet_trainer.py Complete training pipeline with validation, metrics, and checkpointing
feature_extractor.py Extracts clinical features (volumes, ejection fraction, mass, etc.)
niidataloader.py PyTorch Dataset for loading NIfTI medical images with voxel spacing
roi.py Region of Interest extraction for focused cardiac analysis
profiler.py TensorBoard integration for training visualization and analysis

DenseNet Implementation

All DenseNet components were implemented from scratch:

  • Dense blocks with skip connections
  • Transition layers (downsampling/upsampling)
  • Custom loss functions
  • Inception-style initial layer

πŸ”¬ Technical Highlights

  • Medical Imaging: Custom dataloader for NIfTI format with proper voxel spacing handling
  • Deep Learning: Implemented DenseNet architecture with skip connections and dense blocks
  • Computer Vision: Semantic segmentation with connected component analysis
  • Data Augmentation: Rotation, flipping, and intensity transformations
  • Evaluation Metrics: Dice coefficient, IoU, confusion matrix
  • Profiling: TensorBoard integration for comprehensive training analysis

πŸ“ Notes

  • GPU Recommended: Training and inference are computationally intensive. A CUDA-enabled GPU is strongly recommended.
  • Dataset: The dataset is not included in this repository due to size and privacy constraints. The data structure expects NIfTI (.nii) format files organized by patient ID.
  • Research Folder: Contains experimental code and prototype implementations used during development.

🀝 Contributing

This project was developed as part of the IMA205 course challenge. Contributions, issues, and feature requests are welcome!

πŸ‘€ Author

Francisco NicolΓ‘s Noya

  • GitHub: @NicolasNoya
  • Project: IMA205 Challenge - Cardiac Pathology Prediction

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Based on research from: "Densely Connected Fully Convolutional Network for Short-Axis Cardiac Cine MR Image Segmentation and Heart Diagnosis Using Random Forest"
  • Built with PyTorch, TensorFlow, and scikit-learn
  • Medical imaging processing with NiBabel

⭐ If you find this project useful, please consider giving it a star!

πŸ“« Contact

For questions or collaborations, feel free to reach out through GitHub issues or discussions.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors