ShiftIntensity(safe=True) still wraps uint8 values when `offset` is an int
維護者通常 3 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
閱讀 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.
- 主要語言
- Python
- 星號
- 8.8k
- 分支
- 1.7k
- 平均合併
- 6 天 1 小時
- 30 天內合併 PR
- 15
環境準備
- 提供 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
Project-MONAI/MONAI 的其他 Issue
-
Small-integer inputs produce incorrect regression metric values or runtime errors可能已有人在做 @leecoring 於 5 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 70/100
Project-MONAI/MONAI#9246 ·
維護者通常 3 天內回覆
-
partition_dataset可能已有人在做 @rtmalikian 於 10 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 72/100
Project-MONAI/MONAI#9220 ·
維護者通常 3 天內回覆
-
partition_dataset with ratios leaves some items out of every partition可能已有人在做 @mottopanikeiku 於 16 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
Project-MONAI/MONAI#9216 ·
維護者通常 3 天內回覆
-
FocalLoss mean reduction is multiplied by the number of channels when ignore_index is set可能已有人在做 @mottopanikeiku 於 16 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 82/100
Project-MONAI/MONAI#9214 ·
維護者通常 3 天內回覆
-
DiNTS: missing graph-break regression test (follow-up to #9145)可能已有人在做 @nilapate 於 29 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 66/100
Project-MONAI/MONAI#9159 ·
維護者通常 3 天內回覆
查看 Project-MONAI/MONAI 的全部 Issue
相似的 Issue
-
bug
難度 2/5 1-3 小時 新手友好度 75/100
micropython/micropython#19906 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 82/100
FinanceFlash/unvibecode#597 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 65/100
TomAnthony/YourOwnWeigh#21 ·
維護者通常 1 天內回覆
-
[BUG]: Compliance rules never see Unity Catalog views as views可能已有人在做 @wagnercsantos 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
databrickslabs/ontos#1348 ·
維護者通常 3 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 2 天內回覆