Skip to content

box_iou does not work as expected when boxes1 dtype is int #8369

Open
@xkuubix

Description

@xkuubix

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: 46a5272
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions