torchstain

repository·main·Indexed 41 days ago

https://github.com/eidoslab/torchstain

GPU-accelerated stain normalization and augmentation tools for histopathological images, compatible with PyTorch, TensorFlow, and Numpy. Supports algorithms including Macenko, Reinhard, Modified Reinhard, Multi-target Macenko, and Macenko-Aug.

Tokens
561
Snippets
2
Records
3
Agent score
2%

What's inside torchstain

  1. Install torchstain

    main

    Install the base package using pip. You can also specify a backend for GPU acceleration using extras: torchstain[torch] for PyTorch or torchstain[tf] for TensorFlow. The numpy backend is included by default in both.

    pip install torchstain
    # Or for specific backends:
    pip install torchstain[torch]
    pip install torchstain[tf]
  2. Use MacenkoNormalizer for stain normalization

    main

    To normalize histopathological images, use a normalizer from torchstain.normalizers. The workflow involves:

    1. Initializing the normalizer with a specific backend (e.g., 'torch').
    2. Calling .fit(target_image) on a target image to learn the stain characteristics.
    3. Calling .normalize(I=source_image, stains=True) on the source image to perform the transformation.

    Note: The input images should be compatible with the chosen backend (e.g., PyTorch tensors).

    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)
  3. Available normalization and augmentation algorithms

    main

    torchstain supports several algorithms across numpy, torch, and tensorflow backends:

    Normalization Algorithms:

    • Macenko
    • Reinhard
    • Modified Reinhard
    • Multi-target Macenko

    Augmentation Algorithms:

    • Macenko-Aug