Back to Home
NeuroLight - AI Brain Logo

Intro to AI with Neuroimaging Data

A Complete End-to-End Tutorial Using PyTorch

Table of Contents

Introduction

Artificial Intelligence

AI refers to computer systems that can perform tasks typically requiring human intelligence, such as pattern recognition, decision-making, and learning from data.
has revolutionized neuroscience research, enabling us to extract meaningful patterns from complex brain imaging data. This tutorial demonstrates how deep learning
Deep learning uses artificial neural networks with multiple layers to automatically learn hierarchical features from data, mimicking how the brain processes information.
can classify biological sex from structural MRI scans with remarkable accuracy.

What You'll Master

  • Neuroimaging Data Handling: Load, preprocess, and normalize 3D MRI volumes using nibabel and PyTorch
  • SFCN Architecture: Understand the Simple Fully Convolutional Network
    SFCN is a 3D CNN architecture specifically designed for neuroimaging, using only convolutional layers without fully connected layers to reduce overfitting.
    designed for brain imaging
  • Advanced Training Techniques: Implement k-fold cross-validation, early stopping, and stratified sampling
  • Performance Evaluation: Calculate sensitivity
    Sensitivity (also called recall) measures the proportion of actual positive cases that are correctly identified. Formula: True Positives / (True Positives + False Negatives)
    , specificity
    Specificity measures the proportion of actual negative cases that are correctly identified. Formula: True Negatives / (True Negatives + False Positives)
    , and interpret confusion matrices
  • Research Ethics: Understand bias considerations and reproducible research practices
Technical Achievement: This tutorial achieves 87.5% accuracy on sex classification using HCP (Human Connectome Project) T1-weighted MRI data, demonstrating that structural brain differences are detectable using AI methods.

We'll work with real HCP dataset containing 848 subjects with T1-weighted structural MRI scans. The tutorial covers the complete pipeline from raw NIfTI files to trained model, including common pitfalls and solutions that took months to discover through trial and error.

Prerequisites & Setup

Python & Data Science

Intermediate Python, NumPy array manipulation, pandas DataFrames, and matplotlib visualization.

Deep Learning & PyTorch

Understanding of backpropagation

Backpropagation is the algorithm used to train neural networks by calculating gradients and updating weights to minimize loss.
, optimizers, and PyTorch tensor operations.

Medical Imaging

Familiarity with NIfTI format, 3D image processing, and basic neuroanatomy concepts.

Statistics & ML

Cross-validation, confusion matrix

A confusion matrix is a table showing correct vs predicted classifications. It shows True Positives, True Negatives, False Positives, and False Negatives.
interpretation, and statistical significance testing.

System Requirements & Installation

Hardware Requirements: Minimum 16GB RAM (32GB recommended), NVIDIA GPU with 6GB+ VRAM for training, 100GB+ free disk space for datasets.
# Core dependencies - exact versions from tutorial
pip install torch==1.13.1 torchvision==0.14.1
pip install nibabel==4.0.2 numpy==1.23.5 pandas==1.5.2
pip install scikit-learn==1.2.0 matplotlib==3.6.2 seaborn==0.12.1
pip install torchinfo==1.7.1  # For model architecture visualization
pip install jupyter notebook ipywidgets  # Interactive development

# Verify CUDA availability (optional but recommended)
python -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}')"

Common Installation Pitfall

Problem: PyTorch CUDA version mismatch causing slow CPU training.

Solution: Check your CUDA version with nvidia-smi and install matching PyTorch version from pytorch.org.

# Check CUDA version
nvidia-smi

# For CUDA 11.7, install:
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu117

# Verify installation
python -c "import torch; print(torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'CPU only')"

Pro Tips for Setup

  • Use conda environments to avoid dependency conflicts
  • Test with small data first to verify installation before downloading large datasets
  • Monitor GPU memory with nvidia-smi -l 1 during training
  • Set random seeds for reproducible results: torch.manual_seed(42)

Understanding the Dataset

We use the Human Connectome Project (HCP) dataset containing 848 subjects with high-quality T1-weighted structural MRI scans. This dataset demonstrates how AI can detect subtle sexual dimorphism

Sexual dimorphism refers to differences in characteristics between males and females of the same species, beyond reproductive organs. In neuroimaging, this includes structural brain differences.
in brain structure that are invisible to the human eye.

Sample metadata CSV showing dataset structure
HCP metadata structure showing Subject ID, Gender (M/F), and Age ranges for 848 participants
Dataset Statistics: Total: 848 subjects | Males: 395 (46.6%) | Females: 453 (53.4%) | Age range: 22-37 years | Class ratio: 1:1.28 (Male:Female)
1

