Repository navigation
Conversation
Convert pairs of uint8, int8, and int16 inputs before metric computation to prevent integer subtraction overflow and unsupported reductions. Preserve existing floating-point and wider-integer calculation paths. Add regression cases for metric values, batch and list inputs, aggregation, precision preservation, and image similarity metrics. Assisted-by: OpenAI Codex Signed-off-by: leecoring <109904107+leecoring@users.noreply.github.com>
|
No actionable comments were generated in the recent review.
|
Document RegressionMetric._compute_tensor input validation, small-integer promotion, arguments, and return values. This is a docstring-only follow-up; metric computations and tests are unchanged. Local validation run by OpenAI Codex: 62 focused tests pass; changed-file pre-commit checks pass. Assisted-by: OpenAI Codex Signed-off-by: leecoring <109904107+leecoring@users.noreply.github.com>
Fixes #9246.
Description
This change fixes incorrect subtraction for pairs of uint8, int8, and int16 images in MONAI regression metrics. Previously, subtraction could overflow or underflow in the integer dtype; converting the resulting error to floating point afterward could not recover the correct difference. MAE could also fail when taking the mean of an integer tensor. For uint8 images containing 255 and 0, MSE is 1 instead of 65025, RMSE is 1 instead of 255, and PSNR is approximately 48.13 dB instead of 0 dB.
When both inputs use these small-integer dtypes, convert them at the shared
RegressionMetricentry point before computation, using the default floating-point dtype with a float32 floor. Individual metric formulas are unchanged. Existing floating-point and mixed floating/integer paths are left unchanged, and the precision tests retain float64 residuals. Wider integer inputs are deliberately outside this change because casting large int32/int64 values can erase unit differences. Direct functional helper calls are outside this patch.Add regression coverage for five error metrics, all three input dtypes, both subtraction directions, tensor batches and image lists, aggregation, float64 precision, and wider-integer residual preservation. The shared entry point also feeds SSIM and MS-SSIM: comparisons with equivalent float32 images cover both 2D and 3D, including the previous integer-pooling failure in MS-SSIM. This does not change MS-SSIM size validation or its pooling schedule.
Validation
Local CPU checks run by Codex:
python -m unittest tests.metrics.test_compute_regression_metrics tests.metrics.test_ssim_metric tests.metrics.test_compute_multiscalessim_metric -v: 62 tests pass, none skipped, including the 48 new tests../runtests.sh -f: pass (copyright, isort, Black, Ruff, and Pyrefly, using the repository's configured exclusions/suppressions).rtol=1e-5, atol=1e-4. This is an additional local diagnostic; binary image files are not added to the PR.Environment: macOS CPU, Python 3.14.7, PyTorch 2.15.0, NumPy 2.5.4, NiBabel 5.4.2 for the additional file diagnostic only. No new runtime or test dependency is introduced.
Verified against upstream dev commit
827fa24fef4f474f70ead141412a2a0675990684. The same new tests were run against a separate unmodified checkout of this commit and against the patched checkout; the results above include this revalidation.Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.Tool assistance
The submitter provided the explanation of the problem and fix. OpenAI Codex assisted with the investigation, implementation, English translation and preparation of this description, and test execution. The local checks listed above were run by Codex. The validation results describe the tested cases, rather than a guarantee for all input combinations.