kaiko-ai/midnight

Model

21

stars

27

commits

10

repos using this model

16

linked in READMEs

Apr 15, 2025

updated

dino
dinov2
histology
image-feature-extraction
pathology
pytorch
safetensors
self-supervised
vision
vit

README

Kaiko midnight

Midnight - Training State-of-the-Art Pathology Foundation Models with Orders of Magnitude Less Data

This repository contains the model checkpoints for the Midnight-12k model presented in our paper titled "Training state-of-the-art pathology foundation models with orders of magnitude less data." Our approach achieves competitive performance compared to leading pathology foundation models (FMs), despite being trained on significantly fewer whole slide images (WSIs).

Overview

We propose a refined self-supervised training framework based on DINOv2 with modifications that optimize model performance specifically for computational pathology. Our main contributions include:

  • Three novel pathology FMs trained with significantly reduced data (up to 100x fewer WSIs).
  • Introduction of high-resolution post-training to enhance embedding quality.

Model Highlights

  • Midnight-12k: Trained exclusively on the publicly available TCGA dataset (12k WSIs).
  • Midnight-92k: Trained on TCGA and an additional proprietary dataset from the Netherlands Cancer Institute (NKI-80k).
  • Midnight-92k/392: Our top-performing model fine-tuned with high-resolution post-training.

Model Weights

  • Midnight-12k: Publicly available under the permissive MIT license.
  • Midnight-92k & Midnight-92k/392: Trained on proprietary datasets and subject to restricted access.

Usage

Our models are trained on 224x224 images normalized with a mean of (0.5, 0.5, 0.5) and a standard deviation of (0.5, 0.5, 0.5). Please ensure you apply these exact normalization parameters when preparing your datasets for embedding extraction.

from transformers import AutoImageProcessor, AutoModel
from PIL import Image
import requests
from torchvision.transforms import v2

url = 'https://upload.wikimedia.org/wikipedia/commons/8/80/Breast_DCIS_histopathology_%281%29.jpg'
image = Image.open(requests.get(url, stream=True).raw)

transform = v2.Compose(
    [
        v2.Resize(224),
        v2.CenterCrop(224),
        v2.ToTensor(),
        v2.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)),
    ]
)
model = AutoModel.from_pretrained('kaiko-ai/midnight')

Extract embeddings for classification

For segmentation tasks, the model output corresponds to 16x16 patch tokens (derived from 224/14=16).

import torch

def extract_classification_embedding(tensor):
    cls_embedding, patch_embeddings = tensor[:, 0, :], tensor[:, 1:, :]
    return torch.cat([cls_embedding, patch_embeddings.mean(1)], dim=-1)

batch = transform(image).unsqueeze(dim=0)
embedding = extract_classification_embedding(model(batch).last_hidden_state)
print(f"Embedding shape: {embedding[0].shape}")

Extract embeddings for segmentation

import math
import torch

def extract_segmentation_embedding(tensor):
    features = tensor[:, 1:, :].permute(0, 2, 1)
    batch_size, hidden_size, patch_grid = features.shape
    height = width = int(math.sqrt(patch_grid))
    return features.view(batch_size, hidden_size, height, width)

batch = transform(image).unsqueeze(dim=0)
embedding = extract_segmentation_embedding(model(batch).last_hidden_state)
print(f"Embedding shape: {embedding[0].shape}")

Training Datasets

DatasetWSIsSourceComment
TCGA12kPublicFFPE only
NKI-80k80kProprietary10,141 patients, 31 organs

Training Components

  • DINOv2: Self-supervised training with DINOv2.
  • KDE regularizer: Replaced KoLeo in DINOv2 to ensure embedding diversity and training stability.
  • Online patching: Efficient real-time extraction of informative tiles.
  • Color augmentation (HED): Robustness to stain variations.
  • Tile filtering: Removal of low-informative tissue regions.

Evaluation

We comprehensively evaluated the models using two sets of open-source benchmarks:

  • eva: For both tile (classification, segmentation) and slide-level tasks.
  • HEST: For gene expression prediction tasks (regression).

Our best model Midnight-92k/392 consistently outperforms or matches leading models like Virchow2 and UNI-2.

Results Summary

