CAREamics / careamics

A deep-learning library for N2V and friends
https://careamics.github.io/
BSD 3-Clause "New" or "Revised" License
28 stars 5 forks source link

Feature: Transform multiple arrays identically to `patch` and `target` #238

Closed melisande-c closed 3 weeks ago

melisande-c commented 3 weeks ago

Description

Changes Made

Breaking changes

Code calling transforms directly, the output is now a tuple of 3 rather than 2.

Additional Notes and Examples

Note

One of the biggest pain points that hasn't been fully resolved is that the N2VManipulate transform doesn't have the same function signature in the __call__ method as the other transforms, it returns 3 arrays rather than 2. We know it gets applied last in the algorithm but we could make it more explicit somehow, this would resolve some annoying type hinting.

Converting LVAE transform code

https://github.com/CAREamics/careamics/blob/b008187292f269455272c92c9009d45a330e7ca0/src/careamics/lvae_training/dataset/vae_dataset.py#L867-L881

To convert the code snippet above, I think (not tested) the following can be done, assuming self.rotation_transform is created using the CAREamics Compose instead of that from Albumentations:

img_kwargs = {}
for i, img in enumerate(img_tuples):
    for k in range(len(img)):
        img_kwargs[f"img{i}_{k}"] = img[k]

noise_kwargs = {}
for i, nimg in enumerate(noise_tuples):
    for k in range(len(nimg)):
        noise_kwargs[f"noise{i}_{k}"] = nimg[k]

img, _, rot_dic = self._rotation_transform.transform_with_additional_arrays(
    img_tuples[0][0], **img_kwargs, **noise_kwargs
)

Please ensure your PR meets the following requirements:

codecov[bot] commented 3 weeks ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 87.09%. Comparing base (427fa26) to head (d357239). Report is 6 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #238 +/- ## ========================================== - Coverage 87.20% 87.09% -0.11% ========================================== Files 130 127 -3 Lines 4876 4868 -8 ========================================== - Hits 4252 4240 -12 - Misses 624 628 +4 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

jdeschamps commented 3 weeks ago

Yes, the N2VManipulate is annoying. We could pack some of the return arrays into one... Dirty and not clear...

Another way would be to inspect again the loss. If I remember it there is a reason to return the three arguments, but maybe we can rewrite it to only need mask * original_patch. (Unfortunately it probably needs the original patch for something).

jdeschamps commented 3 weeks ago

For the sake of the LVAE submission, let's merge it as it. But we should open issues about the following: