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

オープン 初心者向け
#9,258 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
15

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Project-MONAI/MONAI のほかの issue

Project-MONAI/MONAI の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。