Skip to main content

torchstain

License Full Tests Pip Downloads DOI

GPU-accelerated stain tools for histopathological images. Compatible with PyTorch, TensorFlow, and Numpy.

Normalization algorithms currently implemented:

Augmentation algorithms currently implemented:

Installation

pip install torchstain

To install a specific backend use either torchstain[torch] or torchstain[tf]. The numpy backend is included by default in both.

Example Usage

import torch
from torchvision import transforms
import torchstain
import cv2

target = cv2.cvtColor(cv2.imread("./data/target.png"), cv2.COLOR_BGR2RGB)
to_transform = cv2.cvtColor(cv2.imread("./data/source.png"), cv2.COLOR_BGR2RGB)

T = transforms.Compose([
    transforms.ToTensor(),
    transforms.Lambda(lambda x: x*255)
])

normalizer = torchstain.normalizers.MacenkoNormalizer(backend='torch')
normalizer.fit(T(target))

t_to_transform = T(to_transform)
norm, H, E = normalizer.normalize(I=t_to_transform, stains=True)

alt text

Implemented algorithms

Algorithm numpy torch tensorflow
Macenko ✓ ✓ ✓
Reinhard ✓ ✓ ✓
Modified Reinhard ✓ ✓ ✓
Multi-target Macenko ✗ ✓ ✗
Macenko-Aug ✓ ✓ ✓

Backend comparison

Runtimes using the Macenko algorithm using different backends. Metrics were calculated from 10 repeated runs for each quadratic image size on an Intel(R) Core(TM) i5-8365U CPU @ 1.60GHz.

size numpy avg. time torch avg. time tf avg. time
224 0.0182s ± 0.0016 0.0180s ± 0.0390 0.0048s ± 0.0002
448 0.0880s ± 0.0224 0.0283s ± 0.0172 0.0210s ± 0.0025
672 0.1810s ± 0.0139 0.0463s ± 0.0301 0.0354s ± 0.0018
896 0.3013s ± 0.0377 0.0820s ± 0.0329 0.0713s ± 0.0008
1120 0.4694s ± 0.0350 0.1321s ± 0.0237 0.1036s ± 0.0042
1344 0.6640s ± 0.0553 0.1665s ± 0.0026 0.1663s ± 0.0021
1568 1.1935s ± 0.0739 0.2590s ± 0.0088 0.2531s ± 0.0031
1792 1.4523s ± 0.0207 0.3402s ± 0.0114 0.3080s ± 0.0188

Reference

  • [1] Macenko, Marc et al. "A method for normalizing histology slides for quantitative analysis." 2009 IEEE International Symposium on Biomedical Imaging: From Nano to Macro. IEEE, 2009.
  • [2] Reinhard, Erik et al. "Color transfer between images." IEEE Computer Graphics and Applications. IEEE, 2001.
  • [3] Roy, Santanu et al. "Modified Reinhard Algorithm for Color Normalization of Colorectal Cancer Histopathology Images". 2021 29th European Signal Processing Conference (EUSIPCO), IEEE, 2021.
  • [4] Ivanov, Desislav et al. "Multi-target stain normalization for histology slides". 2nd International Workshop on Medical Optical Imaging and Virtual Microscopy Image Analysis (MOVI 2024), MICCAI. 2024.

Citing

If you find this software useful for your research, please cite it as:

@software{barbano2022torchstain,
  author       = {Carlo Alberto Barbano and André Pedersen},
  title        = {EIDOSLAB/torchstain: v1.2.0-stable},
  month        = aug,
  year         = 2022,
  publisher    = {Zenodo},
  version      = {v1.2.0-stable},
  doi          = {10.5281/zenodo.6979540},
  url          = {https://doi.org/10.5281/zenodo.6979540}
}

Torchstain was originally developed within the UNITOPATHO data collection, which you can cite as:

@inproceedings{barbano2021unitopatho,
  title={UniToPatho, a labeled histopathological dataset for colorectal polyps classification and adenoma dysplasia grading},
  author={Barbano, Carlo Alberto and Perlo, Daniele and Tartaglione, Enzo and Fiandrotti, Attilio and Bertero, Luca and Cassoni, Paola and Grangetto, Marco},
  booktitle={2021 IEEE International Conference on Image Processing (ICIP)},
  pages={76--80},
  year={2021},
  organization={IEEE}
}

Metadata

Release files for torchstain 1.4.1

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

Source distribution (sdist)

Source distribution for torchstain 1.4.1
File Size Uploaded
torchstain-1.4.1.tar.gz 23.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torchstain 1.4.1
File Interpreter ABI Platform
torchstain-1.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 61.0 kB

Release files / torchstain-1.4.1.tar.gz

Download URL torchstain-1.4.1.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6469c29d7dd8e231aff4bd76d23b138cd2ac97cc81128c979ec2d3468641ecdc
BLAKE2b-256 checksum
How to use checksums
acc99a60bbe7e8b0150f656f4153d58209bba7a5b8312c4a9f835d41dc9cfee7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release files / torchstain-1.4.1-py3-none-any.whl

Download URL torchstain-1.4.1-py3-none-any.whl
Size 37.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9861320e082b92cb36520109d83f111b4518c06777bdf207800327f0f4a26b2d
BLAKE2b-256 checksum
How to use checksums
d9dce6740cc6ad34f3bf74cba6346afaf427b521c815857ff5d8c1ea8e783774
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release history Release notifications | RSS feed

This release

1.4.1 This release

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

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