-
Notifications
You must be signed in to change notification settings - Fork 240
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
RescaleIntensity - multiple calls #1115
Comments
Thanks for reporting, @mueller-franzes. Great catch! I think this was introduced in #998. I've introduced a failing test in #1116 that needs to be fixed. @nicoloesch, could you please take a look? We might need to revert the feature you introduced in #998 as this is an important bug. |
@mueller-franzes Thanks for pointing this out! If you instantiate however with The question now is: How should the transformation work in the future @fepegar? In my opinion, the best approach going forward would be to still have the option to specify def rescale(
self,
tensor: torch.Tensor,
mask: torch.Tensor,
image_name: str,
) -> torch.Tensor:
# The tensor is cloned as in-place operations will be used
array = tensor.clone().float().numpy()
mask = mask.numpy()
if not mask.any():
message = (
f'Rescaling image "{image_name}" not possible'
' because the mask to compute the statistics is empty'
)
warnings.warn(message, RuntimeWarning, stacklevel=2)
return tensor
values = array[mask]
cutoff = np.percentile(values, self.percentiles)
np.clip(array, *cutoff, out=array) # type: ignore[call-overload]
# NEW CODE FROM HERE
# ------------------------------------
if self.in_min_max is not None:
in_min, in_max = self.in_min_max
else:
in_min, in _max = self._parse_range(
(array.min(), array.max()), 'in_min_max',
)
in_range = in_max - in_min
# -------------------------------------
# END NEW CODE
if in_range == 0: # should this be compared using a tolerance?
message = (
f'Rescaling image "{image_name}" not possible'
' because all the intensity values are the same'
)
warnings.warn(message, RuntimeWarning, stacklevel=2)
return tensor
out_range = self.out_max - self.out_min
if self.invert_transform:
array -= self.out_min
array /= out_range
array *= in_range
array += self.in_min
else:
array -= self.in_min
array /= in_range
array *= out_range
array += self.out_min
return torch.as_tensor(array) I hope my description/explanation made sense and I am looking forward to your response! |
I think your solution makes sense. I tried something very similar. Basically, we need to avoid modifying class attributes in |
I think we loose the option to invert the transform if |
That sounds good to me! Would you like to contribute the changes? |
hi there |
Amazing! Thanks. It seems that I can't assign you on GitHub, but it's all yours. Thanks @romainVala for your suggestion as well. Let's move the discussion to #1116. |
@nicoloesch I believe I'll be able to assign you once you add a comment to the PR. |
Is there an existing issue for this?
Problem summary
If RescaleIntensity(out_min_max=(0, 1)) is called a second time, an error/unexpected behavior occurs in the normalization. This is because in line 115-116
the variable self.in_min_max (actually set by the initialization) is overwritten on the first call. This problem occurs when in_min_max is not specified (default to None) in the initialization.
Based on the documentation "If None, the minimum and maximum input intensities will be used." I would expect min and max to be re-determined on each call.
Code for reproduction
Actual outcome
Please see the comments next to the code.
Error messages
Expected outcome
Please see the comments next to the code.
System info
The text was updated successfully, but these errors were encountered: