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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
# Project Run Description: YOLO Varroa Small Object Experiments

## 1. Muc Tieu

Muc tieu hien tai la cai tien YOLO-based detector cho bai toan phat hien varroa tren anh ong, tap trung vao:

- Tang `mAP50-95`, uu tien localization on dinh hon thay vi chi tang `mAP50`.
- Cai thien small object detection tren anh kich thuoc nho `160x280`.
- Tranh lap lai cac thu nghiem attention/KV da co san trong `models_related`.
- Giu thay doi chu yeu o `neck`, `head`, loss, NMS, va setting train/inference.

## 2. Hien Trang Codebase

Thu muc chinh:

- `models_related/models_config/`: cac YAML kien truc YOLO.
- `models_related/ultralytics/ultralytics/nn/modules/block.py`: noi dang khai bao custom blocks.
- `models_related/ultralytics/ultralytics/nn/modules/__init__.py`: export custom blocks.
- `models_related/ultralytics/ultralytics/nn/tasks.py`: import block va dang ky trong `parse_model`.
- `models_related/train_eval/train.py`: entrypoint train.
- `models_related/train_eval/eval.py`: entrypoint eval.
- `prepare_dataset.py`: convert label custom `xyxy` sang YOLO format.

Da co cac YAML/huong thu nghiem:

| YAML | Noi dung |
|---|---|
| `yolov8_varroa_custom.yaml` | YOLOv8 template gan baseline |
| `yolov8_varroa_kvca.yaml` | Them `KVCompressedAttention` sau neck P3/P4 |
| `yolov8_varroa_c2fkv.yaml` | Thay C2f bang `C2fKV` o P3/P4 |
| `yolov8_varroa_topk_adaptive_kv.yaml` | Adaptive top-k grouped KV attention |
| `yolov8_varroa_topk_global_kv.yaml` | Global top-k grouped KV attention |
| `yolov8_varroa_bra_original.yaml` | Bi-level routing attention |
| `yolov8_varroa_bralite.yaml` | Region routing attention lite |
| `yolov8_varroa_p2_c2fkv.yaml` | Da co P2 detection head + C2fKV |
| `yolov10_varroa_custom.yaml` | YOLOv10 template |

Ket luan: khong nen tiep tuc chi them KV/top-k/BRA attention vao P3/P4 vi da co nhieu bien the. Can tao cac control va huong moi hon.

## 3. Hien Trang Dataset

Dataset layout:

```text
data/
  train|val|test/
    videos/<video-id>/*.png
    labels/<video-id>/*.txt
```

Label goc:

```text
<num_boxes>
x1 y1 x2 y2
...
```

Thong ke nhanh:

| Split | Images | Positive images | Boxes | Empty/missing |
|---|---:|---:|---:|---:|
| train | 8223 | 2553 | 2904 | 5670 |
| val | 1876 | 451 | 655 | 1425 |
| test | 3408 | 942 | 1068 | 2466 |

Tat ca anh co kich thuoc `160x280`.

Box median:

| Split | Median box w | Median box h | Median area ratio |
|---|---:|---:|---:|
| train | 32.5 px | 33.3 px | 2.36% |
| val | 33.6 px | 30.1 px | 2.18% |
| test | 29.7 px | 27.6 px | 1.81% |

Nhan xet:

- Day la small-object task, nhung khong phai tiny-object cuc doan 4-8 px.
- Dataset co nhieu anh am tinh, nen false positive co the anh huong precision/F1.
- De tang `mAP50-95`, can cai thien chat luong bbox/localization, khong chi tang recall.

## 4. Paper Va Method Nen Tham Khao

