dgenerate-ultralytics-headless 8.3.137__py3-none-any.whl → 8.3.224__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.137.dist-info → dgenerate_ultralytics_headless-8.3.224.dist-info}/METADATA +41 -34
- dgenerate_ultralytics_headless-8.3.224.dist-info/RECORD +285 -0
- {dgenerate_ultralytics_headless-8.3.137.dist-info → dgenerate_ultralytics_headless-8.3.224.dist-info}/WHEEL +1 -1
- tests/__init__.py +7 -6
- tests/conftest.py +15 -39
- tests/test_cli.py +17 -17
- tests/test_cuda.py +17 -8
- tests/test_engine.py +36 -10
- tests/test_exports.py +98 -37
- tests/test_integrations.py +12 -15
- tests/test_python.py +126 -82
- tests/test_solutions.py +319 -135
- ultralytics/__init__.py +27 -9
- ultralytics/cfg/__init__.py +83 -87
- ultralytics/cfg/datasets/Argoverse.yaml +4 -4
- ultralytics/cfg/datasets/DOTAv1.5.yaml +2 -2
- ultralytics/cfg/datasets/DOTAv1.yaml +2 -2
- ultralytics/cfg/datasets/GlobalWheat2020.yaml +2 -2
- ultralytics/cfg/datasets/HomeObjects-3K.yaml +4 -5
- ultralytics/cfg/datasets/ImageNet.yaml +3 -3
- ultralytics/cfg/datasets/Objects365.yaml +24 -20
- ultralytics/cfg/datasets/SKU-110K.yaml +9 -9
- ultralytics/cfg/datasets/VOC.yaml +10 -13
- ultralytics/cfg/datasets/VisDrone.yaml +43 -33
- ultralytics/cfg/datasets/african-wildlife.yaml +5 -5
- ultralytics/cfg/datasets/brain-tumor.yaml +4 -5
- ultralytics/cfg/datasets/carparts-seg.yaml +5 -5
- ultralytics/cfg/datasets/coco-pose.yaml +26 -4
- ultralytics/cfg/datasets/coco.yaml +4 -4
- ultralytics/cfg/datasets/coco128-seg.yaml +2 -2
- ultralytics/cfg/datasets/coco128.yaml +2 -2
- ultralytics/cfg/datasets/coco8-grayscale.yaml +103 -0
- ultralytics/cfg/datasets/coco8-multispectral.yaml +2 -2
- ultralytics/cfg/datasets/coco8-pose.yaml +23 -2
- ultralytics/cfg/datasets/coco8-seg.yaml +2 -2
- ultralytics/cfg/datasets/coco8.yaml +2 -2
- ultralytics/cfg/datasets/construction-ppe.yaml +32 -0
- ultralytics/cfg/datasets/crack-seg.yaml +5 -5
- ultralytics/cfg/datasets/dog-pose.yaml +32 -4
- ultralytics/cfg/datasets/dota8-multispectral.yaml +2 -2
- ultralytics/cfg/datasets/dota8.yaml +2 -2
- ultralytics/cfg/datasets/hand-keypoints.yaml +29 -4
- ultralytics/cfg/datasets/lvis.yaml +9 -9
- ultralytics/cfg/datasets/medical-pills.yaml +4 -5
- ultralytics/cfg/datasets/open-images-v7.yaml +7 -10
- ultralytics/cfg/datasets/package-seg.yaml +5 -5
- ultralytics/cfg/datasets/signature.yaml +4 -4
- ultralytics/cfg/datasets/tiger-pose.yaml +20 -4
- ultralytics/cfg/datasets/xView.yaml +5 -5
- ultralytics/cfg/default.yaml +96 -93
- ultralytics/cfg/trackers/botsort.yaml +16 -17
- ultralytics/cfg/trackers/bytetrack.yaml +9 -11
- ultralytics/data/__init__.py +4 -4
- ultralytics/data/annotator.py +12 -12
- ultralytics/data/augment.py +531 -564
- ultralytics/data/base.py +76 -81
- ultralytics/data/build.py +206 -42
- ultralytics/data/converter.py +179 -78
- ultralytics/data/dataset.py +121 -121
- ultralytics/data/loaders.py +114 -91
- ultralytics/data/split.py +28 -15
- ultralytics/data/split_dota.py +67 -48
- ultralytics/data/utils.py +110 -89
- ultralytics/engine/exporter.py +422 -460
- ultralytics/engine/model.py +224 -252
- ultralytics/engine/predictor.py +94 -89
- ultralytics/engine/results.py +345 -595
- ultralytics/engine/trainer.py +231 -134
- ultralytics/engine/tuner.py +279 -73
- ultralytics/engine/validator.py +53 -46
- ultralytics/hub/__init__.py +26 -28
- ultralytics/hub/auth.py +30 -16
- ultralytics/hub/google/__init__.py +34 -36
- ultralytics/hub/session.py +53 -77
- ultralytics/hub/utils.py +23 -109
- ultralytics/models/__init__.py +1 -1
- ultralytics/models/fastsam/__init__.py +1 -1
- ultralytics/models/fastsam/model.py +36 -18
- ultralytics/models/fastsam/predict.py +33 -44
- ultralytics/models/fastsam/utils.py +4 -5
- ultralytics/models/fastsam/val.py +12 -14
- ultralytics/models/nas/__init__.py +1 -1
- ultralytics/models/nas/model.py +16 -20
- ultralytics/models/nas/predict.py +12 -14
- ultralytics/models/nas/val.py +4 -5
- ultralytics/models/rtdetr/__init__.py +1 -1
- ultralytics/models/rtdetr/model.py +9 -9
- ultralytics/models/rtdetr/predict.py +22 -17
- ultralytics/models/rtdetr/train.py +20 -16
- ultralytics/models/rtdetr/val.py +79 -59
- ultralytics/models/sam/__init__.py +8 -2
- ultralytics/models/sam/amg.py +53 -38
- ultralytics/models/sam/build.py +29 -31
- ultralytics/models/sam/model.py +33 -38
- ultralytics/models/sam/modules/blocks.py +159 -182
- ultralytics/models/sam/modules/decoders.py +38 -47
- ultralytics/models/sam/modules/encoders.py +114 -133
- ultralytics/models/sam/modules/memory_attention.py +38 -31
- ultralytics/models/sam/modules/sam.py +114 -93
- ultralytics/models/sam/modules/tiny_encoder.py +268 -291
- ultralytics/models/sam/modules/transformer.py +59 -66
- ultralytics/models/sam/modules/utils.py +55 -72
- ultralytics/models/sam/predict.py +745 -341
- ultralytics/models/utils/loss.py +118 -107
- ultralytics/models/utils/ops.py +118 -71
- ultralytics/models/yolo/__init__.py +1 -1
- ultralytics/models/yolo/classify/predict.py +28 -26
- ultralytics/models/yolo/classify/train.py +50 -81
- ultralytics/models/yolo/classify/val.py +68 -61
- ultralytics/models/yolo/detect/predict.py +12 -15
- ultralytics/models/yolo/detect/train.py +56 -46
- ultralytics/models/yolo/detect/val.py +279 -223
- ultralytics/models/yolo/model.py +167 -86
- ultralytics/models/yolo/obb/predict.py +7 -11
- ultralytics/models/yolo/obb/train.py +23 -25
- ultralytics/models/yolo/obb/val.py +107 -99
- ultralytics/models/yolo/pose/__init__.py +1 -1
- ultralytics/models/yolo/pose/predict.py +12 -14
- ultralytics/models/yolo/pose/train.py +31 -69
- ultralytics/models/yolo/pose/val.py +119 -254
- ultralytics/models/yolo/segment/predict.py +21 -25
- ultralytics/models/yolo/segment/train.py +12 -66
- ultralytics/models/yolo/segment/val.py +126 -305
- ultralytics/models/yolo/world/train.py +53 -45
- ultralytics/models/yolo/world/train_world.py +51 -32
- ultralytics/models/yolo/yoloe/__init__.py +7 -7
- ultralytics/models/yolo/yoloe/predict.py +30 -37
- ultralytics/models/yolo/yoloe/train.py +89 -71
- ultralytics/models/yolo/yoloe/train_seg.py +15 -17
- ultralytics/models/yolo/yoloe/val.py +56 -41
- ultralytics/nn/__init__.py +9 -11
- ultralytics/nn/autobackend.py +179 -107
- ultralytics/nn/modules/__init__.py +67 -67
- ultralytics/nn/modules/activation.py +8 -7
- ultralytics/nn/modules/block.py +302 -323
- ultralytics/nn/modules/conv.py +61 -104
- ultralytics/nn/modules/head.py +488 -186
- ultralytics/nn/modules/transformer.py +183 -123
- ultralytics/nn/modules/utils.py +15 -20
- ultralytics/nn/tasks.py +327 -203
- ultralytics/nn/text_model.py +81 -65
- ultralytics/py.typed +1 -0
- ultralytics/solutions/__init__.py +12 -12
- ultralytics/solutions/ai_gym.py +19 -27
- ultralytics/solutions/analytics.py +36 -26
- ultralytics/solutions/config.py +29 -28
- ultralytics/solutions/distance_calculation.py +23 -24
- ultralytics/solutions/heatmap.py +17 -19
- ultralytics/solutions/instance_segmentation.py +21 -19
- ultralytics/solutions/object_blurrer.py +16 -17
- ultralytics/solutions/object_counter.py +48 -53
- ultralytics/solutions/object_cropper.py +22 -16
- ultralytics/solutions/parking_management.py +61 -58
- ultralytics/solutions/queue_management.py +19 -19
- ultralytics/solutions/region_counter.py +63 -50
- ultralytics/solutions/security_alarm.py +22 -25
- ultralytics/solutions/similarity_search.py +107 -60
- ultralytics/solutions/solutions.py +343 -262
- ultralytics/solutions/speed_estimation.py +35 -31
- ultralytics/solutions/streamlit_inference.py +104 -40
- ultralytics/solutions/templates/similarity-search.html +31 -24
- ultralytics/solutions/trackzone.py +24 -24
- ultralytics/solutions/vision_eye.py +11 -12
- ultralytics/trackers/__init__.py +1 -1
- ultralytics/trackers/basetrack.py +18 -27
- ultralytics/trackers/bot_sort.py +48 -39
- ultralytics/trackers/byte_tracker.py +94 -94
- ultralytics/trackers/track.py +7 -16
- ultralytics/trackers/utils/gmc.py +37 -69
- ultralytics/trackers/utils/kalman_filter.py +68 -76
- ultralytics/trackers/utils/matching.py +13 -17
- ultralytics/utils/__init__.py +251 -275
- ultralytics/utils/autobatch.py +19 -7
- ultralytics/utils/autodevice.py +68 -38
- ultralytics/utils/benchmarks.py +169 -130
- ultralytics/utils/callbacks/base.py +12 -13
- ultralytics/utils/callbacks/clearml.py +14 -15
- ultralytics/utils/callbacks/comet.py +139 -66
- ultralytics/utils/callbacks/dvc.py +19 -27
- ultralytics/utils/callbacks/hub.py +8 -6
- ultralytics/utils/callbacks/mlflow.py +6 -10
- ultralytics/utils/callbacks/neptune.py +11 -19
- ultralytics/utils/callbacks/platform.py +73 -0
- ultralytics/utils/callbacks/raytune.py +3 -4
- ultralytics/utils/callbacks/tensorboard.py +9 -12
- ultralytics/utils/callbacks/wb.py +33 -30
- ultralytics/utils/checks.py +163 -114
- ultralytics/utils/cpu.py +89 -0
- ultralytics/utils/dist.py +24 -20
- ultralytics/utils/downloads.py +176 -146
- ultralytics/utils/errors.py +11 -13
- ultralytics/utils/events.py +113 -0
- ultralytics/utils/export/__init__.py +7 -0
- ultralytics/utils/{export.py → export/engine.py} +81 -63
- ultralytics/utils/export/imx.py +294 -0
- ultralytics/utils/export/tensorflow.py +217 -0
- ultralytics/utils/files.py +33 -36
- ultralytics/utils/git.py +137 -0
- ultralytics/utils/instance.py +105 -120
- ultralytics/utils/logger.py +404 -0
- ultralytics/utils/loss.py +99 -61
- ultralytics/utils/metrics.py +649 -478
- ultralytics/utils/nms.py +337 -0
- ultralytics/utils/ops.py +263 -451
- ultralytics/utils/patches.py +70 -31
- ultralytics/utils/plotting.py +253 -223
- ultralytics/utils/tal.py +48 -61
- ultralytics/utils/torch_utils.py +244 -251
- ultralytics/utils/tqdm.py +438 -0
- ultralytics/utils/triton.py +22 -23
- ultralytics/utils/tuner.py +11 -10
- dgenerate_ultralytics_headless-8.3.137.dist-info/RECORD +0 -272
- {dgenerate_ultralytics_headless-8.3.137.dist-info → dgenerate_ultralytics_headless-8.3.224.dist-info}/entry_points.txt +0 -0
- {dgenerate_ultralytics_headless-8.3.137.dist-info → dgenerate_ultralytics_headless-8.3.224.dist-info}/licenses/LICENSE +0 -0
- {dgenerate_ultralytics_headless-8.3.137.dist-info → dgenerate_ultralytics_headless-8.3.224.dist-info}/top_level.txt +0 -0
ultralytics/nn/modules/conv.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
|
2
2
|
"""Convolution modules."""
|
|
3
3
|
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
4
6
|
import math
|
|
5
7
|
|
|
6
8
|
import numpy as np
|
|
@@ -8,20 +10,20 @@ import torch
|
|
|
8
10
|
import torch.nn as nn
|
|
9
11
|
|
|
10
12
|
__all__ = (
|
|
13
|
+
"CBAM",
|
|
14
|
+
"ChannelAttention",
|
|
15
|
+
"Concat",
|
|
11
16
|
"Conv",
|
|
12
17
|
"Conv2",
|
|
13
|
-
"
|
|
18
|
+
"ConvTranspose",
|
|
14
19
|
"DWConv",
|
|
15
20
|
"DWConvTranspose2d",
|
|
16
|
-
"ConvTranspose",
|
|
17
21
|
"Focus",
|
|
18
22
|
"GhostConv",
|
|
19
|
-
"ChannelAttention",
|
|
20
|
-
"SpatialAttention",
|
|
21
|
-
"CBAM",
|
|
22
|
-
"Concat",
|
|
23
|
-
"RepConv",
|
|
24
23
|
"Index",
|
|
24
|
+
"LightConv",
|
|
25
|
+
"RepConv",
|
|
26
|
+
"SpatialAttention",
|
|
25
27
|
)
|
|
26
28
|
|
|
27
29
|
|
|
@@ -35,8 +37,7 @@ def autopad(k, p=None, d=1): # kernel, padding, dilation
|
|
|
35
37
|
|
|
36
38
|
|
|
37
39
|
class Conv(nn.Module):
|
|
38
|
-
"""
|
|
39
|
-
Standard convolution module with batch normalization and activation.
|
|
40
|
+
"""Standard convolution module with batch normalization and activation.
|
|
40
41
|
|
|
41
42
|
Attributes:
|
|
42
43
|
conv (nn.Conv2d): Convolutional layer.
|
|
@@ -48,8 +49,7 @@ class Conv(nn.Module):
|
|
|
48
49
|
default_act = nn.SiLU() # default activation
|
|
49
50
|
|
|
50
51
|
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
|
|
51
|
-
"""
|
|
52
|
-
Initialize Conv layer with given parameters.
|
|
52
|
+
"""Initialize Conv layer with given parameters.
|
|
53
53
|
|
|
54
54
|
Args:
|
|
55
55
|
c1 (int): Number of input channels.
|
|
@@ -67,8 +67,7 @@ class Conv(nn.Module):
|
|
|
67
67
|
self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
|
|
68
68
|
|
|
69
69
|
def forward(self, x):
|
|
70
|
-
"""
|
|
71
|
-
Apply convolution, batch normalization and activation to input tensor.
|
|
70
|
+
"""Apply convolution, batch normalization and activation to input tensor.
|
|
72
71
|
|
|
73
72
|
Args:
|
|
74
73
|
x (torch.Tensor): Input tensor.
|
|
@@ -79,8 +78,7 @@ class Conv(nn.Module):
|
|
|
79
78
|
return self.act(self.bn(self.conv(x)))
|
|
80
79
|
|
|
81
80
|
def forward_fuse(self, x):
|
|
82
|
-
"""
|
|
83
|
-
Apply convolution and activation without batch normalization.
|
|
81
|
+
"""Apply convolution and activation without batch normalization.
|
|
84
82
|
|
|
85
83
|
Args:
|
|
86
84
|
x (torch.Tensor): Input tensor.
|
|
@@ -92,8 +90,7 @@ class Conv(nn.Module):
|
|
|
92
90
|
|
|
93
91
|
|
|
94
92
|
class Conv2(Conv):
|
|
95
|
-
"""
|
|
96
|
-
Simplified RepConv module with Conv fusing.
|
|
93
|
+
"""Simplified RepConv module with Conv fusing.
|
|
97
94
|
|
|
98
95
|
Attributes:
|
|
99
96
|
conv (nn.Conv2d): Main 3x3 convolutional layer.
|
|
@@ -103,8 +100,7 @@ class Conv2(Conv):
|
|
|
103
100
|
"""
|
|
104
101
|
|
|
105
102
|
def __init__(self, c1, c2, k=3, s=1, p=None, g=1, d=1, act=True):
|
|
106
|
-
"""
|
|
107
|
-
Initialize Conv2 layer with given parameters.
|
|
103
|
+
"""Initialize Conv2 layer with given parameters.
|
|
108
104
|
|
|
109
105
|
Args:
|
|
110
106
|
c1 (int): Number of input channels.
|
|
@@ -120,8 +116,7 @@ class Conv2(Conv):
|
|
|
120
116
|
self.cv2 = nn.Conv2d(c1, c2, 1, s, autopad(1, p, d), groups=g, dilation=d, bias=False) # add 1x1 conv
|
|
121
117
|
|
|
122
118
|
def forward(self, x):
|
|
123
|
-
"""
|
|
124
|
-
Apply convolution, batch normalization and activation to input tensor.
|
|
119
|
+
"""Apply convolution, batch normalization and activation to input tensor.
|
|
125
120
|
|
|
126
121
|
Args:
|
|
127
122
|
x (torch.Tensor): Input tensor.
|
|
@@ -132,8 +127,7 @@ class Conv2(Conv):
|
|
|
132
127
|
return self.act(self.bn(self.conv(x) + self.cv2(x)))
|
|
133
128
|
|
|
134
129
|
def forward_fuse(self, x):
|
|
135
|
-
"""
|
|
136
|
-
Apply fused convolution, batch normalization and activation to input tensor.
|
|
130
|
+
"""Apply fused convolution, batch normalization and activation to input tensor.
|
|
137
131
|
|
|
138
132
|
Args:
|
|
139
133
|
x (torch.Tensor): Input tensor.
|
|
@@ -154,8 +148,7 @@ class Conv2(Conv):
|
|
|
154
148
|
|
|
155
149
|
|
|
156
150
|
class LightConv(nn.Module):
|
|
157
|
-
"""
|
|
158
|
-
Light convolution module with 1x1 and depthwise convolutions.
|
|
151
|
+
"""Light convolution module with 1x1 and depthwise convolutions.
|
|
159
152
|
|
|
160
153
|
This implementation is based on the PaddleDetection HGNetV2 backbone.
|
|
161
154
|
|
|
@@ -165,8 +158,7 @@ class LightConv(nn.Module):
|
|
|
165
158
|
"""
|
|
166
159
|
|
|
167
160
|
def __init__(self, c1, c2, k=1, act=nn.ReLU()):
|
|
168
|
-
"""
|
|
169
|
-
Initialize LightConv layer with given parameters.
|
|
161
|
+
"""Initialize LightConv layer with given parameters.
|
|
170
162
|
|
|
171
163
|
Args:
|
|
172
164
|
c1 (int): Number of input channels.
|
|
@@ -179,8 +171,7 @@ class LightConv(nn.Module):
|
|
|
179
171
|
self.conv2 = DWConv(c2, c2, k, act=act)
|
|
180
172
|
|
|
181
173
|
def forward(self, x):
|
|
182
|
-
"""
|
|
183
|
-
Apply 2 convolutions to input tensor.
|
|
174
|
+
"""Apply 2 convolutions to input tensor.
|
|
184
175
|
|
|
185
176
|
Args:
|
|
186
177
|
x (torch.Tensor): Input tensor.
|
|
@@ -195,8 +186,7 @@ class DWConv(Conv):
|
|
|
195
186
|
"""Depth-wise convolution module."""
|
|
196
187
|
|
|
197
188
|
def __init__(self, c1, c2, k=1, s=1, d=1, act=True):
|
|
198
|
-
"""
|
|
199
|
-
Initialize depth-wise convolution with given parameters.
|
|
189
|
+
"""Initialize depth-wise convolution with given parameters.
|
|
200
190
|
|
|
201
191
|
Args:
|
|
202
192
|
c1 (int): Number of input channels.
|
|
@@ -213,8 +203,7 @@ class DWConvTranspose2d(nn.ConvTranspose2d):
|
|
|
213
203
|
"""Depth-wise transpose convolution module."""
|
|
214
204
|
|
|
215
205
|
def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0):
|
|
216
|
-
"""
|
|
217
|
-
Initialize depth-wise transpose convolution with given parameters.
|
|
206
|
+
"""Initialize depth-wise transpose convolution with given parameters.
|
|
218
207
|
|
|
219
208
|
Args:
|
|
220
209
|
c1 (int): Number of input channels.
|
|
@@ -228,8 +217,7 @@ class DWConvTranspose2d(nn.ConvTranspose2d):
|
|
|
228
217
|
|
|
229
218
|
|
|
230
219
|
class ConvTranspose(nn.Module):
|
|
231
|
-
"""
|
|
232
|
-
Convolution transpose module with optional batch normalization and activation.
|
|
220
|
+
"""Convolution transpose module with optional batch normalization and activation.
|
|
233
221
|
|
|
234
222
|
Attributes:
|
|
235
223
|
conv_transpose (nn.ConvTranspose2d): Transposed convolution layer.
|
|
@@ -241,8 +229,7 @@ class ConvTranspose(nn.Module):
|
|
|
241
229
|
default_act = nn.SiLU() # default activation
|
|
242
230
|
|
|
243
231
|
def __init__(self, c1, c2, k=2, s=2, p=0, bn=True, act=True):
|
|
244
|
-
"""
|
|
245
|
-
Initialize ConvTranspose layer with given parameters.
|
|
232
|
+
"""Initialize ConvTranspose layer with given parameters.
|
|
246
233
|
|
|
247
234
|
Args:
|
|
248
235
|
c1 (int): Number of input channels.
|
|
@@ -259,8 +246,7 @@ class ConvTranspose(nn.Module):
|
|
|
259
246
|
self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
|
|
260
247
|
|
|
261
248
|
def forward(self, x):
|
|
262
|
-
"""
|
|
263
|
-
Apply transposed convolution, batch normalization and activation to input.
|
|
249
|
+
"""Apply transposed convolution, batch normalization and activation to input.
|
|
264
250
|
|
|
265
251
|
Args:
|
|
266
252
|
x (torch.Tensor): Input tensor.
|
|
@@ -271,8 +257,7 @@ class ConvTranspose(nn.Module):
|
|
|
271
257
|
return self.act(self.bn(self.conv_transpose(x)))
|
|
272
258
|
|
|
273
259
|
def forward_fuse(self, x):
|
|
274
|
-
"""
|
|
275
|
-
Apply activation and convolution transpose operation to input.
|
|
260
|
+
"""Apply activation and convolution transpose operation to input.
|
|
276
261
|
|
|
277
262
|
Args:
|
|
278
263
|
x (torch.Tensor): Input tensor.
|
|
@@ -284,8 +269,7 @@ class ConvTranspose(nn.Module):
|
|
|
284
269
|
|
|
285
270
|
|
|
286
271
|
class Focus(nn.Module):
|
|
287
|
-
"""
|
|
288
|
-
Focus module for concentrating feature information.
|
|
272
|
+
"""Focus module for concentrating feature information.
|
|
289
273
|
|
|
290
274
|
Slices input tensor into 4 parts and concatenates them in the channel dimension.
|
|
291
275
|
|
|
@@ -294,8 +278,7 @@ class Focus(nn.Module):
|
|
|
294
278
|
"""
|
|
295
279
|
|
|
296
280
|
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):
|
|
297
|
-
"""
|
|
298
|
-
Initialize Focus module with given parameters.
|
|
281
|
+
"""Initialize Focus module with given parameters.
|
|
299
282
|
|
|
300
283
|
Args:
|
|
301
284
|
c1 (int): Number of input channels.
|
|
@@ -311,10 +294,9 @@ class Focus(nn.Module):
|
|
|
311
294
|
# self.contract = Contract(gain=2)
|
|
312
295
|
|
|
313
296
|
def forward(self, x):
|
|
314
|
-
"""
|
|
315
|
-
Apply Focus operation and convolution to input tensor.
|
|
297
|
+
"""Apply Focus operation and convolution to input tensor.
|
|
316
298
|
|
|
317
|
-
Input shape is (
|
|
299
|
+
Input shape is (B, C, W, H) and output shape is (B, 4C, W/2, H/2).
|
|
318
300
|
|
|
319
301
|
Args:
|
|
320
302
|
x (torch.Tensor): Input tensor.
|
|
@@ -327,8 +309,7 @@ class Focus(nn.Module):
|
|
|
327
309
|
|
|
328
310
|
|
|
329
311
|
class GhostConv(nn.Module):
|
|
330
|
-
"""
|
|
331
|
-
Ghost Convolution module.
|
|
312
|
+
"""Ghost Convolution module.
|
|
332
313
|
|
|
333
314
|
Generates more features with fewer parameters by using cheap operations.
|
|
334
315
|
|
|
@@ -341,8 +322,7 @@ class GhostConv(nn.Module):
|
|
|
341
322
|
"""
|
|
342
323
|
|
|
343
324
|
def __init__(self, c1, c2, k=1, s=1, g=1, act=True):
|
|
344
|
-
"""
|
|
345
|
-
Initialize Ghost Convolution module with given parameters.
|
|
325
|
+
"""Initialize Ghost Convolution module with given parameters.
|
|
346
326
|
|
|
347
327
|
Args:
|
|
348
328
|
c1 (int): Number of input channels.
|
|
@@ -358,8 +338,7 @@ class GhostConv(nn.Module):
|
|
|
358
338
|
self.cv2 = Conv(c_, c_, 5, 1, None, c_, act=act)
|
|
359
339
|
|
|
360
340
|
def forward(self, x):
|
|
361
|
-
"""
|
|
362
|
-
Apply Ghost Convolution to input tensor.
|
|
341
|
+
"""Apply Ghost Convolution to input tensor.
|
|
363
342
|
|
|
364
343
|
Args:
|
|
365
344
|
x (torch.Tensor): Input tensor.
|
|
@@ -372,8 +351,7 @@ class GhostConv(nn.Module):
|
|
|
372
351
|
|
|
373
352
|
|
|
374
353
|
class RepConv(nn.Module):
|
|
375
|
-
"""
|
|
376
|
-
RepConv module with training and deploy modes.
|
|
354
|
+
"""RepConv module with training and deploy modes.
|
|
377
355
|
|
|
378
356
|
This module is used in RT-DETR and can fuse convolutions during inference for efficiency.
|
|
379
357
|
|
|
@@ -391,8 +369,7 @@ class RepConv(nn.Module):
|
|
|
391
369
|
default_act = nn.SiLU() # default activation
|
|
392
370
|
|
|
393
371
|
def __init__(self, c1, c2, k=3, s=1, p=1, g=1, d=1, act=True, bn=False, deploy=False):
|
|
394
|
-
"""
|
|
395
|
-
Initialize RepConv module with given parameters.
|
|
372
|
+
"""Initialize RepConv module with given parameters.
|
|
396
373
|
|
|
397
374
|
Args:
|
|
398
375
|
c1 (int): Number of input channels.
|
|
@@ -418,8 +395,7 @@ class RepConv(nn.Module):
|
|
|
418
395
|
self.conv2 = Conv(c1, c2, 1, s, p=(p - k // 2), g=g, act=False)
|
|
419
396
|
|
|
420
397
|
def forward_fuse(self, x):
|
|
421
|
-
"""
|
|
422
|
-
Forward pass for deploy mode.
|
|
398
|
+
"""Forward pass for deploy mode.
|
|
423
399
|
|
|
424
400
|
Args:
|
|
425
401
|
x (torch.Tensor): Input tensor.
|
|
@@ -430,8 +406,7 @@ class RepConv(nn.Module):
|
|
|
430
406
|
return self.act(self.conv(x))
|
|
431
407
|
|
|
432
408
|
def forward(self, x):
|
|
433
|
-
"""
|
|
434
|
-
Forward pass for training mode.
|
|
409
|
+
"""Forward pass for training mode.
|
|
435
410
|
|
|
436
411
|
Args:
|
|
437
412
|
x (torch.Tensor): Input tensor.
|
|
@@ -443,8 +418,7 @@ class RepConv(nn.Module):
|
|
|
443
418
|
return self.act(self.conv1(x) + self.conv2(x) + id_out)
|
|
444
419
|
|
|
445
420
|
def get_equivalent_kernel_bias(self):
|
|
446
|
-
"""
|
|
447
|
-
Calculate equivalent kernel and bias by fusing convolutions.
|
|
421
|
+
"""Calculate equivalent kernel and bias by fusing convolutions.
|
|
448
422
|
|
|
449
423
|
Returns:
|
|
450
424
|
(torch.Tensor): Equivalent kernel
|
|
@@ -457,8 +431,7 @@ class RepConv(nn.Module):
|
|
|
457
431
|
|
|
458
432
|
@staticmethod
|
|
459
433
|
def _pad_1x1_to_3x3_tensor(kernel1x1):
|
|
460
|
-
"""
|
|
461
|
-
Pad a 1x1 kernel to 3x3 size.
|
|
434
|
+
"""Pad a 1x1 kernel to 3x3 size.
|
|
462
435
|
|
|
463
436
|
Args:
|
|
464
437
|
kernel1x1 (torch.Tensor): 1x1 convolution kernel.
|
|
@@ -472,15 +445,14 @@ class RepConv(nn.Module):
|
|
|
472
445
|
return torch.nn.functional.pad(kernel1x1, [1, 1, 1, 1])
|
|
473
446
|
|
|
474
447
|
def _fuse_bn_tensor(self, branch):
|
|
475
|
-
"""
|
|
476
|
-
Fuse batch normalization with convolution weights.
|
|
448
|
+
"""Fuse batch normalization with convolution weights.
|
|
477
449
|
|
|
478
450
|
Args:
|
|
479
451
|
branch (Conv | nn.BatchNorm2d | None): Branch to fuse.
|
|
480
452
|
|
|
481
453
|
Returns:
|
|
482
|
-
(torch.Tensor): Fused kernel
|
|
483
|
-
(torch.Tensor): Fused bias
|
|
454
|
+
kernel (torch.Tensor): Fused kernel.
|
|
455
|
+
bias (torch.Tensor): Fused bias.
|
|
484
456
|
"""
|
|
485
457
|
if branch is None:
|
|
486
458
|
return 0, 0
|
|
@@ -538,8 +510,7 @@ class RepConv(nn.Module):
|
|
|
538
510
|
|
|
539
511
|
|
|
540
512
|
class ChannelAttention(nn.Module):
|
|
541
|
-
"""
|
|
542
|
-
Channel-attention module for feature recalibration.
|
|
513
|
+
"""Channel-attention module for feature recalibration.
|
|
543
514
|
|
|
544
515
|
Applies attention weights to channels based on global average pooling.
|
|
545
516
|
|
|
@@ -553,8 +524,7 @@ class ChannelAttention(nn.Module):
|
|
|
553
524
|
"""
|
|
554
525
|
|
|
555
526
|
def __init__(self, channels: int) -> None:
|
|
556
|
-
"""
|
|
557
|
-
Initialize Channel-attention module.
|
|
527
|
+
"""Initialize Channel-attention module.
|
|
558
528
|
|
|
559
529
|
Args:
|
|
560
530
|
channels (int): Number of input channels.
|
|
@@ -565,8 +535,7 @@ class ChannelAttention(nn.Module):
|
|
|
565
535
|
self.act = nn.Sigmoid()
|
|
566
536
|
|
|
567
537
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
568
|
-
"""
|
|
569
|
-
Apply channel attention to input tensor.
|
|
538
|
+
"""Apply channel attention to input tensor.
|
|
570
539
|
|
|
571
540
|
Args:
|
|
572
541
|
x (torch.Tensor): Input tensor.
|
|
@@ -578,8 +547,7 @@ class ChannelAttention(nn.Module):
|
|
|
578
547
|
|
|
579
548
|
|
|
580
549
|
class SpatialAttention(nn.Module):
|
|
581
|
-
"""
|
|
582
|
-
Spatial-attention module for feature recalibration.
|
|
550
|
+
"""Spatial-attention module for feature recalibration.
|
|
583
551
|
|
|
584
552
|
Applies attention weights to spatial dimensions based on channel statistics.
|
|
585
553
|
|
|
@@ -589,8 +557,7 @@ class SpatialAttention(nn.Module):
|
|
|
589
557
|
"""
|
|
590
558
|
|
|
591
559
|
def __init__(self, kernel_size=7):
|
|
592
|
-
"""
|
|
593
|
-
Initialize Spatial-attention module.
|
|
560
|
+
"""Initialize Spatial-attention module.
|
|
594
561
|
|
|
595
562
|
Args:
|
|
596
563
|
kernel_size (int): Size of the convolutional kernel (3 or 7).
|
|
@@ -602,8 +569,7 @@ class SpatialAttention(nn.Module):
|
|
|
602
569
|
self.act = nn.Sigmoid()
|
|
603
570
|
|
|
604
571
|
def forward(self, x):
|
|
605
|
-
"""
|
|
606
|
-
Apply spatial attention to input tensor.
|
|
572
|
+
"""Apply spatial attention to input tensor.
|
|
607
573
|
|
|
608
574
|
Args:
|
|
609
575
|
x (torch.Tensor): Input tensor.
|
|
@@ -615,8 +581,7 @@ class SpatialAttention(nn.Module):
|
|
|
615
581
|
|
|
616
582
|
|
|
617
583
|
class CBAM(nn.Module):
|
|
618
|
-
"""
|
|
619
|
-
Convolutional Block Attention Module.
|
|
584
|
+
"""Convolutional Block Attention Module.
|
|
620
585
|
|
|
621
586
|
Combines channel and spatial attention mechanisms for comprehensive feature refinement.
|
|
622
587
|
|
|
@@ -626,8 +591,7 @@ class CBAM(nn.Module):
|
|
|
626
591
|
"""
|
|
627
592
|
|
|
628
593
|
def __init__(self, c1, kernel_size=7):
|
|
629
|
-
"""
|
|
630
|
-
Initialize CBAM with given parameters.
|
|
594
|
+
"""Initialize CBAM with given parameters.
|
|
631
595
|
|
|
632
596
|
Args:
|
|
633
597
|
c1 (int): Number of input channels.
|
|
@@ -638,8 +602,7 @@ class CBAM(nn.Module):
|
|
|
638
602
|
self.spatial_attention = SpatialAttention(kernel_size)
|
|
639
603
|
|
|
640
604
|
def forward(self, x):
|
|
641
|
-
"""
|
|
642
|
-
Apply channel and spatial attention sequentially to input tensor.
|
|
605
|
+
"""Apply channel and spatial attention sequentially to input tensor.
|
|
643
606
|
|
|
644
607
|
Args:
|
|
645
608
|
x (torch.Tensor): Input tensor.
|
|
@@ -651,16 +614,14 @@ class CBAM(nn.Module):
|
|
|
651
614
|
|
|
652
615
|
|
|
653
616
|
class Concat(nn.Module):
|
|
654
|
-
"""
|
|
655
|
-
Concatenate a list of tensors along specified dimension.
|
|
617
|
+
"""Concatenate a list of tensors along specified dimension.
|
|
656
618
|
|
|
657
619
|
Attributes:
|
|
658
620
|
d (int): Dimension along which to concatenate tensors.
|
|
659
621
|
"""
|
|
660
622
|
|
|
661
623
|
def __init__(self, dimension=1):
|
|
662
|
-
"""
|
|
663
|
-
Initialize Concat module.
|
|
624
|
+
"""Initialize Concat module.
|
|
664
625
|
|
|
665
626
|
Args:
|
|
666
627
|
dimension (int): Dimension along which to concatenate tensors.
|
|
@@ -668,12 +629,11 @@ class Concat(nn.Module):
|
|
|
668
629
|
super().__init__()
|
|
669
630
|
self.d = dimension
|
|
670
631
|
|
|
671
|
-
def forward(self, x):
|
|
672
|
-
"""
|
|
673
|
-
Concatenate input tensors along specified dimension.
|
|
632
|
+
def forward(self, x: list[torch.Tensor]):
|
|
633
|
+
"""Concatenate input tensors along specified dimension.
|
|
674
634
|
|
|
675
635
|
Args:
|
|
676
|
-
x (
|
|
636
|
+
x (list[torch.Tensor]): List of input tensors.
|
|
677
637
|
|
|
678
638
|
Returns:
|
|
679
639
|
(torch.Tensor): Concatenated tensor.
|
|
@@ -682,16 +642,14 @@ class Concat(nn.Module):
|
|
|
682
642
|
|
|
683
643
|
|
|
684
644
|
class Index(nn.Module):
|
|
685
|
-
"""
|
|
686
|
-
Returns a particular index of the input.
|
|
645
|
+
"""Returns a particular index of the input.
|
|
687
646
|
|
|
688
647
|
Attributes:
|
|
689
648
|
index (int): Index to select from input.
|
|
690
649
|
"""
|
|
691
650
|
|
|
692
651
|
def __init__(self, index=0):
|
|
693
|
-
"""
|
|
694
|
-
Initialize Index module.
|
|
652
|
+
"""Initialize Index module.
|
|
695
653
|
|
|
696
654
|
Args:
|
|
697
655
|
index (int): Index to select from input.
|
|
@@ -699,12 +657,11 @@ class Index(nn.Module):
|
|
|
699
657
|
super().__init__()
|
|
700
658
|
self.index = index
|
|
701
659
|
|
|
702
|
-
def forward(self, x):
|
|
703
|
-
"""
|
|
704
|
-
Select and return a particular index from input.
|
|
660
|
+
def forward(self, x: list[torch.Tensor]):
|
|
661
|
+
"""Select and return a particular index from input.
|
|
705
662
|
|
|
706
663
|
Args:
|
|
707
|
-
x (
|
|
664
|
+
x (list[torch.Tensor]): List of input tensors.
|
|
708
665
|
|
|
709
666
|
Returns:
|
|
710
667
|
(torch.Tensor): Selected tensor.
|