Project-MONAI / Project-MONAI/MONAI

box_iou does not work as expected when boxes1 dtype is int

Open
#8,369 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.7k
Forks
1.6k
Avg merge
5d 1h
Merged PRs (30d)
20

Description

Describe the bug
box_iou() does not work as expected when boxes1 dtype is int, resulting in returned IoU=0 (after converting back to int dtype). No warning messages are generated in this case.

To Reproduce
Steps to reproduce the behavior:

  1. Install monai.
  2. Run commands:
import torch
from monai.data.box_utils import box_iou
a = torch.tensor([1., 1., 1., 2., 2., 2.]).unsqueeze(0)
b = torch.tensor([1, 1, 1, 2, 2, 2]).unsqueeze(0)

print(a, a.dtype)
print(b, b.dtype)

iou_aa = box_iou(a,a)
print(f"{iou_aa=}")
iou_ab = box_iou(a,b)
print(f"{iou_ab=}")
iou_bb = box_iou(b,b)
print(f"{iou_bb=}")
iou_ba = box_iou(b,a)
print(f"{iou_ba=}")

OUTPUT:

tensor([[1., 1., 1., 2., 2., 2.]]) torch.float32
tensor([[1, 1, 1, 2, 2, 2]]) torch.int64
iou_aa=tensor([[1.0000]])
iou_ab=tensor([[1.0000]])
iou_bb=tensor([[0]])
iou_ba=tensor([[0]])

Expected behavior
All IoUs should be (in this case) values close to one. However, if boxes1.dtype is of type int
the line in the definition of box_iou():
iou_t = iou_t.to(dtype=box_dtype)
makes those IoUs to be 0. Maybe casting the IoU result back to integer should be avoided or at least warning message can be generated (iou_t is always in range [0.; 1.] just before type conversion).

Environment

================================
Printing MONAI config...

MONAI version: 1.4.0
Numpy version: 1.26.4
Pytorch version: 2.5.0
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: 46a5272196a6c2590ca2589029eed8e4d56ff008
MONAI file: /home//anaconda3/envs/crc/lib/python3.9/site-packages/monai/init.py

Optional dependencies:
Pytorch Ignite version: 0.4.11
ITK version: 5.4.2
Nibabel version: 5.3.2
scikit-image version: 0.22.0
scipy version: 1.13.1
Pillow version: 10.4.0
Tensorboard version: 2.19.0
gdown version: 5.2.0
TorchVision version: 0.20.0
tqdm version: 4.66.5
lmdb version: 1.6.2
psutil version: 6.0.0
pandas version: 2.2.2
einops version: 0.8.1
transformers version: 4.40.2
mlflow version: 2.20.2
pynrrd version: 1.1.3
clearml version: 1.17.2rc0

For details about installing the optional dependencies, please visit:
https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies

================================
Printing system config...

System: Linux
Linux version: Ubuntu 22.04.5 LTS
Platform: Linux-6.8.0-51-generic-x86_64-with-glibc2.35
Processor: x86_64
Machine: x86_64
Python version: 3.9.20
Process name: python
Command: ['python', '-c', 'import monai; monai.config.print_debug_info()']
Num physical CPUs: 16
Num logical CPUs: 32
Num usable CPUs: 32
CPU freq. (MHz): 4467
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 62.0
Available memory (GB): 40.0
Used memory (GB): 21.3

================================
Printing GPU config...

Num GPUs: 2
Has CUDA: True
CUDA version: 11.8
cuDNN enabled: True
NVIDIA_TF32_OVERRIDE: None
TORCH_ALLOW_TF32_CUBLAS_OVERRIDE: None
cuDNN version: 90100
Current device: 0
Library compiled for CUDA architectures: ['sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'sm_80', 'sm_86', 'sm_37', 'sm_90', 'compute_37']
GPU 0 Name: NVIDIA GeForce RTX 4090
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 128
GPU 0 Total memory (GB): 23.6
GPU 0 CUDA capability (maj.min): 8.9
GPU 1 Name: NVIDIA GeForce RTX 4090
GPU 1 Is integrated: False
GPU 1 Is multi GPU board: False
GPU 1 Multi processor count: 128
GPU 1 Total memory (GB): 23.6
GPU 1 CUDA capability (maj.min): 8.9

Additional context

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the box_iou entry point in monai.data.box_utils and run the reproduction with float and integer boxes. Check the dtype conversion highlighted in the issue and add regression coverage for integer inputs. Done means integer-box IoUs remain close to one instead of becoming zero, with the intended behavior verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
computer-vision
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.