| Method | Paper goc / link | Diem lien quan |
|---|---|---|
| P2 small-object detection head + ASF + Soft-NMS | [SOD-YOLO: Enhancing YOLO-Based Detection of Small Objects in UAV Imagery](https://arxiv.org/abs/2507.12727) | Them P2 stride 4, ASF neck fusion, Soft-NMS; tang manh `mAP50-95` tren VisDrone |
| P2 + BiFPN | [An Improved YOLOv8 Approach for Small Target Detection of Rice Spikelet Flowering](https://arxiv.org/abs/2507.20506) | Thay PAN/FPN bang BiFPN va them P2 cho small target |
| P2 + NWD loss | [DroneScan-YOLO](https://arxiv.org/abs/2604.13278) va [Normalized Gaussian Wasserstein Distance](https://arxiv.org/abs/2110.13389) | NWD giam do nhay cua IoU voi bbox nho, huu ich cho `mAP50-95` |
| Soft-NMS | [Soft-NMS: Improving Object Detection With One Line of Code](https://arxiv.org/abs/1704.04503) | Thay hard NMS bang score decay, co the giam miss khi box gan nhau |
| GhostHead / Ghost modules | [YOLO-Drone: GhostHead Network](https://arxiv.org/abs/2511.10905), nen tang [GhostNet](https://arxiv.org/abs/1911.11907) | Lam head nhe hon, giam redundancy, co the tang speed va regularization |
| Light neck attention GAM/CBAM/SA | [YOLO-AMC](https://arxiv.org/abs/2606.12958), nen tang [CBAM](https://arxiv.org/abs/1807.06521) | Them attention nhe o neck fusion; can can than vi repo da co nhieu attention |
| IGD / gather-distribute neck fusion | [YOLO-APD](https://arxiv.org/abs/2507.05376) | Huong feature fusion o neck, khac voi KV attention |
| BiFPN goc | [EfficientDet: Scalable and Efficient Object Detection](https://arxiv.org/abs/1911.09070) | Paper goc cua weighted bidirectional feature fusion |
| Deformable localization head | [Deformable ConvNets v2](https://arxiv.org/abs/1811.11168) | Dung deformable conv nhe o regression/head branch de feature sampling bam sat object hon, uu tien `mAP50-95` |
| DFL/TAL localization tuning | [Generalized Focal Loss](https://arxiv.org/abs/2006.04388), [GFLV2](https://arxiv.org/abs/2011.12885), [TOOD/TAL](https://arxiv.org/abs/2108.07755) | Tang do min bbox distribution va chinh positive assignment de cai thien AP o nguong IoU cao |

Ghi chu: nhieu paper 2025-2026 hien tai la arXiv/preprint. Neu luan van yeu cau Q1/Q2, can kiem tra them Scopus/JCR hoac tim ban journal tuong ung.

## 5. Nguyen Tac Thiet Ke Thu Nghiem

Can tach ro dong gop cua tung thanh phan:

1. P2 co giup khong?
2. Neu P2 giup, attention/KV tren P2/P3 co giup them khong?
3. Neu mAP50 tang nhung mAP50-95 khong tang, can chuyen sang loss/localization.
4. Neu recall tang nhung precision giam, can dieu chinh NMS/conf/negative handling.
5. Khong cong nhieu block cung luc ngay tu dau vi se khong biet block nao co tac dung.

## 6. Ma Tran Thu Nghiem De Xuat

### A0. Baseline Sach

Muc dich: tao moc so sanh.

Model:

```text
yolov8n.pt
```

Train setting de xuat:

```bash
python -m models_related.train_eval.train \
  --root data \
  --weights yolov8n.pt \
  --epochs 100 \
  --imgsz 640 \
  --batch-size 16 \
  --workers 4 \
  --device cuda \
  --patience 25 \
  --name yolov8n_baseline_img640
```

Them mot run input lon hon:

```bash
python -m models_related.train_eval.train \
  --root data \
  --weights yolov8n.pt \
  --epochs 100 \
  --imgsz 960 \
  --batch-size 8 \
  --workers 4 \
  --device cuda \
  --patience 25 \
  --name yolov8n_baseline_img960
```

Tac dung voi pipeline:

- Xac dinh baseline thuc te tren dataset varroa.
- Biet `imgsz=960` co dang tien hon `640` khong.

### A1. P2 Plain Control

Muc dich: tach rieng tac dung cua P2 detection head.

Can them YAML:

```text
models_related/models_config/yolov8_varroa_p2.yaml
```

Noi dung:

- Giu backbone YOLOv8n nhu goc.
- Them nhanh upsample tu P3 ve P2.
- Detect tren `[P2, P3, P4, P5]`.
- Khong them attention, khong C2fKV.

Paper lien quan:

- [SOD-YOLO](https://arxiv.org/abs/2507.12727)
- [Rice Spikelet YOLOv8-P2](https://arxiv.org/abs/2507.20506)
- [DroneScan-YOLO](https://arxiv.org/abs/2604.13278)

Train:

```bash
python -m models_related.train_eval.train \
  --root data \
  --model-yaml models_related/models_config/yolov8_varroa_p2.yaml \
  --pretrained yolov8n.pt \
  --epochs 100 \
  --imgsz 960 \
  --batch-size 8 \
  --workers 4 \
  --device cuda \
  --patience 25 \
  --name yolov8n_varroa_p2_img960
```

Tac dung ky vong:

- Tang recall cho varroa nho.
- Cai thien localization vi P2 stride 4 co feature map min hon P3 stride 8.
- La control bat buoc de so voi `yolov8_varroa_p2_c2fkv.yaml`.

Tieu chi quyet dinh:

- Neu A1 tang gan bang A2, thi P2 moi la dong gop chinh.
- Neu A2 vuot A1 ro rang, C2fKV tren P2/P3 co gia tri.

### A2. P2 + C2fKV Da Co San

YAML da co:

```text
models_related/models_config/yolov8_varroa_p2_c2fkv.yaml
```

Paper/method lien quan:

- Gan voi huong P2 cua [SOD-YOLO](https://arxiv.org/abs/2507.12727)
- Gan voi KV-compressed attention/PSA-style attention tu implementation hien tai.

Train:

```bash
python -m models_related.train_eval.train \
  --root data \
  --model-yaml models_related/models_config/yolov8_varroa_p2_c2fkv.yaml \
  --pretrained yolov8n.pt \
  --epochs 100 \
  --imgsz 960 \
  --batch-size 8 \
  --workers 4 \
  --device cuda \
  --patience 25 \
  --name yolov8n_varroa_p2_c2fkv_img960
```

Tac dung ky vong:

- P2 giu chi tiet spatial.
- C2fKV refine feature P2/P3 bang attention nhe co KV compression.

Rui ro:

- Anh goc chi `160x280`, khi train `imgsz=960` feature map P2 rat lon; C2fKV tren P2 co the ton VRAM.
- Neu OOM, giam `batch-size=4` hoac `imgsz=768`.

### A3. P2 + BiFPN / Weighted Fusion

Muc dich: thu neck fusion khac voi attention/KV da co.

Can them block:

```text
WeightedFeatureFusion
BiFPNFusion2
BiFPNFusion3
```

Noi them:

```text
models_related/ultralytics/ultralytics/nn/modules/block.py
models_related/ultralytics/ultralytics/nn/modules/__init__.py
models_related/ultralytics/ultralytics/nn/tasks.py
```

Can them YAML:

```text
models_related/models_config/yolov8_varroa_p2_bifpn.yaml
```

Paper goc:

- [EfficientDet / BiFPN](https://arxiv.org/abs/1911.09070)
- [Rice Spikelet YOLOv8-P2 + BiFPN](https://arxiv.org/abs/2507.20506)

Config de xuat:

- Dung P2/P3/P4/P5 head.
- Thay `Concat + C2f` o neck bang weighted fusion:
  - top-down: P5 -> P4 -> P3 -> P2
  - bottom-up: P2 -> P3 -> P4 -> P5
- Moi fusion co learnable weights duong va normalize:

```python
w = relu(self.w)
y = sum(w_i * x_i) / (sum(w) + eps)
```

Tac dung ky vong:

- Hoc trong so scale nao quan trong hon cho varroa.
- It trung voi cac block attention hien co.
- Nhe hon top-k/BRA attention.

Train:

```bash
python -m models_related.train_eval.train \
  --root data \
  --model-yaml models_related/models_config/yolov8_varroa_p2_bifpn.yaml \
  --pretrained yolov8n.pt \
  --epochs 100 \
  --imgsz 960 \
  --batch-size 8 \
  --workers 4 \
  --device cuda \
  --patience 25 \
  --name yolov8n_varroa_p2_bifpn_img960
```

### A4. P2 + ASF Neck Fusion

Muc dich: thu Adaptive Scale Fusion, gan voi SOD-YOLO.

Paper goc:

- [SOD-YOLO](https://arxiv.org/abs/2507.12727)

Can them block:

```text
ASF2
ASF3
```

Vi tri tich hop:

- Sau khi resize cac feature ve cung spatial size.
- Dung conv 1x1 de dua ve cung channel.
- Tao weight theo scale bang global/local pooling + sigmoid/softmax.
- Fuse cac scale roi dua vao C2f/Detect.

Tac dung ky vong:

- Giup model tu chon scale phu hop giua P2/P3/P4.
- Huu ich khi varroa co kich thuoc thay doi nhung van nho.

Rui ro:

- De trung y tuong voi attention neu thiet ke qua phuc tap.
- Nen giu ASF nhe, uu tien scale weighting hon token attention.

### A5. Localization Loss: NWD / MPDIoU / SIoU

Muc dich: tang `mAP50-95`.

Paper goc:

- [Normalized Gaussian Wasserstein Distance](https://arxiv.org/abs/2110.13389)
- [DroneScan-YOLO](https://arxiv.org/abs/2604.13278)

Ly do voi varroa:

- Box median chi khoang `30x30 px`.
- IoU rat nhay voi sai lech vai pixel.
- `mAP50` co the tot nhung `mAP50-95` thap neu bbox chua khit.

Can sua:

```text
models_related/ultralytics/ultralytics/utils/loss.py
models_related/ultralytics/ultralytics/utils/metrics.py
```

Huong tich hop toi thieu:

- Giu TaskAlignedAssigner nhu cu de giam rui ro.
- Them term NWD vao box loss:

```text
loss_box = alpha * CIoU_loss + (1 - alpha) * NWD_loss
```

De xuat:

```text
alpha = 0.7
nwd_weight = 0.3
```

Sau do moi thu NWD trong assignment neu can.

Train:

```bash
python -m models_related.train_eval.train \
  --root data \
  --model-yaml models_related/models_config/yolov8_varroa_p2.yaml \
  --pretrained yolov8n.pt \
  --epochs 100 \
  --imgsz 960 \
  --batch-size 8 \
  --workers 4 \
  --device cuda \
  --patience 25 \
  --name yolov8n_varroa_p2_nwd_img960
```

Tac dung ky vong:

- Tang AP o nguong IoU cao.
- Tang `mAP50-95` nhieu hon `mAP50`.

### A6. Soft-NMS Inference

Muc dich: cai thien post-processing ma khong can train lai.

Paper goc:

- [Soft-NMS](https://arxiv.org/abs/1704.04503)
- SOD-YOLO cung dung Soft-NMS trong pipeline small object.

Can sua:

```text
models_related/ultralytics/ultralytics/utils/ops.py
```

Hoac them mode rieng trong eval script de khong anh huong train.

Setting de xuat:

```text
conf = 0.001 khi tinh mAP
nms_iou = 0.5 hoac 0.6
soft_nms_sigma = 0.5
score_threshold = 0.001
```

Tac dung ky vong:

- Giam viec loai nham box khi varroa/bee texture gay duplicate proposals.
- Co the tang recall.

Rui ro:

- Dataset nhieu anh am tinh, Soft-NMS co the tang false positive neu conf qua thap.
- Can bao cao precision/recall/F1 cung voi mAP.

### A7. GhostHead / Lightweight Head

Muc dich: thu thay doi head nhe, khac voi attention/KV.

Paper goc:

- [YOLO-Drone GhostHead](https://arxiv.org/abs/2511.10905)
- [GhostNet](https://arxiv.org/abs/1911.11907)

Can them block/head:

```text
GhostDetect
GhostHeadConv
```

Vi tri:

- Thay cac conv trong Detect head bang GhostConv/Ghost modules.
- Uu tien P2/P3 branch truoc.

Tac dung ky vong:

- Giam tham so/FLOPs.
- Co the giam overfitting vi dataset positive khong lon.
- Neu mAP giu nguyen nhung speed tang, van co gia tri.

Rui ro:

- Loi ich accuracy co the nho.
- Khong nen uu tien truoc P2/BiFPN/NWD neu muc tieu chinh la `mAP50-95`.

### A8. Deformable Localization Head

Muc dich: uu tien localization ma khong lap lai cac huong da thu trong bang hien tai nhu `WIoU`, `KVCA`, `M3NAT`, `BiRouting`, `BiFPN`.

Paper goc:

- [Deformable ConvNets v2](https://arxiv.org/abs/1811.11168)

Y tuong:

- Giu backbone va neck gan nhu cu.
- Chi thay mot phan nho o head/regression branch bang deformable conv.
- Uu tien chen tren feature dung de predict bbox, dac biet `P3/P4`; neu dang thu P2 4-head thi thu `P2/P3`.
- Khong them token attention/global attention, khong dung KV, khong doi loss luc dau.

Can them block:

```text
DeformableHeadConv
```

Vi tri tich hop de xuat:

```text
P3/P4/P5 feature -> DeformableHeadConv -> Detect
```

Hoac neu tach duoc branch trong `Detect`:

```text
regression branch: Conv -> DeformableHeadConv -> bbox logits
classification branch: giu Conv goc
```

Ly do voi varroa:

- Varroa la object nho, bbox sai lech vai pixel lam rot AP o nguong IoU cao.
- Regular conv lay mau theo grid co dinh; deformable conv cho phep feature sampling lech theo hinh dang/vi tri object.
- Huong nay danh vao chat luong box nen phu hop voi muc tieu `mAP50-95` hon so voi chi tang recall.

Can sua:

```text
models_related/ultralytics/ultralytics/nn/modules/block.py
models_related/ultralytics/ultralytics/nn/modules/__init__.py
models_related/ultralytics/ultralytics/nn/tasks.py
models_related/models_config/yolov8_varroa_p4_dcnhead.yaml
```

Goi y implement:

- Neu `torchvision.ops.DeformConv2d` co san tren Molab, dung no de tranh them dependency lon.
- Offset conv khoi tao zero de luc dau gan voi conv thuong.
- Ban dau chi dat 1 block truoc Detect, khong thay tat ca C2f.
- Bat dau voi P4/P3 truoc; neu VRAM on moi thu P2.

Train de xuat:

```bash
python -m models_related.train_eval.train \
  --root data \
  --model-yaml models_related/models_config/yolov8_varroa_p4_dcnhead.yaml \
  --pretrained yolov8n.pt \
  --epochs 100 \
  --imgsz 960 \
  --batch-size 8 \
  --workers 4 \
  --device cuda \
  --patience 25 \
  --name yolov8n_varroa_p4_dcnhead_img960
```

Tieu chi doc ket qua:

- Neu `mAP50` gan nhu giu nguyen nhung `mAP50-95` tang, DCN dang giup bbox khit hon.
- Neu recall tang nhung precision giam, DCN co the dang overfit texture; thu chi gan tren P4 thay vi P2/P3.
- Neu OOM/cham, thay DCN bang depthwise DCN hoac chi dung o 1 scale P4.

Do uu tien: cao neu ket qua hien tai dang `mAP50` tot nhung `mAP50-95` thap.

### A9. DFL Reg-Max + Task-Aligned Assigner Tuning

Muc dich: cai thien localization bang cach tang do min cua bbox distribution va lua chon positive samples chat hon, khong them block attention moi.

Paper goc:

- [Generalized Focal Loss](https://arxiv.org/abs/2006.04388)
- [Generalized Focal Loss V2](https://arxiv.org/abs/2011.12885)
- [TOOD / Task Alignment Learning](https://arxiv.org/abs/2108.07755)

Ly do voi pipeline hien tai:

- YOLOv8 da dung Distribution Focal Loss, nhung `reg_max` mac dinh co the chua toi uu cho anh upsample `imgsz=960`.
- Neu box thuc te khoang `30x30 px`, sai lech nho van lam rot AP75/AP90.
- Chinh `reg_max` va assigner khong trung voi cac method trong bang hien tai vi no tac dong vao regression resolution/positive matching thay vi them attention hay loss IoU moi.

Can sua:

```text
models_related/ultralytics/ultralytics/nn/modules/head.py
models_related/ultralytics/ultralytics/utils/loss.py
models_related/models_config/yolov8_varroa_p4_regmax24.yaml
```

Config de xuat:

```text
reg_max: 16 -> 24 hoac 32
tal_topk: 10 -> 15 hoac 20
tal_alpha: giu mac dinh truoc
tal_beta: giu mac dinh truoc
```

Huong tich hop:

- Them tham so `reg_max` vao YAML/head neu local Ultralytics chua expose.
- Them tham so `tal_topk` vao loss config hoac train args.
- Chay ablation tung cai:

```text
A9a: reg_max=24, topk giu mac dinh
A9b: reg_max=32, topk giu mac dinh
A9c: reg_max=24, tal_topk=15
```

Train de xuat:

```bash
python -m models_related.train_eval.train \
  --root data \
  --model-yaml models_related/models_config/yolov8_varroa_p4_regmax24.yaml \
  --pretrained yolov8n.pt \
  --epochs 100 \
  --imgsz 960 \
  --batch-size 8 \
  --workers 4 \
  --device cuda \
  --patience 25 \
  --name yolov8n_varroa_p4_regmax24_img960
```

Tac dung ky vong:

- Tang AP o cac nguong IoU cao, nhat la AP75 tro len.
- Score cua prediction lien ket tot hon voi localization quality, giup ranking trong mAP on hon.
- It thay doi kien truc nen de so sanh voi B3/P4CSLA va P2 4-head.

Rui ro:

- `reg_max=32` tang channel bbox logits, co the cham hon va ton VRAM hon.
- `tal_topk` qua cao co the dua them positive kem chat luong, lam box lech hon.
- Nen uu tien `reg_max=24` truoc vi can bang chi phi va localization.

## 7. Thu Tu Uu Tien De Chay

Nen chay theo thu tu:

```text
1. A0 yolov8n baseline img640
2. A0 yolov8n baseline img960
3. A1 yolov8n + P2 plain img960
4. A2 yolov8n + P2 + C2fKV img960
5. A3 yolov8n + P2 + BiFPN img960
6. A5 best(P2 plain/BiFPN/C2fKV) + NWD loss
7. A8 yolov8n + Deformable Localization Head img960
8. A9 yolov8n + reg_max24 / TAL tuning img960
9. A6 Soft-NMS inference ablation tren best checkpoint
10. A7 GhostHead neu can speed/lightweight contribution
```

Khong nen chay ngay:

```text
P2 + C2fKV + BiFPN + NWD + Soft-NMS
```

Vi se khong biet thanh phan nao dong gop.

## 8. Setting Train Chung De Xuat

Cho baseline va YAML custom:

```text
epochs: 100
imgsz: 960
batch-size: 8 neu GPU du, nguoc lai 4
workers: 4
device: cuda
patience: 25
pretrained: yolov8n.pt
```

Neu VRAM yeu:

```text
imgsz: 768
batch-size: 4
```

Neu model on va muon day small object hon:

```text
imgsz: 1280
batch-size: 2-4
```

Nen them cac Ultralytics train args vao `train_eval/train.py` neu can:

```text
close_mosaic: 20
cos_lr: True
cache: False hoac ram neu du RAM
single_cls: True
```

Ly do:

- `close_mosaic=20`: cuoi training dung mosaic de bbox that hon.
- `cos_lr=True`: schedule mem hon.
- `single_cls=True`: task chi co 1 class, tranh overhead khong can thiet.

## 9. Eval Setting De Xuat

Eval tren val sau moi run:

```bash
python -m models_related.train_eval.eval \
  --root data \
  --weights <run>/weights/best.pt \
  --split val \
  --imgsz 960 \
  --batch-size 8 \
  --device cuda \
  --workers 4 \
  --name <experiment_name>_val
```

Eval cuoi tren test:

```bash
python -m models_related.train_eval.eval \
  --root data \
  --weights <best_run>/weights/best.pt \
  --split test \
  --imgsz 960 \
  --batch-size 8 \
  --device cuda \
  --workers 4 \
  --name <experiment_name>_test
```

Can ghi lai:

- `mAP50`
- `mAP50-95`
- precision
- recall
- F1
- TP/FP/FN
- Params
- GFLOPs
- FPS / inference ms

Vi dataset co nhieu anh am tinh, khong chi xem mAP.

## 10. Cach Tich Hop Block Moi

Quy trinh them block moi:

1. Them class vao:

```text
models_related/ultralytics/ultralytics/nn/modules/block.py
```

2. Export trong:

```text
models_related/ultralytics/ultralytics/nn/modules/__init__.py
```

3. Import va dang ky trong:

```text
models_related/ultralytics/ultralytics/nn/tasks.py
```

4. Neu block co signature `(c1, c2, ...)`, dua block vao `base_modules` trong `parse_model`.

5. Neu block can repeat count `n`, dua vao `repeat_modules`.

6. Tao YAML moi trong:

```text
models_related/models_config/
```

7. Smoke test model build:

```bash
PYTHONPATH=models_related/ultralytics \
python -c "from ultralytics import YOLO; m=YOLO('models_related/models_config/yolov8_varroa_p2.yaml'); m.info(detailed=True)"
```

8. Smoke train voi limit nho:

```bash
python -m models_related.train_eval.train \
  --root data \
  --model-yaml models_related/models_config/yolov8_varroa_p2.yaml \
  --pretrained yolov8n.pt \
  --epochs 1 \
  --imgsz 640 \
  --batch-size 2 \
  --workers 0 \
  --device cuda \
  --limit 32 \
  --name smoke_p2
```

## 11. Bang Ghi Ket Qua De Dien Sau

| ID | Model | imgsz | Params | GFLOPs | mAP50 val | mAP50-95 val | P | R | F1 | FP | FN | Ghi chu |
|---|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---|
| A0-640 | YOLOv8n baseline | 640 | | | | | | | | | | |
| A0-960 | YOLOv8n baseline | 960 | | | | | | | | | | |
| A1 | YOLOv8n + P2 plain | 960 | | | | | | | | | | |
| A2 | YOLOv8n + P2 + C2fKV | 960 | | | | | | | | | | |
| A3 | YOLOv8n + P2 + BiFPN | 960 | | | | | | | | | | |
| A5 | Best + NWD loss | 960 | | | | | | | | | | |
| A6 | Best + Soft-NMS | 960 | | | | | | | | | | |
| A8 | YOLOv8n + DCN localization head | 960 | | | | | | | | | | |
| A9 | YOLOv8n + reg_max24/TAL tuning | 960 | | | | | | | | | | |

## 12. De Xuat Ket Luan Tam Thoi

Huong nen lam truoc:

```text
yolov8_varroa_p2.yaml
```

Ly do:

- Repo da co `p2_c2fkv` nhung chua co P2 plain control.
- Neu khong co P2 plain, khong the ket luan C2fKV/attention co dong gop hay khong.
- P2 la huong duoc nhieu paper small object gan day su dung.

Huong thu hai:

```text
yolov8_varroa_p2_bifpn.yaml
```

Ly do:

- Khac voi cac attention/KV da co.
- Hop voi small object va multi-scale fusion.
- De giai thich trong luan van hon so voi top-k/BRA phuc tap.

Huong thu ba:

```text
NWD loss tren best P2/BiFPN checkpoint
```

Ly do:

- Target chinh la `mAP50-95`.
- NWD danh truc tiep vao do nhay localization cua bbox nho.

Huong moi nen xem xet neu cac ket qua hien tai giong bang Dataset 1, tuc `mAP50` da cao nhung `mAP50-95` chi quanh `0.27-0.28`:

```text
A8 Deformable Localization Head
A9 DFL reg_max24 + TAL tuning
```

Ly do:

- Cac run hien tai da thu nhieu bien the `WIoU`, `P2`, `KVCA`, `M3NAT`, `BiRouting`, `BiFPN`.
- Can mot huong tac dong truc tiep vao bbox quality/localization, khong chi tang recall.
- A8 tac dong vao feature sampling o head/regression branch.
- A9 tac dong vao do min bbox distribution va cach gan positive sample.
- Ca hai deu de lam ablation sach hon so voi viec cong them attention moi vao neck.

## 13. B3 Setup: P4-Centered Cross-Scale Local Attention

Trang thai: da setup code.

Files:

```text
models_related/ultralytics/ultralytics/nn/modules/block.py
models_related/ultralytics/ultralytics/nn/modules/__init__.py
models_related/ultralytics/ultralytics/nn/tasks.py
models_related/models_config/yolov8_varroa_p4csla.yaml
notebook_run_marimo.ipynb
```

Block moi:

```text
P4CrossScaleLocalAttention
```

Y tuong:

- Input block la `[P2, P3, P4, P5]`.
- `P4` lam query trung tam vi can bang semantic va localization.
- `P3`, `P4`, `P5` cung cap local K/V windows cho attention.
- `P2` khong dua vao Detect; chi dung lam localization gate cho output P4.
- Detect van la 3-head `[P3, P4*, P5]`, nen day la B3, khong trung voi ablation P2 detection head.

YAML:

```text
models_related/models_config/yolov8_varroa_p4csla.yaml
```

Core line:

```yaml
- [[2, 15, 18, 21], 1, P4CrossScaleLocalAttention, [512, 4, 7, 0.1]]
- [[15, 22, 21], 1, Detect, [nc]]
```

Args:

```text
[c2, num_heads, local_window, p2_gate_scale]
```

Default de xuat:

```text
c2 = 512 YAML nominal, se duoc scale theo YOLOv8n width multiplier
num_heads = 4
local_window = 7
p2_gate_scale = 0.1
gamma init = 0
```

Ly do `gamma init = 0`:

- Khi load `yolov8n.pt`, model khoi dau gan voi baseline.
- Giam rui ro custom block lam mat on dinh training trong cac epoch dau.

Train cell da them vao:

```text
notebook_run_marimo.ipynb
```

Ten run:

```text
yolov8n_varroa_p4csla_img960
```

Lenh tuong duong:

```bash
python -m models_related.train_eval.train \
  --root data \
  --model-yaml models_related/models_config/yolov8_varroa_p4csla.yaml \
  --pretrained yolov8n.pt \
  --epochs 100 \
  --imgsz 960 \
  --batch-size 8 \
  --workers 4 \
  --device cuda \
  --patience 25 \
  --name yolov8n_varroa_p4csla_img960
```

So sanh bat buoc:

```text
B0: yolov8n baseline img960
B1: yolov8n + P2 plain img960
B2: yolov8n + P2 + C2fKV img960
B3: yolov8n + P4CrossScaleLocalAttention img960
```

Dieu can doc trong ket qua:

- Neu B3 tang `mAP50-95` hon B0 nhung khong tang recall nhieu, block dang giup localization/semantic balance.
- Neu B3 thua B1, P2 detect head quan trong hon P4-centric fusion.
- Neu B3 tang precision/F1 tren anh am tinh, P2 gate + P5 context co tac dung giam false positive.