Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

MISFIT: Medical Imaging Semantic Foundation Toolkit

Python PyPI Docker Docs Tests Coverage

MISFIT is a simple, scalable, end-to-end framework for pretraining 3D medical imaging foundation models using masked autoencoders (MAE). Give it a directory of unlabeled NIfTI files and it produces a pretrained encoder — no labels required.


What it does

MISFIT trains a masked autoencoder on 3D medical images, using a SwinUNETR or Primus transformer or a ResEnc CNN built from MIST's own models. At each training step, 75% of the image patches are randomly hidden, and the model learns to reconstruct them from the remaining context. This forces the encoder to build rich spatial representations of anatomy — representations that transfer well to downstream tasks like classification, segmentation, and retrieval.

Unlabeled NIfTIs  →  misfit_index  →  misfit_train  →  Pretrained Encoder
                                                               ↓
                                             misfit_encode  →  Raw Spatial Features (N_crops, C, D', H', W')
                                                               ↓
                                        misfit_embed_train  →  Trained Aggregator (optional)
                                                               ↓
                                              misfit_embed  →  Global Embedding (C,)  →  Retrieval / Classifier

Key Features

  • No labels required — pretrains entirely on unlabeled NIfTI volumes
  • Built for MIST — encoders are MIST's own SwinUNETR-V2, ResEnc, and Primus, so pretrained weights and config.json load directly into mist_train
  • Starts from nnFoundation — the openly released nnFoundationCNN and nnFoundationViT work in every command, as a baseline encoder or a starting point for pretraining
  • End-to-end pipeline — seven CLI commands take you from raw files to downstream-ready embeddings
  • 3D-native — operates on full volumetric data, not 2D slices
  • Mixed modality — the normalized_masked_mse loss normalizes per-patch variance, handling CT and MRI in the same training run
  • Scalable — single-GPU to multi-node training via torchrun; step-based epochs, a fixed validation subset, and mid-epoch checkpoints keep long runs on large corpora practical; the same command runs everywhere (CPU too, for testing)
  • BF16 AMP — automatic bfloat16 mixed precision on GPUs with BF16 matrix hardware (NVIDIA Ampere+ — A100, H100, RTX 30xx+ — and AMD CDNA / RDNA3+), with an automatic FP32 fallback on older GPUs and CPU
  • Reproducible — config.json captures every architecture and training hyperparameter; downstream commands require it rather than re-accepting flags
  • 100% test coverage

Installation

From PyPI:

pip install misfit-medical

Or run the container (CUDA 12.8, torch 2.9.1):

docker pull mistmedical/misfit:latest

From source (for development, or to add a new model / loss):

git clone https://github.com/mist-medical/MISFIT.git
cd MISFIT
pip install -e .

On an AMD ROCm machine, install a ROCm-enabled PyTorch build first (PyPI's default torch wheel is CUDA-only; the Docker image is CUDA-only too), matching your driver's ROCm version, then MISFIT on top with nothing extra:

pip install torch --index-url https://download.pytorch.org/whl/rocm6.4
pip install misfit-medical

Requirements: Python ≥ 3.10. A GPU with BF16 matrix hardware — NVIDIA Ampere or newer (A100, H100, RTX 30xx+) or AMD CDNA / RDNA3+ (MI200/MI300, RX 7000+) — is recommended, since it enables BF16 mixed precision. Pre-Ampere NVIDIA GPUs, older AMD GPUs (RDNA1/2), and CPU-only machines work too; MISFIT automatically falls back to FP32.


Quick Start

# 1. Build an index from a CSV of NIfTI paths
misfit_index --input  paths.csv \
             --output index.parquet

# 2. Pretrain a SwinMAE encoder
misfit_train --index   index.parquet \
             --results /runs/exp1

# 3. Evaluate reconstruction quality
misfit_evaluate --checkpoint /runs/exp1/models/final_model.pt \
                --index      index.parquet \
                --config     /runs/exp1/config.json \
                --output-csv /runs/exp1/eval_results.csv

# 4. Extract volume embeddings
misfit_embed --encoder-checkpoint /runs/exp1/models/final_model.pt \
             --index               index.parquet \
             --config              /runs/exp1/config.json \
             --output-dir          /data/embeddings

Pipeline

Stage 1 — Indexing (misfit_index)

Scans your NIfTI files in parallel and computes per-volume intensity statistics (p1, p99, foreground mean/std, bounding box) and voxel spacing. Assigns each volume to a train, val, or test split. The resulting Parquet index is the single input to all downstream commands.

misfit_index --input  /data/paths.csv \
             --output /data/index.parquet

The --input CSV must have a path column with absolute paths to .nii or .nii.gz files. Val gets 5% of the volumes, capped at 128, train gets the rest, and there's no test split; change this in the auto-generated index_config.json sidecar. Add an optional group column (e.g. a subject ID) to keep all of a patient's volumes in the same split.

Stage 2 — Pretraining (misfit_train)

Trains a masked autoencoder. At each step, 75% of the volume's patches are masked and the model reconstructs them from visible context. Supports single-GPU, multi-GPU, and multi-node training out of the box.

# Single GPU
misfit_train --index   /data/index.parquet \
             --results /runs/exp1

# 4 GPUs — batch size scales automatically
torchrun --nproc_per_node=4 -m misfit.cli.train_entrypoint \
    --index   /data/index.parquet \
    --results /runs/exp1

# Resume an interrupted run
misfit_train --index   /data/index.parquet \
             --results /runs/exp1 \
             --resume

Key options:

Flag Default Description
--model swinunetr-base swinunetr-*, resenc*, or primus-* (below)
--patch-size X Y Z 96 96 96 Spatial crop size (must be divisible by 32)
--target-spacing X Y Z off (native) Resample volumes to this spacing (e.g. 1 1 1)
--epochs 1000 Training epochs of --steps-per-epoch steps each
--steps-per-epoch 250 Optimizer steps per epoch
--batch-size 2 Per-GPU batch size
--loss normalized_masked_mse Loss function
--normalization-policy clip_and_z_score Intensity normalization (z_score_only for nnssl)

Training writes a config.json to --results that captures every architecture and hyperparameter decision. All downstream commands read this file — you never have to re-specify model flags.

Stage 3 — Evaluation (misfit_evaluate) and Inspection (misfit_inspect)

misfit_evaluate computes reconstruction metrics (MAE, MSE, PSNR, SSIM) and writes a per-volume CSV. It defaults to the val split; pass --split "" for all rows.

misfit_evaluate --checkpoint /runs/exp1/models/final_model.pt \
                --index      /data/index.parquet \
                --config     /runs/exp1/config.json \
                --output-csv /runs/exp1/eval_results.csv

misfit_inspect reconstructs volumes and saves outputs under two subdirectories — reconstructions/ (denormalized NIfTIs) and masks/ (binary masks, 1=masked/reconstructed, 0=visible). Load both in ITK-SNAP or 3D Slicer and overlay the mask (1=reconstructed, 0=visible) to highlight exactly which regions the model had to fill in from context. Defaults to all rows; use --split val to restrict to the validation set.

misfit_inspect --checkpoint /runs/exp1/models/final_model.pt \
               --index      /data/index.parquet \
               --config     /runs/exp1/config.json \
               --output-dir /runs/exp1/inspect

# Inspect only the validation split
misfit_inspect --checkpoint /runs/exp1/models/final_model.pt \
               --index      /data/index.parquet \
               --config     /runs/exp1/config.json \
               --output-dir /runs/exp1/inspect \
               --split      val

Output structure:

inspect/
    reconstructions/   <volume_id>.nii.gz   — full-volume reconstruction
    masks/             <volume_id>.nii.gz   — 1=masked (reconstructed), 0=visible

Stage 4 — Encoding & Embedding (misfit_encode, misfit_embed + misfit_embed_train)

misfit_encode caches the full spatial bottleneck feature map (N_crops, C, D', H', W') for every crop to disk — useful for fast aggregator training without re-running the encoder.

misfit_encode --encoder-checkpoint /runs/exp1/models/final_model.pt \
              --index               /data/index.parquet \
              --config              /runs/exp1/config.json \
              --output-dir          /data/encodings

misfit_embed runs the full encode-and-aggregate pipeline end-to-end, saving a single global (C,) embedding vector per volume. With --aggregator mean_pool (default), no aggregator training is needed — embeddings are ready immediately for zero-shot retrieval or UMAP visualization. Use --split to restrict to a specific split.

misfit_embed --encoder-checkpoint /runs/exp1/models/final_model.pt \
             --index               /data/index.parquet \
             --config              /runs/exp1/config.json \
             --output-dir          /data/embeddings

misfit_embed_train fine-tunes a lightweight aggregator on the cached features from misfit_encode for a downstream task. Supports classification (cross-entropy) and contrastive (Supervised Contrastive) objectives.

misfit_embed_train --input      /data/train_manifest.csv \
                   --output-dir /runs/agg \
                   --embed-dim  768

The input CSV has four columns: volume_id, split, features_path, label. features_path points to the .npz files produced by misfit_encode. Only split='train' rows are used for training.


Model Variants

Variant --model Parameters Recommended for
SwinUNETR Small swinunetr-small ~7M (4.8M encoder) Rapid prototyping, small datasets
SwinUNETR Base swinunetr-base ~28M (18.6M encoder) Standard pretraining (default)
SwinUNETR Large swinunetr-large ~110M (73.9M encoder) Large-scale datasets, maximum capacity
ResEnc resenc ~102M (90M encoder)¹ nnU-Net/nnssl-style CNN pretraining
ResEnc Pocket resenc-pocket ~1.8M (1.4M encoder)¹ Small CNN; needs MIST ≥ 3.0
Primus Medium primus-medium ~166M (146M encoder)² nnssl-style transformer pretraining
Primus Large primus-large ~381M (324M encoder)² Larger Primus
PrimusX primus-x ~649M (539M encoder)² The nnFoundationViT network

¹ At a patch size of 128³ or larger. ResEnc plans its depth from --patch-size; see Model Variants.

² At a patch size of 96³; the position embeddings grow with --patch-size.

Every architecture is built from MIST, so --model names match the MIST model each encoder fine-tunes into.

resenc and primus-x are the same networks as the openly released nnFoundationCNN and nnFoundationViT: their weights work directly in every MISFIT command, as a baseline encoder or as the starting point for --init-weights. See Starting from nnFoundation.


Output Structure

results/
    checkpoints/
        checkpoint.pt       Latest checkpoint (each epoch, and every 30 min)
    models/
        final_model.pt      Weights at the end of training (refreshed at each validation)
        encoder_weights.pt  The same encoder, in the MIST model's format
    logs/                   TensorBoard event files
    config.json             Reproducibility record — architecture, patch size,
                            all hyperparameters, and MISFIT version

Reproducibility

config.json is the single source of truth for model architecture. Every downstream command (misfit_evaluate, misfit_inspect, misfit_embed) requires it via --config rather than re-accepting architecture flags. This prevents silent mismatches between training and inference.

When resuming with --resume, MISFIT validates that the model name and patch size are unchanged (hard error if not). Changes to other hyperparameters emit a warning but are allowed.

Metadata

Release files for misfit-medical 0.3.0a0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for misfit-medical 0.3.0a0
File Size Uploaded Provenance Actions
misfit_medical-0.3.0a0.tar.gz 121.8 kB Without provenance Details for misfit_medical-0.3.0a0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for misfit-medical 0.3.0a0
File Interpreter ABI Platform Actions
misfit_medical-0.3.0a0-py3-none-any.whl Python 3 none any Details for misfit_medical-0.3.0a0-py3-none-any.whl

Total release size: 273.2 kB

Release files / misfit_medical-0.3.0a0.tar.gz

Download URL misfit_medical-0.3.0a0.tar.gz
Size 121.8 kB
Tags Source
SHA-256 checksum
How to use checksums
71c8c00c3cf1c900b49d433e49ac14e4e2c866339661683fb25315e9261131a8
BLAKE2b-256 checksum
How to use checksums
19e46b91637024d4f838f29e4e400a9bd619cc3297bd5a06d136c68ae82d3ef1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / misfit_medical-0.3.0a0-py3-none-any.whl

Download URL misfit_medical-0.3.0a0-py3-none-any.whl
Size 151.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0ffb9eb1cfa696af1cebd6e3056ce7e33a9aa16fc142c8306d3f89b491df3b6e
BLAKE2b-256 checksum
How to use checksums
b3b9bbc04603d1dd28253c03a64318ed3641d99c841f1672026348c6ab39d2da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for misfit-medical 0.3.0a0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page