zhiqwang / yolort

yolort is a runtime stack for yolov5 on specialized accelerators such as tensorrt, libtorch, onnxruntime, tvm and ncnn.
https://zhiqwang.com/yolort
GNU General Public License v3.0
708 stars 153 forks source link

SetCriterion's forward() incompatible with P6 models. Can't train P6 models. #494

Open SarBH opened 1 year ago

SarBH commented 1 year ago

🐛 Describe the bug

Cannot train the yolov5s6 yolov5m6 yolov5l6 yolov5n6 variants.

The P6 variants are not trainable because the SetCriterion module is not compatible with the values for anchor_grids and strides on the P6 models.

for example, for the yolov5s6 model, the defined strides and anchor grid values are:

yolov5-rt-stack/yolort/models/yolo.py at 8b578eb9a7910f1dcb28188a36c8c540d15a9430 · zhiqwang/yolov5-rt-stack

strides = [8, 16, 32, 64]
anchor_grids = [
    [19, 27, 44, 40, 38, 94],
    [96, 68, 86, 152, 180, 137],
    [140, 301, 303, 264, 238, 542],
    [436, 615, 739, 380, 925, 792],
]

These lead to this shape mismatch in the SetCriterion forward() method, which is only ran during training: yolov5-rt-stack/yolort/models/box_head.py at 8b578eb9a7910f1dcb28188a36c8c540d15a9430 · zhiqwang/yolov5-rt-stack

anchor_grids = torch.as_tensor(self.anchor_grids, dtype=torch.float32, device=device).view(self.num_anchors, -1, 2)
strides = torch.as_tensor(self.strides, dtype=torch.float32, device=device).view(-1, 1, 1)
anchor_grids /= strides

Gives the error

  File "XXX/yolort/models/box_head.py", line XXX, in forward
    anchor_grids /= strides
RuntimeError: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 0

Reproducibility info:

I dont have straightforward repro info since ive wrapped your models with a LightningModule so i can train it in any data.

Versions

Collecting environment information...
PyTorch version: 2.0.0+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Debian GNU/Linux 11 (bullseye) (x86_64)
GCC version: (Debian 10.2.1-6) 10.2.1 20210110
Clang version: Could not collect
CMake version: version 3.26.4
Libc version: glibc-2.31

Python version: 3.8.13 (default, Aug  2 2022, 11:41:59)  [GCC 10.2.1 20210110] (64-bit runtime)
Python platform: Linux-5.15.0-71-generic-x86_64-with-glibc2.2.5
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA RTX A4500
GPU 1: NVIDIA RTX A4500

Nvidia driver version: 530.30.02
cuDNN version: Could not collect
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
Byte Order:                      Little Endian
Address sizes:                   46 bits physical, 48 bits virtual
CPU(s):                          24
On-line CPU(s) list:             0-23
Thread(s) per core:              2
Core(s) per socket:              12
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           85
Model name:                      Intel(R) Core(TM) i9-10920X CPU @ 3.50GHz
Stepping:                        7
CPU MHz:                         1200.005
CPU max MHz:                     4800.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        6999.82
Virtualization:                  VT-x
L1d cache:                       384 KiB
L1i cache:                       384 KiB
L2 cache:                        12 MiB
L3 cache:                        19.3 MiB
NUMA node0 CPU(s):               0-23
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
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:   Mitigation; TSX disabled
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] efficientnet-pytorch==0.7.1
[pip3] flake8==5.0.4
[pip3] flake8-copyright==0.2.3
[pip3] mypy==0.991
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.23.5
[pip3] pytorch-lightning==2.0.0
[pip3] pytorchcv==0.0.67
[pip3] torch==2.0.0
[pip3] torch-cuda121==2.0.0
[pip3] torchinfo==1.6.6
[pip3] torchmetrics==0.7.2
[pip3] torchvision==0.15.1
[pip3] torchvision-cuda121==0.15.1
[pip3] triton==2.0.0
[conda] Could not collect