dgenerate-ultralytics-headless 8.3.134__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.134.dist-info/METADATA +400 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/RECORD +272 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/WHEEL +5 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/entry_points.txt +3 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/licenses/LICENSE +661 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/top_level.txt +1 -0
- tests/__init__.py +22 -0
- tests/conftest.py +83 -0
- tests/test_cli.py +138 -0
- tests/test_cuda.py +215 -0
- tests/test_engine.py +131 -0
- tests/test_exports.py +236 -0
- tests/test_integrations.py +154 -0
- tests/test_python.py +694 -0
- tests/test_solutions.py +187 -0
- ultralytics/__init__.py +30 -0
- ultralytics/assets/bus.jpg +0 -0
- ultralytics/assets/zidane.jpg +0 -0
- ultralytics/cfg/__init__.py +1023 -0
- ultralytics/cfg/datasets/Argoverse.yaml +77 -0
- ultralytics/cfg/datasets/DOTAv1.5.yaml +37 -0
- ultralytics/cfg/datasets/DOTAv1.yaml +36 -0
- ultralytics/cfg/datasets/GlobalWheat2020.yaml +68 -0
- ultralytics/cfg/datasets/HomeObjects-3K.yaml +33 -0
- ultralytics/cfg/datasets/ImageNet.yaml +2025 -0
- ultralytics/cfg/datasets/Objects365.yaml +443 -0
- ultralytics/cfg/datasets/SKU-110K.yaml +58 -0
- ultralytics/cfg/datasets/VOC.yaml +106 -0
- ultralytics/cfg/datasets/VisDrone.yaml +77 -0
- ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
- ultralytics/cfg/datasets/brain-tumor.yaml +23 -0
- ultralytics/cfg/datasets/carparts-seg.yaml +44 -0
- ultralytics/cfg/datasets/coco-pose.yaml +42 -0
- ultralytics/cfg/datasets/coco.yaml +118 -0
- ultralytics/cfg/datasets/coco128-seg.yaml +101 -0
- ultralytics/cfg/datasets/coco128.yaml +101 -0
- ultralytics/cfg/datasets/coco8-multispectral.yaml +104 -0
- ultralytics/cfg/datasets/coco8-pose.yaml +26 -0
- ultralytics/cfg/datasets/coco8-seg.yaml +101 -0
- ultralytics/cfg/datasets/coco8.yaml +101 -0
- ultralytics/cfg/datasets/crack-seg.yaml +22 -0
- ultralytics/cfg/datasets/dog-pose.yaml +24 -0
- ultralytics/cfg/datasets/dota8-multispectral.yaml +38 -0
- ultralytics/cfg/datasets/dota8.yaml +35 -0
- ultralytics/cfg/datasets/hand-keypoints.yaml +26 -0
- ultralytics/cfg/datasets/lvis.yaml +1240 -0
- ultralytics/cfg/datasets/medical-pills.yaml +22 -0
- ultralytics/cfg/datasets/open-images-v7.yaml +666 -0
- ultralytics/cfg/datasets/package-seg.yaml +22 -0
- ultralytics/cfg/datasets/signature.yaml +21 -0
- ultralytics/cfg/datasets/tiger-pose.yaml +25 -0
- ultralytics/cfg/datasets/xView.yaml +155 -0
- ultralytics/cfg/default.yaml +127 -0
- ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +17 -0
- ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
- ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
- ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
- ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
- ultralytics/cfg/models/11/yolo11.yaml +50 -0
- ultralytics/cfg/models/11/yoloe-11-seg.yaml +48 -0
- ultralytics/cfg/models/11/yoloe-11.yaml +48 -0
- ultralytics/cfg/models/12/yolo12-cls.yaml +32 -0
- ultralytics/cfg/models/12/yolo12-obb.yaml +48 -0
- ultralytics/cfg/models/12/yolo12-pose.yaml +49 -0
- ultralytics/cfg/models/12/yolo12-seg.yaml +48 -0
- ultralytics/cfg/models/12/yolo12.yaml +48 -0
- ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +53 -0
- ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +45 -0
- ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +45 -0
- ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +57 -0
- ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
- ultralytics/cfg/models/v3/yolov3-spp.yaml +49 -0
- ultralytics/cfg/models/v3/yolov3-tiny.yaml +40 -0
- ultralytics/cfg/models/v3/yolov3.yaml +49 -0
- ultralytics/cfg/models/v5/yolov5-p6.yaml +62 -0
- ultralytics/cfg/models/v5/yolov5.yaml +51 -0
- ultralytics/cfg/models/v6/yolov6.yaml +56 -0
- ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +45 -0
- ultralytics/cfg/models/v8/yoloe-v8.yaml +45 -0
- ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +28 -0
- ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +28 -0
- ultralytics/cfg/models/v8/yolov8-cls.yaml +32 -0
- ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +58 -0
- ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +60 -0
- ultralytics/cfg/models/v8/yolov8-ghost.yaml +50 -0
- ultralytics/cfg/models/v8/yolov8-obb.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-p2.yaml +57 -0
- ultralytics/cfg/models/v8/yolov8-p6.yaml +59 -0
- ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +60 -0
- ultralytics/cfg/models/v8/yolov8-pose.yaml +50 -0
- ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +59 -0
- ultralytics/cfg/models/v8/yolov8-seg.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-world.yaml +51 -0
- ultralytics/cfg/models/v8/yolov8-worldv2.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8.yaml +49 -0
- ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9c.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
- ultralytics/cfg/models/v9/yolov9e.yaml +64 -0
- ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
- ultralytics/cfg/trackers/botsort.yaml +22 -0
- ultralytics/cfg/trackers/bytetrack.yaml +14 -0
- ultralytics/data/__init__.py +26 -0
- ultralytics/data/annotator.py +66 -0
- ultralytics/data/augment.py +2945 -0
- ultralytics/data/base.py +438 -0
- ultralytics/data/build.py +258 -0
- ultralytics/data/converter.py +754 -0
- ultralytics/data/dataset.py +834 -0
- ultralytics/data/loaders.py +676 -0
- ultralytics/data/scripts/download_weights.sh +18 -0
- ultralytics/data/scripts/get_coco.sh +61 -0
- ultralytics/data/scripts/get_coco128.sh +18 -0
- ultralytics/data/scripts/get_imagenet.sh +52 -0
- ultralytics/data/split.py +125 -0
- ultralytics/data/split_dota.py +325 -0
- ultralytics/data/utils.py +777 -0
- ultralytics/engine/__init__.py +1 -0
- ultralytics/engine/exporter.py +1519 -0
- ultralytics/engine/model.py +1156 -0
- ultralytics/engine/predictor.py +502 -0
- ultralytics/engine/results.py +1840 -0
- ultralytics/engine/trainer.py +853 -0
- ultralytics/engine/tuner.py +243 -0
- ultralytics/engine/validator.py +377 -0
- ultralytics/hub/__init__.py +168 -0
- ultralytics/hub/auth.py +137 -0
- ultralytics/hub/google/__init__.py +176 -0
- ultralytics/hub/session.py +446 -0
- ultralytics/hub/utils.py +248 -0
- ultralytics/models/__init__.py +9 -0
- ultralytics/models/fastsam/__init__.py +7 -0
- ultralytics/models/fastsam/model.py +61 -0
- ultralytics/models/fastsam/predict.py +181 -0
- ultralytics/models/fastsam/utils.py +24 -0
- ultralytics/models/fastsam/val.py +40 -0
- ultralytics/models/nas/__init__.py +7 -0
- ultralytics/models/nas/model.py +102 -0
- ultralytics/models/nas/predict.py +58 -0
- ultralytics/models/nas/val.py +39 -0
- ultralytics/models/rtdetr/__init__.py +7 -0
- ultralytics/models/rtdetr/model.py +63 -0
- ultralytics/models/rtdetr/predict.py +84 -0
- ultralytics/models/rtdetr/train.py +85 -0
- ultralytics/models/rtdetr/val.py +191 -0
- ultralytics/models/sam/__init__.py +6 -0
- ultralytics/models/sam/amg.py +260 -0
- ultralytics/models/sam/build.py +358 -0
- ultralytics/models/sam/model.py +170 -0
- ultralytics/models/sam/modules/__init__.py +1 -0
- ultralytics/models/sam/modules/blocks.py +1129 -0
- ultralytics/models/sam/modules/decoders.py +515 -0
- ultralytics/models/sam/modules/encoders.py +854 -0
- ultralytics/models/sam/modules/memory_attention.py +299 -0
- ultralytics/models/sam/modules/sam.py +1006 -0
- ultralytics/models/sam/modules/tiny_encoder.py +1002 -0
- ultralytics/models/sam/modules/transformer.py +351 -0
- ultralytics/models/sam/modules/utils.py +394 -0
- ultralytics/models/sam/predict.py +1605 -0
- ultralytics/models/utils/__init__.py +1 -0
- ultralytics/models/utils/loss.py +455 -0
- ultralytics/models/utils/ops.py +268 -0
- ultralytics/models/yolo/__init__.py +7 -0
- ultralytics/models/yolo/classify/__init__.py +7 -0
- ultralytics/models/yolo/classify/predict.py +88 -0
- ultralytics/models/yolo/classify/train.py +233 -0
- ultralytics/models/yolo/classify/val.py +215 -0
- ultralytics/models/yolo/detect/__init__.py +7 -0
- ultralytics/models/yolo/detect/predict.py +124 -0
- ultralytics/models/yolo/detect/train.py +217 -0
- ultralytics/models/yolo/detect/val.py +451 -0
- ultralytics/models/yolo/model.py +354 -0
- ultralytics/models/yolo/obb/__init__.py +7 -0
- ultralytics/models/yolo/obb/predict.py +66 -0
- ultralytics/models/yolo/obb/train.py +81 -0
- ultralytics/models/yolo/obb/val.py +283 -0
- ultralytics/models/yolo/pose/__init__.py +7 -0
- ultralytics/models/yolo/pose/predict.py +79 -0
- ultralytics/models/yolo/pose/train.py +154 -0
- ultralytics/models/yolo/pose/val.py +394 -0
- ultralytics/models/yolo/segment/__init__.py +7 -0
- ultralytics/models/yolo/segment/predict.py +113 -0
- ultralytics/models/yolo/segment/train.py +123 -0
- ultralytics/models/yolo/segment/val.py +428 -0
- ultralytics/models/yolo/world/__init__.py +5 -0
- ultralytics/models/yolo/world/train.py +119 -0
- ultralytics/models/yolo/world/train_world.py +176 -0
- ultralytics/models/yolo/yoloe/__init__.py +22 -0
- ultralytics/models/yolo/yoloe/predict.py +169 -0
- ultralytics/models/yolo/yoloe/train.py +298 -0
- ultralytics/models/yolo/yoloe/train_seg.py +124 -0
- ultralytics/models/yolo/yoloe/val.py +191 -0
- ultralytics/nn/__init__.py +29 -0
- ultralytics/nn/autobackend.py +842 -0
- ultralytics/nn/modules/__init__.py +182 -0
- ultralytics/nn/modules/activation.py +53 -0
- ultralytics/nn/modules/block.py +1966 -0
- ultralytics/nn/modules/conv.py +712 -0
- ultralytics/nn/modules/head.py +880 -0
- ultralytics/nn/modules/transformer.py +713 -0
- ultralytics/nn/modules/utils.py +164 -0
- ultralytics/nn/tasks.py +1627 -0
- ultralytics/nn/text_model.py +351 -0
- ultralytics/solutions/__init__.py +41 -0
- ultralytics/solutions/ai_gym.py +116 -0
- ultralytics/solutions/analytics.py +252 -0
- ultralytics/solutions/config.py +106 -0
- ultralytics/solutions/distance_calculation.py +124 -0
- ultralytics/solutions/heatmap.py +127 -0
- ultralytics/solutions/instance_segmentation.py +84 -0
- ultralytics/solutions/object_blurrer.py +90 -0
- ultralytics/solutions/object_counter.py +195 -0
- ultralytics/solutions/object_cropper.py +84 -0
- ultralytics/solutions/parking_management.py +273 -0
- ultralytics/solutions/queue_management.py +93 -0
- ultralytics/solutions/region_counter.py +120 -0
- ultralytics/solutions/security_alarm.py +154 -0
- ultralytics/solutions/similarity_search.py +172 -0
- ultralytics/solutions/solutions.py +724 -0
- ultralytics/solutions/speed_estimation.py +110 -0
- ultralytics/solutions/streamlit_inference.py +196 -0
- ultralytics/solutions/templates/similarity-search.html +160 -0
- ultralytics/solutions/trackzone.py +88 -0
- ultralytics/solutions/vision_eye.py +68 -0
- ultralytics/trackers/__init__.py +7 -0
- ultralytics/trackers/basetrack.py +124 -0
- ultralytics/trackers/bot_sort.py +260 -0
- ultralytics/trackers/byte_tracker.py +480 -0
- ultralytics/trackers/track.py +125 -0
- ultralytics/trackers/utils/__init__.py +1 -0
- ultralytics/trackers/utils/gmc.py +376 -0
- ultralytics/trackers/utils/kalman_filter.py +493 -0
- ultralytics/trackers/utils/matching.py +157 -0
- ultralytics/utils/__init__.py +1435 -0
- ultralytics/utils/autobatch.py +106 -0
- ultralytics/utils/autodevice.py +174 -0
- ultralytics/utils/benchmarks.py +695 -0
- ultralytics/utils/callbacks/__init__.py +5 -0
- ultralytics/utils/callbacks/base.py +234 -0
- ultralytics/utils/callbacks/clearml.py +153 -0
- ultralytics/utils/callbacks/comet.py +552 -0
- ultralytics/utils/callbacks/dvc.py +205 -0
- ultralytics/utils/callbacks/hub.py +108 -0
- ultralytics/utils/callbacks/mlflow.py +138 -0
- ultralytics/utils/callbacks/neptune.py +140 -0
- ultralytics/utils/callbacks/raytune.py +43 -0
- ultralytics/utils/callbacks/tensorboard.py +132 -0
- ultralytics/utils/callbacks/wb.py +185 -0
- ultralytics/utils/checks.py +897 -0
- ultralytics/utils/dist.py +119 -0
- ultralytics/utils/downloads.py +499 -0
- ultralytics/utils/errors.py +43 -0
- ultralytics/utils/export.py +219 -0
- ultralytics/utils/files.py +221 -0
- ultralytics/utils/instance.py +499 -0
- ultralytics/utils/loss.py +813 -0
- ultralytics/utils/metrics.py +1356 -0
- ultralytics/utils/ops.py +885 -0
- ultralytics/utils/patches.py +143 -0
- ultralytics/utils/plotting.py +1011 -0
- ultralytics/utils/tal.py +416 -0
- ultralytics/utils/torch_utils.py +990 -0
- ultralytics/utils/triton.py +116 -0
- ultralytics/utils/tuner.py +159 -0
@@ -0,0 +1,712 @@
|
|
1
|
+
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
2
|
+
"""Convolution modules."""
|
3
|
+
|
4
|
+
import math
|
5
|
+
|
6
|
+
import numpy as np
|
7
|
+
import torch
|
8
|
+
import torch.nn as nn
|
9
|
+
|
10
|
+
__all__ = (
|
11
|
+
"Conv",
|
12
|
+
"Conv2",
|
13
|
+
"LightConv",
|
14
|
+
"DWConv",
|
15
|
+
"DWConvTranspose2d",
|
16
|
+
"ConvTranspose",
|
17
|
+
"Focus",
|
18
|
+
"GhostConv",
|
19
|
+
"ChannelAttention",
|
20
|
+
"SpatialAttention",
|
21
|
+
"CBAM",
|
22
|
+
"Concat",
|
23
|
+
"RepConv",
|
24
|
+
"Index",
|
25
|
+
)
|
26
|
+
|
27
|
+
|
28
|
+
def autopad(k, p=None, d=1): # kernel, padding, dilation
|
29
|
+
"""Pad to 'same' shape outputs."""
|
30
|
+
if d > 1:
|
31
|
+
k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k] # actual kernel-size
|
32
|
+
if p is None:
|
33
|
+
p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
|
34
|
+
return p
|
35
|
+
|
36
|
+
|
37
|
+
class Conv(nn.Module):
|
38
|
+
"""
|
39
|
+
Standard convolution module with batch normalization and activation.
|
40
|
+
|
41
|
+
Attributes:
|
42
|
+
conv (nn.Conv2d): Convolutional layer.
|
43
|
+
bn (nn.BatchNorm2d): Batch normalization layer.
|
44
|
+
act (nn.Module): Activation function layer.
|
45
|
+
default_act (nn.Module): Default activation function (SiLU).
|
46
|
+
"""
|
47
|
+
|
48
|
+
default_act = nn.SiLU() # default activation
|
49
|
+
|
50
|
+
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.
|
53
|
+
|
54
|
+
Args:
|
55
|
+
c1 (int): Number of input channels.
|
56
|
+
c2 (int): Number of output channels.
|
57
|
+
k (int): Kernel size.
|
58
|
+
s (int): Stride.
|
59
|
+
p (int, optional): Padding.
|
60
|
+
g (int): Groups.
|
61
|
+
d (int): Dilation.
|
62
|
+
act (bool | nn.Module): Activation function.
|
63
|
+
"""
|
64
|
+
super().__init__()
|
65
|
+
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
|
66
|
+
self.bn = nn.BatchNorm2d(c2)
|
67
|
+
self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
|
68
|
+
|
69
|
+
def forward(self, x):
|
70
|
+
"""
|
71
|
+
Apply convolution, batch normalization and activation to input tensor.
|
72
|
+
|
73
|
+
Args:
|
74
|
+
x (torch.Tensor): Input tensor.
|
75
|
+
|
76
|
+
Returns:
|
77
|
+
(torch.Tensor): Output tensor.
|
78
|
+
"""
|
79
|
+
return self.act(self.bn(self.conv(x)))
|
80
|
+
|
81
|
+
def forward_fuse(self, x):
|
82
|
+
"""
|
83
|
+
Apply convolution and activation without batch normalization.
|
84
|
+
|
85
|
+
Args:
|
86
|
+
x (torch.Tensor): Input tensor.
|
87
|
+
|
88
|
+
Returns:
|
89
|
+
(torch.Tensor): Output tensor.
|
90
|
+
"""
|
91
|
+
return self.act(self.conv(x))
|
92
|
+
|
93
|
+
|
94
|
+
class Conv2(Conv):
|
95
|
+
"""
|
96
|
+
Simplified RepConv module with Conv fusing.
|
97
|
+
|
98
|
+
Attributes:
|
99
|
+
conv (nn.Conv2d): Main 3x3 convolutional layer.
|
100
|
+
cv2 (nn.Conv2d): Additional 1x1 convolutional layer.
|
101
|
+
bn (nn.BatchNorm2d): Batch normalization layer.
|
102
|
+
act (nn.Module): Activation function layer.
|
103
|
+
"""
|
104
|
+
|
105
|
+
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.
|
108
|
+
|
109
|
+
Args:
|
110
|
+
c1 (int): Number of input channels.
|
111
|
+
c2 (int): Number of output channels.
|
112
|
+
k (int): Kernel size.
|
113
|
+
s (int): Stride.
|
114
|
+
p (int, optional): Padding.
|
115
|
+
g (int): Groups.
|
116
|
+
d (int): Dilation.
|
117
|
+
act (bool | nn.Module): Activation function.
|
118
|
+
"""
|
119
|
+
super().__init__(c1, c2, k, s, p, g=g, d=d, act=act)
|
120
|
+
self.cv2 = nn.Conv2d(c1, c2, 1, s, autopad(1, p, d), groups=g, dilation=d, bias=False) # add 1x1 conv
|
121
|
+
|
122
|
+
def forward(self, x):
|
123
|
+
"""
|
124
|
+
Apply convolution, batch normalization and activation to input tensor.
|
125
|
+
|
126
|
+
Args:
|
127
|
+
x (torch.Tensor): Input tensor.
|
128
|
+
|
129
|
+
Returns:
|
130
|
+
(torch.Tensor): Output tensor.
|
131
|
+
"""
|
132
|
+
return self.act(self.bn(self.conv(x) + self.cv2(x)))
|
133
|
+
|
134
|
+
def forward_fuse(self, x):
|
135
|
+
"""
|
136
|
+
Apply fused convolution, batch normalization and activation to input tensor.
|
137
|
+
|
138
|
+
Args:
|
139
|
+
x (torch.Tensor): Input tensor.
|
140
|
+
|
141
|
+
Returns:
|
142
|
+
(torch.Tensor): Output tensor.
|
143
|
+
"""
|
144
|
+
return self.act(self.bn(self.conv(x)))
|
145
|
+
|
146
|
+
def fuse_convs(self):
|
147
|
+
"""Fuse parallel convolutions."""
|
148
|
+
w = torch.zeros_like(self.conv.weight.data)
|
149
|
+
i = [x // 2 for x in w.shape[2:]]
|
150
|
+
w[:, :, i[0] : i[0] + 1, i[1] : i[1] + 1] = self.cv2.weight.data.clone()
|
151
|
+
self.conv.weight.data += w
|
152
|
+
self.__delattr__("cv2")
|
153
|
+
self.forward = self.forward_fuse
|
154
|
+
|
155
|
+
|
156
|
+
class LightConv(nn.Module):
|
157
|
+
"""
|
158
|
+
Light convolution module with 1x1 and depthwise convolutions.
|
159
|
+
|
160
|
+
This implementation is based on the PaddleDetection HGNetV2 backbone.
|
161
|
+
|
162
|
+
Attributes:
|
163
|
+
conv1 (Conv): 1x1 convolution layer.
|
164
|
+
conv2 (DWConv): Depthwise convolution layer.
|
165
|
+
"""
|
166
|
+
|
167
|
+
def __init__(self, c1, c2, k=1, act=nn.ReLU()):
|
168
|
+
"""
|
169
|
+
Initialize LightConv layer with given parameters.
|
170
|
+
|
171
|
+
Args:
|
172
|
+
c1 (int): Number of input channels.
|
173
|
+
c2 (int): Number of output channels.
|
174
|
+
k (int): Kernel size for depthwise convolution.
|
175
|
+
act (nn.Module): Activation function.
|
176
|
+
"""
|
177
|
+
super().__init__()
|
178
|
+
self.conv1 = Conv(c1, c2, 1, act=False)
|
179
|
+
self.conv2 = DWConv(c2, c2, k, act=act)
|
180
|
+
|
181
|
+
def forward(self, x):
|
182
|
+
"""
|
183
|
+
Apply 2 convolutions to input tensor.
|
184
|
+
|
185
|
+
Args:
|
186
|
+
x (torch.Tensor): Input tensor.
|
187
|
+
|
188
|
+
Returns:
|
189
|
+
(torch.Tensor): Output tensor.
|
190
|
+
"""
|
191
|
+
return self.conv2(self.conv1(x))
|
192
|
+
|
193
|
+
|
194
|
+
class DWConv(Conv):
|
195
|
+
"""Depth-wise convolution module."""
|
196
|
+
|
197
|
+
def __init__(self, c1, c2, k=1, s=1, d=1, act=True):
|
198
|
+
"""
|
199
|
+
Initialize depth-wise convolution with given parameters.
|
200
|
+
|
201
|
+
Args:
|
202
|
+
c1 (int): Number of input channels.
|
203
|
+
c2 (int): Number of output channels.
|
204
|
+
k (int): Kernel size.
|
205
|
+
s (int): Stride.
|
206
|
+
d (int): Dilation.
|
207
|
+
act (bool | nn.Module): Activation function.
|
208
|
+
"""
|
209
|
+
super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), d=d, act=act)
|
210
|
+
|
211
|
+
|
212
|
+
class DWConvTranspose2d(nn.ConvTranspose2d):
|
213
|
+
"""Depth-wise transpose convolution module."""
|
214
|
+
|
215
|
+
def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0):
|
216
|
+
"""
|
217
|
+
Initialize depth-wise transpose convolution with given parameters.
|
218
|
+
|
219
|
+
Args:
|
220
|
+
c1 (int): Number of input channels.
|
221
|
+
c2 (int): Number of output channels.
|
222
|
+
k (int): Kernel size.
|
223
|
+
s (int): Stride.
|
224
|
+
p1 (int): Padding.
|
225
|
+
p2 (int): Output padding.
|
226
|
+
"""
|
227
|
+
super().__init__(c1, c2, k, s, p1, p2, groups=math.gcd(c1, c2))
|
228
|
+
|
229
|
+
|
230
|
+
class ConvTranspose(nn.Module):
|
231
|
+
"""
|
232
|
+
Convolution transpose module with optional batch normalization and activation.
|
233
|
+
|
234
|
+
Attributes:
|
235
|
+
conv_transpose (nn.ConvTranspose2d): Transposed convolution layer.
|
236
|
+
bn (nn.BatchNorm2d | nn.Identity): Batch normalization layer.
|
237
|
+
act (nn.Module): Activation function layer.
|
238
|
+
default_act (nn.Module): Default activation function (SiLU).
|
239
|
+
"""
|
240
|
+
|
241
|
+
default_act = nn.SiLU() # default activation
|
242
|
+
|
243
|
+
def __init__(self, c1, c2, k=2, s=2, p=0, bn=True, act=True):
|
244
|
+
"""
|
245
|
+
Initialize ConvTranspose layer with given parameters.
|
246
|
+
|
247
|
+
Args:
|
248
|
+
c1 (int): Number of input channels.
|
249
|
+
c2 (int): Number of output channels.
|
250
|
+
k (int): Kernel size.
|
251
|
+
s (int): Stride.
|
252
|
+
p (int): Padding.
|
253
|
+
bn (bool): Use batch normalization.
|
254
|
+
act (bool | nn.Module): Activation function.
|
255
|
+
"""
|
256
|
+
super().__init__()
|
257
|
+
self.conv_transpose = nn.ConvTranspose2d(c1, c2, k, s, p, bias=not bn)
|
258
|
+
self.bn = nn.BatchNorm2d(c2) if bn else nn.Identity()
|
259
|
+
self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
|
260
|
+
|
261
|
+
def forward(self, x):
|
262
|
+
"""
|
263
|
+
Apply transposed convolution, batch normalization and activation to input.
|
264
|
+
|
265
|
+
Args:
|
266
|
+
x (torch.Tensor): Input tensor.
|
267
|
+
|
268
|
+
Returns:
|
269
|
+
(torch.Tensor): Output tensor.
|
270
|
+
"""
|
271
|
+
return self.act(self.bn(self.conv_transpose(x)))
|
272
|
+
|
273
|
+
def forward_fuse(self, x):
|
274
|
+
"""
|
275
|
+
Apply activation and convolution transpose operation to input.
|
276
|
+
|
277
|
+
Args:
|
278
|
+
x (torch.Tensor): Input tensor.
|
279
|
+
|
280
|
+
Returns:
|
281
|
+
(torch.Tensor): Output tensor.
|
282
|
+
"""
|
283
|
+
return self.act(self.conv_transpose(x))
|
284
|
+
|
285
|
+
|
286
|
+
class Focus(nn.Module):
|
287
|
+
"""
|
288
|
+
Focus module for concentrating feature information.
|
289
|
+
|
290
|
+
Slices input tensor into 4 parts and concatenates them in the channel dimension.
|
291
|
+
|
292
|
+
Attributes:
|
293
|
+
conv (Conv): Convolution layer.
|
294
|
+
"""
|
295
|
+
|
296
|
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):
|
297
|
+
"""
|
298
|
+
Initialize Focus module with given parameters.
|
299
|
+
|
300
|
+
Args:
|
301
|
+
c1 (int): Number of input channels.
|
302
|
+
c2 (int): Number of output channels.
|
303
|
+
k (int): Kernel size.
|
304
|
+
s (int): Stride.
|
305
|
+
p (int, optional): Padding.
|
306
|
+
g (int): Groups.
|
307
|
+
act (bool | nn.Module): Activation function.
|
308
|
+
"""
|
309
|
+
super().__init__()
|
310
|
+
self.conv = Conv(c1 * 4, c2, k, s, p, g, act=act)
|
311
|
+
# self.contract = Contract(gain=2)
|
312
|
+
|
313
|
+
def forward(self, x):
|
314
|
+
"""
|
315
|
+
Apply Focus operation and convolution to input tensor.
|
316
|
+
|
317
|
+
Input shape is (b,c,w,h) and output shape is (b,4c,w/2,h/2).
|
318
|
+
|
319
|
+
Args:
|
320
|
+
x (torch.Tensor): Input tensor.
|
321
|
+
|
322
|
+
Returns:
|
323
|
+
(torch.Tensor): Output tensor.
|
324
|
+
"""
|
325
|
+
return self.conv(torch.cat((x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]), 1))
|
326
|
+
# return self.conv(self.contract(x))
|
327
|
+
|
328
|
+
|
329
|
+
class GhostConv(nn.Module):
|
330
|
+
"""
|
331
|
+
Ghost Convolution module.
|
332
|
+
|
333
|
+
Generates more features with fewer parameters by using cheap operations.
|
334
|
+
|
335
|
+
Attributes:
|
336
|
+
cv1 (Conv): Primary convolution.
|
337
|
+
cv2 (Conv): Cheap operation convolution.
|
338
|
+
|
339
|
+
References:
|
340
|
+
https://github.com/huawei-noah/Efficient-AI-Backbones
|
341
|
+
"""
|
342
|
+
|
343
|
+
def __init__(self, c1, c2, k=1, s=1, g=1, act=True):
|
344
|
+
"""
|
345
|
+
Initialize Ghost Convolution module with given parameters.
|
346
|
+
|
347
|
+
Args:
|
348
|
+
c1 (int): Number of input channels.
|
349
|
+
c2 (int): Number of output channels.
|
350
|
+
k (int): Kernel size.
|
351
|
+
s (int): Stride.
|
352
|
+
g (int): Groups.
|
353
|
+
act (bool | nn.Module): Activation function.
|
354
|
+
"""
|
355
|
+
super().__init__()
|
356
|
+
c_ = c2 // 2 # hidden channels
|
357
|
+
self.cv1 = Conv(c1, c_, k, s, None, g, act=act)
|
358
|
+
self.cv2 = Conv(c_, c_, 5, 1, None, c_, act=act)
|
359
|
+
|
360
|
+
def forward(self, x):
|
361
|
+
"""
|
362
|
+
Apply Ghost Convolution to input tensor.
|
363
|
+
|
364
|
+
Args:
|
365
|
+
x (torch.Tensor): Input tensor.
|
366
|
+
|
367
|
+
Returns:
|
368
|
+
(torch.Tensor): Output tensor with concatenated features.
|
369
|
+
"""
|
370
|
+
y = self.cv1(x)
|
371
|
+
return torch.cat((y, self.cv2(y)), 1)
|
372
|
+
|
373
|
+
|
374
|
+
class RepConv(nn.Module):
|
375
|
+
"""
|
376
|
+
RepConv module with training and deploy modes.
|
377
|
+
|
378
|
+
This module is used in RT-DETR and can fuse convolutions during inference for efficiency.
|
379
|
+
|
380
|
+
Attributes:
|
381
|
+
conv1 (Conv): 3x3 convolution.
|
382
|
+
conv2 (Conv): 1x1 convolution.
|
383
|
+
bn (nn.BatchNorm2d, optional): Batch normalization for identity branch.
|
384
|
+
act (nn.Module): Activation function.
|
385
|
+
default_act (nn.Module): Default activation function (SiLU).
|
386
|
+
|
387
|
+
References:
|
388
|
+
https://github.com/DingXiaoH/RepVGG/blob/main/repvgg.py
|
389
|
+
"""
|
390
|
+
|
391
|
+
default_act = nn.SiLU() # default activation
|
392
|
+
|
393
|
+
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.
|
396
|
+
|
397
|
+
Args:
|
398
|
+
c1 (int): Number of input channels.
|
399
|
+
c2 (int): Number of output channels.
|
400
|
+
k (int): Kernel size.
|
401
|
+
s (int): Stride.
|
402
|
+
p (int): Padding.
|
403
|
+
g (int): Groups.
|
404
|
+
d (int): Dilation.
|
405
|
+
act (bool | nn.Module): Activation function.
|
406
|
+
bn (bool): Use batch normalization for identity branch.
|
407
|
+
deploy (bool): Deploy mode for inference.
|
408
|
+
"""
|
409
|
+
super().__init__()
|
410
|
+
assert k == 3 and p == 1
|
411
|
+
self.g = g
|
412
|
+
self.c1 = c1
|
413
|
+
self.c2 = c2
|
414
|
+
self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
|
415
|
+
|
416
|
+
self.bn = nn.BatchNorm2d(num_features=c1) if bn and c2 == c1 and s == 1 else None
|
417
|
+
self.conv1 = Conv(c1, c2, k, s, p=p, g=g, act=False)
|
418
|
+
self.conv2 = Conv(c1, c2, 1, s, p=(p - k // 2), g=g, act=False)
|
419
|
+
|
420
|
+
def forward_fuse(self, x):
|
421
|
+
"""
|
422
|
+
Forward pass for deploy mode.
|
423
|
+
|
424
|
+
Args:
|
425
|
+
x (torch.Tensor): Input tensor.
|
426
|
+
|
427
|
+
Returns:
|
428
|
+
(torch.Tensor): Output tensor.
|
429
|
+
"""
|
430
|
+
return self.act(self.conv(x))
|
431
|
+
|
432
|
+
def forward(self, x):
|
433
|
+
"""
|
434
|
+
Forward pass for training mode.
|
435
|
+
|
436
|
+
Args:
|
437
|
+
x (torch.Tensor): Input tensor.
|
438
|
+
|
439
|
+
Returns:
|
440
|
+
(torch.Tensor): Output tensor.
|
441
|
+
"""
|
442
|
+
id_out = 0 if self.bn is None else self.bn(x)
|
443
|
+
return self.act(self.conv1(x) + self.conv2(x) + id_out)
|
444
|
+
|
445
|
+
def get_equivalent_kernel_bias(self):
|
446
|
+
"""
|
447
|
+
Calculate equivalent kernel and bias by fusing convolutions.
|
448
|
+
|
449
|
+
Returns:
|
450
|
+
(torch.Tensor): Equivalent kernel
|
451
|
+
(torch.Tensor): Equivalent bias
|
452
|
+
"""
|
453
|
+
kernel3x3, bias3x3 = self._fuse_bn_tensor(self.conv1)
|
454
|
+
kernel1x1, bias1x1 = self._fuse_bn_tensor(self.conv2)
|
455
|
+
kernelid, biasid = self._fuse_bn_tensor(self.bn)
|
456
|
+
return kernel3x3 + self._pad_1x1_to_3x3_tensor(kernel1x1) + kernelid, bias3x3 + bias1x1 + biasid
|
457
|
+
|
458
|
+
@staticmethod
|
459
|
+
def _pad_1x1_to_3x3_tensor(kernel1x1):
|
460
|
+
"""
|
461
|
+
Pad a 1x1 kernel to 3x3 size.
|
462
|
+
|
463
|
+
Args:
|
464
|
+
kernel1x1 (torch.Tensor): 1x1 convolution kernel.
|
465
|
+
|
466
|
+
Returns:
|
467
|
+
(torch.Tensor): Padded 3x3 kernel.
|
468
|
+
"""
|
469
|
+
if kernel1x1 is None:
|
470
|
+
return 0
|
471
|
+
else:
|
472
|
+
return torch.nn.functional.pad(kernel1x1, [1, 1, 1, 1])
|
473
|
+
|
474
|
+
def _fuse_bn_tensor(self, branch):
|
475
|
+
"""
|
476
|
+
Fuse batch normalization with convolution weights.
|
477
|
+
|
478
|
+
Args:
|
479
|
+
branch (Conv | nn.BatchNorm2d | None): Branch to fuse.
|
480
|
+
|
481
|
+
Returns:
|
482
|
+
(torch.Tensor): Fused kernel
|
483
|
+
(torch.Tensor): Fused bias
|
484
|
+
"""
|
485
|
+
if branch is None:
|
486
|
+
return 0, 0
|
487
|
+
if isinstance(branch, Conv):
|
488
|
+
kernel = branch.conv.weight
|
489
|
+
running_mean = branch.bn.running_mean
|
490
|
+
running_var = branch.bn.running_var
|
491
|
+
gamma = branch.bn.weight
|
492
|
+
beta = branch.bn.bias
|
493
|
+
eps = branch.bn.eps
|
494
|
+
elif isinstance(branch, nn.BatchNorm2d):
|
495
|
+
if not hasattr(self, "id_tensor"):
|
496
|
+
input_dim = self.c1 // self.g
|
497
|
+
kernel_value = np.zeros((self.c1, input_dim, 3, 3), dtype=np.float32)
|
498
|
+
for i in range(self.c1):
|
499
|
+
kernel_value[i, i % input_dim, 1, 1] = 1
|
500
|
+
self.id_tensor = torch.from_numpy(kernel_value).to(branch.weight.device)
|
501
|
+
kernel = self.id_tensor
|
502
|
+
running_mean = branch.running_mean
|
503
|
+
running_var = branch.running_var
|
504
|
+
gamma = branch.weight
|
505
|
+
beta = branch.bias
|
506
|
+
eps = branch.eps
|
507
|
+
std = (running_var + eps).sqrt()
|
508
|
+
t = (gamma / std).reshape(-1, 1, 1, 1)
|
509
|
+
return kernel * t, beta - running_mean * gamma / std
|
510
|
+
|
511
|
+
def fuse_convs(self):
|
512
|
+
"""Fuse convolutions for inference by creating a single equivalent convolution."""
|
513
|
+
if hasattr(self, "conv"):
|
514
|
+
return
|
515
|
+
kernel, bias = self.get_equivalent_kernel_bias()
|
516
|
+
self.conv = nn.Conv2d(
|
517
|
+
in_channels=self.conv1.conv.in_channels,
|
518
|
+
out_channels=self.conv1.conv.out_channels,
|
519
|
+
kernel_size=self.conv1.conv.kernel_size,
|
520
|
+
stride=self.conv1.conv.stride,
|
521
|
+
padding=self.conv1.conv.padding,
|
522
|
+
dilation=self.conv1.conv.dilation,
|
523
|
+
groups=self.conv1.conv.groups,
|
524
|
+
bias=True,
|
525
|
+
).requires_grad_(False)
|
526
|
+
self.conv.weight.data = kernel
|
527
|
+
self.conv.bias.data = bias
|
528
|
+
for para in self.parameters():
|
529
|
+
para.detach_()
|
530
|
+
self.__delattr__("conv1")
|
531
|
+
self.__delattr__("conv2")
|
532
|
+
if hasattr(self, "nm"):
|
533
|
+
self.__delattr__("nm")
|
534
|
+
if hasattr(self, "bn"):
|
535
|
+
self.__delattr__("bn")
|
536
|
+
if hasattr(self, "id_tensor"):
|
537
|
+
self.__delattr__("id_tensor")
|
538
|
+
|
539
|
+
|
540
|
+
class ChannelAttention(nn.Module):
|
541
|
+
"""
|
542
|
+
Channel-attention module for feature recalibration.
|
543
|
+
|
544
|
+
Applies attention weights to channels based on global average pooling.
|
545
|
+
|
546
|
+
Attributes:
|
547
|
+
pool (nn.AdaptiveAvgPool2d): Global average pooling.
|
548
|
+
fc (nn.Conv2d): Fully connected layer implemented as 1x1 convolution.
|
549
|
+
act (nn.Sigmoid): Sigmoid activation for attention weights.
|
550
|
+
|
551
|
+
References:
|
552
|
+
https://github.com/open-mmlab/mmdetection/tree/v3.0.0rc1/configs/rtmdet
|
553
|
+
"""
|
554
|
+
|
555
|
+
def __init__(self, channels: int) -> None:
|
556
|
+
"""
|
557
|
+
Initialize Channel-attention module.
|
558
|
+
|
559
|
+
Args:
|
560
|
+
channels (int): Number of input channels.
|
561
|
+
"""
|
562
|
+
super().__init__()
|
563
|
+
self.pool = nn.AdaptiveAvgPool2d(1)
|
564
|
+
self.fc = nn.Conv2d(channels, channels, 1, 1, 0, bias=True)
|
565
|
+
self.act = nn.Sigmoid()
|
566
|
+
|
567
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
568
|
+
"""
|
569
|
+
Apply channel attention to input tensor.
|
570
|
+
|
571
|
+
Args:
|
572
|
+
x (torch.Tensor): Input tensor.
|
573
|
+
|
574
|
+
Returns:
|
575
|
+
(torch.Tensor): Channel-attended output tensor.
|
576
|
+
"""
|
577
|
+
return x * self.act(self.fc(self.pool(x)))
|
578
|
+
|
579
|
+
|
580
|
+
class SpatialAttention(nn.Module):
|
581
|
+
"""
|
582
|
+
Spatial-attention module for feature recalibration.
|
583
|
+
|
584
|
+
Applies attention weights to spatial dimensions based on channel statistics.
|
585
|
+
|
586
|
+
Attributes:
|
587
|
+
cv1 (nn.Conv2d): Convolution layer for spatial attention.
|
588
|
+
act (nn.Sigmoid): Sigmoid activation for attention weights.
|
589
|
+
"""
|
590
|
+
|
591
|
+
def __init__(self, kernel_size=7):
|
592
|
+
"""
|
593
|
+
Initialize Spatial-attention module.
|
594
|
+
|
595
|
+
Args:
|
596
|
+
kernel_size (int): Size of the convolutional kernel (3 or 7).
|
597
|
+
"""
|
598
|
+
super().__init__()
|
599
|
+
assert kernel_size in {3, 7}, "kernel size must be 3 or 7"
|
600
|
+
padding = 3 if kernel_size == 7 else 1
|
601
|
+
self.cv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)
|
602
|
+
self.act = nn.Sigmoid()
|
603
|
+
|
604
|
+
def forward(self, x):
|
605
|
+
"""
|
606
|
+
Apply spatial attention to input tensor.
|
607
|
+
|
608
|
+
Args:
|
609
|
+
x (torch.Tensor): Input tensor.
|
610
|
+
|
611
|
+
Returns:
|
612
|
+
(torch.Tensor): Spatial-attended output tensor.
|
613
|
+
"""
|
614
|
+
return x * self.act(self.cv1(torch.cat([torch.mean(x, 1, keepdim=True), torch.max(x, 1, keepdim=True)[0]], 1)))
|
615
|
+
|
616
|
+
|
617
|
+
class CBAM(nn.Module):
|
618
|
+
"""
|
619
|
+
Convolutional Block Attention Module.
|
620
|
+
|
621
|
+
Combines channel and spatial attention mechanisms for comprehensive feature refinement.
|
622
|
+
|
623
|
+
Attributes:
|
624
|
+
channel_attention (ChannelAttention): Channel attention module.
|
625
|
+
spatial_attention (SpatialAttention): Spatial attention module.
|
626
|
+
"""
|
627
|
+
|
628
|
+
def __init__(self, c1, kernel_size=7):
|
629
|
+
"""
|
630
|
+
Initialize CBAM with given parameters.
|
631
|
+
|
632
|
+
Args:
|
633
|
+
c1 (int): Number of input channels.
|
634
|
+
kernel_size (int): Size of the convolutional kernel for spatial attention.
|
635
|
+
"""
|
636
|
+
super().__init__()
|
637
|
+
self.channel_attention = ChannelAttention(c1)
|
638
|
+
self.spatial_attention = SpatialAttention(kernel_size)
|
639
|
+
|
640
|
+
def forward(self, x):
|
641
|
+
"""
|
642
|
+
Apply channel and spatial attention sequentially to input tensor.
|
643
|
+
|
644
|
+
Args:
|
645
|
+
x (torch.Tensor): Input tensor.
|
646
|
+
|
647
|
+
Returns:
|
648
|
+
(torch.Tensor): Attended output tensor.
|
649
|
+
"""
|
650
|
+
return self.spatial_attention(self.channel_attention(x))
|
651
|
+
|
652
|
+
|
653
|
+
class Concat(nn.Module):
|
654
|
+
"""
|
655
|
+
Concatenate a list of tensors along specified dimension.
|
656
|
+
|
657
|
+
Attributes:
|
658
|
+
d (int): Dimension along which to concatenate tensors.
|
659
|
+
"""
|
660
|
+
|
661
|
+
def __init__(self, dimension=1):
|
662
|
+
"""
|
663
|
+
Initialize Concat module.
|
664
|
+
|
665
|
+
Args:
|
666
|
+
dimension (int): Dimension along which to concatenate tensors.
|
667
|
+
"""
|
668
|
+
super().__init__()
|
669
|
+
self.d = dimension
|
670
|
+
|
671
|
+
def forward(self, x):
|
672
|
+
"""
|
673
|
+
Concatenate input tensors along specified dimension.
|
674
|
+
|
675
|
+
Args:
|
676
|
+
x (List[torch.Tensor]): List of input tensors.
|
677
|
+
|
678
|
+
Returns:
|
679
|
+
(torch.Tensor): Concatenated tensor.
|
680
|
+
"""
|
681
|
+
return torch.cat(x, self.d)
|
682
|
+
|
683
|
+
|
684
|
+
class Index(nn.Module):
|
685
|
+
"""
|
686
|
+
Returns a particular index of the input.
|
687
|
+
|
688
|
+
Attributes:
|
689
|
+
index (int): Index to select from input.
|
690
|
+
"""
|
691
|
+
|
692
|
+
def __init__(self, index=0):
|
693
|
+
"""
|
694
|
+
Initialize Index module.
|
695
|
+
|
696
|
+
Args:
|
697
|
+
index (int): Index to select from input.
|
698
|
+
"""
|
699
|
+
super().__init__()
|
700
|
+
self.index = index
|
701
|
+
|
702
|
+
def forward(self, x):
|
703
|
+
"""
|
704
|
+
Select and return a particular index from input.
|
705
|
+
|
706
|
+
Args:
|
707
|
+
x (List[torch.Tensor]): List of input tensors.
|
708
|
+
|
709
|
+
Returns:
|
710
|
+
(torch.Tensor): Selected tensor.
|
711
|
+
"""
|
712
|
+
return x[self.index]
|