Data Structure & Preprocessing

Each subject has a preprocessed T1-weighted scan (91×109×91 voxels) already skull-stripped and normalized to MNI152 space. The compact size enables efficient training while preserving anatomical detail.

# Actual data loading from tutorial
RANDOM_IMAGE_PATH = '/data/ninad/fieldmaps/HCP_T1w/sub-100610.nii.gz'
raw_image = nib.load(RANDOM_IMAGE_PATH).get_fdata()
print(f'Raw image shape: {raw_image.shape}')  # (91, 109, 91)

# After dataset processing
train_image = train_dataset.__getitem__(1)[0]
print(f'Train image shape: {train_image.shape}')  # torch.Size([1, 91, 109, 91])

# Batch processing
train_batch = next(iter(train_dl))
print(f'Batch shape: {train_batch[0].shape}')  # torch.Size([16, 1, 91, 109, 91])
2

Label Encoding & Data Splits

Gender labels are encoded as Male=0, Female=1. Age ranges (e.g., "22-25") are converted to midpoint values for potential regression tasks.

# Label preprocessing from the actual code
df['Gender'] = df['Gender'].map({'M': 0, 'F': 1}).astype(int)
df['Age'] = df['Age'].apply(lambda x: 
    (int(x.split('-')[0]) + int(x.split('-')[1])) // 2 
    if '-' in x else int(x[:-1])).astype(float)

Critical Data Pitfall: Class Imbalance

Problem: Initial training showed poor performance due to ignoring the 1:1.28 class ratio.

Solution: Implement stratified sampling

Stratified sampling ensures that training, validation, and test sets maintain the same class distribution as the original dataset.
and consider weighted loss functions.

# Stratified splitting to maintain class balance
def stratified_split_classification(dataset, test_size):
    labels = [dataset[i][-1] for i in range(len(dataset))]
    train_indices, test_indices = train_test_split(
        range(len(dataset)), test_size=test_size, stratify=labels
    )
    return Subset(dataset, train_indices), Subset(dataset, test_indices)

# Alternative: Use weighted loss for imbalanced classes
pos_weight = torch.tensor(1.28, dtype=torch.float32)  # Female/Male ratio
criterion = nn.BCEWithLogitsLoss(pos_weight=pos_weight)

Data Quality Checks

  • Verify data integrity: Check for corrupt NIfTI files and missing labels
  • Inspect intensity distributions: Ensure proper normalization across subjects
  • Validate splits: Confirm stratification maintains class ratios in all splits
  • Monitor for data leakage: Ensure no subject appears in multiple splits

What Makes This Dataset Ideal for Learning?

  • High Quality: HCP uses standardized protocols and rigorous quality control
  • Preprocessed: Data is already skull-stripped, registered, and normalized
  • Balanced Task: Sex classification is challenging but achievable (~85-90% accuracy ceiling)
  • Clear Ground Truth: Biological sex is unambiguous and well-documented
  • Reasonable Size: Large enough for deep learning but manageable for learning

Data Preprocessing

Proper preprocessing is crucial for neuroimaging data. MRI scans need to be normalized, potentially resized, and prepared for deep learning models.

Key Preprocessing Steps

1

Data Loading

Load NIfTI files using nibabel and extract the volumetric data arrays.

import nibabel as nib
import numpy as np
import torch

def load_nifti(file_path):
    """Load NIfTI file and return the data array"""
    nii = nib.load(file_path)
    data = nii.get_fdata()
    return data
2

Normalization

Normalize intensity values to ensure consistent input ranges across different scans.

3

Resizing

Resize volumes to a consistent shape suitable for your neural network architecture.

Preprocessing Best Practices

  • Always normalize your data to prevent training instabilities
  • Consider data augmentation for better generalization
  • Ensure consistent preprocessing across training and test sets
  • Monitor memory usage when working with large 3D volumes

Building the Neural Network

For neuroimaging data, we'll use a 3D Convolutional Neural Network (3D CNN) that can effectively process volumetric brain data.

Detailed SFCN model architecture summary
Detailed model architecture showing the SFCN (Simple Fully Convolutional Network) structure

Model Architecture

The Simple Fully Convolutional Network (SFCN) is designed specifically for neuroimaging data. It uses 3D convolutions to capture spatial relationships in brain volumes.

import torch
import torch.nn as nn
import torch.nn.functional as F