ModelAVG.PCam 10 shotsBACHBRACSBreaKHisCRCGleasonMHISTPCamCam16 (small)Panda (small)CoNSePMoNuSACHEST
Midnight-92k/3920.7780.9000.9040.6460.8020.9660.8070.8280.9510.8680.6510.6620.7080.415
UNI-20.7760.8850.9240.6510.8630.9700.7770.8290.9510.8730.6660.6260.6440.431
Midnight-92k0.7670.8820.8890.6150.7930.9670.8230.8310.9480.8720.6430.6290.6560.425
Virchow20.7660.8350.8900.6330.8180.9660.7910.8650.9380.8600.6460.6400.6740.403
Midnight-12k0.7630.8030.9070.6390.8400.9670.7900.8150.9310.8690.6560.6250.6640.412
Kaiko-B80.7570.7990.8760.6410.8420.9600.7610.8300.9200.8360.6500.6440.6860.391
H-Optimus-00.7550.8310.7520.6200.8130.9620.7690.8500.9430.8470.6720.6440.6870.425
Prov_GigaPath0.7520.8530.7940.6260.8460.9590.7270.8310.9440.8120.6570.6280.6880.405
Hibou-L0.7510.8250.7920.6430.7670.9540.7660.8500.9490.8520.6540.6460.6680.397
UNI0.7490.8330.7970.6130.8080.9540.7590.8410.9370.8540.6620.6270.6620.391
Phikon0.7240.8260.7440.5790.7150.9460.7430.8240.9190.8220.6480.6240.6440.377
Phikon-v20.7180.7560.7370.6070.7250.9530.7530.7960.9000.8070.6340.6260.6450.391
Lunit0.7140.7630.7850.6270.7590.9430.7580.7850.9050.7590.6040.6000.6300.362
vitg14 (nat. img.)0.6740.7210.7240.5780.7830.9430.7400.8550.8810.5000.5090.5650.6140.351
vitg14 (initial)0.4930.6520.4740.4130.4250.7540.4590.5780.7630.5260.3040.4620.4320.166

Citation

@article{KDK2025,
  title={Training state-of-the-art pathology foundation models with orders of magnitude less data},
  author={Mikhail Karasikov and Joost van Doorn and Nicolas Känzig and Melis Erdal Cesur and Hugo Mark Horlings and Robert Berke and Fei Tang and Sebastian Otálora},
  year={2025},
  journal={arXiv preprint arXiv:2504.05186},
  url={https://arxiv.org/abs/2504.05186},
}

Contributors

MK
sebastianffx

7 commits

kaiko-ai-user

4 commits

karasikov

4 commits

Linked in READMEs

kaiko-ai/midnight

Model

21

stars

27

commits

10

repos using this model

16

linked in READMEs

Apr 15, 2025

updated

dino
dinov2
histology
image-feature-extraction
pathology
pytorch
safetensors
self-supervised
vision
vit

README

Kaiko midnight

Midnight - Training State-of-the-Art Pathology Foundation Models with Orders of Magnitude Less Data

This repository contains the model checkpoints for the Midnight-12k model presented in our paper titled "Training state-of-the-art pathology foundation models with orders of magnitude less data." Our approach achieves competitive performance compared to leading pathology foundation models (FMs), despite being trained on significantly fewer whole slide images (WSIs).

Overview

We propose a refined self-supervised training framework based on DINOv2 with modifications that optimize model performance specifically for computational pathology. Our main contributions include:

  • Three novel pathology FMs trained with significantly reduced data (up to 100x fewer WSIs).
  • Introduction of high-resolution post-training to enhance embedding quality.

Model Highlights

  • Midnight-12k: Trained exclusively on the publicly available TCGA dataset (12k WSIs).
  • Midnight-92k: Trained on TCGA and an additional proprietary dataset from the Netherlands Cancer Institute (NKI-80k).
  • Midnight-92k/392: Our top-performing model fine-tuned with high-resolution post-training.

Model Weights

  • Midnight-12k: Publicly available under the permissive MIT license.
  • Midnight-92k & Midnight-92k/392: Trained on proprietary datasets and subject to restricted access.

Usage

Our models are trained on 224x224 images normalized with a mean of (0.5, 0.5, 0.5) and a standard deviation of (0.5, 0.5, 0.5). Please ensure you apply these exact normalization parameters when preparing your datasets for embedding extraction.

from transformers import AutoImageProcessor, AutoModel
from PIL import Image
import requests
from torchvision.transforms import v2

url = 'https://upload.wikimedia.org/wikipedia/commons/8/80/Breast_DCIS_histopathology_%281%29.jpg'
image = Image.open(requests.get(url, stream=True).raw)

transform = v2.Compose(
    [
        v2.Resize(224),
        v2.CenterCrop(224),
        v2.ToTensor(),
        v2.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)),
    ]
)
model = AutoModel.from_pretrained('kaiko-ai/midnight')

Extract embeddings for classification

For segmentation tasks, the model output corresponds to 16x16 patch tokens (derived from 224/14=16).

import torch

def extract_classification_embedding(tensor):
    cls_embedding, patch_embeddings = tensor[:, 0, :], tensor[:, 1:, :]
    return torch.cat([cls_embedding, patch_embeddings.mean(1)], dim=-1)

batch = transform(image).unsqueeze(dim=0)
embedding = extract_classification_embedding(model(batch).last_hidden_state)
print(f"Embedding shape: {embedding[0].shape}")

Extract embeddings for segmentation

import math
import torch

def extract_segmentation_embedding(tensor):
    features = tensor[:, 1:, :].permute(0, 2, 1)
    batch_size, hidden_size, patch_grid = features.shape
    height = width = int(math.sqrt(patch_grid))
    return features.view(batch_size, hidden_size, height, width)

batch = transform(image).unsqueeze(dim=0)
embedding = extract_segmentation_embedding(model(batch).last_hidden_state)
print(f"Embedding shape: {embedding[0].shape}")

Training Datasets

DatasetWSIsSourceComment
TCGA12kPublicFFPE only
NKI-80k80kProprietary10,141 patients, 31 organs

Training Components

  • DINOv2: Self-supervised training with DINOv2.
  • KDE regularizer: Replaced KoLeo in DINOv2 to ensure embedding diversity and training stability.
  • Online patching: Efficient real-time extraction of informative tiles.
  • Color augmentation (HED): Robustness to stain variations.
  • Tile filtering: Removal of low-informative tissue regions.

Evaluation

We comprehensively evaluated the models using two sets of open-source benchmarks:

  • eva: For both tile (classification, segmentation) and slide-level tasks.
  • HEST: For gene expression prediction tasks (regression).

Our best model Midnight-92k/392 consistently outperforms or matches leading models like Virchow2 and UNI-2.

Results Summary

ModelAVG.PCam 10 shotsBACHBRACSBreaKHisCRCGleasonMHISTPCamCam16 (small)Panda (small)CoNSePMoNuSACHEST
Midnight-92k/3920.7780.9000.9040.6460.8020.9660.8070.8280.9510.8680.6510.6620.7080.415
UNI-20.7760.8850.9240.6510.8630.9700.7770.8290.9510.8730.6660.6260.6440.431
Midnight-92k0.7670.8820.8890.6150.7930.9670.8230.8310.9480.8720.6430.6290.6560.425
Virchow20.7660.8350.8900.6330.8180.9660.7910.8650.9380.8600.6460.6400.6740.403
Midnight-12k0.7630.8030.9070.6390.8400.9670.7900.8150.9310.8690.6560.6250.6640.412
Kaiko-B80.7570.7990.8760.6410.8420.9600.7610.8300.9200.8360.6500.6440.6860.391
H-Optimus-00.7550.8310.7520.6200.8130.9620.7690.8500.9430.8470.6720.6440.6870.425
Prov_GigaPath0.7520.8530.7940.6260.8460.9590.7270.8310.9440.8120.6570.6280.6880.405
Hibou-L0.7510.8250.7920.6430.7670.9540.7660.8500.9490.8520.6540.6460.6680.397
UNI0.7490.8330.7970.6130.8080.9540.7590.8410.9370.8540.6620.6270.6620.391
Phikon0.7240.8260.7440.5790.7150.9460.7430.8240.9190.8220.6480.6240.6440.377
Phikon-v20.7180.7560.7370.6070.7250.9530.7530.7960.9000.8070.6340.6260.6450.391
Lunit0.7140.7630.7850.6270.7590.9430.7580.7850.9050.7590.6040.6000.6300.362
vitg14 (nat. img.)0.6740.7210.7240.5780.7830.9430.7400.8550.8810.5000.5090.5650.6140.351
vitg14 (initial)0.4930.6520.4740.4130.4250.7540.4590.5780.7630.5260.3040.4620.4320.166

Citation

@article{KDK2025,
  title={Training state-of-the-art pathology foundation models with orders of magnitude less data},
  author={Mikhail Karasikov and Joost van Doorn and Nicolas Känzig and Melis Erdal Cesur and Hugo Mark Horlings and Robert Berke and Fei Tang and Sebastian Otálora},
  year={2025},
  journal={arXiv preprint arXiv:2504.05186},
  url={https://arxiv.org/abs/2504.05186},
}

Linked in READMEs

Contributors

MK
sebastianffx

7 commits

kaiko-ai-user

4 commits

karasikov

4 commits