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

未關閉 適合新手
#9,258 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 3 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
84/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
numpy, python, pytorch
領域
data

研究方向

閱讀 monai/transforms/intensity/array.py 中的 ShiftIntensity.__call__,以及 tests/transforms/test_shift_intensity*.py 中的測試。使用整數和浮點偏移量重現回報的 uint8 情況。接著,若利用現有測試可以輕鬆完成,就為 safe=True 以及相關的 Random 或 Dictionary 轉換補上測試涵蓋。整數偏移量產生預期的截斷值且測試通過後,即告完成。

由索引模型根據 Issue 內容生成。

描述

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

主要語言
Python
星號
8.8k
分支
1.7k
平均合併
6 天 1 小時
30 天內合併 PR
15

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

Project-MONAI/MONAI 的其他 Issue

查看 Project-MONAI/MONAI 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。