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

Aperta Adatta ai principianti
#9,258 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
84/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
numpy, python, pytorch
Ambito
data

Direzione di ricerca

Leggi ShiftIntensity.__call__ in monai/transforms/intensity/array.py e i test in tests/transforms/test_shift_intensity*.py. Riproduci il caso segnalato con uint8 e offset interi e in virgola mobile. Aggiungi quindi test per safe=True e per le trasformazioni Random o Dictionary correlate, se i test esistenti consentono di farlo facilmente. Il lavoro è concluso quando gli offset interi producono i valori limitati previsti e i test passano.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

[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)

Lingua principale
Python
Stelle
8.8k
Fork
1.7k
Merge medio
6g 1h
PR unite (30g)
15

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Project-MONAI/MONAI

Tutte le issue di Project-MONAI/MONAI

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.