facebookresearch / facebookresearch/detectron2
pairwise_iou_rotated (CUDA) returns large negative value for specific boxes
- Dominant language
- Python
- Stars
- 34.7k
- Forks
- 7.9k
- PR merge metrics
- No merged PRs in 30d
Description
I was investigating the following bug in training:
```
Traceback (most recent call last):
File "XXXXXX/train_debug.py", line 95, in _try_forward_pass
trainer.model(data)
File "/opt/conda/envs/XXXXXX/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "XXXXXX/detectron2/detectron2/modeling/meta_arch/rcnn.py", line 163, in forward
_, detector_losses = self.roi_heads(images, features, proposals, gt_instances)
File "/opt/conda/envs/XXXXXX/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "XXXXXX/detectron2/detectron2/modeling/roi_heads/roi_heads.py", line 735, in forward
proposals = self.label_and_sample_proposals(proposals, targets)
File "/opt/conda/envs/XXXXXX/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 28, in decorate_context
return func(*args, **kwargs)
File "XXXXXX/detectron2/detectron2/modeling/roi_heads/rotated_fast_rcnn.py", line 249, in label_and_sample_proposals
matched_idxs, matched_labels = self.proposal_matcher(match_quality_matrix)
File "XXXXXX/repos/detectron2/detectron2/modeling/matcher.py", line 89, in __call__
assert torch.all(match_quality_matrix >= 0)
AssertionError
```
After implementing the steps in https://github.com/facebookresearch/detectron2/issues/350 (removing NaN boxes, boxes with minimum width and height, etc etc) The issue kept occurring.
I was able to catch and serialize the offending bounding boxes. It turns out the boxes produce an IoU that is negative with respect to themselves (which happens in the `roi_head` if `MODEL.ROI_HEADS.PROPOSAL_APPEND_GT` is True). The boxes have in common that they are quite small (few pixels) and have a relatively small _negative_ angle.
I am quite convinced that there is a bug in the CUDA implementation of `pairwise_iou_rotated`. Calculating the IoU on `cpu` tensors yields a value of `1` as expected.
## Instructions To Reproduce the 🐛 Bug:
Create a new conda environment with Python 3.8 and install environment:
```
conda install cudatoolkit=11.3 -c nvidia
conda install pytorch=1.10.2=py3.8_cuda11.3_cudnn8.2.0_0 torchvision=0.11.3 -c pytorch
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu113/torch1.10/index.html
```
Download the pickled boxes: https://drive.google.com/file/d/1xylu014duVxtEeGlOGaAPIDb69rTsD9R/view?usp=sharing
Run the following script in the environment:
```
import torch
from detectron2.structures.rotated_boxes import pairwise_iou, RotatedBoxes
if __name__ == '__main__':
assert torch.cuda.is_available()
data = torch.load('./offending_boxes.pth')
data = RotatedBoxes.cat(data)
match_quality_matrix_cpu = pairwise_iou(data, data)
print(match_quality_matrix_cpu) # Will give 1s on the matrix diagonal
match_quality_matrix_gpu = pairwise_iou(data.to('cuda'), data.to('cuda'))
print(match_quality_matrix_gpu) # Will give large negative values on the matrix diagonal
```
## Expected behavior:
The provided boxes should produce an IoU of 1 when IoU is calculated with respect to themselves.
## Environment:
Provide your environment information using the following command:
```
---------------------- ---------------------------------------------------------------------------------------------------------------------------
sys.platform linux
Python 3.8.16 (default, Mar 2 2023, 03:21:46) [GCC 11.2.0]
numpy 1.23.5
detectron2 0.6 @XXXXXX/lib/python3.8/site-packages/detectron2
Compiler GCC 7.3
CUDA compiler CUDA 11.3
detectron2 arch flags XXXXXX/lib/python3.8/site-packages/detectron2/_C.cpython-38-x86_64-linux-gnu.so
DETECTRON2_ENV_MODULE
PyTorch 1.10.2 @XXXXXX/lib/python3.8/site-packages/torch
PyTorch debug build False
GPU available Yes
GPU 0 NVIDIA GeForce GTX 1650 with Max-Q Design (arch=7.5)
Driver version 510.108.03
CUDA_HOME None - invalid!
Pillow 9.4.0
torchvision 0.11.3 @XXXXXX/lib/python3.8/site-packages/torchvision
torchvision arch flags XXXXXX/lib/python3.8/site-packages/torchvision/_C.so
fvcore 0.1.5.post20221221
iopath 0.1.9
cv2 Not found
---------------------- ---------------------------------------------------------------------------------------------------------------------------
PyTorch built with:
- GCC 7.3
- C++ Version: 201402
- Intel(R) oneAPI Math Kernel Library Version 2021.4-Product Build 20210904 for Intel(R) 64 architecture applications
- Intel(R) MKL-DNN v2.2.3 (Git Hash 7336ca9f055cf1bfa13efb658fe15dc9b41f0740)
- OpenMP 201511 (a.k.a. OpenMP 4.5)
- LAPACK is enabled (usually provided by MKL)
- NNPACK is enabled
- CPU capability usage: AVX512
- CUDA Runtime 11.3
- NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_86,code=sm_86;-gencode;arch=compute_37,code=compute_37
- CuDNN 8.2
- Magma 2.5.2
- Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=11.3, CUDNN_VERSION=8.2.0, CXX_COMPILER=/opt/rh/devtoolset-7/root/usr/bin/c++, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_KINETO -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -DEDGE_PROFILER_USE_KINETO -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Wno-stringop-overflow, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_VERSION=1.10.2, USE_CUDA=ON, USE_CUDNN=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON,
```
It also has been reproduced with CUDA 11.1 and on an GCP A2 machine with Nvidia A100 GPU
For now I will mitigate this by adding a small epsilon to the angle, but of course that is not really a sustainable solution :sweat_smile:
Contributor guide
Assessment
This issue has not been assessed yet.