facebookresearch / detectron2

Detectron2 is a platform for object detection, segmentation and other visual recognition tasks.
https://detectron2.readthedocs.io/en/latest/
Apache License 2.0
29.86k stars 7.39k forks source link

print_instances_class_histogram raises exception ZeroDivisionError: integer division or modulo by zero #5323

Open ShulyAvraham opened 1 month ago

ShulyAvraham commented 1 month ago

If by mistake we pass an empty list of classes, we get a ZeroDivisionError exception.

  1. Shortest full runnable code:
    from detectron2 import data 
    data.print_instances_class_histogram([], [])
  2. Full output:
    Traceback (most recent call last):
    File "/breedingeye/a.py", line 2, in <module>
    data.print_instances_class_histogram([], [])
    File "/usr/local/lib/python3.10/site-packages/detectron2/data/build.py", line 198, in print_instances_class_histogram
    data.extend([None] * (N_COLS - (len(data) % N_COLS)))
    ZeroDivisionError: integer division or modulo by zero

Expected behavior:

An exception indicating the lack of classes, or no exception at all.

Environment:

-------------------------------  -----------------------------------------------------------------
sys.platform                     linux
Python                           3.10.11 (main, May  3 2023, 09:31:23) [GCC 8.3.0]
numpy                            1.22.4
detectron2                       0.6 @/usr/local/lib/python3.10/site-packages/detectron2
Compiler                         GCC 8.3
CUDA compiler                    not available
DETECTRON2_ENV_MODULE            <not set>
PyTorch                          2.0.1+cu117 @/usr/local/lib/python3.10/site-packages/torch
PyTorch debug build              False
torch._C._GLIBCXX_USE_CXX11_ABI  False
GPU available                    No: torch.cuda.is_available() == False
Pillow                           9.5.0
torchvision                      0.15.2+cu117 @/usr/local/lib/python3.10/site-packages/torchvision
fvcore                           0.1.5.post20221221
iopath                           0.1.9
cv2                              4.7.0
-------------------------------  -----------------------------------------------------------------
PyTorch built with:
  - GCC 9.3
  - C++ Version: 201703
  - Intel(R) oneAPI Math Kernel Library Version 2022.2-Product Build 20220804 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v2.7.3 (Git Hash 6dbeffbae1f23cbbeae17adb7b5b13f1f37c080e)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - LAPACK is enabled (usually provided by MKL)
  - NNPACK is enabled
  - CPU capability usage: AVX2
  - Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=11.7, CUDNN_VERSION=8.5.0, CXX_COMPILER=/opt/rh/devtoolset-9/root/usr/bin/c++, CXX_FLAGS= -D_GLIBCXX_USE_CXX11_ABI=0 -fabi-version=11 -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -DNDEBUG -DUSE_KINETO -DLIBKINETO_NOROCTRACER -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -O2 -fPIC -Wall -Wextra -Werror=return-type -Werror=non-virtual-dtor -Werror=bool-operation -Wnarrowing -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wunused-local-typedefs -Wno-unused-parameter -Wno-unused-function -Wno-unused-result -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 -Werror=cast-function-type -Wno-stringop-overflow, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_DISABLE_GPU_ASSERTS=ON, TORCH_VERSION=2.0.1, 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=1, USE_NNPACK=ON, USE_OPENMP=ON, USE_ROCM=OFF, 
github-actions[bot] commented 1 month ago

You've chosen to report an unexpected problem or bug. Unless you already know the root cause of it, please include details about it by filling the issue template. The following information is missing: "Instructions To Reproduce the Issue and Full Logs";

Programmer-RD-AI commented 1 month ago

Hi, ZeroDivisionError is raised due to the lists being empty, the print_instances_class_histogram function expects something as follows... I don't exactly get the point that you are trying to bring up, is it that there should be a better way that this should be informed to the user?

dummy_dataset = [{
    "file_name": "dummy_image.jpg",
    "image_id": 0,
    "height": 100,
    "width": 100,
    "annotations": [{"category_id": 0, "bbox": [10, 10, 20, 20], "bbox_mode": 0}]
}]

# Call the function with the dummy dataset
print_instances_class_histogram(dummy_dataset, ["class_0"])

Thank you

ShulyAvraham commented 1 month ago

Thanks for your response. The problem appeared at a bigger piece of code. This is the shortest example that could reproduce the issue. Indeed, the exception is raised due to the lists being empty, however, I would expect a better report of the issue. E.g. "Empty list of classes" or "No classes were provided" (rather than ZeroDivisionError), to indicate that the exception was caused by incorrect parameters provided by the caller and not something internal in detectron2.

ShulyAvraham commented 1 month ago

Some more background: our real code looks something like the following:

cfg = dt_config.get_cfg()
# many more configuration...
cfg.MODEL.RETINANET.NUM_CLASSES = len(classes)  # classes is an empty list
trainer = dt_engine.DefaultTrainer(cfg)

It's clear to us that we shouldn't run the trainer without any classes, our point is that is would be nicer to get a clearer message when catching invalid input.