Skip to content

StainX

StainX Logo
CI Python bioRxiv DOI

Overview

StainX is a Torch-first stain normalization library for histopathology images, designed for efficient batch processing on CPU, CUDA, and MPS, with an optional compiled torch_cuda extension.

Key Advantages

  • Batch Processing: Process multiple images simultaneously, maximizing GPU throughput
  • Multi-Device Support: CPU, CUDA (NVIDIA), and MPS (Apple Silicon)
  • Multiple Algorithms: Histogram Matching, Reinhard, and Macenko
  • Automatic Backend Selection: Chooses between PyTorch ops and compiled CUDA kernels when available

Why Batch Processing Matters

Batch processing is crucial for histopathology workflows where you often need to normalize hundreds or thousands of images:

  • Better GPU Utilization: Parallel processing across the batch
  • Reduced Overhead: Fewer kernel launches than per-image loops
  • Higher Throughput: Batch sizes of 64–128 typically outperform single-image transforms (see Benchmarks; regenerate numbers on your hardware)

Quick Example

Use float tensors in [0, 1] (or uint8). Prefer torch.rand — Macenko does not accept negative pixels from torch.randn.

import torch
from stainx import Reinhard, Macenko, HistogramMatching

reference_image = torch.rand(1, 3, 512, 512)  # NCHW, float [0, 1]
source_images = torch.rand(10, 3, 512, 512)

normalizer = Reinhard(device="cuda")  # or "cpu", "mps"
normalizer.fit(reference_image)
normalized = normalizer.transform(source_images)

Performance

On an NVIDIA RTX A6000 during 0.1.x development, torch_cuda Reinhard was about ~5.7× the pure Torch backend at mid batch sizes, with throughput climbing into the tens of thousands of images/second for 256² tiles. Those figures are historical — re-run benchmarks/benchmark_stainx_backend.py and see Benchmarks.

Installation

pip install stainx
# from source: make install

Requires Python >= 3.11 and PyTorch >= 2.0.0. PyPI ships an sdist (Torch backends out of the box). The optional torch_cuda extension compiles locally when a CUDA GPU is visible to PyTorch and nvcc is available. See Installation.

Features

  • Multiple algorithms: Histogram Matching, Reinhard, and Macenko
  • Torch backends: torch and optional torch_cuda
  • Training transforms: StainNormalizerTransform for DataLoader pipelines
  • Batch processing: Efficient multi-image normalization
  • Flexible device support: CPU, CUDA, MPS (Apple Silicon)

Documentation

Contributing

We welcome contributions! See our Contributing Guide for details.

License

This project is licensed under the GNU General Public License v3 (GPL-3.0-or-later).

  • GitHub: https://github.com/rendeirolab/stainx
  • Issues: https://github.com/rendeirolab/stainx/issues
  • Documentation: https://stainx.readthedocs.io/