Skip to content

Intensity overflow in ShiftIntensity #5621

Closed

Description

@KumoLiu

Describe the bug
Now in ShiftIntensity, the default is to use the input dtype as the output dtype. As we discussed in #5535, it will cause an issue when the intensity goes beyond the original range.

out, *_ = convert_data_type(data=out, dtype=img.dtype)

I followed @wyli's suggestion and tried to add convert_data_dtype referring to https://github.com/pytorch/vision/blob/ad128b753c7e8cc0c600dfddac22ff48fc73c9d9/torchvision/transforms/functional_tensor.py#L64, but I find that what they did may be different with what we want.

>>> a = torch.tensor([175, 175], dtype=torch.uint8)
>>> b = convert_image_dtype(a, dtype=torch.float32)
>>> print(b, b.dtype)
tensor([0.6863, 0.6863]) torch.float32

I try to add some warning in this draft pr. But that still doesn't solve the problem. Because even if the user sees the warning, the user cannot modify it because the ShiftIntensity does not have the dtype parameter passed in. And if we add convert_data_dtype for each transform, it still doesn't look right, because we change the intensity inside the transform, which may not be what the user wants.
Like what @ericspod and @myron said in #5535, adding a clip transform is indeed a good solution, but it does not solve the current bug, because the data dtype has been changed inside the transform, that is, the intensity has been changed.
At the moment, I have not thought of a better way than adding a WARNING.
Do you have any suggestions? cc @wyli @ericspod @myron @Nic-Ma

Activity

  1. wyli commented on Dec 1, 2022

    @wyli
    Contributor

    I meant something like this:

    import torch
    from torchvision.transforms.functional_tensor import _max_value
    
    def safe_int_convert(val, dtype):
        m = _max_value(dtype)
        return val.clamp(0, m).to(dtype)
    
    
    a = torch.tensor([-15, 260, 0])
    b = a.to(torch.uint8)
    print(b)
    c = safe_int_convert(a, torch.uint8)
    print(c)

    it'll remove the overflow:

    tensor([241,   4,   0], dtype=torch.uint8)
    tensor([  0, 255,   0], dtype=torch.uint8)
    
  2. KumoLiu commented on Dec 1, 2022

    @KumoLiu
    ContributorAuthor

    I meant something like this:

    import torch
    from torchvision.transforms.functional_tensor import _max_value
    
    def safe_int_convert(val, dtype):
        m = _max_value(dtype)
        return val.clamp(0, m).to(dtype)
    
    
    a = torch.tensor([-15, 260, 0])
    b = a.to(torch.uint8)
    print(b)
    c = safe_int_convert(a, torch.uint8)
    print(c)

    it'll remove the overflow:

    tensor([241,   4,   0], dtype=torch.uint8)
    tensor([  0, 255,   0], dtype=torch.uint8)
    

    Hi @wyli, thanks for your explanation, and yes I know that but it still clips intensity inside the dtype convert. What if the user does not want to clip?Not sure whether should do it? It looks like this is a clip operation shouldn't it be distinguished from convert_data_dtype?

  3. wyli commented on Dec 1, 2022

    @wyli
    Contributor

    we can add a flag safe default to false (just do a.to(dtype));
    when safe=True,

    out, *_ = convert_data_type(data=out, dtype=img.dtype, safe=True) 
    

    we check and remove potential over/udnderflow

  4. KumoLiu commented on Dec 1, 2022

    @KumoLiu
    ContributorAuthor

    @wyli Ok, I will do that. Thanks!

  5. added a commit that references this issue on Dec 8, 2022
    0737a33
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