A high-performance medical image classification system for brain MRI scans, built using the Mojo programming language for near-C performance with Python interoperability for data handling.
- High-Performance Training: SIMD-optimized tensor operations in Mojo
- 3D CNN Architectures: VGG-style and ResNet-style models for volumetric data
- Medical Format Support: NIfTI (.nii, .nii.gz) and DICOM loading via Python
- Patient-Aware Splitting: Prevents data leakage between train/val/test sets
- Complete Pipeline: Data loading β Preprocessing β Training β Evaluation
- 3D Augmentations: Rotations, flips, elastic deformations
mri/
βββ data/ # Python data loading
β βββ loader.py # NIfTI/DICOM loaders
β βββ preprocessing.py # Intensity normalization
β βββ augmentation.py # 3D augmentations
βββ models/ # Mojo neural network
β βββ tensor3d.mojo # SIMD-optimized tensors
β βββ layers.mojo # Conv3D, BatchNorm, Pool
β βββ activations.mojo # ReLU, Softmax
β βββ network.mojo # VGG3D, ResNet3D
βββ training/ # Training pipeline
β βββ loss.mojo # Cross-entropy loss
β βββ optimizers.mojo # SGD, Adam, AdamW
β βββ scheduler.mojo # LR scheduling
β βββ trainer.mojo # Training loop
βββ evaluation/ # Metrics & visualization
β βββ metrics.mojo # Accuracy, F1, confusion
β βββ visualize.py # Plotting utilities
βββ scripts/ # Entry points
β βββ train.mojo # Training script
β βββ evaluate.mojo # Evaluation script
βββ config/
β βββ config.yaml # Hyperparameters
βββ requirements.txt # Python dependencies
βββ setup_wsl.sh # WSL2 setup script
βββ README.md
- Windows 10/11 with WSL2 (Ubuntu recommended)
- 8GB+ RAM (16GB recommended for large volumes)
- GPU: Optional but recommended (CUDA-compatible)
- WSL2 with Ubuntu 22.04
- Mojo SDK (via Modular CLI)
- Python 3.10+
# In PowerShell as Administrator
wsl --install# Run the setup script
cd /mnt/c/mri
chmod +x setup_wsl.sh
./setup_wsl.shOr manually:
# Install Modular CLI
curl -s https://get.modular.com | sh -
# Install Mojo
modular install mojo
# Add to PATH
echo 'export MODULAR_HOME="$HOME/.modular"' >> ~/.bashrc
echo 'export PATH="$MODULAR_HOME/pkg/packages.modular.com_mojo/bin:$PATH"' >> ~/.bashrc
source ~/.bashrccd /mnt/c/mri
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtdata/
βββ healthy/ # Class 0
β βββ patient_001.nii.gz
β βββ patient_002.nii.gz
β βββ ...
βββ diseased/ # Class 1
βββ patient_101.nii.gz
βββ patient_102.nii.gz
βββ ...
- NIfTI:
.nii,.nii.gz(recommended) - DICOM: Directory per patient with
.dcmfiles
# From WSL2
cd /mnt/c/mri
source .venv/bin/activate
# Run training
mojo run scripts/train.mojomojo run scripts/evaluate.mojofrom data import load_nifti, PreprocessingPipeline, AugmentationPipeline
# Load a single volume
volume, header = load_nifti("path/to/scan.nii.gz", return_header=True)
# Create preprocessing pipeline
preprocess = PreprocessingPipeline()
preprocess.add_normalize("zscore")
preprocess.add_resample((64, 64, 64))
processed = preprocess(volume)
# Create augmentation pipeline
augment = AugmentationPipeline()
augment.add_flip(prob=0.5)
augment.add_rotation(max_angle=15, prob=0.5)
augmented = augment(processed)Edit config/config.yaml to adjust hyperparameters:
data:
input_shape: [64, 64, 64]
num_channels: 1
model:
type: "vgg3d"
num_classes: 2
base_filters: 32
dropout_rate: 0.5
training:
epochs: 100
batch_size: 4
learning_rate: 0.001
optimizer: "adam"Input (1, 64, 64, 64)
β
Conv3D(32) β BN β ReLU β MaxPool
β
Conv3D(64) β BN β ReLU β MaxPool
β
Conv3D(128) β BN β ReLU β MaxPool
β
Conv3D(256) β BN β ReLU β GlobalAvgPool
β
Linear(128) β ReLU β Dropout(0.5)
β
Linear(num_classes) β Softmax
β
Output (num_classes)
Benchmarks on synthetic 64Γ64Γ64 volumes:
| Operation | Pure Python/NumPy | Mojo | Speedup |
|---|---|---|---|
| Forward Pass (batch=4) | ~2.5s | ~0.08s | ~31x |
| Backward Pass | ~4.2s | ~0.15s | ~28x |
| Full Epoch (100 batches) | ~12 min | ~30s | ~24x |
Benchmarks on Intel i7-10700, 32GB RAM. Mojo provides significant speedups through SIMD vectorization and efficient memory access.
β οΈ Disclaimer: This is a research/development implementation. Clinical deployment requires:
- Regulatory approval (FDA, CE marking, etc.)
- Extensive clinical validation
- Professional medical oversight
- Should not replace professional medical judgment
- Patient data should be de-identified
- Follow HIPAA/GDPR compliance requirements
- Use secure data handling practices
-
Mojo not found: Ensure Modular is in PATH
source ~/.bashrc mojo --version
-
Memory errors: Reduce batch size in config
training: batch_size: 2 # Reduce from 4
-
NIfTI loading error: Install nibabel
pip install nibabel
Contributions are welcome! Areas of interest:
- GPU acceleration via MAX Engine
- Additional model architectures (U-Net, Attention)
- More medical formats (ANALYZE, BrainVoyager)
- Improved checkpoint serialization
MIT License - see LICENSE for details.