Upload custom YOLO code
Browse files- models_related/models_config/yolov8_varroa_p2_c2fkv_dcnhead_p2p3only.yaml +52 -0
- models_related/models_config/yolov8_varroa_p2_c2fkv_dcnhead_p2p3p4.yaml +53 -0
- models_related/models_config/yolov8_varroa_p2_context_p3_c2fkv_dcnhead.yaml +55 -0
- models_related/models_config/yolov8_varroa_p2_context_p3_c2fkv_ldown_dcnhead.yaml +56 -0
- models_related/models_config/yolov8_varroa_p2_context_p3_dcnhead.yaml +55 -0
- models_related/models_config/yolov8_varroa_p2_context_p3_irdcb_dcnhead.yaml +55 -0
- models_related/models_config/yolov8_varroa_p2_context_p3_only_dcnhead.yaml +45 -0
- models_related/models_config/yolov8_varroa_p2_context_p3_partialkv_dcnhead.yaml +54 -0
- models_related/ultralytics/ultralytics/nn/modules/__init__.py +8 -0
- models_related/ultralytics/ultralytics/nn/modules/block.py +176 -0
- models_related/ultralytics/ultralytics/nn/tasks.py +8 -1
models_related/models_config/yolov8_varroa_p2_c2fkv_dcnhead_p2p3only.yaml
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv8 Varroa H3 ablation:
|
| 2 |
+
# Keep 4-head P2 + C2fKV topology, but apply DeformableHeadConv only to P2/P3.
|
| 3 |
+
|
| 4 |
+
nc: 1
|
| 5 |
+
scales:
|
| 6 |
+
n: [0.33, 0.25, 1024]
|
| 7 |
+
s: [0.33, 0.50, 1024]
|
| 8 |
+
m: [0.67, 0.75, 768]
|
| 9 |
+
l: [1.00, 1.00, 512]
|
| 10 |
+
x: [1.00, 1.25, 512]
|
| 11 |
+
|
| 12 |
+
backbone:
|
| 13 |
+
- [-1, 1, Conv, [64, 3, 2]]
|
| 14 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 15 |
+
- [-1, 3, C2f, [128, True]]
|
| 16 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 17 |
+
- [-1, 6, C2f, [256, True]]
|
| 18 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 19 |
+
- [-1, 6, C2f, [512, True]]
|
| 20 |
+
- [-1, 1, Conv, [1024, 3, 2]]
|
| 21 |
+
- [-1, 3, C2f, [1024, True]]
|
| 22 |
+
- [-1, 1, SPPF, [1024, 5]]
|
| 23 |
+
|
| 24 |
+
head:
|
| 25 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 26 |
+
- [[-1, 6], 1, Concat, [1]]
|
| 27 |
+
- [-1, 3, C2f, [512]]
|
| 28 |
+
|
| 29 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 30 |
+
- [[-1, 4], 1, Concat, [1]]
|
| 31 |
+
- [-1, 3, C2fKV, [256, False, 1, 0.5, 4, 2, dwconv]]
|
| 32 |
+
|
| 33 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 34 |
+
- [[-1, 2], 1, Concat, [1]]
|
| 35 |
+
- [-1, 3, C2fKV, [128, False, 1, 0.5, 4, 4, dwconv]]
|
| 36 |
+
|
| 37 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 38 |
+
- [[-1, 15], 1, Concat, [1]]
|
| 39 |
+
- [-1, 3, C2fKV, [256, False, 1, 0.5, 4, 2, dwconv]]
|
| 40 |
+
|
| 41 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 42 |
+
- [[-1, 12], 1, Concat, [1]]
|
| 43 |
+
- [-1, 3, C2f, [512]]
|
| 44 |
+
|
| 45 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 46 |
+
- [[-1, 9], 1, Concat, [1]]
|
| 47 |
+
- [-1, 3, C2f, [1024]]
|
| 48 |
+
|
| 49 |
+
- [18, 1, DeformableHeadConv, [128, 3, 0.0]]
|
| 50 |
+
- [21, 1, DeformableHeadConv, [256, 3, 0.0]]
|
| 51 |
+
|
| 52 |
+
- [[28, 29, 24, 27], 1, Detect, [nc]]
|
models_related/models_config/yolov8_varroa_p2_c2fkv_dcnhead_p2p3p4.yaml
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv8 Varroa H3 ablation:
|
| 2 |
+
# Keep 4-head P2 + C2fKV topology, but skip DeformableHeadConv on P5.
|
| 3 |
+
|
| 4 |
+
nc: 1
|
| 5 |
+
scales:
|
| 6 |
+
n: [0.33, 0.25, 1024]
|
| 7 |
+
s: [0.33, 0.50, 1024]
|
| 8 |
+
m: [0.67, 0.75, 768]
|
| 9 |
+
l: [1.00, 1.00, 512]
|
| 10 |
+
x: [1.00, 1.25, 512]
|
| 11 |
+
|
| 12 |
+
backbone:
|
| 13 |
+
- [-1, 1, Conv, [64, 3, 2]]
|
| 14 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 15 |
+
- [-1, 3, C2f, [128, True]]
|
| 16 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 17 |
+
- [-1, 6, C2f, [256, True]]
|
| 18 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 19 |
+
- [-1, 6, C2f, [512, True]]
|
| 20 |
+
- [-1, 1, Conv, [1024, 3, 2]]
|
| 21 |
+
- [-1, 3, C2f, [1024, True]]
|
| 22 |
+
- [-1, 1, SPPF, [1024, 5]]
|
| 23 |
+
|
| 24 |
+
head:
|
| 25 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 26 |
+
- [[-1, 6], 1, Concat, [1]]
|
| 27 |
+
- [-1, 3, C2f, [512]]
|
| 28 |
+
|
| 29 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 30 |
+
- [[-1, 4], 1, Concat, [1]]
|
| 31 |
+
- [-1, 3, C2fKV, [256, False, 1, 0.5, 4, 2, dwconv]]
|
| 32 |
+
|
| 33 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 34 |
+
- [[-1, 2], 1, Concat, [1]]
|
| 35 |
+
- [-1, 3, C2fKV, [128, False, 1, 0.5, 4, 4, dwconv]]
|
| 36 |
+
|
| 37 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 38 |
+
- [[-1, 15], 1, Concat, [1]]
|
| 39 |
+
- [-1, 3, C2fKV, [256, False, 1, 0.5, 4, 2, dwconv]]
|
| 40 |
+
|
| 41 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 42 |
+
- [[-1, 12], 1, Concat, [1]]
|
| 43 |
+
- [-1, 3, C2f, [512]]
|
| 44 |
+
|
| 45 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 46 |
+
- [[-1, 9], 1, Concat, [1]]
|
| 47 |
+
- [-1, 3, C2f, [1024]]
|
| 48 |
+
|
| 49 |
+
- [18, 1, DeformableHeadConv, [128, 3, 0.0]]
|
| 50 |
+
- [21, 1, DeformableHeadConv, [256, 3, 0.0]]
|
| 51 |
+
- [24, 1, DeformableHeadConv, [512, 3, 0.0]]
|
| 52 |
+
|
| 53 |
+
- [[28, 29, 30, 27], 1, Detect, [nc]]
|
models_related/models_config/yolov8_varroa_p2_context_p3_c2fkv_dcnhead.yaml
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv8 Varroa C2 ablation:
|
| 2 |
+
# P2 context -> P3, then C2fKV/KVCA only at P3 scale. Detect remains [P3, P4, P5].
|
| 3 |
+
|
| 4 |
+
nc: 1
|
| 5 |
+
scales:
|
| 6 |
+
n: [0.33, 0.25, 1024]
|
| 7 |
+
s: [0.33, 0.50, 1024]
|
| 8 |
+
m: [0.67, 0.75, 768]
|
| 9 |
+
l: [1.00, 1.00, 512]
|
| 10 |
+
x: [1.00, 1.25, 512]
|
| 11 |
+
|
| 12 |
+
backbone:
|
| 13 |
+
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
|
| 14 |
+
- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
|
| 15 |
+
- [-1, 3, C2f, [128, True]] # 2-P2/4
|
| 16 |
+
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
|
| 17 |
+
- [-1, 6, C2f, [256, True]] # 4-P3/8
|
| 18 |
+
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
|
| 19 |
+
- [-1, 6, C2f, [512, True]] # 6-P4/16
|
| 20 |
+
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
|
| 21 |
+
- [-1, 3, C2f, [1024, True]] # 8-P5/32
|
| 22 |
+
- [-1, 1, SPPF, [1024, 5]] # 9
|
| 23 |
+
|
| 24 |
+
head:
|
| 25 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 26 |
+
- [[-1, 6], 1, Concat, [1]]
|
| 27 |
+
- [-1, 3, C2f, [512]] # 12-P4/16 top-down
|
| 28 |
+
|
| 29 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 30 |
+
- [[-1, 4], 1, Concat, [1]]
|
| 31 |
+
- [-1, 3, C2f, [256]] # 15-P3/8 top-down
|
| 32 |
+
|
| 33 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 34 |
+
- [[-1, 2], 1, Concat, [1]]
|
| 35 |
+
- [-1, 3, C2f, [128]] # 18-P2/4 context source
|
| 36 |
+
|
| 37 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 38 |
+
- [[-1, 15], 1, Concat, [1]]
|
| 39 |
+
- [-1, 3, C2f, [256]] # 21-P3/8
|
| 40 |
+
|
| 41 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 42 |
+
- [[-1, 12], 1, Concat, [1]]
|
| 43 |
+
- [-1, 3, C2f, [512]] # 24-P4/16
|
| 44 |
+
|
| 45 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 46 |
+
- [[-1, 9], 1, Concat, [1]]
|
| 47 |
+
- [-1, 3, C2f, [1024]] # 27-P5/32
|
| 48 |
+
|
| 49 |
+
- [[18, 21], 1, P2LocalToP3, [256, 3, 0.0, 1.0]] # 28-P3 context-enhanced
|
| 50 |
+
- [-1, 1, C2fKV, [256, False, 1, 0.5, 4, 2, dwconv]] # 29-P3 KVCA only
|
| 51 |
+
- [29, 1, DeformableHeadConv, [256, 3, 0.0]] # 30-P3*
|
| 52 |
+
- [24, 1, DeformableHeadConv, [512, 3, 0.0]] # 31-P4*
|
| 53 |
+
- [27, 1, DeformableHeadConv, [1024, 3, 0.0]] # 32-P5*
|
| 54 |
+
|
| 55 |
+
- [[30, 31, 32], 1, Detect, [nc]]
|
models_related/models_config/yolov8_varroa_p2_context_p3_c2fkv_ldown_dcnhead.yaml
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv8 Varroa H2 ablation:
|
| 2 |
+
# C2 architecture with LDown replacing bottom-up stride-2 convolutions in the neck.
|
| 3 |
+
# LDown is paper-inspired from HierLight-YOLO's lightweight downsample description.
|
| 4 |
+
|
| 5 |
+
nc: 1
|
| 6 |
+
scales:
|
| 7 |
+
n: [0.33, 0.25, 1024]
|
| 8 |
+
s: [0.33, 0.50, 1024]
|
| 9 |
+
m: [0.67, 0.75, 768]
|
| 10 |
+
l: [1.00, 1.00, 512]
|
| 11 |
+
x: [1.00, 1.25, 512]
|
| 12 |
+
|
| 13 |
+
backbone:
|
| 14 |
+
- [-1, 1, Conv, [64, 3, 2]]
|
| 15 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 16 |
+
- [-1, 3, C2f, [128, True]]
|
| 17 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 18 |
+
- [-1, 6, C2f, [256, True]]
|
| 19 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 20 |
+
- [-1, 6, C2f, [512, True]]
|
| 21 |
+
- [-1, 1, Conv, [1024, 3, 2]]
|
| 22 |
+
- [-1, 3, C2f, [1024, True]]
|
| 23 |
+
- [-1, 1, SPPF, [1024, 5]]
|
| 24 |
+
|
| 25 |
+
head:
|
| 26 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 27 |
+
- [[-1, 6], 1, Concat, [1]]
|
| 28 |
+
- [-1, 3, C2f, [512]]
|
| 29 |
+
|
| 30 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 31 |
+
- [[-1, 4], 1, Concat, [1]]
|
| 32 |
+
- [-1, 3, C2f, [256]]
|
| 33 |
+
|
| 34 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 35 |
+
- [[-1, 2], 1, Concat, [1]]
|
| 36 |
+
- [-1, 3, C2f, [128]]
|
| 37 |
+
|
| 38 |
+
- [-1, 1, LDown, [128, 3, 2]]
|
| 39 |
+
- [[-1, 15], 1, Concat, [1]]
|
| 40 |
+
- [-1, 3, C2f, [256]]
|
| 41 |
+
|
| 42 |
+
- [-1, 1, LDown, [256, 3, 2]]
|
| 43 |
+
- [[-1, 12], 1, Concat, [1]]
|
| 44 |
+
- [-1, 3, C2f, [512]]
|
| 45 |
+
|
| 46 |
+
- [-1, 1, LDown, [512, 3, 2]]
|
| 47 |
+
- [[-1, 9], 1, Concat, [1]]
|
| 48 |
+
- [-1, 3, C2f, [1024]]
|
| 49 |
+
|
| 50 |
+
- [[18, 21], 1, P2LocalToP3, [256, 3, 0.0, 1.0]]
|
| 51 |
+
- [-1, 1, C2fKV, [256, False, 1, 0.5, 4, 2, dwconv]]
|
| 52 |
+
- [29, 1, DeformableHeadConv, [256, 3, 0.0]]
|
| 53 |
+
- [24, 1, DeformableHeadConv, [512, 3, 0.0]]
|
| 54 |
+
- [27, 1, DeformableHeadConv, [1024, 3, 0.0]]
|
| 55 |
+
|
| 56 |
+
- [[30, 31, 32], 1, Detect, [nc]]
|
models_related/models_config/yolov8_varroa_p2_context_p3_dcnhead.yaml
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv8 Varroa C1a ablation:
|
| 2 |
+
# Build P2, use it only as local context for P3, then Detect on [P3, P4, P5].
|
| 3 |
+
# This removes the P2 detection head and P2 DCNHead from the 4-head P2 + DCN baseline.
|
| 4 |
+
|
| 5 |
+
nc: 1
|
| 6 |
+
scales:
|
| 7 |
+
n: [0.33, 0.25, 1024]
|
| 8 |
+
s: [0.33, 0.50, 1024]
|
| 9 |
+
m: [0.67, 0.75, 768]
|
| 10 |
+
l: [1.00, 1.00, 512]
|
| 11 |
+
x: [1.00, 1.25, 512]
|
| 12 |
+
|
| 13 |
+
backbone:
|
| 14 |
+
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
|
| 15 |
+
- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
|
| 16 |
+
- [-1, 3, C2f, [128, True]] # 2-P2/4
|
| 17 |
+
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
|
| 18 |
+
- [-1, 6, C2f, [256, True]] # 4-P3/8
|
| 19 |
+
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
|
| 20 |
+
- [-1, 6, C2f, [512, True]] # 6-P4/16
|
| 21 |
+
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
|
| 22 |
+
- [-1, 3, C2f, [1024, True]] # 8-P5/32
|
| 23 |
+
- [-1, 1, SPPF, [1024, 5]] # 9
|
| 24 |
+
|
| 25 |
+
head:
|
| 26 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 27 |
+
- [[-1, 6], 1, Concat, [1]]
|
| 28 |
+
- [-1, 3, C2f, [512]] # 12-P4/16 top-down
|
| 29 |
+
|
| 30 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 31 |
+
- [[-1, 4], 1, Concat, [1]]
|
| 32 |
+
- [-1, 3, C2f, [256]] # 15-P3/8 top-down
|
| 33 |
+
|
| 34 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 35 |
+
- [[-1, 2], 1, Concat, [1]]
|
| 36 |
+
- [-1, 3, C2f, [128]] # 18-P2/4 context source
|
| 37 |
+
|
| 38 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 39 |
+
- [[-1, 15], 1, Concat, [1]]
|
| 40 |
+
- [-1, 3, C2f, [256]] # 21-P3/8
|
| 41 |
+
|
| 42 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 43 |
+
- [[-1, 12], 1, Concat, [1]]
|
| 44 |
+
- [-1, 3, C2f, [512]] # 24-P4/16
|
| 45 |
+
|
| 46 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 47 |
+
- [[-1, 9], 1, Concat, [1]]
|
| 48 |
+
- [-1, 3, C2f, [1024]] # 27-P5/32
|
| 49 |
+
|
| 50 |
+
- [[18, 21], 1, P2LocalToP3, [256, 3, 0.0, 1.0]] # 28-P3 context-enhanced
|
| 51 |
+
- [28, 1, DeformableHeadConv, [256, 3, 0.0]] # 29-P3*
|
| 52 |
+
- [24, 1, DeformableHeadConv, [512, 3, 0.0]] # 30-P4*
|
| 53 |
+
- [27, 1, DeformableHeadConv, [1024, 3, 0.0]] # 31-P5*
|
| 54 |
+
|
| 55 |
+
- [[29, 30, 31], 1, Detect, [nc]]
|
models_related/models_config/yolov8_varroa_p2_context_p3_irdcb_dcnhead.yaml
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv8 Varroa H1 ablation:
|
| 2 |
+
# P2 context -> P3 with IRDCB replacing selected neck C2f blocks.
|
| 3 |
+
# IRDCB is paper-inspired from HierLight-YOLO, adapted to C2f-style YAML compatibility.
|
| 4 |
+
|
| 5 |
+
nc: 1
|
| 6 |
+
scales:
|
| 7 |
+
n: [0.33, 0.25, 1024]
|
| 8 |
+
s: [0.33, 0.50, 1024]
|
| 9 |
+
m: [0.67, 0.75, 768]
|
| 10 |
+
l: [1.00, 1.00, 512]
|
| 11 |
+
x: [1.00, 1.25, 512]
|
| 12 |
+
|
| 13 |
+
backbone:
|
| 14 |
+
- [-1, 1, Conv, [64, 3, 2]]
|
| 15 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 16 |
+
- [-1, 3, C2f, [128, True]]
|
| 17 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 18 |
+
- [-1, 6, C2f, [256, True]]
|
| 19 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 20 |
+
- [-1, 6, C2f, [512, True]]
|
| 21 |
+
- [-1, 1, Conv, [1024, 3, 2]]
|
| 22 |
+
- [-1, 3, C2f, [1024, True]]
|
| 23 |
+
- [-1, 1, SPPF, [1024, 5]]
|
| 24 |
+
|
| 25 |
+
head:
|
| 26 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 27 |
+
- [[-1, 6], 1, Concat, [1]]
|
| 28 |
+
- [-1, 3, IRDCB, [512, 2.0, True]]
|
| 29 |
+
|
| 30 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 31 |
+
- [[-1, 4], 1, Concat, [1]]
|
| 32 |
+
- [-1, 3, IRDCB, [256, 2.0, True]]
|
| 33 |
+
|
| 34 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 35 |
+
- [[-1, 2], 1, Concat, [1]]
|
| 36 |
+
- [-1, 3, IRDCB, [128, 2.0, True]]
|
| 37 |
+
|
| 38 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 39 |
+
- [[-1, 15], 1, Concat, [1]]
|
| 40 |
+
- [-1, 3, IRDCB, [256, 2.0, True]]
|
| 41 |
+
|
| 42 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 43 |
+
- [[-1, 12], 1, Concat, [1]]
|
| 44 |
+
- [-1, 3, IRDCB, [512, 2.0, True]]
|
| 45 |
+
|
| 46 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 47 |
+
- [[-1, 9], 1, Concat, [1]]
|
| 48 |
+
- [-1, 3, IRDCB, [1024, 2.0, True]]
|
| 49 |
+
|
| 50 |
+
- [[18, 21], 1, P2LocalToP3, [256, 3, 0.0, 1.0]]
|
| 51 |
+
- [28, 1, DeformableHeadConv, [256, 3, 0.0]]
|
| 52 |
+
- [24, 1, DeformableHeadConv, [512, 3, 0.0]]
|
| 53 |
+
- [27, 1, DeformableHeadConv, [1024, 3, 0.0]]
|
| 54 |
+
|
| 55 |
+
- [[29, 30, 31], 1, Detect, [nc]]
|
models_related/models_config/yolov8_varroa_p2_context_p3_only_dcnhead.yaml
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv8 Varroa C1b ablation:
|
| 2 |
+
# Build P2, fuse P2 context into P3, then use only a P3 Detect head.
|
| 3 |
+
# This is intentionally aggressive and should be read as a hypothesis test, not a default candidate.
|
| 4 |
+
|
| 5 |
+
nc: 1
|
| 6 |
+
scales:
|
| 7 |
+
n: [0.33, 0.25, 1024]
|
| 8 |
+
s: [0.33, 0.50, 1024]
|
| 9 |
+
m: [0.67, 0.75, 768]
|
| 10 |
+
l: [1.00, 1.00, 512]
|
| 11 |
+
x: [1.00, 1.25, 512]
|
| 12 |
+
|
| 13 |
+
backbone:
|
| 14 |
+
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
|
| 15 |
+
- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
|
| 16 |
+
- [-1, 3, C2f, [128, True]] # 2-P2/4
|
| 17 |
+
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
|
| 18 |
+
- [-1, 6, C2f, [256, True]] # 4-P3/8
|
| 19 |
+
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
|
| 20 |
+
- [-1, 6, C2f, [512, True]] # 6-P4/16
|
| 21 |
+
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
|
| 22 |
+
- [-1, 3, C2f, [1024, True]] # 8-P5/32
|
| 23 |
+
- [-1, 1, SPPF, [1024, 5]] # 9
|
| 24 |
+
|
| 25 |
+
head:
|
| 26 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 27 |
+
- [[-1, 6], 1, Concat, [1]]
|
| 28 |
+
- [-1, 3, C2f, [512]] # 12-P4/16 top-down
|
| 29 |
+
|
| 30 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 31 |
+
- [[-1, 4], 1, Concat, [1]]
|
| 32 |
+
- [-1, 3, C2f, [256]] # 15-P3/8 top-down
|
| 33 |
+
|
| 34 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 35 |
+
- [[-1, 2], 1, Concat, [1]]
|
| 36 |
+
- [-1, 3, C2f, [128]] # 18-P2/4 context source
|
| 37 |
+
|
| 38 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 39 |
+
- [[-1, 15], 1, Concat, [1]]
|
| 40 |
+
- [-1, 3, C2f, [256]] # 21-P3/8
|
| 41 |
+
|
| 42 |
+
- [[18, 21], 1, P2LocalToP3, [256, 3, 0.0, 1.0]] # 22-P3 context-enhanced
|
| 43 |
+
- [22, 1, DeformableHeadConv, [256, 3, 0.0]] # 23-P3*
|
| 44 |
+
|
| 45 |
+
- [[23], 1, Detect, [nc]]
|
models_related/models_config/yolov8_varroa_p2_context_p3_partialkv_dcnhead.yaml
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv8 Varroa C3 ablation:
|
| 2 |
+
# P3 queries attend to compressed P2 keys/values on a small channel subset, then Detect [P3, P4, P5].
|
| 3 |
+
|
| 4 |
+
nc: 1
|
| 5 |
+
scales:
|
| 6 |
+
n: [0.33, 0.25, 1024]
|
| 7 |
+
s: [0.33, 0.50, 1024]
|
| 8 |
+
m: [0.67, 0.75, 768]
|
| 9 |
+
l: [1.00, 1.00, 512]
|
| 10 |
+
x: [1.00, 1.25, 512]
|
| 11 |
+
|
| 12 |
+
backbone:
|
| 13 |
+
- [-1, 1, Conv, [64, 3, 2]]
|
| 14 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 15 |
+
- [-1, 3, C2f, [128, True]]
|
| 16 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 17 |
+
- [-1, 6, C2f, [256, True]]
|
| 18 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 19 |
+
- [-1, 6, C2f, [512, True]]
|
| 20 |
+
- [-1, 1, Conv, [1024, 3, 2]]
|
| 21 |
+
- [-1, 3, C2f, [1024, True]]
|
| 22 |
+
- [-1, 1, SPPF, [1024, 5]]
|
| 23 |
+
|
| 24 |
+
head:
|
| 25 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 26 |
+
- [[-1, 6], 1, Concat, [1]]
|
| 27 |
+
- [-1, 3, C2f, [512]]
|
| 28 |
+
|
| 29 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 30 |
+
- [[-1, 4], 1, Concat, [1]]
|
| 31 |
+
- [-1, 3, C2f, [256]]
|
| 32 |
+
|
| 33 |
+
- [-1, 1, nn.Upsample, [None, 2, nearest]]
|
| 34 |
+
- [[-1, 2], 1, Concat, [1]]
|
| 35 |
+
- [-1, 3, C2f, [128]]
|
| 36 |
+
|
| 37 |
+
- [-1, 1, Conv, [128, 3, 2]]
|
| 38 |
+
- [[-1, 15], 1, Concat, [1]]
|
| 39 |
+
- [-1, 3, C2f, [256]]
|
| 40 |
+
|
| 41 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
| 42 |
+
- [[-1, 12], 1, Concat, [1]]
|
| 43 |
+
- [-1, 3, C2f, [512]]
|
| 44 |
+
|
| 45 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
| 46 |
+
- [[-1, 9], 1, Concat, [1]]
|
| 47 |
+
- [-1, 3, C2f, [1024]]
|
| 48 |
+
|
| 49 |
+
- [[18, 21], 1, P2PartialKVToP3, [256, 4, 4, 0.25, 0.0]] # 28-P3 partial KV from P2
|
| 50 |
+
- [28, 1, DeformableHeadConv, [256, 3, 0.0]]
|
| 51 |
+
- [24, 1, DeformableHeadConv, [512, 3, 0.0]]
|
| 52 |
+
- [27, 1, DeformableHeadConv, [1024, 3, 0.0]]
|
| 53 |
+
|
| 54 |
+
- [[29, 30, 31], 1, Detect, [nc]]
|
models_related/ultralytics/ultralytics/nn/modules/__init__.py
CHANGED
|
@@ -55,12 +55,16 @@ from .block import (
|
|
| 55 |
HGBlock,
|
| 56 |
HGStem,
|
| 57 |
ImagePoolingAttn,
|
|
|
|
| 58 |
KVCompressedAttention,
|
| 59 |
KVCompressedAttentionPartial,
|
| 60 |
KVCompressedTransformerEncoder,
|
| 61 |
LearnableEnSimAM,
|
|
|
|
| 62 |
MaxSigmoidAttnBlock,
|
| 63 |
P4CrossScaleLocalAttention,
|
|
|
|
|
|
|
| 64 |
Proto,
|
| 65 |
RegionRoutingAttentionLite,
|
| 66 |
RepC3,
|
|
@@ -176,10 +180,12 @@ __all__ = (
|
|
| 176 |
"HGStem",
|
| 177 |
"ImagePoolingAttn",
|
| 178 |
"Index",
|
|
|
|
| 179 |
"KVCompressedAttention",
|
| 180 |
"KVCompressedAttentionPartial",
|
| 181 |
"KVCompressedTransformerEncoder",
|
| 182 |
"LearnableEnSimAM",
|
|
|
|
| 183 |
"LRPCHead",
|
| 184 |
"LayerNorm2d",
|
| 185 |
"LightConv",
|
|
@@ -187,6 +193,8 @@ __all__ = (
|
|
| 187 |
"MSDeformAttn",
|
| 188 |
"MaxSigmoidAttnBlock",
|
| 189 |
"P4CrossScaleLocalAttention",
|
|
|
|
|
|
|
| 190 |
"Pose",
|
| 191 |
"Pose26",
|
| 192 |
"Proto",
|
|
|
|
| 55 |
HGBlock,
|
| 56 |
HGStem,
|
| 57 |
ImagePoolingAttn,
|
| 58 |
+
IRDCB,
|
| 59 |
KVCompressedAttention,
|
| 60 |
KVCompressedAttentionPartial,
|
| 61 |
KVCompressedTransformerEncoder,
|
| 62 |
LearnableEnSimAM,
|
| 63 |
+
LDown,
|
| 64 |
MaxSigmoidAttnBlock,
|
| 65 |
P4CrossScaleLocalAttention,
|
| 66 |
+
P2LocalToP3,
|
| 67 |
+
P2PartialKVToP3,
|
| 68 |
Proto,
|
| 69 |
RegionRoutingAttentionLite,
|
| 70 |
RepC3,
|
|
|
|
| 180 |
"HGStem",
|
| 181 |
"ImagePoolingAttn",
|
| 182 |
"Index",
|
| 183 |
+
"IRDCB",
|
| 184 |
"KVCompressedAttention",
|
| 185 |
"KVCompressedAttentionPartial",
|
| 186 |
"KVCompressedTransformerEncoder",
|
| 187 |
"LearnableEnSimAM",
|
| 188 |
+
"LDown",
|
| 189 |
"LRPCHead",
|
| 190 |
"LayerNorm2d",
|
| 191 |
"LightConv",
|
|
|
|
| 193 |
"MSDeformAttn",
|
| 194 |
"MaxSigmoidAttnBlock",
|
| 195 |
"P4CrossScaleLocalAttention",
|
| 196 |
+
"P2LocalToP3",
|
| 197 |
+
"P2PartialKVToP3",
|
| 198 |
"Pose",
|
| 199 |
"Pose26",
|
| 200 |
"Proto",
|
models_related/ultralytics/ultralytics/nn/modules/block.py
CHANGED
|
@@ -49,11 +49,15 @@ __all__ = (
|
|
| 49 |
"HGBlock",
|
| 50 |
"HGStem",
|
| 51 |
"ImagePoolingAttn",
|
|
|
|
| 52 |
"KVCompressedAttention",
|
| 53 |
"KVCompressedAttentionPartial",
|
| 54 |
"KVCompressedTransformerEncoder",
|
| 55 |
"LearnableEnSimAM",
|
|
|
|
| 56 |
"P4CrossScaleLocalAttention",
|
|
|
|
|
|
|
| 57 |
"Proto",
|
| 58 |
"RegionRoutingAttentionLite",
|
| 59 |
"TopKAdaptiveGroupKVAttention",
|
|
@@ -955,6 +959,178 @@ class P4CrossScaleLocalAttention(nn.Module):
|
|
| 955 |
return p4 + self.gamma * refined
|
| 956 |
|
| 957 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 958 |
class Proto(nn.Module):
|
| 959 |
"""Ultralytics YOLO models mask Proto module for segmentation models."""
|
| 960 |
|
|
|
|
| 49 |
"HGBlock",
|
| 50 |
"HGStem",
|
| 51 |
"ImagePoolingAttn",
|
| 52 |
+
"IRDCB",
|
| 53 |
"KVCompressedAttention",
|
| 54 |
"KVCompressedAttentionPartial",
|
| 55 |
"KVCompressedTransformerEncoder",
|
| 56 |
"LearnableEnSimAM",
|
| 57 |
+
"LDown",
|
| 58 |
"P4CrossScaleLocalAttention",
|
| 59 |
+
"P2LocalToP3",
|
| 60 |
+
"P2PartialKVToP3",
|
| 61 |
"Proto",
|
| 62 |
"RegionRoutingAttentionLite",
|
| 63 |
"TopKAdaptiveGroupKVAttention",
|
|
|
|
| 959 |
return p4 + self.gamma * refined
|
| 960 |
|
| 961 |
|
| 962 |
+
class LDown(nn.Module):
|
| 963 |
+
"""Lightweight downsample: depthwise spatial reduction followed by pointwise channel projection.
|
| 964 |
+
|
| 965 |
+
This is a paper-inspired implementation of HierLight-YOLO's LDown idea, adapted to the local
|
| 966 |
+
Ultralytics Conv/DWConv style instead of copying an upstream implementation.
|
| 967 |
+
"""
|
| 968 |
+
|
| 969 |
+
def __init__(self, c1: int, c2: int, k: int = 3, s: int = 2, act: bool = True):
|
| 970 |
+
super().__init__()
|
| 971 |
+
self.dw = Conv(c1, c1, k, s, g=c1, act=act)
|
| 972 |
+
self.pw = Conv(c1, c2, 1, 1, act=act)
|
| 973 |
+
|
| 974 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 975 |
+
"""Downsample spatially, then project channels."""
|
| 976 |
+
return self.pw(self.dw(x))
|
| 977 |
+
|
| 978 |
+
|
| 979 |
+
class IRDCBUnit(nn.Module):
|
| 980 |
+
"""Inverted residual depthwise unit used inside IRDCB."""
|
| 981 |
+
|
| 982 |
+
def __init__(self, c: int, expansion: float = 2.0, shortcut: bool = True):
|
| 983 |
+
super().__init__()
|
| 984 |
+
hidden = max(8, int(c * expansion))
|
| 985 |
+
self.expand = Conv(c, hidden, 1, 1)
|
| 986 |
+
self.dw1 = Conv(hidden, hidden, 3, 1, g=hidden)
|
| 987 |
+
self.dw2 = Conv(hidden, hidden, 3, 1, g=hidden)
|
| 988 |
+
self.project = Conv(hidden, c, 1, 1, act=False)
|
| 989 |
+
self.shortcut = shortcut
|
| 990 |
+
|
| 991 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 992 |
+
"""Apply expand, two depthwise spatial filters, and projection."""
|
| 993 |
+
y = self.project(self.dw2(self.dw1(self.expand(x))))
|
| 994 |
+
return x + y if self.shortcut else y
|
| 995 |
+
|
| 996 |
+
|
| 997 |
+
class IRDCB(nn.Module):
|
| 998 |
+
"""C2f-compatible inverted residual depthwise convolution block.
|
| 999 |
+
|
| 1000 |
+
Inspired by HierLight-YOLO's IRDCB, this implementation keeps the YOLOv8 C2f-style split/concat
|
| 1001 |
+
interface so it can replace selected neck C2f layers without changing surrounding YAML topology.
|
| 1002 |
+
"""
|
| 1003 |
+
|
| 1004 |
+
def __init__(self, c1: int, c2: int, n: int = 1, expansion: float = 2.0, shortcut: bool = True, e: float = 0.5):
|
| 1005 |
+
super().__init__()
|
| 1006 |
+
self.c = int(c2 * e)
|
| 1007 |
+
self.cv1 = Conv(c1, 2 * self.c, 1, 1)
|
| 1008 |
+
self.cv2 = Conv((2 + n) * self.c, c2, 1, 1)
|
| 1009 |
+
self.m = nn.ModuleList(IRDCBUnit(self.c, expansion=expansion, shortcut=shortcut) for _ in range(n))
|
| 1010 |
+
|
| 1011 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 1012 |
+
"""Forward pass using C2f-style feature accumulation with IRDCB units."""
|
| 1013 |
+
y = list(self.cv1(x).chunk(2, 1))
|
| 1014 |
+
y.extend(m(y[-1]) for m in self.m)
|
| 1015 |
+
return self.cv2(torch.cat(y, 1))
|
| 1016 |
+
|
| 1017 |
+
def forward_split(self, x: torch.Tensor) -> torch.Tensor:
|
| 1018 |
+
"""Forward pass using split() instead of chunk()."""
|
| 1019 |
+
y = list(self.cv1(x).split((self.c, self.c), 1))
|
| 1020 |
+
y.extend(m(y[-1]) for m in self.m)
|
| 1021 |
+
return self.cv2(torch.cat(y, 1))
|
| 1022 |
+
|
| 1023 |
+
|
| 1024 |
+
class P2LocalToP3(nn.Module):
|
| 1025 |
+
"""Fuse high-resolution P2 detail into P3 without exposing P2 to Detect."""
|
| 1026 |
+
|
| 1027 |
+
def __init__(
|
| 1028 |
+
self,
|
| 1029 |
+
channels: list[int],
|
| 1030 |
+
c2: int,
|
| 1031 |
+
k: int = 3,
|
| 1032 |
+
gamma_init: float = 0.0,
|
| 1033 |
+
gate_scale: float = 1.0,
|
| 1034 |
+
):
|
| 1035 |
+
super().__init__()
|
| 1036 |
+
if len(channels) != 2:
|
| 1037 |
+
raise ValueError(f"P2LocalToP3 expects [P2, P3] channels, got {channels}")
|
| 1038 |
+
c_p2, c_p3 = channels
|
| 1039 |
+
self.p2_proj = Conv(c_p2, c2, 1, 1)
|
| 1040 |
+
self.p3_proj = Conv(c_p3, c2, 1, 1)
|
| 1041 |
+
self.gate = nn.Sequential(
|
| 1042 |
+
Conv(2 * c2, c2, k, 1, g=c2),
|
| 1043 |
+
nn.Conv2d(c2, c2, 1),
|
| 1044 |
+
nn.Sigmoid(),
|
| 1045 |
+
)
|
| 1046 |
+
self.context_down = LDown(c2, c2, k=3, s=2)
|
| 1047 |
+
self.gamma = nn.Parameter(torch.tensor(float(gamma_init)))
|
| 1048 |
+
self.gate_scale = float(gate_scale)
|
| 1049 |
+
|
| 1050 |
+
@staticmethod
|
| 1051 |
+
def _resize_to(x: torch.Tensor, size: tuple[int, int]) -> torch.Tensor:
|
| 1052 |
+
if x.shape[-2:] == size:
|
| 1053 |
+
return x
|
| 1054 |
+
return F.interpolate(x, size=size, mode="bilinear", align_corners=False)
|
| 1055 |
+
|
| 1056 |
+
def _to_p3_size(self, x: torch.Tensor, target_size: tuple[int, int]) -> torch.Tensor:
|
| 1057 |
+
if x.shape[-2:] == target_size:
|
| 1058 |
+
return x
|
| 1059 |
+
if x.shape[-2] == target_size[0] * 2 and x.shape[-1] == target_size[1] * 2:
|
| 1060 |
+
return self.context_down(x)
|
| 1061 |
+
return F.interpolate(x, size=target_size, mode="bilinear", align_corners=False)
|
| 1062 |
+
|
| 1063 |
+
def forward(self, x: list[torch.Tensor]) -> torch.Tensor:
|
| 1064 |
+
"""Return a P3-scale tensor enriched by local P2 detail."""
|
| 1065 |
+
p2, p3 = x
|
| 1066 |
+
target_size = p3.shape[-2:]
|
| 1067 |
+
p3_base = self.p3_proj(p3)
|
| 1068 |
+
p2_detail = self.p2_proj(p2)
|
| 1069 |
+
p3_up = self._resize_to(p3_base, p2_detail.shape[-2:])
|
| 1070 |
+
gate = self.gate(torch.cat((p2_detail, p3_up), dim=1))
|
| 1071 |
+
context = self._to_p3_size(gate * p2_detail, target_size)
|
| 1072 |
+
return p3_base + self.gamma * self.gate_scale * context
|
| 1073 |
+
|
| 1074 |
+
|
| 1075 |
+
class P2PartialKVToP3(nn.Module):
|
| 1076 |
+
"""Partial cross-scale KV attention from compressed P2 keys/values into P3 queries."""
|
| 1077 |
+
|
| 1078 |
+
def __init__(
|
| 1079 |
+
self,
|
| 1080 |
+
channels: list[int],
|
| 1081 |
+
c2: int,
|
| 1082 |
+
num_heads: int = 4,
|
| 1083 |
+
sr_ratio: int = 4,
|
| 1084 |
+
attn_ratio: float = 0.25,
|
| 1085 |
+
gamma_init: float = 0.0,
|
| 1086 |
+
):
|
| 1087 |
+
super().__init__()
|
| 1088 |
+
if len(channels) != 2:
|
| 1089 |
+
raise ValueError(f"P2PartialKVToP3 expects [P2, P3] channels, got {channels}")
|
| 1090 |
+
c_p2, c_p3 = channels
|
| 1091 |
+
self.p2_proj = Conv(c_p2, c2, 1, 1)
|
| 1092 |
+
self.p3_proj = Conv(c_p3, c2, 1, 1)
|
| 1093 |
+
self.attn_c = max(8, int(c2 * attn_ratio))
|
| 1094 |
+
self.attn_c = min(self.attn_c, c2)
|
| 1095 |
+
self.num_heads = _choose_attention_heads(self.attn_c, num_heads)
|
| 1096 |
+
self.head_dim = self.attn_c // self.num_heads
|
| 1097 |
+
self.scale = self.head_dim**-0.5
|
| 1098 |
+
self.sr_ratio = max(1, int(sr_ratio))
|
| 1099 |
+
self.q = nn.Conv2d(self.attn_c, self.attn_c, 1, bias=False)
|
| 1100 |
+
self.k = nn.Conv2d(self.attn_c, self.attn_c, 1, bias=False)
|
| 1101 |
+
self.v = nn.Conv2d(self.attn_c, self.attn_c, 1, bias=False)
|
| 1102 |
+
self.out = nn.Sequential(nn.Conv2d(self.attn_c, c2, 1, bias=False), nn.BatchNorm2d(c2))
|
| 1103 |
+
self.gamma = nn.Parameter(torch.tensor(float(gamma_init)))
|
| 1104 |
+
|
| 1105 |
+
def _compress_p2(self, x: torch.Tensor) -> torch.Tensor:
|
| 1106 |
+
if self.sr_ratio <= 1:
|
| 1107 |
+
return x
|
| 1108 |
+
if x.shape[-2] < self.sr_ratio or x.shape[-1] < self.sr_ratio:
|
| 1109 |
+
return F.adaptive_avg_pool2d(x, (1, 1))
|
| 1110 |
+
return F.avg_pool2d(x, kernel_size=self.sr_ratio, stride=self.sr_ratio)
|
| 1111 |
+
|
| 1112 |
+
def forward(self, x: list[torch.Tensor]) -> torch.Tensor:
|
| 1113 |
+
"""Apply partial cross-attention and return a P3-scale tensor."""
|
| 1114 |
+
p2, p3 = x
|
| 1115 |
+
p3_base = self.p3_proj(p3)
|
| 1116 |
+
p2_base = self.p2_proj(p2)
|
| 1117 |
+
q_src = p3_base[:, : self.attn_c]
|
| 1118 |
+
kv_src = self._compress_p2(p2_base[:, : self.attn_c])
|
| 1119 |
+
|
| 1120 |
+
b, _, h, w = q_src.shape
|
| 1121 |
+
q = self.q(q_src).flatten(2).transpose(1, 2).reshape(b, h * w, self.num_heads, self.head_dim)
|
| 1122 |
+
q = q.permute(0, 2, 1, 3)
|
| 1123 |
+
k = self.k(kv_src).flatten(2).transpose(1, 2).reshape(b, -1, self.num_heads, self.head_dim)
|
| 1124 |
+
v = self.v(kv_src).flatten(2).transpose(1, 2).reshape(b, -1, self.num_heads, self.head_dim)
|
| 1125 |
+
k = k.permute(0, 2, 1, 3)
|
| 1126 |
+
v = v.permute(0, 2, 1, 3)
|
| 1127 |
+
|
| 1128 |
+
attn = (q @ k.transpose(-2, -1)) * self.scale
|
| 1129 |
+
refined = (attn.softmax(dim=-1) @ v).transpose(1, 2).reshape(b, h * w, self.attn_c)
|
| 1130 |
+
refined = refined.transpose(1, 2).reshape(b, self.attn_c, h, w)
|
| 1131 |
+
return p3_base + self.gamma * self.out(refined)
|
| 1132 |
+
|
| 1133 |
+
|
| 1134 |
class Proto(nn.Module):
|
| 1135 |
"""Ultralytics YOLO models mask Proto module for segmentation models."""
|
| 1136 |
|
models_related/ultralytics/ultralytics/nn/tasks.py
CHANGED
|
@@ -59,11 +59,15 @@ from models_related.ultralytics.ultralytics.nn.modules import (
|
|
| 59 |
HGStem,
|
| 60 |
ImagePoolingAttn,
|
| 61 |
Index,
|
|
|
|
| 62 |
KVCompressedAttention,
|
| 63 |
KVCompressedAttentionPartial,
|
| 64 |
KVCompressedTransformerEncoder,
|
| 65 |
LearnableEnSimAM,
|
|
|
|
| 66 |
LRPCHead,
|
|
|
|
|
|
|
| 67 |
Pose,
|
| 68 |
Pose26,
|
| 69 |
RegionRoutingAttentionLite,
|
|
@@ -1815,6 +1819,8 @@ def parse_model(d, ch, verbose=True):
|
|
| 1815 |
C2,
|
| 1816 |
C2f,
|
| 1817 |
C2fKV,
|
|
|
|
|
|
|
| 1818 |
C3k2,
|
| 1819 |
RepNCSPELAN4,
|
| 1820 |
ELAN1,
|
|
@@ -1850,6 +1856,7 @@ def parse_model(d, ch, verbose=True):
|
|
| 1850 |
C2,
|
| 1851 |
C2f,
|
| 1852 |
C2fKV,
|
|
|
|
| 1853 |
C3k2,
|
| 1854 |
C2fAttn,
|
| 1855 |
C3,
|
|
@@ -1943,7 +1950,7 @@ def parse_model(d, ch, verbose=True):
|
|
| 1943 |
args.insert(1, [ch[x] for x in f]) # channels as second arg
|
| 1944 |
elif m in {EnSimAM, LearnableEnSimAM}:
|
| 1945 |
c2 = ch[f]
|
| 1946 |
-
elif m
|
| 1947 |
c2 = args[0]
|
| 1948 |
c2 = make_divisible(min(c2, max_channels) * width, 8)
|
| 1949 |
args = [[ch[x] for x in f], c2, *args[1:]]
|
|
|
|
| 59 |
HGStem,
|
| 60 |
ImagePoolingAttn,
|
| 61 |
Index,
|
| 62 |
+
IRDCB,
|
| 63 |
KVCompressedAttention,
|
| 64 |
KVCompressedAttentionPartial,
|
| 65 |
KVCompressedTransformerEncoder,
|
| 66 |
LearnableEnSimAM,
|
| 67 |
+
LDown,
|
| 68 |
LRPCHead,
|
| 69 |
+
P2LocalToP3,
|
| 70 |
+
P2PartialKVToP3,
|
| 71 |
Pose,
|
| 72 |
Pose26,
|
| 73 |
RegionRoutingAttentionLite,
|
|
|
|
| 1819 |
C2,
|
| 1820 |
C2f,
|
| 1821 |
C2fKV,
|
| 1822 |
+
IRDCB,
|
| 1823 |
+
LDown,
|
| 1824 |
C3k2,
|
| 1825 |
RepNCSPELAN4,
|
| 1826 |
ELAN1,
|
|
|
|
| 1856 |
C2,
|
| 1857 |
C2f,
|
| 1858 |
C2fKV,
|
| 1859 |
+
IRDCB,
|
| 1860 |
C3k2,
|
| 1861 |
C2fAttn,
|
| 1862 |
C3,
|
|
|
|
| 1950 |
args.insert(1, [ch[x] for x in f]) # channels as second arg
|
| 1951 |
elif m in {EnSimAM, LearnableEnSimAM}:
|
| 1952 |
c2 = ch[f]
|
| 1953 |
+
elif m in {P4CrossScaleLocalAttention, P2LocalToP3, P2PartialKVToP3}:
|
| 1954 |
c2 = args[0]
|
| 1955 |
c2 = make_divisible(min(c2, max_channels) * width, 8)
|
| 1956 |
args = [[ch[x] for x in f], c2, *args[1:]]
|