ShiftIntensity(safe=True) still wraps uint8 values when `offset` is an int

Aberta Para iniciantes
#9,258 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 3 dias

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
84/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
numpy, python, pytorch
Domínio
data

Direção de pesquisa

Leia ShiftIntensity.__call__ em monai/transforms/intensity/array.py e os testes em tests/transforms/test_shift_intensity*.py. Reproduza o caso relatado com uint8 e deslocamentos inteiros e de ponto flutuante. Em seguida, adicione testes para safe=True e para as transformações Random ou Dictionary relacionadas, se isso for simples com os testes existentes. O trabalho estará concluído quando os deslocamentos inteiros produzirem os valores limitados esperados e os testes passarem.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

[Describe the bug
safe=True in ShiftIntensity (added for #5621) clamps overflow for float offsets but not for int offsets. With a uint8 image and an int offset, img + offset stays uint8, so values wrap during the addition, before convert_data_type(..., safe=True) runs (monai/transforms/intensity/array.py, ShiftIntensity.__call__). The safe check then has nothing to clamp. This also affects RandShiftIntensity and the dictionary versions, which use ShiftIntensity.

To Reproduce

import torch
from monai.transforms import ShiftIntensity

img = torch.tensor([0, 100, 255], dtype=torch.uint8)

print(ShiftIntensity(offset=1.0, safe=True)(img))  # [1, 101, 255]  clamped, as documented
print(ShiftIntensity(offset=1, safe=True)(img))    # [1, 101, 0]    wraps despite safe=True
print(ShiftIntensity(offset=-1, safe=True)(img))   # [255, 99, 254] wraps despite safe=True

Expected behavior
With safe=True, an int offset should give the same result as the equivalent float offset: [1, 101, 255] and [0, 99, 254]

Screenshots
If applicable, add screenshots to help explain your problem.

Environment

MONAI 1.6.1rc0+65.gc5bddb21 (dev @ c5bddb21), PyTorch 2.15.0, NumPy 2.4.6, Python 3.11.5, macOS arm64.

Additional context
One possible fix is to compute the shift in a wider dtype (e.g. cast offset to float, or promote img) before the safe conversion. As far as I can tell there's no test covering safe in tests/transforms/test_shift_intensity*.py. I'm happy to open a PR with a fix and a test if this is considered a bug.

Investigation and draft assisted by Claude (AI); I reproduced and verified the behavior myself.

](https://github.com/Project-MONAI/MONAI/issues/new)

Linguagem predominante
Python
Estrelas
8.8k
Forks
1.7k
Merge médio
6d 1h
PRs com merge (30d)
15

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de Project-MONAI/MONAI

Todas as issues de Project-MONAI/MONAI

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.