Skip to content

Normalize silently ignores in_min or in_max when only one is provided #1509

Description

@Sal-sal-sal

Description

Normalize ignores an explicitly supplied in_min when in_max is None, and vice versa.
Both bounds are calculated from the input data instead.

The parameter documentation describes automatic calculation when the corresponding bound is None, so I would expect an explicitly supplied bound to be preserved.

Reproduction

import torch
import torchio as tio

image = tio.ScalarImage(
    torch.tensor([0., 50., 100.]).reshape(1, 1, 1, 3)
)

for bounds in ({"in_min": 50.}, {"in_max": 50.}):
    result = tio.Normalize(
        out_min=0.,
        out_max=1.,
        **bounds,
    )(image)
    print(bounds, result.data.flatten().tolist())

Actual behavior

{'in_min': 50.0} [0.0, 0.5, 1.0]
{'in_max': 50.0} [0.0, 0.5, 1.0]

Expected behavior

{'in_min': 50.0} [0.0, 0.0, 1.0]
{'in_max': 50.0} [0.0, 1.0, 1.0]

Possible cause

Normalize.make_params() uses the explicit bounds only when both are non-None:

if self.in_min is not None and self.in_max is not None:

Otherwise, it computes both bounds from percentiles.
Each bound could be resolved independently, calculating only the missing one.

Version

Reproduced on main, commit 2b019d2a9659c838408c73ae77327ac5eb87b190, TorchIO 2.0.0a2, using synthetic CPU tensors.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions