Nidl

Nidl is a Python library to perform distributed training and evaluation of deep learning models on large-scale neuroimaging data (anatomical volumes and surfaces, fMRI).

It follows the PyTorch design for the training logic and the scikit-learn API for the models (in particular fit, predict and transform).

Supervised, self-supervised and unsupervised models are available (with pre-trained weights) along with open datasets.

Quickstart

Get started with Nidl

Quickstart
Examples

Discover functionalities by reading examples

auto_examples/index.html
User guide

Learn about neuroimaging analysis

User guide

Key features

Datasets

Large-scale open neuroimaging datasets ready for PyTorch, fetched on demand from Hugging Face and streamed during training: OpenBHB (~4k quality-checked brain T1w MRI uniformly preprocessed) and OpenMind (114k brain MRIs). Bring your own data with ImageDataFrameDataset.

Models

Self-supervised (SimCLR, BarlowTwins, DCL, DINO, NeuroJEPA,), weakly supervised (YAwareContrastiveLearning) and generative (VAE) models, all exposing the scikit-learn fit / transform API.

Augmentations

3D-native preprocessing and data augmentation: RandomResizedCrop, RandomFlip, RandomRotation, RandomGaussianNoise, … Generate multiple views per sample with MultiViewsTransform.

Evaluation

Assess learned representations with any scikit-learn probe using ModelProbing, or monitor them while training with ModelProbingCallback and MetricsCallback.

Fit a model in a few lines

Models are configured like scikit-learn estimators and trained like Lightning modules: any Trainer argument (accelerator, devices, strategy, max_epochs, …) can be given to the estimator, so going from one GPU to a multi-node cluster requires no code change.

from torch.utils.data import DataLoader
from torchvision.transforms import Compose

from nidl.backbones.volume import resnet18
from nidl.datasets import OpenBHB
from nidl.estimators.ssl import SimCLR
from nidl.transforms import MultiViewsTransform
from nidl.transforms.volume.augmentation import (
    RandomFlip,
    RandomResizedCrop,
)

# Two augmented views of each brain volume
augment = Compose([
    RandomResizedCrop(target_shape=128, scale=(0.5, 1.0)),
    RandomFlip(axes=0, flip_probability=0.5),
])
dataset = OpenBHB(
    "/data/openbhb",
    modality="vbm",
    target=None,
    transforms=MultiViewsTransform(augment, n_views=2),
)
loader = DataLoader(dataset, batch_size=32, shuffle=True)

# Contrastive pre-training of a 3D ResNet-18 on 4 GPUs
model = SimCLR(
    encoder=resnet18(n_embedding=512),
    proj_input_dim=512,
    max_epochs=100,
    accelerator="gpu",
    devices=4,
)
model.fit(loader)
embeddings = model.transform(test_loader)

See Decoupled Contrastive Learning for a full example.

from nidl.estimators.ssl import YAwareContrastiveLearning

# The dataset now returns (views, age): age guides the
# contrastive objective through a Gaussian kernel
dataset = OpenBHB(
    "/data/openbhb",
    modality="vbm",
    target="age",
    transforms=MultiViewsTransform(augment, n_views=2),
)
loader = DataLoader(dataset, batch_size=32, shuffle=True)

model = YAwareContrastiveLearning(
    encoder=resnet18(n_embedding=512),
    proj_input_dim=512,
    bandwidth=4**2,
    max_epochs=100,
)
model.fit(loader)

See Weakly Supervised Contrastive Learning with y-Aware for a full example.

from sklearn.linear_model import Ridge

from nidl.estimators.probes import ModelProbing

# Fit a linear probe on top of the frozen embeddings
probing = ModelProbing(
    embedding_estimator=model,
    probe=Ridge(),
    scoring=["neg_mean_absolute_error", "r2"],
)
probing.fit(train_loader)
probing.score(test_loader)
# {'neg_mean_absolute_error': -5.1, 'r2': 0.78}

See Model probing of embedding estimators for a full example.

Backbones

3D architectures are available in nidl.backbones.volume: ResNet, DenseNet, AlexNet and Vision Transformers (including Mixture-of-Experts), with pre-trained weights on large neuroimaging cohorts. See the architectures guide.