Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounding boxes are drawn in random order instead of ordered by confidence #1061

Closed
laszlovandenhoek opened this issue May 23, 2023 · 2 comments

Comments

@laszlovandenhoek
Copy link
Contributor

🐛 Describe the bug

Problem

This is the method that draws bounding boxes around predictions:

def draw(self, box_thickness: int = 2, show_confidence: bool = True, color_mapping: Optional[List[Tuple[int, int, int]]] = None) -> np.ndarray:
"""Draw the predicted bboxes on the image.
:param box_thickness: Thickness of bounding boxes.
:param show_confidence: Whether to show confidence scores on the image.
:param color_mapping: List of tuples representing the colors for each class.
Default is None, which generates a default color mapping based on the number of class names.
:return: Image with predicted bboxes. Note that this does not modify the original image.
"""
image = self.image.copy()
color_mapping = color_mapping or generate_color_mapping(len(self.class_names))
for pred_i in range(len(self.prediction)):
class_id = int(self.prediction.labels[pred_i])
score = "" if not show_confidence else str(round(self.prediction.confidence[pred_i], 2))
image = draw_bbox(
image=image,
title=f"{self.class_names[class_id]} {score}",
color=color_mapping[class_id],
box_thickness=box_thickness,
x1=int(self.prediction.bboxes_xyxy[pred_i, 0]),
y1=int(self.prediction.bboxes_xyxy[pred_i, 1]),
x2=int(self.prediction.bboxes_xyxy[pred_i, 2]),
y2=int(self.prediction.bboxes_xyxy[pred_i, 3]),
)
return image

However, the code does not take into account that bounding boxes are drawn on top of each other. Since the arrays are not in any particular order, if an object has multiple overlapping predictions, it can happen that a less likely prediction is drawn later, obscuring the more likely prediction in the resulting image.

Solution

This could be fixed by changing this line:

for pred_i in range(len(self.prediction)):

into:

for pred_i in np.argsort(self.prediction.confidence):

This way, the most likely prediction is drawn last, i.e. on top of any overlapping other ones, and is therefore the most visible in the resulting image.

Example

The examples below were created using this code:

from super_gradients.training import models
from super_gradients.common.object_names import Models

net = models.get(Models.YOLO_NAS_L, pretrained_weights="coco")

prediction = net.predict("https://www.krijskraan.nl/catbird-input.mp4")

prediction.save(output_path="catbird-output.mp4")

As you can see in these examples, there is less "flashing" in the resulting movie. Also, the fact that the most likely class for this animal is "bird" more often than "cat" immediately becomes apparent, highlighting the necessity for finetuning.

Before (=current code): https://krijskraan.nl/catbird-output-original.mp4
After ordering by confidence (=with proposed change): https://krijskraan.nl/catbird-output-ordered.mp4

Versions

PyTorch version: 1.12.1+cu102
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0
Clang version: Could not collect
CMake version: version 3.26.0
Libc version: glibc-2.35

Python version: 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 39 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 16
On-line CPU(s) list: 0-15
Vendor ID: GenuineIntel
Model name: 11th Gen Intel(R) Core(TM) i9-11950H @ 2.60GHz
CPU family: 6
Model: 141
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 1
Stepping: 1
BogoMIPS: 5222.39
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq ssse3 fma cx16 pdcm pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512vbmi umip avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid fsrm avx512_vp2intersect flush_l1d arch_capabilities
Hypervisor vendor: Microsoft
Virtualization type: full
L1d cache: 384 KiB (8 instances)
L1i cache: 256 KiB (8 instances)
L2 cache: 10 MiB (8 instances)
L3 cache: 24 MiB (1 instance)
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Retbleed: Mitigation; Enhanced IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected

Versions of relevant libraries:
[pip3] numpy==1.23.0
[pip3] torch==1.12.1
[pip3] torchmetrics==0.8.0
[pip3] torchreid==0.2.5
[pip3] torchvision==0.13.1
[conda] Could not collect

@dagshub
Copy link

dagshub bot commented May 23, 2023

laszlovandenhoek added a commit to laszlovandenhoek/super-gradients that referenced this issue May 23, 2023
@Louis-Dupont
Copy link
Contributor

Good catch @laszlovandenhoek !

BloodAxe added a commit that referenced this issue May 24, 2023
Co-authored-by: Shay Aharon <80472096+shaydeci@users.noreply.github.com>
Co-authored-by: Eugene Khvedchenya <ekhvedchenya@gmail.com>
geoffrey-g-delhomme pushed a commit to geoffrey-g-delhomme/super-gradients that referenced this issue May 26, 2023
Co-authored-by: Shay Aharon <80472096+shaydeci@users.noreply.github.com>
Co-authored-by: Eugene Khvedchenya <ekhvedchenya@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants