dgenerate-ultralytics-headless 8.3.222__py3-none-any.whl → 8.3.225__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dgenerate_ultralytics_headless-8.3.222.dist-info → dgenerate_ultralytics_headless-8.3.225.dist-info}/METADATA +2 -2
- dgenerate_ultralytics_headless-8.3.225.dist-info/RECORD +286 -0
- tests/conftest.py +5 -8
- tests/test_cli.py +1 -8
- tests/test_python.py +1 -2
- ultralytics/__init__.py +1 -1
- ultralytics/cfg/__init__.py +34 -49
- ultralytics/cfg/datasets/ImageNet.yaml +1 -1
- ultralytics/cfg/datasets/kitti.yaml +27 -0
- ultralytics/cfg/datasets/lvis.yaml +5 -5
- ultralytics/cfg/datasets/open-images-v7.yaml +1 -1
- ultralytics/data/annotator.py +3 -4
- ultralytics/data/augment.py +244 -323
- ultralytics/data/base.py +12 -22
- ultralytics/data/build.py +47 -40
- ultralytics/data/converter.py +32 -42
- ultralytics/data/dataset.py +43 -71
- ultralytics/data/loaders.py +22 -34
- ultralytics/data/split.py +5 -6
- ultralytics/data/split_dota.py +8 -15
- ultralytics/data/utils.py +27 -36
- ultralytics/engine/exporter.py +49 -116
- ultralytics/engine/model.py +144 -180
- ultralytics/engine/predictor.py +18 -29
- ultralytics/engine/results.py +165 -231
- ultralytics/engine/trainer.py +11 -19
- ultralytics/engine/tuner.py +13 -23
- ultralytics/engine/validator.py +6 -10
- ultralytics/hub/__init__.py +7 -12
- ultralytics/hub/auth.py +6 -12
- ultralytics/hub/google/__init__.py +7 -10
- ultralytics/hub/session.py +15 -25
- ultralytics/hub/utils.py +3 -6
- ultralytics/models/fastsam/model.py +6 -8
- ultralytics/models/fastsam/predict.py +5 -10
- ultralytics/models/fastsam/utils.py +1 -2
- ultralytics/models/fastsam/val.py +2 -4
- ultralytics/models/nas/model.py +5 -8
- ultralytics/models/nas/predict.py +7 -9
- ultralytics/models/nas/val.py +1 -2
- ultralytics/models/rtdetr/model.py +5 -8
- ultralytics/models/rtdetr/predict.py +15 -18
- ultralytics/models/rtdetr/train.py +10 -13
- ultralytics/models/rtdetr/val.py +13 -20
- ultralytics/models/sam/amg.py +12 -18
- ultralytics/models/sam/build.py +6 -9
- ultralytics/models/sam/model.py +16 -23
- ultralytics/models/sam/modules/blocks.py +62 -84
- ultralytics/models/sam/modules/decoders.py +17 -24
- ultralytics/models/sam/modules/encoders.py +40 -56
- ultralytics/models/sam/modules/memory_attention.py +10 -16
- ultralytics/models/sam/modules/sam.py +41 -47
- ultralytics/models/sam/modules/tiny_encoder.py +64 -83
- ultralytics/models/sam/modules/transformer.py +17 -27
- ultralytics/models/sam/modules/utils.py +31 -42
- ultralytics/models/sam/predict.py +172 -209
- ultralytics/models/utils/loss.py +14 -26
- ultralytics/models/utils/ops.py +13 -17
- ultralytics/models/yolo/classify/predict.py +8 -11
- ultralytics/models/yolo/classify/train.py +8 -16
- ultralytics/models/yolo/classify/val.py +13 -20
- ultralytics/models/yolo/detect/predict.py +4 -8
- ultralytics/models/yolo/detect/train.py +11 -20
- ultralytics/models/yolo/detect/val.py +38 -48
- ultralytics/models/yolo/model.py +35 -47
- ultralytics/models/yolo/obb/predict.py +5 -8
- ultralytics/models/yolo/obb/train.py +11 -14
- ultralytics/models/yolo/obb/val.py +20 -28
- ultralytics/models/yolo/pose/predict.py +5 -8
- ultralytics/models/yolo/pose/train.py +4 -8
- ultralytics/models/yolo/pose/val.py +31 -39
- ultralytics/models/yolo/segment/predict.py +9 -14
- ultralytics/models/yolo/segment/train.py +3 -6
- ultralytics/models/yolo/segment/val.py +16 -26
- ultralytics/models/yolo/world/train.py +8 -14
- ultralytics/models/yolo/world/train_world.py +11 -16
- ultralytics/models/yolo/yoloe/predict.py +16 -23
- ultralytics/models/yolo/yoloe/train.py +30 -43
- ultralytics/models/yolo/yoloe/train_seg.py +5 -10
- ultralytics/models/yolo/yoloe/val.py +15 -20
- ultralytics/nn/autobackend.py +10 -18
- ultralytics/nn/modules/activation.py +4 -6
- ultralytics/nn/modules/block.py +99 -185
- ultralytics/nn/modules/conv.py +45 -90
- ultralytics/nn/modules/head.py +44 -98
- ultralytics/nn/modules/transformer.py +44 -76
- ultralytics/nn/modules/utils.py +14 -19
- ultralytics/nn/tasks.py +86 -146
- ultralytics/nn/text_model.py +25 -40
- ultralytics/solutions/ai_gym.py +10 -16
- ultralytics/solutions/analytics.py +7 -10
- ultralytics/solutions/config.py +4 -5
- ultralytics/solutions/distance_calculation.py +9 -12
- ultralytics/solutions/heatmap.py +7 -13
- ultralytics/solutions/instance_segmentation.py +5 -8
- ultralytics/solutions/object_blurrer.py +7 -10
- ultralytics/solutions/object_counter.py +8 -12
- ultralytics/solutions/object_cropper.py +5 -8
- ultralytics/solutions/parking_management.py +12 -14
- ultralytics/solutions/queue_management.py +4 -6
- ultralytics/solutions/region_counter.py +7 -10
- ultralytics/solutions/security_alarm.py +14 -19
- ultralytics/solutions/similarity_search.py +7 -12
- ultralytics/solutions/solutions.py +31 -53
- ultralytics/solutions/speed_estimation.py +6 -9
- ultralytics/solutions/streamlit_inference.py +2 -4
- ultralytics/solutions/trackzone.py +7 -10
- ultralytics/solutions/vision_eye.py +5 -8
- ultralytics/trackers/basetrack.py +2 -4
- ultralytics/trackers/bot_sort.py +6 -11
- ultralytics/trackers/byte_tracker.py +10 -15
- ultralytics/trackers/track.py +3 -6
- ultralytics/trackers/utils/gmc.py +6 -12
- ultralytics/trackers/utils/kalman_filter.py +35 -43
- ultralytics/trackers/utils/matching.py +6 -10
- ultralytics/utils/__init__.py +61 -100
- ultralytics/utils/autobatch.py +2 -4
- ultralytics/utils/autodevice.py +11 -13
- ultralytics/utils/benchmarks.py +25 -35
- ultralytics/utils/callbacks/base.py +8 -10
- ultralytics/utils/callbacks/clearml.py +2 -4
- ultralytics/utils/callbacks/comet.py +30 -44
- ultralytics/utils/callbacks/dvc.py +13 -18
- ultralytics/utils/callbacks/mlflow.py +4 -5
- ultralytics/utils/callbacks/neptune.py +4 -6
- ultralytics/utils/callbacks/raytune.py +3 -4
- ultralytics/utils/callbacks/tensorboard.py +4 -6
- ultralytics/utils/callbacks/wb.py +10 -13
- ultralytics/utils/checks.py +29 -56
- ultralytics/utils/cpu.py +1 -2
- ultralytics/utils/dist.py +8 -12
- ultralytics/utils/downloads.py +17 -27
- ultralytics/utils/errors.py +6 -8
- ultralytics/utils/events.py +2 -4
- ultralytics/utils/export/__init__.py +4 -239
- ultralytics/utils/export/engine.py +237 -0
- ultralytics/utils/export/imx.py +11 -17
- ultralytics/utils/export/tensorflow.py +217 -0
- ultralytics/utils/files.py +10 -15
- ultralytics/utils/git.py +5 -7
- ultralytics/utils/instance.py +30 -51
- ultralytics/utils/logger.py +11 -15
- ultralytics/utils/loss.py +8 -14
- ultralytics/utils/metrics.py +98 -138
- ultralytics/utils/nms.py +13 -16
- ultralytics/utils/ops.py +47 -74
- ultralytics/utils/patches.py +11 -18
- ultralytics/utils/plotting.py +29 -42
- ultralytics/utils/tal.py +25 -39
- ultralytics/utils/torch_utils.py +45 -73
- ultralytics/utils/tqdm.py +6 -8
- ultralytics/utils/triton.py +9 -12
- ultralytics/utils/tuner.py +1 -2
- dgenerate_ultralytics_headless-8.3.222.dist-info/RECORD +0 -283
- {dgenerate_ultralytics_headless-8.3.222.dist-info → dgenerate_ultralytics_headless-8.3.225.dist-info}/WHEEL +0 -0
- {dgenerate_ultralytics_headless-8.3.222.dist-info → dgenerate_ultralytics_headless-8.3.225.dist-info}/entry_points.txt +0 -0
- {dgenerate_ultralytics_headless-8.3.222.dist-info → dgenerate_ultralytics_headless-8.3.225.dist-info}/licenses/LICENSE +0 -0
- {dgenerate_ultralytics_headless-8.3.222.dist-info → dgenerate_ultralytics_headless-8.3.225.dist-info}/top_level.txt +0 -0
ultralytics/nn/modules/block.py
CHANGED
|
@@ -56,15 +56,13 @@ __all__ = (
|
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
class DFL(nn.Module):
|
|
59
|
-
"""
|
|
60
|
-
Integral module of Distribution Focal Loss (DFL).
|
|
59
|
+
"""Integral module of Distribution Focal Loss (DFL).
|
|
61
60
|
|
|
62
61
|
Proposed in Generalized Focal Loss https://ieeexplore.ieee.org/document/9792391
|
|
63
62
|
"""
|
|
64
63
|
|
|
65
64
|
def __init__(self, c1: int = 16):
|
|
66
|
-
"""
|
|
67
|
-
Initialize a convolutional layer with a given number of input channels.
|
|
65
|
+
"""Initialize a convolutional layer with a given number of input channels.
|
|
68
66
|
|
|
69
67
|
Args:
|
|
70
68
|
c1 (int): Number of input channels.
|
|
@@ -86,8 +84,7 @@ class Proto(nn.Module):
|
|
|
86
84
|
"""Ultralytics YOLO models mask Proto module for segmentation models."""
|
|
87
85
|
|
|
88
86
|
def __init__(self, c1: int, c_: int = 256, c2: int = 32):
|
|
89
|
-
"""
|
|
90
|
-
Initialize the Ultralytics YOLO models mask Proto module with specified number of protos and masks.
|
|
87
|
+
"""Initialize the Ultralytics YOLO models mask Proto module with specified number of protos and masks.
|
|
91
88
|
|
|
92
89
|
Args:
|
|
93
90
|
c1 (int): Input channels.
|
|
@@ -106,15 +103,13 @@ class Proto(nn.Module):
|
|
|
106
103
|
|
|
107
104
|
|
|
108
105
|
class HGStem(nn.Module):
|
|
109
|
-
"""
|
|
110
|
-
StemBlock of PPHGNetV2 with 5 convolutions and one maxpool2d.
|
|
106
|
+
"""StemBlock of PPHGNetV2 with 5 convolutions and one maxpool2d.
|
|
111
107
|
|
|
112
108
|
https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/backbones/hgnet_v2.py
|
|
113
109
|
"""
|
|
114
110
|
|
|
115
111
|
def __init__(self, c1: int, cm: int, c2: int):
|
|
116
|
-
"""
|
|
117
|
-
Initialize the StemBlock of PPHGNetV2.
|
|
112
|
+
"""Initialize the StemBlock of PPHGNetV2.
|
|
118
113
|
|
|
119
114
|
Args:
|
|
120
115
|
c1 (int): Input channels.
|
|
@@ -144,8 +139,7 @@ class HGStem(nn.Module):
|
|
|
144
139
|
|
|
145
140
|
|
|
146
141
|
class HGBlock(nn.Module):
|
|
147
|
-
"""
|
|
148
|
-
HG_Block of PPHGNetV2 with 2 convolutions and LightConv.
|
|
142
|
+
"""HG_Block of PPHGNetV2 with 2 convolutions and LightConv.
|
|
149
143
|
|
|
150
144
|
https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/backbones/hgnet_v2.py
|
|
151
145
|
"""
|
|
@@ -161,8 +155,7 @@ class HGBlock(nn.Module):
|
|
|
161
155
|
shortcut: bool = False,
|
|
162
156
|
act: nn.Module = nn.ReLU(),
|
|
163
157
|
):
|
|
164
|
-
"""
|
|
165
|
-
Initialize HGBlock with specified parameters.
|
|
158
|
+
"""Initialize HGBlock with specified parameters.
|
|
166
159
|
|
|
167
160
|
Args:
|
|
168
161
|
c1 (int): Input channels.
|
|
@@ -193,8 +186,7 @@ class SPP(nn.Module):
|
|
|
193
186
|
"""Spatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729."""
|
|
194
187
|
|
|
195
188
|
def __init__(self, c1: int, c2: int, k: tuple[int, ...] = (5, 9, 13)):
|
|
196
|
-
"""
|
|
197
|
-
Initialize the SPP layer with input/output channels and pooling kernel sizes.
|
|
189
|
+
"""Initialize the SPP layer with input/output channels and pooling kernel sizes.
|
|
198
190
|
|
|
199
191
|
Args:
|
|
200
192
|
c1 (int): Input channels.
|
|
@@ -217,8 +209,7 @@ class SPPF(nn.Module):
|
|
|
217
209
|
"""Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher."""
|
|
218
210
|
|
|
219
211
|
def __init__(self, c1: int, c2: int, k: int = 5):
|
|
220
|
-
"""
|
|
221
|
-
Initialize the SPPF layer with given input/output channels and kernel size.
|
|
212
|
+
"""Initialize the SPPF layer with given input/output channels and kernel size.
|
|
222
213
|
|
|
223
214
|
Args:
|
|
224
215
|
c1 (int): Input channels.
|
|
@@ -245,8 +236,7 @@ class C1(nn.Module):
|
|
|
245
236
|
"""CSP Bottleneck with 1 convolution."""
|
|
246
237
|
|
|
247
238
|
def __init__(self, c1: int, c2: int, n: int = 1):
|
|
248
|
-
"""
|
|
249
|
-
Initialize the CSP Bottleneck with 1 convolution.
|
|
239
|
+
"""Initialize the CSP Bottleneck with 1 convolution.
|
|
250
240
|
|
|
251
241
|
Args:
|
|
252
242
|
c1 (int): Input channels.
|
|
@@ -267,8 +257,7 @@ class C2(nn.Module):
|
|
|
267
257
|
"""CSP Bottleneck with 2 convolutions."""
|
|
268
258
|
|
|
269
259
|
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5):
|
|
270
|
-
"""
|
|
271
|
-
Initialize a CSP Bottleneck with 2 convolutions.
|
|
260
|
+
"""Initialize a CSP Bottleneck with 2 convolutions.
|
|
272
261
|
|
|
273
262
|
Args:
|
|
274
263
|
c1 (int): Input channels.
|
|
@@ -295,8 +284,7 @@ class C2f(nn.Module):
|
|
|
295
284
|
"""Faster Implementation of CSP Bottleneck with 2 convolutions."""
|
|
296
285
|
|
|
297
286
|
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = False, g: int = 1, e: float = 0.5):
|
|
298
|
-
"""
|
|
299
|
-
Initialize a CSP bottleneck with 2 convolutions.
|
|
287
|
+
"""Initialize a CSP bottleneck with 2 convolutions.
|
|
300
288
|
|
|
301
289
|
Args:
|
|
302
290
|
c1 (int): Input channels.
|
|
@@ -330,8 +318,7 @@ class C3(nn.Module):
|
|
|
330
318
|
"""CSP Bottleneck with 3 convolutions."""
|
|
331
319
|
|
|
332
320
|
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5):
|
|
333
|
-
"""
|
|
334
|
-
Initialize the CSP Bottleneck with 3 convolutions.
|
|
321
|
+
"""Initialize the CSP Bottleneck with 3 convolutions.
|
|
335
322
|
|
|
336
323
|
Args:
|
|
337
324
|
c1 (int): Input channels.
|
|
@@ -357,8 +344,7 @@ class C3x(C3):
|
|
|
357
344
|
"""C3 module with cross-convolutions."""
|
|
358
345
|
|
|
359
346
|
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5):
|
|
360
|
-
"""
|
|
361
|
-
Initialize C3 module with cross-convolutions.
|
|
347
|
+
"""Initialize C3 module with cross-convolutions.
|
|
362
348
|
|
|
363
349
|
Args:
|
|
364
350
|
c1 (int): Input channels.
|
|
@@ -377,8 +363,7 @@ class RepC3(nn.Module):
|
|
|
377
363
|
"""Rep C3."""
|
|
378
364
|
|
|
379
365
|
def __init__(self, c1: int, c2: int, n: int = 3, e: float = 1.0):
|
|
380
|
-
"""
|
|
381
|
-
Initialize CSP Bottleneck with a single convolution.
|
|
366
|
+
"""Initialize CSP Bottleneck with a single convolution.
|
|
382
367
|
|
|
383
368
|
Args:
|
|
384
369
|
c1 (int): Input channels.
|
|
@@ -402,8 +387,7 @@ class C3TR(C3):
|
|
|
402
387
|
"""C3 module with TransformerBlock()."""
|
|
403
388
|
|
|
404
389
|
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5):
|
|
405
|
-
"""
|
|
406
|
-
Initialize C3 module with TransformerBlock.
|
|
390
|
+
"""Initialize C3 module with TransformerBlock.
|
|
407
391
|
|
|
408
392
|
Args:
|
|
409
393
|
c1 (int): Input channels.
|
|
@@ -422,8 +406,7 @@ class C3Ghost(C3):
|
|
|
422
406
|
"""C3 module with GhostBottleneck()."""
|
|
423
407
|
|
|
424
408
|
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5):
|
|
425
|
-
"""
|
|
426
|
-
Initialize C3 module with GhostBottleneck.
|
|
409
|
+
"""Initialize C3 module with GhostBottleneck.
|
|
427
410
|
|
|
428
411
|
Args:
|
|
429
412
|
c1 (int): Input channels.
|
|
@@ -442,8 +425,7 @@ class GhostBottleneck(nn.Module):
|
|
|
442
425
|
"""Ghost Bottleneck https://github.com/huawei-noah/Efficient-AI-Backbones."""
|
|
443
426
|
|
|
444
427
|
def __init__(self, c1: int, c2: int, k: int = 3, s: int = 1):
|
|
445
|
-
"""
|
|
446
|
-
Initialize Ghost Bottleneck module.
|
|
428
|
+
"""Initialize Ghost Bottleneck module.
|
|
447
429
|
|
|
448
430
|
Args:
|
|
449
431
|
c1 (int): Input channels.
|
|
@@ -473,8 +455,7 @@ class Bottleneck(nn.Module):
|
|
|
473
455
|
def __init__(
|
|
474
456
|
self, c1: int, c2: int, shortcut: bool = True, g: int = 1, k: tuple[int, int] = (3, 3), e: float = 0.5
|
|
475
457
|
):
|
|
476
|
-
"""
|
|
477
|
-
Initialize a standard bottleneck module.
|
|
458
|
+
"""Initialize a standard bottleneck module.
|
|
478
459
|
|
|
479
460
|
Args:
|
|
480
461
|
c1 (int): Input channels.
|
|
@@ -499,8 +480,7 @@ class BottleneckCSP(nn.Module):
|
|
|
499
480
|
"""CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks."""
|
|
500
481
|
|
|
501
482
|
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5):
|
|
502
|
-
"""
|
|
503
|
-
Initialize CSP Bottleneck.
|
|
483
|
+
"""Initialize CSP Bottleneck.
|
|
504
484
|
|
|
505
485
|
Args:
|
|
506
486
|
c1 (int): Input channels.
|
|
@@ -531,8 +511,7 @@ class ResNetBlock(nn.Module):
|
|
|
531
511
|
"""ResNet block with standard convolution layers."""
|
|
532
512
|
|
|
533
513
|
def __init__(self, c1: int, c2: int, s: int = 1, e: int = 4):
|
|
534
|
-
"""
|
|
535
|
-
Initialize ResNet block.
|
|
514
|
+
"""Initialize ResNet block.
|
|
536
515
|
|
|
537
516
|
Args:
|
|
538
517
|
c1 (int): Input channels.
|
|
@@ -556,8 +535,7 @@ class ResNetLayer(nn.Module):
|
|
|
556
535
|
"""ResNet layer with multiple ResNet blocks."""
|
|
557
536
|
|
|
558
537
|
def __init__(self, c1: int, c2: int, s: int = 1, is_first: bool = False, n: int = 1, e: int = 4):
|
|
559
|
-
"""
|
|
560
|
-
Initialize ResNet layer.
|
|
538
|
+
"""Initialize ResNet layer.
|
|
561
539
|
|
|
562
540
|
Args:
|
|
563
541
|
c1 (int): Input channels.
|
|
@@ -588,8 +566,7 @@ class MaxSigmoidAttnBlock(nn.Module):
|
|
|
588
566
|
"""Max Sigmoid attention block."""
|
|
589
567
|
|
|
590
568
|
def __init__(self, c1: int, c2: int, nh: int = 1, ec: int = 128, gc: int = 512, scale: bool = False):
|
|
591
|
-
"""
|
|
592
|
-
Initialize MaxSigmoidAttnBlock.
|
|
569
|
+
"""Initialize MaxSigmoidAttnBlock.
|
|
593
570
|
|
|
594
571
|
Args:
|
|
595
572
|
c1 (int): Input channels.
|
|
@@ -609,8 +586,7 @@ class MaxSigmoidAttnBlock(nn.Module):
|
|
|
609
586
|
self.scale = nn.Parameter(torch.ones(1, nh, 1, 1)) if scale else 1.0
|
|
610
587
|
|
|
611
588
|
def forward(self, x: torch.Tensor, guide: torch.Tensor) -> torch.Tensor:
|
|
612
|
-
"""
|
|
613
|
-
Forward pass of MaxSigmoidAttnBlock.
|
|
589
|
+
"""Forward pass of MaxSigmoidAttnBlock.
|
|
614
590
|
|
|
615
591
|
Args:
|
|
616
592
|
x (torch.Tensor): Input tensor.
|
|
@@ -653,8 +629,7 @@ class C2fAttn(nn.Module):
|
|
|
653
629
|
g: int = 1,
|
|
654
630
|
e: float = 0.5,
|
|
655
631
|
):
|
|
656
|
-
"""
|
|
657
|
-
Initialize C2f module with attention mechanism.
|
|
632
|
+
"""Initialize C2f module with attention mechanism.
|
|
658
633
|
|
|
659
634
|
Args:
|
|
660
635
|
c1 (int): Input channels.
|
|
@@ -675,8 +650,7 @@ class C2fAttn(nn.Module):
|
|
|
675
650
|
self.attn = MaxSigmoidAttnBlock(self.c, self.c, gc=gc, ec=ec, nh=nh)
|
|
676
651
|
|
|
677
652
|
def forward(self, x: torch.Tensor, guide: torch.Tensor) -> torch.Tensor:
|
|
678
|
-
"""
|
|
679
|
-
Forward pass through C2f layer with attention.
|
|
653
|
+
"""Forward pass through C2f layer with attention.
|
|
680
654
|
|
|
681
655
|
Args:
|
|
682
656
|
x (torch.Tensor): Input tensor.
|
|
@@ -691,8 +665,7 @@ class C2fAttn(nn.Module):
|
|
|
691
665
|
return self.cv2(torch.cat(y, 1))
|
|
692
666
|
|
|
693
667
|
def forward_split(self, x: torch.Tensor, guide: torch.Tensor) -> torch.Tensor:
|
|
694
|
-
"""
|
|
695
|
-
Forward pass using split() instead of chunk().
|
|
668
|
+
"""Forward pass using split() instead of chunk().
|
|
696
669
|
|
|
697
670
|
Args:
|
|
698
671
|
x (torch.Tensor): Input tensor.
|
|
@@ -713,8 +686,7 @@ class ImagePoolingAttn(nn.Module):
|
|
|
713
686
|
def __init__(
|
|
714
687
|
self, ec: int = 256, ch: tuple[int, ...] = (), ct: int = 512, nh: int = 8, k: int = 3, scale: bool = False
|
|
715
688
|
):
|
|
716
|
-
"""
|
|
717
|
-
Initialize ImagePoolingAttn module.
|
|
689
|
+
"""Initialize ImagePoolingAttn module.
|
|
718
690
|
|
|
719
691
|
Args:
|
|
720
692
|
ec (int): Embedding channels.
|
|
@@ -741,8 +713,7 @@ class ImagePoolingAttn(nn.Module):
|
|
|
741
713
|
self.k = k
|
|
742
714
|
|
|
743
715
|
def forward(self, x: list[torch.Tensor], text: torch.Tensor) -> torch.Tensor:
|
|
744
|
-
"""
|
|
745
|
-
Forward pass of ImagePoolingAttn.
|
|
716
|
+
"""Forward pass of ImagePoolingAttn.
|
|
746
717
|
|
|
747
718
|
Args:
|
|
748
719
|
x (list[torch.Tensor]): List of input feature maps.
|
|
@@ -785,8 +756,7 @@ class ContrastiveHead(nn.Module):
|
|
|
785
756
|
self.logit_scale = nn.Parameter(torch.ones([]) * torch.tensor(1 / 0.07).log())
|
|
786
757
|
|
|
787
758
|
def forward(self, x: torch.Tensor, w: torch.Tensor) -> torch.Tensor:
|
|
788
|
-
"""
|
|
789
|
-
Forward function of contrastive learning.
|
|
759
|
+
"""Forward function of contrastive learning.
|
|
790
760
|
|
|
791
761
|
Args:
|
|
792
762
|
x (torch.Tensor): Image features.
|
|
@@ -802,16 +772,14 @@ class ContrastiveHead(nn.Module):
|
|
|
802
772
|
|
|
803
773
|
|
|
804
774
|
class BNContrastiveHead(nn.Module):
|
|
805
|
-
"""
|
|
806
|
-
Batch Norm Contrastive Head using batch norm instead of l2-normalization.
|
|
775
|
+
"""Batch Norm Contrastive Head using batch norm instead of l2-normalization.
|
|
807
776
|
|
|
808
777
|
Args:
|
|
809
778
|
embed_dims (int): Embed dimensions of text and image features.
|
|
810
779
|
"""
|
|
811
780
|
|
|
812
781
|
def __init__(self, embed_dims: int):
|
|
813
|
-
"""
|
|
814
|
-
Initialize BNContrastiveHead.
|
|
782
|
+
"""Initialize BNContrastiveHead.
|
|
815
783
|
|
|
816
784
|
Args:
|
|
817
785
|
embed_dims (int): Embedding dimensions for features.
|
|
@@ -835,8 +803,7 @@ class BNContrastiveHead(nn.Module):
|
|
|
835
803
|
return x
|
|
836
804
|
|
|
837
805
|
def forward(self, x: torch.Tensor, w: torch.Tensor) -> torch.Tensor:
|
|
838
|
-
"""
|
|
839
|
-
Forward function of contrastive learning with batch normalization.
|
|
806
|
+
"""Forward function of contrastive learning with batch normalization.
|
|
840
807
|
|
|
841
808
|
Args:
|
|
842
809
|
x (torch.Tensor): Image features.
|
|
@@ -858,8 +825,7 @@ class RepBottleneck(Bottleneck):
|
|
|
858
825
|
def __init__(
|
|
859
826
|
self, c1: int, c2: int, shortcut: bool = True, g: int = 1, k: tuple[int, int] = (3, 3), e: float = 0.5
|
|
860
827
|
):
|
|
861
|
-
"""
|
|
862
|
-
Initialize RepBottleneck.
|
|
828
|
+
"""Initialize RepBottleneck.
|
|
863
829
|
|
|
864
830
|
Args:
|
|
865
831
|
c1 (int): Input channels.
|
|
@@ -878,8 +844,7 @@ class RepCSP(C3):
|
|
|
878
844
|
"""Repeatable Cross Stage Partial Network (RepCSP) module for efficient feature extraction."""
|
|
879
845
|
|
|
880
846
|
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5):
|
|
881
|
-
"""
|
|
882
|
-
Initialize RepCSP layer.
|
|
847
|
+
"""Initialize RepCSP layer.
|
|
883
848
|
|
|
884
849
|
Args:
|
|
885
850
|
c1 (int): Input channels.
|
|
@@ -898,8 +863,7 @@ class RepNCSPELAN4(nn.Module):
|
|
|
898
863
|
"""CSP-ELAN."""
|
|
899
864
|
|
|
900
865
|
def __init__(self, c1: int, c2: int, c3: int, c4: int, n: int = 1):
|
|
901
|
-
"""
|
|
902
|
-
Initialize CSP-ELAN layer.
|
|
866
|
+
"""Initialize CSP-ELAN layer.
|
|
903
867
|
|
|
904
868
|
Args:
|
|
905
869
|
c1 (int): Input channels.
|
|
@@ -932,8 +896,7 @@ class ELAN1(RepNCSPELAN4):
|
|
|
932
896
|
"""ELAN1 module with 4 convolutions."""
|
|
933
897
|
|
|
934
898
|
def __init__(self, c1: int, c2: int, c3: int, c4: int):
|
|
935
|
-
"""
|
|
936
|
-
Initialize ELAN1 layer.
|
|
899
|
+
"""Initialize ELAN1 layer.
|
|
937
900
|
|
|
938
901
|
Args:
|
|
939
902
|
c1 (int): Input channels.
|
|
@@ -953,8 +916,7 @@ class AConv(nn.Module):
|
|
|
953
916
|
"""AConv."""
|
|
954
917
|
|
|
955
918
|
def __init__(self, c1: int, c2: int):
|
|
956
|
-
"""
|
|
957
|
-
Initialize AConv module.
|
|
919
|
+
"""Initialize AConv module.
|
|
958
920
|
|
|
959
921
|
Args:
|
|
960
922
|
c1 (int): Input channels.
|
|
@@ -973,8 +935,7 @@ class ADown(nn.Module):
|
|
|
973
935
|
"""ADown."""
|
|
974
936
|
|
|
975
937
|
def __init__(self, c1: int, c2: int):
|
|
976
|
-
"""
|
|
977
|
-
Initialize ADown module.
|
|
938
|
+
"""Initialize ADown module.
|
|
978
939
|
|
|
979
940
|
Args:
|
|
980
941
|
c1 (int): Input channels.
|
|
@@ -999,8 +960,7 @@ class SPPELAN(nn.Module):
|
|
|
999
960
|
"""SPP-ELAN."""
|
|
1000
961
|
|
|
1001
962
|
def __init__(self, c1: int, c2: int, c3: int, k: int = 5):
|
|
1002
|
-
"""
|
|
1003
|
-
Initialize SPP-ELAN block.
|
|
963
|
+
"""Initialize SPP-ELAN block.
|
|
1004
964
|
|
|
1005
965
|
Args:
|
|
1006
966
|
c1 (int): Input channels.
|
|
@@ -1027,8 +987,7 @@ class CBLinear(nn.Module):
|
|
|
1027
987
|
"""CBLinear."""
|
|
1028
988
|
|
|
1029
989
|
def __init__(self, c1: int, c2s: list[int], k: int = 1, s: int = 1, p: int | None = None, g: int = 1):
|
|
1030
|
-
"""
|
|
1031
|
-
Initialize CBLinear module.
|
|
990
|
+
"""Initialize CBLinear module.
|
|
1032
991
|
|
|
1033
992
|
Args:
|
|
1034
993
|
c1 (int): Input channels.
|
|
@@ -1051,8 +1010,7 @@ class CBFuse(nn.Module):
|
|
|
1051
1010
|
"""CBFuse."""
|
|
1052
1011
|
|
|
1053
1012
|
def __init__(self, idx: list[int]):
|
|
1054
|
-
"""
|
|
1055
|
-
Initialize CBFuse module.
|
|
1013
|
+
"""Initialize CBFuse module.
|
|
1056
1014
|
|
|
1057
1015
|
Args:
|
|
1058
1016
|
idx (list[int]): Indices for feature selection.
|
|
@@ -1061,8 +1019,7 @@ class CBFuse(nn.Module):
|
|
|
1061
1019
|
self.idx = idx
|
|
1062
1020
|
|
|
1063
1021
|
def forward(self, xs: list[torch.Tensor]) -> torch.Tensor:
|
|
1064
|
-
"""
|
|
1065
|
-
Forward pass through CBFuse layer.
|
|
1022
|
+
"""Forward pass through CBFuse layer.
|
|
1066
1023
|
|
|
1067
1024
|
Args:
|
|
1068
1025
|
xs (list[torch.Tensor]): List of input tensors.
|
|
@@ -1079,8 +1036,7 @@ class C3f(nn.Module):
|
|
|
1079
1036
|
"""Faster Implementation of CSP Bottleneck with 2 convolutions."""
|
|
1080
1037
|
|
|
1081
1038
|
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = False, g: int = 1, e: float = 0.5):
|
|
1082
|
-
"""
|
|
1083
|
-
Initialize CSP bottleneck layer with two convolutions.
|
|
1039
|
+
"""Initialize CSP bottleneck layer with two convolutions.
|
|
1084
1040
|
|
|
1085
1041
|
Args:
|
|
1086
1042
|
c1 (int): Input channels.
|
|
@@ -1110,8 +1066,7 @@ class C3k2(C2f):
|
|
|
1110
1066
|
def __init__(
|
|
1111
1067
|
self, c1: int, c2: int, n: int = 1, c3k: bool = False, e: float = 0.5, g: int = 1, shortcut: bool = True
|
|
1112
1068
|
):
|
|
1113
|
-
"""
|
|
1114
|
-
Initialize C3k2 module.
|
|
1069
|
+
"""Initialize C3k2 module.
|
|
1115
1070
|
|
|
1116
1071
|
Args:
|
|
1117
1072
|
c1 (int): Input channels.
|
|
@@ -1132,8 +1087,7 @@ class C3k(C3):
|
|
|
1132
1087
|
"""C3k is a CSP bottleneck module with customizable kernel sizes for feature extraction in neural networks."""
|
|
1133
1088
|
|
|
1134
1089
|
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5, k: int = 3):
|
|
1135
|
-
"""
|
|
1136
|
-
Initialize C3k module.
|
|
1090
|
+
"""Initialize C3k module.
|
|
1137
1091
|
|
|
1138
1092
|
Args:
|
|
1139
1093
|
c1 (int): Input channels.
|
|
@@ -1154,8 +1108,7 @@ class RepVGGDW(torch.nn.Module):
|
|
|
1154
1108
|
"""RepVGGDW is a class that represents a depth wise separable convolutional block in RepVGG architecture."""
|
|
1155
1109
|
|
|
1156
1110
|
def __init__(self, ed: int) -> None:
|
|
1157
|
-
"""
|
|
1158
|
-
Initialize RepVGGDW module.
|
|
1111
|
+
"""Initialize RepVGGDW module.
|
|
1159
1112
|
|
|
1160
1113
|
Args:
|
|
1161
1114
|
ed (int): Input and output channels.
|
|
@@ -1167,8 +1120,7 @@ class RepVGGDW(torch.nn.Module):
|
|
|
1167
1120
|
self.act = nn.SiLU()
|
|
1168
1121
|
|
|
1169
1122
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1170
|
-
"""
|
|
1171
|
-
Perform a forward pass of the RepVGGDW block.
|
|
1123
|
+
"""Perform a forward pass of the RepVGGDW block.
|
|
1172
1124
|
|
|
1173
1125
|
Args:
|
|
1174
1126
|
x (torch.Tensor): Input tensor.
|
|
@@ -1179,8 +1131,7 @@ class RepVGGDW(torch.nn.Module):
|
|
|
1179
1131
|
return self.act(self.conv(x) + self.conv1(x))
|
|
1180
1132
|
|
|
1181
1133
|
def forward_fuse(self, x: torch.Tensor) -> torch.Tensor:
|
|
1182
|
-
"""
|
|
1183
|
-
Perform a forward pass of the RepVGGDW block without fusing the convolutions.
|
|
1134
|
+
"""Perform a forward pass of the RepVGGDW block without fusing the convolutions.
|
|
1184
1135
|
|
|
1185
1136
|
Args:
|
|
1186
1137
|
x (torch.Tensor): Input tensor.
|
|
@@ -1192,8 +1143,7 @@ class RepVGGDW(torch.nn.Module):
|
|
|
1192
1143
|
|
|
1193
1144
|
@torch.no_grad()
|
|
1194
1145
|
def fuse(self):
|
|
1195
|
-
"""
|
|
1196
|
-
Fuse the convolutional layers in the RepVGGDW block.
|
|
1146
|
+
"""Fuse the convolutional layers in the RepVGGDW block.
|
|
1197
1147
|
|
|
1198
1148
|
This method fuses the convolutional layers and updates the weights and biases accordingly.
|
|
1199
1149
|
"""
|
|
@@ -1218,8 +1168,7 @@ class RepVGGDW(torch.nn.Module):
|
|
|
1218
1168
|
|
|
1219
1169
|
|
|
1220
1170
|
class CIB(nn.Module):
|
|
1221
|
-
"""
|
|
1222
|
-
Conditional Identity Block (CIB) module.
|
|
1171
|
+
"""Conditional Identity Block (CIB) module.
|
|
1223
1172
|
|
|
1224
1173
|
Args:
|
|
1225
1174
|
c1 (int): Number of input channels.
|
|
@@ -1230,8 +1179,7 @@ class CIB(nn.Module):
|
|
|
1230
1179
|
"""
|
|
1231
1180
|
|
|
1232
1181
|
def __init__(self, c1: int, c2: int, shortcut: bool = True, e: float = 0.5, lk: bool = False):
|
|
1233
|
-
"""
|
|
1234
|
-
Initialize the CIB module.
|
|
1182
|
+
"""Initialize the CIB module.
|
|
1235
1183
|
|
|
1236
1184
|
Args:
|
|
1237
1185
|
c1 (int): Input channels.
|
|
@@ -1253,8 +1201,7 @@ class CIB(nn.Module):
|
|
|
1253
1201
|
self.add = shortcut and c1 == c2
|
|
1254
1202
|
|
|
1255
1203
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1256
|
-
"""
|
|
1257
|
-
Forward pass of the CIB module.
|
|
1204
|
+
"""Forward pass of the CIB module.
|
|
1258
1205
|
|
|
1259
1206
|
Args:
|
|
1260
1207
|
x (torch.Tensor): Input tensor.
|
|
@@ -1266,8 +1213,7 @@ class CIB(nn.Module):
|
|
|
1266
1213
|
|
|
1267
1214
|
|
|
1268
1215
|
class C2fCIB(C2f):
|
|
1269
|
-
"""
|
|
1270
|
-
C2fCIB class represents a convolutional block with C2f and CIB modules.
|
|
1216
|
+
"""C2fCIB class represents a convolutional block with C2f and CIB modules.
|
|
1271
1217
|
|
|
1272
1218
|
Args:
|
|
1273
1219
|
c1 (int): Number of input channels.
|
|
@@ -1282,8 +1228,7 @@ class C2fCIB(C2f):
|
|
|
1282
1228
|
def __init__(
|
|
1283
1229
|
self, c1: int, c2: int, n: int = 1, shortcut: bool = False, lk: bool = False, g: int = 1, e: float = 0.5
|
|
1284
1230
|
):
|
|
1285
|
-
"""
|
|
1286
|
-
Initialize C2fCIB module.
|
|
1231
|
+
"""Initialize C2fCIB module.
|
|
1287
1232
|
|
|
1288
1233
|
Args:
|
|
1289
1234
|
c1 (int): Input channels.
|
|
@@ -1299,8 +1244,7 @@ class C2fCIB(C2f):
|
|
|
1299
1244
|
|
|
1300
1245
|
|
|
1301
1246
|
class Attention(nn.Module):
|
|
1302
|
-
"""
|
|
1303
|
-
Attention module that performs self-attention on the input tensor.
|
|
1247
|
+
"""Attention module that performs self-attention on the input tensor.
|
|
1304
1248
|
|
|
1305
1249
|
Args:
|
|
1306
1250
|
dim (int): The input tensor dimension.
|
|
@@ -1318,8 +1262,7 @@ class Attention(nn.Module):
|
|
|
1318
1262
|
"""
|
|
1319
1263
|
|
|
1320
1264
|
def __init__(self, dim: int, num_heads: int = 8, attn_ratio: float = 0.5):
|
|
1321
|
-
"""
|
|
1322
|
-
Initialize multi-head attention module.
|
|
1265
|
+
"""Initialize multi-head attention module.
|
|
1323
1266
|
|
|
1324
1267
|
Args:
|
|
1325
1268
|
dim (int): Input dimension.
|
|
@@ -1338,8 +1281,7 @@ class Attention(nn.Module):
|
|
|
1338
1281
|
self.pe = Conv(dim, dim, 3, 1, g=dim, act=False)
|
|
1339
1282
|
|
|
1340
1283
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1341
|
-
"""
|
|
1342
|
-
Forward pass of the Attention module.
|
|
1284
|
+
"""Forward pass of the Attention module.
|
|
1343
1285
|
|
|
1344
1286
|
Args:
|
|
1345
1287
|
x (torch.Tensor): The input tensor.
|
|
@@ -1362,8 +1304,7 @@ class Attention(nn.Module):
|
|
|
1362
1304
|
|
|
1363
1305
|
|
|
1364
1306
|
class PSABlock(nn.Module):
|
|
1365
|
-
"""
|
|
1366
|
-
PSABlock class implementing a Position-Sensitive Attention block for neural networks.
|
|
1307
|
+
"""PSABlock class implementing a Position-Sensitive Attention block for neural networks.
|
|
1367
1308
|
|
|
1368
1309
|
This class encapsulates the functionality for applying multi-head attention and feed-forward neural network layers
|
|
1369
1310
|
with optional shortcut connections.
|
|
@@ -1384,8 +1325,7 @@ class PSABlock(nn.Module):
|
|
|
1384
1325
|
"""
|
|
1385
1326
|
|
|
1386
1327
|
def __init__(self, c: int, attn_ratio: float = 0.5, num_heads: int = 4, shortcut: bool = True) -> None:
|
|
1387
|
-
"""
|
|
1388
|
-
Initialize the PSABlock.
|
|
1328
|
+
"""Initialize the PSABlock.
|
|
1389
1329
|
|
|
1390
1330
|
Args:
|
|
1391
1331
|
c (int): Input and output channels.
|
|
@@ -1400,8 +1340,7 @@ class PSABlock(nn.Module):
|
|
|
1400
1340
|
self.add = shortcut
|
|
1401
1341
|
|
|
1402
1342
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1403
|
-
"""
|
|
1404
|
-
Execute a forward pass through PSABlock.
|
|
1343
|
+
"""Execute a forward pass through PSABlock.
|
|
1405
1344
|
|
|
1406
1345
|
Args:
|
|
1407
1346
|
x (torch.Tensor): Input tensor.
|
|
@@ -1415,8 +1354,7 @@ class PSABlock(nn.Module):
|
|
|
1415
1354
|
|
|
1416
1355
|
|
|
1417
1356
|
class PSA(nn.Module):
|
|
1418
|
-
"""
|
|
1419
|
-
PSA class for implementing Position-Sensitive Attention in neural networks.
|
|
1357
|
+
"""PSA class for implementing Position-Sensitive Attention in neural networks.
|
|
1420
1358
|
|
|
1421
1359
|
This class encapsulates the functionality for applying position-sensitive attention and feed-forward networks to
|
|
1422
1360
|
input tensors, enhancing feature extraction and processing capabilities.
|
|
@@ -1439,8 +1377,7 @@ class PSA(nn.Module):
|
|
|
1439
1377
|
"""
|
|
1440
1378
|
|
|
1441
1379
|
def __init__(self, c1: int, c2: int, e: float = 0.5):
|
|
1442
|
-
"""
|
|
1443
|
-
Initialize PSA module.
|
|
1380
|
+
"""Initialize PSA module.
|
|
1444
1381
|
|
|
1445
1382
|
Args:
|
|
1446
1383
|
c1 (int): Input channels.
|
|
@@ -1457,8 +1394,7 @@ class PSA(nn.Module):
|
|
|
1457
1394
|
self.ffn = nn.Sequential(Conv(self.c, self.c * 2, 1), Conv(self.c * 2, self.c, 1, act=False))
|
|
1458
1395
|
|
|
1459
1396
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1460
|
-
"""
|
|
1461
|
-
Execute forward pass in PSA module.
|
|
1397
|
+
"""Execute forward pass in PSA module.
|
|
1462
1398
|
|
|
1463
1399
|
Args:
|
|
1464
1400
|
x (torch.Tensor): Input tensor.
|
|
@@ -1473,8 +1409,7 @@ class PSA(nn.Module):
|
|
|
1473
1409
|
|
|
1474
1410
|
|
|
1475
1411
|
class C2PSA(nn.Module):
|
|
1476
|
-
"""
|
|
1477
|
-
C2PSA module with attention mechanism for enhanced feature extraction and processing.
|
|
1412
|
+
"""C2PSA module with attention mechanism for enhanced feature extraction and processing.
|
|
1478
1413
|
|
|
1479
1414
|
This module implements a convolutional block with attention mechanisms to enhance feature extraction and processing
|
|
1480
1415
|
capabilities. It includes a series of PSABlock modules for self-attention and feed-forward operations.
|
|
@@ -1488,18 +1423,17 @@ class C2PSA(nn.Module):
|
|
|
1488
1423
|
Methods:
|
|
1489
1424
|
forward: Performs a forward pass through the C2PSA module, applying attention and feed-forward operations.
|
|
1490
1425
|
|
|
1491
|
-
Notes:
|
|
1492
|
-
This module essentially is the same as PSA module, but refactored to allow stacking more PSABlock modules.
|
|
1493
|
-
|
|
1494
1426
|
Examples:
|
|
1495
1427
|
>>> c2psa = C2PSA(c1=256, c2=256, n=3, e=0.5)
|
|
1496
1428
|
>>> input_tensor = torch.randn(1, 256, 64, 64)
|
|
1497
1429
|
>>> output_tensor = c2psa(input_tensor)
|
|
1430
|
+
|
|
1431
|
+
Notes:
|
|
1432
|
+
This module essentially is the same as PSA module, but refactored to allow stacking more PSABlock modules.
|
|
1498
1433
|
"""
|
|
1499
1434
|
|
|
1500
1435
|
def __init__(self, c1: int, c2: int, n: int = 1, e: float = 0.5):
|
|
1501
|
-
"""
|
|
1502
|
-
Initialize C2PSA module.
|
|
1436
|
+
"""Initialize C2PSA module.
|
|
1503
1437
|
|
|
1504
1438
|
Args:
|
|
1505
1439
|
c1 (int): Input channels.
|
|
@@ -1516,8 +1450,7 @@ class C2PSA(nn.Module):
|
|
|
1516
1450
|
self.m = nn.Sequential(*(PSABlock(self.c, attn_ratio=0.5, num_heads=self.c // 64) for _ in range(n)))
|
|
1517
1451
|
|
|
1518
1452
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1519
|
-
"""
|
|
1520
|
-
Process the input tensor through a series of PSA blocks.
|
|
1453
|
+
"""Process the input tensor through a series of PSA blocks.
|
|
1521
1454
|
|
|
1522
1455
|
Args:
|
|
1523
1456
|
x (torch.Tensor): Input tensor.
|
|
@@ -1531,10 +1464,10 @@ class C2PSA(nn.Module):
|
|
|
1531
1464
|
|
|
1532
1465
|
|
|
1533
1466
|
class C2fPSA(C2f):
|
|
1534
|
-
"""
|
|
1535
|
-
C2fPSA module with enhanced feature extraction using PSA blocks.
|
|
1467
|
+
"""C2fPSA module with enhanced feature extraction using PSA blocks.
|
|
1536
1468
|
|
|
1537
|
-
This class extends the C2f module by incorporating PSA blocks for improved attention mechanisms and feature
|
|
1469
|
+
This class extends the C2f module by incorporating PSA blocks for improved attention mechanisms and feature
|
|
1470
|
+
extraction.
|
|
1538
1471
|
|
|
1539
1472
|
Attributes:
|
|
1540
1473
|
c (int): Number of hidden channels.
|
|
@@ -1556,8 +1489,7 @@ class C2fPSA(C2f):
|
|
|
1556
1489
|
"""
|
|
1557
1490
|
|
|
1558
1491
|
def __init__(self, c1: int, c2: int, n: int = 1, e: float = 0.5):
|
|
1559
|
-
"""
|
|
1560
|
-
Initialize C2fPSA module.
|
|
1492
|
+
"""Initialize C2fPSA module.
|
|
1561
1493
|
|
|
1562
1494
|
Args:
|
|
1563
1495
|
c1 (int): Input channels.
|
|
@@ -1571,8 +1503,7 @@ class C2fPSA(C2f):
|
|
|
1571
1503
|
|
|
1572
1504
|
|
|
1573
1505
|
class SCDown(nn.Module):
|
|
1574
|
-
"""
|
|
1575
|
-
SCDown module for downsampling with separable convolutions.
|
|
1506
|
+
"""SCDown module for downsampling with separable convolutions.
|
|
1576
1507
|
|
|
1577
1508
|
This module performs downsampling using a combination of pointwise and depthwise convolutions, which helps in
|
|
1578
1509
|
efficiently reducing the spatial dimensions of the input tensor while maintaining the channel information.
|
|
@@ -1595,8 +1526,7 @@ class SCDown(nn.Module):
|
|
|
1595
1526
|
"""
|
|
1596
1527
|
|
|
1597
1528
|
def __init__(self, c1: int, c2: int, k: int, s: int):
|
|
1598
|
-
"""
|
|
1599
|
-
Initialize SCDown module.
|
|
1529
|
+
"""Initialize SCDown module.
|
|
1600
1530
|
|
|
1601
1531
|
Args:
|
|
1602
1532
|
c1 (int): Input channels.
|
|
@@ -1609,8 +1539,7 @@ class SCDown(nn.Module):
|
|
|
1609
1539
|
self.cv2 = Conv(c2, c2, k=k, s=s, g=c2, act=False)
|
|
1610
1540
|
|
|
1611
1541
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1612
|
-
"""
|
|
1613
|
-
Apply convolution and downsampling to the input tensor.
|
|
1542
|
+
"""Apply convolution and downsampling to the input tensor.
|
|
1614
1543
|
|
|
1615
1544
|
Args:
|
|
1616
1545
|
x (torch.Tensor): Input tensor.
|
|
@@ -1622,27 +1551,26 @@ class SCDown(nn.Module):
|
|
|
1622
1551
|
|
|
1623
1552
|
|
|
1624
1553
|
class TorchVision(nn.Module):
|
|
1625
|
-
"""
|
|
1626
|
-
TorchVision module to allow loading any torchvision model.
|
|
1554
|
+
"""TorchVision module to allow loading any torchvision model.
|
|
1627
1555
|
|
|
1628
|
-
This class provides a way to load a model from the torchvision library, optionally load pre-trained weights, and
|
|
1629
|
-
|
|
1630
|
-
Attributes:
|
|
1631
|
-
m (nn.Module): The loaded torchvision model, possibly truncated and unwrapped.
|
|
1556
|
+
This class provides a way to load a model from the torchvision library, optionally load pre-trained weights, and
|
|
1557
|
+
customize the model by truncating or unwrapping layers.
|
|
1632
1558
|
|
|
1633
1559
|
Args:
|
|
1634
1560
|
model (str): Name of the torchvision model to load.
|
|
1635
1561
|
weights (str, optional): Pre-trained weights to load. Default is "DEFAULT".
|
|
1636
|
-
unwrap (bool, optional):
|
|
1562
|
+
unwrap (bool, optional): Unwraps the model to a sequential containing all but the last `truncate` layers.
|
|
1637
1563
|
truncate (int, optional): Number of layers to truncate from the end if `unwrap` is True. Default is 2.
|
|
1638
1564
|
split (bool, optional): Returns output from intermediate child modules as list. Default is False.
|
|
1565
|
+
|
|
1566
|
+
Attributes:
|
|
1567
|
+
m (nn.Module): The loaded torchvision model, possibly truncated and unwrapped.
|
|
1639
1568
|
"""
|
|
1640
1569
|
|
|
1641
1570
|
def __init__(
|
|
1642
1571
|
self, model: str, weights: str = "DEFAULT", unwrap: bool = True, truncate: int = 2, split: bool = False
|
|
1643
1572
|
):
|
|
1644
|
-
"""
|
|
1645
|
-
Load the model and weights from torchvision.
|
|
1573
|
+
"""Load the model and weights from torchvision.
|
|
1646
1574
|
|
|
1647
1575
|
Args:
|
|
1648
1576
|
model (str): Name of the torchvision model to load.
|
|
@@ -1669,8 +1597,7 @@ class TorchVision(nn.Module):
|
|
|
1669
1597
|
self.m.head = self.m.heads = nn.Identity()
|
|
1670
1598
|
|
|
1671
1599
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1672
|
-
"""
|
|
1673
|
-
Forward pass through the model.
|
|
1600
|
+
"""Forward pass through the model.
|
|
1674
1601
|
|
|
1675
1602
|
Args:
|
|
1676
1603
|
x (torch.Tensor): Input tensor.
|
|
@@ -1687,8 +1614,7 @@ class TorchVision(nn.Module):
|
|
|
1687
1614
|
|
|
1688
1615
|
|
|
1689
1616
|
class AAttn(nn.Module):
|
|
1690
|
-
"""
|
|
1691
|
-
Area-attention module for YOLO models, providing efficient attention mechanisms.
|
|
1617
|
+
"""Area-attention module for YOLO models, providing efficient attention mechanisms.
|
|
1692
1618
|
|
|
1693
1619
|
This module implements an area-based attention mechanism that processes input features in a spatially-aware manner,
|
|
1694
1620
|
making it particularly effective for object detection tasks.
|
|
@@ -1713,8 +1639,7 @@ class AAttn(nn.Module):
|
|
|
1713
1639
|
"""
|
|
1714
1640
|
|
|
1715
1641
|
def __init__(self, dim: int, num_heads: int, area: int = 1):
|
|
1716
|
-
"""
|
|
1717
|
-
Initialize an Area-attention module for YOLO models.
|
|
1642
|
+
"""Initialize an Area-attention module for YOLO models.
|
|
1718
1643
|
|
|
1719
1644
|
Args:
|
|
1720
1645
|
dim (int): Number of hidden channels.
|
|
@@ -1733,8 +1658,7 @@ class AAttn(nn.Module):
|
|
|
1733
1658
|
self.pe = Conv(all_head_dim, dim, 7, 1, 3, g=dim, act=False)
|
|
1734
1659
|
|
|
1735
1660
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1736
|
-
"""
|
|
1737
|
-
Process the input tensor through the area-attention.
|
|
1661
|
+
"""Process the input tensor through the area-attention.
|
|
1738
1662
|
|
|
1739
1663
|
Args:
|
|
1740
1664
|
x (torch.Tensor): Input tensor.
|
|
@@ -1773,8 +1697,7 @@ class AAttn(nn.Module):
|
|
|
1773
1697
|
|
|
1774
1698
|
|
|
1775
1699
|
class ABlock(nn.Module):
|
|
1776
|
-
"""
|
|
1777
|
-
Area-attention block module for efficient feature extraction in YOLO models.
|
|
1700
|
+
"""Area-attention block module for efficient feature extraction in YOLO models.
|
|
1778
1701
|
|
|
1779
1702
|
This module implements an area-attention mechanism combined with a feed-forward network for processing feature maps.
|
|
1780
1703
|
It uses a novel area-based attention approach that is more efficient than traditional self-attention while
|
|
@@ -1797,8 +1720,7 @@ class ABlock(nn.Module):
|
|
|
1797
1720
|
"""
|
|
1798
1721
|
|
|
1799
1722
|
def __init__(self, dim: int, num_heads: int, mlp_ratio: float = 1.2, area: int = 1):
|
|
1800
|
-
"""
|
|
1801
|
-
Initialize an Area-attention block module.
|
|
1723
|
+
"""Initialize an Area-attention block module.
|
|
1802
1724
|
|
|
1803
1725
|
Args:
|
|
1804
1726
|
dim (int): Number of input channels.
|
|
@@ -1815,8 +1737,7 @@ class ABlock(nn.Module):
|
|
|
1815
1737
|
self.apply(self._init_weights)
|
|
1816
1738
|
|
|
1817
1739
|
def _init_weights(self, m: nn.Module):
|
|
1818
|
-
"""
|
|
1819
|
-
Initialize weights using a truncated normal distribution.
|
|
1740
|
+
"""Initialize weights using a truncated normal distribution.
|
|
1820
1741
|
|
|
1821
1742
|
Args:
|
|
1822
1743
|
m (nn.Module): Module to initialize.
|
|
@@ -1827,8 +1748,7 @@ class ABlock(nn.Module):
|
|
|
1827
1748
|
nn.init.constant_(m.bias, 0)
|
|
1828
1749
|
|
|
1829
1750
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1830
|
-
"""
|
|
1831
|
-
Forward pass through ABlock.
|
|
1751
|
+
"""Forward pass through ABlock.
|
|
1832
1752
|
|
|
1833
1753
|
Args:
|
|
1834
1754
|
x (torch.Tensor): Input tensor.
|
|
@@ -1841,8 +1761,7 @@ class ABlock(nn.Module):
|
|
|
1841
1761
|
|
|
1842
1762
|
|
|
1843
1763
|
class A2C2f(nn.Module):
|
|
1844
|
-
"""
|
|
1845
|
-
Area-Attention C2f module for enhanced feature extraction with area-based attention mechanisms.
|
|
1764
|
+
"""Area-Attention C2f module for enhanced feature extraction with area-based attention mechanisms.
|
|
1846
1765
|
|
|
1847
1766
|
This module extends the C2f architecture by incorporating area-attention and ABlock layers for improved feature
|
|
1848
1767
|
processing. It supports both area-attention and standard convolution modes.
|
|
@@ -1877,8 +1796,7 @@ class A2C2f(nn.Module):
|
|
|
1877
1796
|
g: int = 1,
|
|
1878
1797
|
shortcut: bool = True,
|
|
1879
1798
|
):
|
|
1880
|
-
"""
|
|
1881
|
-
Initialize Area-Attention C2f module.
|
|
1799
|
+
"""Initialize Area-Attention C2f module.
|
|
1882
1800
|
|
|
1883
1801
|
Args:
|
|
1884
1802
|
c1 (int): Number of input channels.
|
|
@@ -1908,8 +1826,7 @@ class A2C2f(nn.Module):
|
|
|
1908
1826
|
)
|
|
1909
1827
|
|
|
1910
1828
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
1911
|
-
"""
|
|
1912
|
-
Forward pass through A2C2f layer.
|
|
1829
|
+
"""Forward pass through A2C2f layer.
|
|
1913
1830
|
|
|
1914
1831
|
Args:
|
|
1915
1832
|
x (torch.Tensor): Input tensor.
|
|
@@ -1929,8 +1846,7 @@ class SwiGLUFFN(nn.Module):
|
|
|
1929
1846
|
"""SwiGLU Feed-Forward Network for transformer-based architectures."""
|
|
1930
1847
|
|
|
1931
1848
|
def __init__(self, gc: int, ec: int, e: int = 4) -> None:
|
|
1932
|
-
"""
|
|
1933
|
-
Initialize SwiGLU FFN with input dimension, output dimension, and expansion factor.
|
|
1849
|
+
"""Initialize SwiGLU FFN with input dimension, output dimension, and expansion factor.
|
|
1934
1850
|
|
|
1935
1851
|
Args:
|
|
1936
1852
|
gc (int): Guide channels.
|
|
@@ -1953,8 +1869,7 @@ class Residual(nn.Module):
|
|
|
1953
1869
|
"""Residual connection wrapper for neural network modules."""
|
|
1954
1870
|
|
|
1955
1871
|
def __init__(self, m: nn.Module) -> None:
|
|
1956
|
-
"""
|
|
1957
|
-
Initialize residual module with the wrapped module.
|
|
1872
|
+
"""Initialize residual module with the wrapped module.
|
|
1958
1873
|
|
|
1959
1874
|
Args:
|
|
1960
1875
|
m (nn.Module): Module to wrap with residual connection.
|
|
@@ -1975,8 +1890,7 @@ class SAVPE(nn.Module):
|
|
|
1975
1890
|
"""Spatial-Aware Visual Prompt Embedding module for feature enhancement."""
|
|
1976
1891
|
|
|
1977
1892
|
def __init__(self, ch: list[int], c3: int, embed: int):
|
|
1978
|
-
"""
|
|
1979
|
-
Initialize SAVPE module with channels, intermediate channels, and embedding dimension.
|
|
1893
|
+
"""Initialize SAVPE module with channels, intermediate channels, and embedding dimension.
|
|
1980
1894
|
|
|
1981
1895
|
Args:
|
|
1982
1896
|
ch (list[int]): List of input channel dimensions.
|