Closed VinayMushunuri closed 1 year ago
Hi @drbeh, could you please help take a look at this issue? Thanks in advance!
Hi @VinayMushunuri,
You are right, this implementation is based on numpy but we do have a cupy implementation that is way faster than numpy. For you to use the cupy-based version, there are two options:
CuCIM
or CuCIMd
wrapper.
from tranforms import CuCIM
normalizer = CuCIM(
name="normalize_colors_pca",
source_intensity=240.0,
alpha=1.0,
beta=0.345,
ref_stain_coeff=((0.5626, 0.2159), (0.7201, 0.8012), (0.4062, 0.5581)),
ref_max_conc=(1.9705, 1.0308),
image_type="intensity",
channel_axis=0,
)
normalized_image = normalizer(image)
or simply:
from tranforms import CuCIM
normalizer = CuCIM(name="normalize_colors_pca")
normalized_image = normalizer(image)
Hello @drbeh, Thankyou for the response I would check it very quickly and let u know how well it works
Hello,
I am new to MONAI framework and I am working on stain normalization approach to generate images similar to target image using MONAI framework. I have checked the blog for the implementation of Stain normalization and I found out that the implementation is using numpy rather than cupy or something similar.
Here is the implementation, I found from MONAI blog : Copyright (c) MONAI Consortium Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
from future import annotations
import numpy as np
from monai.transforms.transform import Transform
[docs] class ExtractHEStains(Transform): “”"Class to extract a target stain from an image, using stain deconvolution (see Note).
Args: tli: transmitted light intensity. Defaults to 240. alpha: tolerance in percentile for the pseudo-min (alpha percentile) and pseudo-max (100 - alpha percentile). Defaults to 1. beta: absorbance threshold for transparent pixels. Defaults to 0.15 max_cref: reference maximum stain concentrations for Hematoxylin & Eosin (H&E). Defaults to (1.9705, 1.0308).
Note: For more information refer to:
the previous implementations:
def init( self, tli: float = 240, alpha: float = 1, beta: float = 0.15, max_cref: tuple | np.ndarray = (1.9705, 1.0308) ) -> None: self.tli = tli self.alpha = alpha self.beta = beta self.max_cref = np.array(max_cref)
def _deconvolution_extract_stain(self, image: np.ndarray) -> np.ndarray: """Perform Stain Deconvolution and return stain matrix for the image.
def call(self, image: np.ndarray) -> np.ndarray: """Perform stain extraction.
[docs] class NormalizeHEStains(Transform): “”"Class to normalize patches/images to a reference or target image stain (see Note).
Performs stain deconvolution of the source image using the ExtractHEStains class, to obtain the stain matrix and calculate the stain concentration matrix for the image. Then, performs the inverse Beer-Lambert transform to recreate the patch using the target H&E stain matrix provided. If no target stain provided, a default reference stain is used. Similarly, if no maximum stain concentrations are provided, a reference maximum stain concentrations matrix is used.
Args: tli: transmitted light intensity. Defaults to 240. alpha: tolerance in percentile for the pseudo-min (alpha percentile) and pseudo-max (100 - alpha percentile). Defaults to 1. beta: absorbance threshold for transparent pixels. Defaults to 0.15. target_he: target stain matrix. Defaults to ((0.5626, 0.2159), (0.7201, 0.8012), (0.4062, 0.5581)). max_cref: reference maximum stain concentrations for Hematoxylin & Eosin (H&E). Defaults to [1.9705, 1.0308].
Note: For more information refer to:
the previous implementations:
"""
def init( self, tli: float = 240, alpha: float = 1, beta: float = 0.15, target_he: tuple | np.ndarray = ((0.5626, 0.2159), (0.7201, 0.8012), (0.4062, 0.5581)), max_cref: tuple | np.ndarray = (1.9705, 1.0308), ) -> None: self.tli = tli self.target_he = np.array(target_he) self.max_cref = np.array(max_cref) self.stain_extractor = ExtractHEStains(tli=self.tli, alpha=alpha, beta=beta, max_cref=self.max_cref)
def call(self, image: np.ndarray) -> np.ndarray: """Perform stain normalization.
For me when I am running on a folder which contains 5400 webp tiles it is taking around half an hour. I have more than 2000 such folders and it takes at least a month to complete this procedure.
Could u please let me know if there is anything I could do to speed up the process.