class SFCN(nn.Module):
    def __init__(self, num_classes=2):
        super(SFCN, self).__init__()
        
        # 3D Convolutional layers
        self.conv1 = nn.Conv3d(1, 32, kernel_size=3, padding=1)
        self.conv2 = nn.Conv3d(32, 64, kernel_size=3, padding=1)
        self.conv3 = nn.Conv3d(64, 128, kernel_size=3, padding=1)
        
        # Pooling and dropout
        self.pool = nn.MaxPool3d(2, 2)
        self.dropout = nn.Dropout3d(0.5)
        
        # Global average pooling
        self.global_avg_pool = nn.AdaptiveAvgPool3d(1)
        
        # Final classifier
        self.classifier = nn.Linear(128, num_classes)
    
    def forward(self, x):
        # Convolutional layers with activation and pooling
        x = F.relu(self.conv1(x))
        x = self.pool(x)
        
        x = F.relu(self.conv2(x))
        x = self.pool(x)
        
        x = F.relu(self.conv3(x))
        x = self.pool(x)
        
        # Apply dropout
        x = self.dropout(x)
        
        # Global average pooling
        x = self.global_avg_pool(x)
        x = x.view(x.size(0), -1)
        
        # Final classification
        x = self.classifier(x)
        
        return x

Architecture Highlights

  • 3D Convolutions: Capture spatial relationships in 3D brain volumes
  • Progressive Feature Learning: Increasing filter numbers (32→64→128)
  • Global Average Pooling: Reduces overfitting compared to fully connected layers
  • Dropout: Regularization technique to improve generalization

Training Process

Training a neural network on neuroimaging data requires careful consideration of batch size, learning rate, and regularization techniques.

Training Configuration

# Training configuration
BATCH_SIZE = 8  # Small batch size due to memory constraints
LEARNING_RATE = 0.001
NUM_EPOCHS = 50
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Model, loss, and optimizer
model = SFCN(num_classes=2).to(DEVICE)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)

# Learning rate scheduler
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=20, gamma=0.5)
1

Data Loading

Create DataLoaders with appropriate batch sizes for memory-efficient training.

2

Training Loop

Implement forward pass, loss calculation, backpropagation, and parameter updates.

3

Validation

Regular evaluation on validation set to monitor overfitting and model performance.

Memory Considerations

3D neuroimaging data is memory-intensive. Use smaller batch sizes (typically 4-8) and consider gradient accumulation if needed. Monitor GPU memory usage during training.

Model Evaluation

Proper evaluation is crucial for understanding model performance and ensuring reliable results in neuroimaging applications.

Initial confusion matrix with poor results
Initial confusion matrix showing poor performance without proper training techniques
Final confusion matrix with improved results
Final confusion matrix after implementing cross-validation and early stopping

Key Evaluation Metrics

Accuracy

Overall percentage of correct predictions across all classes.

Precision

Proportion of positive identifications that were actually correct.

Recall

Proportion of actual positives that were correctly identified.

F1-Score

Harmonic mean of precision and recall, useful for imbalanced datasets.

Model Improvements

  • Cross-Validation: Implemented k-fold cross-validation for robust evaluation
  • Early Stopping: Prevented overfitting by stopping when validation loss plateaued
  • Data Augmentation: Increased dataset diversity with geometric transformations
  • Regularization: Used dropout and weight decay for better generalization

Results & Discussion

Final Model Performance

87.5%

Final Accuracy

0.89

F1-Score

92.1%

Precision

85.3%

Recall

Sample model predictions on test data
Sample predictions showing the model's performance on test brain scans

Key Findings

Biological Insights

The successful classification suggests that there are measurable structural differences between male and female brains that can be detected by deep learning models. This has implications for understanding brain development and potential clinical applications.

Conclusion

This tutorial demonstrated a complete workflow for applying AI to neuroimaging data using PyTorch. We covered essential aspects from data preprocessing to model evaluation, showing how deep learning can extract meaningful patterns from brain MRI scans.

Key Takeaways

1

Data Preprocessing is Critical

Proper normalization and preprocessing significantly impact model performance in neuroimaging applications.

2

3D CNNs are Powerful

3D convolutional networks can effectively learn from volumetric brain data, capturing spatial relationships.

3

Proper Evaluation Matters

Cross-validation and multiple metrics provide a comprehensive view of model performance.

4

Regularization Improves Generalization

Techniques like dropout and early stopping help prevent overfitting on neuroimaging data.

Future Directions

Ready to Explore Further?

This tutorial provides a foundation for AI in neuroscience. Consider exploring our curated neuroimaging datasets on NeuroDataHub to apply these techniques to different research questions and brain disorders.

Read the Complete Tutorial on Medium