Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiMacenkoNormalizer #57

Open
yuvfried opened this issue Dec 29, 2024 · 8 comments · May be fixed by #66
Open

MultiMacenkoNormalizer #57

yuvfried opened this issue Dec 29, 2024 · 8 comments · May be fixed by #66

Comments

@yuvfried
Copy link

Hi,

I came across the following reference to MultiMacenkoNormalizer in the paper "MULTI-TARGET STAIN NORMALIZATION FOR HISTOLOGY SLIDES":

SOFTWARE IMPLEMENTATION
We make our implementation available in the torchstain2 normalization library. Our method is contained in the MultiMacenkoNormalizer class. A code snippet is presented below.

from torchstain.normalizers import *

# Sample random images
ref_images = sample_random_images(dataset, N)

# Initialize and fit the normalizer
normalizer = MultiMacenkoNormalizer('avg-post')
normalizer.fit(ref_images)

# Normalize an image
img = next(iter(dataset))
norm, _, _ = normalizer.normalize(img)

Could you please direct me to the location of the implementation of the MultiMacenkoNormalizer class within the repository? I would appreciate any guidance on where I can find the corresponding code.

Thank you!

@andreped
Copy link
Collaborator

@carloalbertobarbano

@carloalbertobarbano
Copy link
Member

Just merged here https://github.com/EIDOSLAB/torchstain/blob/main/torchstain/torch/normalizers/multitarget.py should work with torch :)

@andreped
Copy link
Collaborator

@carloalbertobarbano Unfortunately, there seems to be a bug making it challenging for me to use it. I opened a PR to make it working, at least similarly to the other methods. See PR #64.

@carloalbertobarbano
Copy link
Member

Should be fixed in the development branch in commit 7c2a95f you should be able to install it with

pip3 install git+https://github.com/EIDOSLAB/torchstain.git@development

Example usage:

target = cv2.resize(cv2.cvtColor(cv2.imread(os.path.join(curr_file_path, "../data/target.png")), cv2.COLOR_BGR2RGB), (size, size))
to_transform = cv2.resize(cv2.cvtColor(cv2.imread(os.path.join(curr_file_path, "../data/source.png")), cv2.COLOR_BGR2RGB), (size, size))

# setup preprocessing and preprocess image to be normalized
T = transforms.Compose([
    transforms.ToTensor(),
    transforms.Lambda(lambda x: x * 255)
])
target = T(target)
t_to_transform = T(to_transform)

multi_normalizer = torchstain.normalizers.MultiMacenkoNormalizer(backend="torch", norm_mode="avg-post")
multi_normalizer.fit([target, target, target])

result_multi, _, _ = multi_normalizer.normalize(I=t_to_transform, stains=True)

of course instead of [target, target, target] you are supposed to open multiple reference images

@andreped
Copy link
Collaborator

andreped commented Jan 13, 2025

@carloalbertobarbano Oh, this fit([target, target, target]) was new to me.

Why don't we just do .fit(target) and move this special case handling inside the specific MultiMacenkoNormalizer.fit()?

I argue that it should be as easy as possible for the user to use this method, and that it should follow the same API, as much as possible, for all normalizers.

@carloalbertobarbano
Copy link
Member

Because it is supposed to work with multiple different reference images, so .fit should take an array

@andreped
Copy link
Collaborator

Because it is supposed to work with multiple different reference images, so .fit should take an array

Aaah, makes sense. Thanks for the clarification. But then after this is merged, I will make a PR to extend this to the different backends, and add the appropriate tests.

@andreped
Copy link
Collaborator

andreped commented Jan 14, 2025

@carloalbertobarbano Created PR #66 to add support for the missing NumPy and TensorFlow backends :]

Seems to work! At least new tests pass, indicating that we reach the same result as for PyTorch backend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants