hunterFormsBS 0.3.2__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.
- hunterFormsBS/__init__.py +30 -0
- hunterFormsBS/attend.py +570 -0
- hunterFormsBS/bandSplitRotator.py +830 -0
- hunterFormsBS/bands.py +118 -0
- hunterFormsBS/hyperACE.py +2496 -0
- hunterFormsBS/loss.py +157 -0
- hunterFormsBS/mask.py +391 -0
- hunterFormsBS/py.typed +0 -0
- hunterFormsBS/theTypes.py +313 -0
- hunterFormsBS/transform.py +305 -0
- hunterformsbs-0.3.2.dist-info/METADATA +388 -0
- hunterformsbs-0.3.2.dist-info/RECORD +17 -0
- hunterformsbs-0.3.2.dist-info/WHEEL +5 -0
- hunterformsbs-0.3.2.dist-info/licenses/LICENSE +407 -0
- hunterformsbs-0.3.2.dist-info/licenses/LICENSE-Solovyev +23 -0
- hunterformsbs-0.3.2.dist-info/licenses/LICENSE-Wang +23 -0
- hunterformsbs-0.3.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,2496 @@
|
|
|
1
|
+
# ruff: noqa: E741
|
|
2
|
+
"""Assemble a HyperACE-guided spectrogram branch for band-wise mask estimation.
|
|
3
|
+
|
|
4
|
+
You can use this module to build the optional spectrogram branch that augments
|
|
5
|
+
`hunterFormsBS.mask.MaskEstimator` [1] with a segmentation-style network. The module adapts the
|
|
6
|
+
Hypergraph-based Adaptive Correlation Enhancement (HyperACE) and FullPAD ideas from YOLOv13 [2] to
|
|
7
|
+
spectrogram features produced in the BS-RoFormer and Mel-RoFormer model family [3][4], following
|
|
8
|
+
unwa's BS-Roformer-HyperACE implementation [7]. It also uses TFC-TDF residual refinement blocks from
|
|
9
|
+
the music-source-separation literature [5][6].
|
|
10
|
+
|
|
11
|
+
Contents
|
|
12
|
+
--------
|
|
13
|
+
Functions
|
|
14
|
+
autopad
|
|
15
|
+
Compute same-style padding for scalar or per-axis kernel sizes.
|
|
16
|
+
|
|
17
|
+
Classes
|
|
18
|
+
AdaptiveHyperedgeGeneration
|
|
19
|
+
Generate a continuous vertex-to-hyperedge participation matrix from feature tokens.
|
|
20
|
+
AdaptiveHypergraphComputation
|
|
21
|
+
Apply adaptive hypergraph computation to one spatial feature map.
|
|
22
|
+
Backbone
|
|
23
|
+
Encode one spectrogram-like feature map into four progressively coarser stages.
|
|
24
|
+
C3AH
|
|
25
|
+
Combine a CSP-style split with adaptive hypergraph computation.
|
|
26
|
+
Conv
|
|
27
|
+
Apply one convolution-normalization-activation block to 2D features.
|
|
28
|
+
Decoder
|
|
29
|
+
Fuse encoder stages with HyperACE features and decode them toward the input resolution.
|
|
30
|
+
DS_Bottleneck
|
|
31
|
+
Apply two depthwise-separable convolution blocks with an optional residual shortcut.
|
|
32
|
+
DS_C3k
|
|
33
|
+
Apply a CSP-style depthwise-separable feature block.
|
|
34
|
+
DS_C3k2
|
|
35
|
+
Apply a lightweight C3k-style refinement block with one inner DS_C3k stage.
|
|
36
|
+
DSConv
|
|
37
|
+
Apply one depthwise-separable convolution-normalization-activation block to 2D features.
|
|
38
|
+
FreqPixelShuffle
|
|
39
|
+
Expand frequency resolution with a channel-to-frequency rearrangement.
|
|
40
|
+
GatedFusion
|
|
41
|
+
Fuse one decoder feature map with one resized HyperACE feature map.
|
|
42
|
+
HyperACE
|
|
43
|
+
Aggregate multi-scale encoder features with high-order and low-order branches.
|
|
44
|
+
HypergraphConvolution
|
|
45
|
+
Propagate features through hyperedges and back to vertices.
|
|
46
|
+
ProgressiveUpsampleHead
|
|
47
|
+
Recover target frequency resolution through repeated frequency-axis upsampling.
|
|
48
|
+
SegmModel
|
|
49
|
+
Assemble the full segmentation-style HyperACE branch used by the mask estimator.
|
|
50
|
+
TFC_TDF
|
|
51
|
+
Refine one feature map with residual TFC-TDF blocks.
|
|
52
|
+
|
|
53
|
+
References
|
|
54
|
+
----------
|
|
55
|
+
[1] `hunterFormsBS.mask.MaskEstimator`
|
|
56
|
+
|
|
57
|
+
[2] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
58
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive Visual
|
|
59
|
+
Perception. https://arxiv.org/abs/2506.17733
|
|
60
|
+
[3] Lu, W.-T., Wang, J.-C., Kong, Q., and Hung, Y.-N. (2023). Music Source Separation
|
|
61
|
+
with Band-Split RoPE Transformer. https://arxiv.org/abs/2309.02612
|
|
62
|
+
[4] Wang, J.-C., Lu, W.-T., and Chen, J. (2024). Mel-RoFormer for Vocal Separation and
|
|
63
|
+
Vocal Melody Transcription. https://arxiv.org/abs/2409.04702
|
|
64
|
+
[5] Chen, J., Wu, L., Suryotrisongko, H., and Kim, M. (2024). Music source separation
|
|
65
|
+
based on a lightweight deep learning framework (DTTNET: DUAL-PATH TFC-TDF UNET).
|
|
66
|
+
https://arxiv.org/abs/2309.08684
|
|
67
|
+
[6] Kim, M., Lee, J. H., and Jung, S. (2023). Sound Demixing Challenge 2023 Music
|
|
68
|
+
Demixing Track Technical Report: TFC-TDF-UNet v3. https://arxiv.org/abs/2306.09382
|
|
69
|
+
[7] unwa (pcunwa). (2025). BS-Roformer-HyperACE.
|
|
70
|
+
https://huggingface.co/pcunwa/BS-Roformer-HyperACE/resolve/main/v2_voc/bs_roformer.py
|
|
71
|
+
"""
|
|
72
|
+
from __future__ import annotations
|
|
73
|
+
|
|
74
|
+
from more_itertools import loops
|
|
75
|
+
from torch import nn, Tensor
|
|
76
|
+
from typing import cast, overload, TYPE_CHECKING
|
|
77
|
+
import torch
|
|
78
|
+
import torch.nn.functional as F
|
|
79
|
+
|
|
80
|
+
if TYPE_CHECKING:
|
|
81
|
+
from collections.abc import Callable
|
|
82
|
+
|
|
83
|
+
class Conv(nn.Module):
|
|
84
|
+
"""Apply one convolution-normalization-activation block to 2D features.
|
|
85
|
+
|
|
86
|
+
(AI generated docstring)
|
|
87
|
+
|
|
88
|
+
You can use `Conv` when one feature map needs one 2D convolution, one instance normalization
|
|
89
|
+
layer, and one activation layer in sequence [1]. `Conv` computes same-style padding with `autopad`
|
|
90
|
+
[2], applies the convolution, normalizes the projected features, and then applies the chosen
|
|
91
|
+
activation unless `act` disables it.
|
|
92
|
+
|
|
93
|
+
Mathematics
|
|
94
|
+
-----------
|
|
95
|
+
convolution block : equation
|
|
96
|
+
```
|
|
97
|
+
Let X ≜ `x`, C ≜ `self.conv`, N ≜ `self.bn`, σ ≜ `self.act`
|
|
98
|
+
|
|
99
|
+
Y = σ(N(C(X)))
|
|
100
|
+
|
|
101
|
+
where Y ≜ `transformedTensor`
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
References
|
|
105
|
+
----------
|
|
106
|
+
[1] PyTorch.
|
|
107
|
+
https://context7.com/pytorch/pytorch
|
|
108
|
+
[2] `autopad`
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
def __init__(self, c1: int, c2: int, k: int = 1, s: int = 1, p: int | None = None, g: int = 1
|
|
112
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
113
|
+
, norm_eps: float = 1e-8
|
|
114
|
+
, *
|
|
115
|
+
, act: bool = True
|
|
116
|
+
, norm_affine: bool = True, bias: bool = False) -> None:
|
|
117
|
+
"""Configure one convolution-normalization-activation block.
|
|
118
|
+
|
|
119
|
+
(AI generated docstring)
|
|
120
|
+
|
|
121
|
+
You can use `__init__` to choose the input width, output width, kernel size, stride, group
|
|
122
|
+
count, and activation family for one `Conv` block. `__init__` stores one `nn.Conv2d`, one
|
|
123
|
+
`nn.InstanceNorm2d`, and one activation module.
|
|
124
|
+
|
|
125
|
+
Parameters
|
|
126
|
+
----------
|
|
127
|
+
c1 : int
|
|
128
|
+
Input channel count.
|
|
129
|
+
c2 : int
|
|
130
|
+
Output channel count.
|
|
131
|
+
k : int = 1
|
|
132
|
+
Convolution kernel size.
|
|
133
|
+
s : int = 1
|
|
134
|
+
Convolution stride.
|
|
135
|
+
p : int | None = None
|
|
136
|
+
Explicit padding. When `p is None`, `autopad` computes same-style padding [1].
|
|
137
|
+
g : int = 1
|
|
138
|
+
Convolution group count.
|
|
139
|
+
activation : type[nn.Module] = nn.SiLU
|
|
140
|
+
Activation class instantiated after normalization when `act` is `True`.
|
|
141
|
+
norm_eps : float = 1e-8
|
|
142
|
+
Epsilon passed to `nn.InstanceNorm2d` [2].
|
|
143
|
+
act : bool = True
|
|
144
|
+
Whether to instantiate `activation` instead of `nn.Identity` [2].
|
|
145
|
+
norm_affine : bool = True
|
|
146
|
+
Whether instance normalization learns affine parameters.
|
|
147
|
+
bias : bool = False
|
|
148
|
+
Whether `nn.Conv2d` learns a bias term.
|
|
149
|
+
|
|
150
|
+
References
|
|
151
|
+
----------
|
|
152
|
+
[1] `autopad`
|
|
153
|
+
|
|
154
|
+
[2] PyTorch.
|
|
155
|
+
https://context7.com/pytorch/pytorch
|
|
156
|
+
"""
|
|
157
|
+
super().__init__()
|
|
158
|
+
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=bias)
|
|
159
|
+
self.bn = nn.InstanceNorm2d(c2, affine=norm_affine, eps=norm_eps)
|
|
160
|
+
self.act: nn.Module = activation() if act else nn.Identity()
|
|
161
|
+
|
|
162
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
163
|
+
"""Transform one 2D feature tensor with the stored convolution block.
|
|
164
|
+
|
|
165
|
+
(AI generated docstring)
|
|
166
|
+
|
|
167
|
+
You can use `forward` to map one feature tensor `x` through the stored convolution,
|
|
168
|
+
instance-normalization, and activation layers in `Conv`.
|
|
169
|
+
|
|
170
|
+
Parameters
|
|
171
|
+
----------
|
|
172
|
+
x : Tensor
|
|
173
|
+
Input tensor of shape `(batch, c1, height, width)`.
|
|
174
|
+
|
|
175
|
+
Returns
|
|
176
|
+
-------
|
|
177
|
+
transformedTensor : Tensor
|
|
178
|
+
Output tensor of shape `(batch, c2, height_out, width_out)`.
|
|
179
|
+
|
|
180
|
+
PyTorch
|
|
181
|
+
-------
|
|
182
|
+
tensor layout : `nn.Conv2d` and `nn.InstanceNorm2d`
|
|
183
|
+
`forward` expects `x` in `NCHW` layout because `Conv` stores one `nn.Conv2d` and one
|
|
184
|
+
`nn.InstanceNorm2d` [1]. The spatial size of `transformedTensor` follows the stride and
|
|
185
|
+
padding stored in `self.conv`.
|
|
186
|
+
|
|
187
|
+
References
|
|
188
|
+
----------
|
|
189
|
+
[1] PyTorch.
|
|
190
|
+
https://context7.com/pytorch/pytorch
|
|
191
|
+
"""
|
|
192
|
+
return self.act(self.bn(self.conv(x)))
|
|
193
|
+
|
|
194
|
+
@overload
|
|
195
|
+
def autopad(k: int, p: int | None = None) -> int: ...
|
|
196
|
+
@overload
|
|
197
|
+
def autopad(k: list[int], p: list[int] | None = None) -> list[int]: ...
|
|
198
|
+
def autopad(k: int | list[int], p: int | list[int] | None = None) -> int | list[int]:
|
|
199
|
+
"""Compute same-style padding for one kernel specification.
|
|
200
|
+
|
|
201
|
+
(AI generated docstring)
|
|
202
|
+
|
|
203
|
+
You can use `autopad` to derive one padding specification from one kernel specification when a
|
|
204
|
+
convolution block should preserve spatial size under unit stride. If `p` is already provided,
|
|
205
|
+
`autopad` returns it unchanged. Otherwise `autopad` returns `k // 2` for a scalar kernel size or
|
|
206
|
+
the per-axis half-width list for a list-valued kernel size.
|
|
207
|
+
|
|
208
|
+
Parameters
|
|
209
|
+
----------
|
|
210
|
+
k : int | list[int]
|
|
211
|
+
Kernel size specification.
|
|
212
|
+
p : int | list[int] | None = None
|
|
213
|
+
Explicit padding specification. When provided, `autopad` returns `p` unchanged.
|
|
214
|
+
|
|
215
|
+
Returns
|
|
216
|
+
-------
|
|
217
|
+
padding : int | list[int]
|
|
218
|
+
Padding specification compatible with `k`.
|
|
219
|
+
"""
|
|
220
|
+
if p is None:
|
|
221
|
+
if isinstance(k, int):
|
|
222
|
+
p = k // 2
|
|
223
|
+
else:
|
|
224
|
+
p = [x // 2 for x in k]
|
|
225
|
+
return p
|
|
226
|
+
|
|
227
|
+
class DSConv(nn.Module):
|
|
228
|
+
"""Apply one depthwise-separable convolution block to 2D features.
|
|
229
|
+
|
|
230
|
+
(AI generated docstring)
|
|
231
|
+
|
|
232
|
+
You can use `DSConv` when one feature map should be processed by one depthwise convolution, one
|
|
233
|
+
pointwise convolution, one instance normalization layer, and one activation layer [1]. The block
|
|
234
|
+
matches the depthwise-separable building unit used in the YOLOv13 DS-series design [2].
|
|
235
|
+
|
|
236
|
+
Mathematics
|
|
237
|
+
-----------
|
|
238
|
+
depthwise-separable block [2 at Equation (14)] : equation
|
|
239
|
+
```
|
|
240
|
+
Let X ≜ `x`, D ≜ `self.dwconv`, P ≜ `self.pwconv`,
|
|
241
|
+
N ≜ `self.bn`, σ ≜ `self.act`
|
|
242
|
+
|
|
243
|
+
Y = σ(N(P(D(X))))
|
|
244
|
+
|
|
245
|
+
where Y ≜ `transformedTensor`
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
References
|
|
249
|
+
----------
|
|
250
|
+
[1] PyTorch.
|
|
251
|
+
https://context7.com/pytorch/pytorch
|
|
252
|
+
[2] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
253
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
254
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
255
|
+
"""
|
|
256
|
+
|
|
257
|
+
def __init__(self, c1: int, c2: int, k: int = 3, s: int | tuple[int, int] = 1, p: int | None = None
|
|
258
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
259
|
+
, norm_eps: float = 1e-8
|
|
260
|
+
, *
|
|
261
|
+
# TODO Why not `activation: type[nn.Module] | None`? or `activation: type[nn.Module] = nn.Identity`?
|
|
262
|
+
, act: bool = True
|
|
263
|
+
, norm_affine: bool = True, bias: bool = False) -> None:
|
|
264
|
+
"""Configure one depthwise-separable convolution block.
|
|
265
|
+
|
|
266
|
+
(AI generated docstring)
|
|
267
|
+
|
|
268
|
+
You can use `__init__` to choose the channel counts, kernel size, stride, explicit padding,
|
|
269
|
+
and activation family for one `DSConv` block.
|
|
270
|
+
|
|
271
|
+
Parameters
|
|
272
|
+
----------
|
|
273
|
+
c1 : int
|
|
274
|
+
Input channel count.
|
|
275
|
+
c2 : int
|
|
276
|
+
Output channel count.
|
|
277
|
+
k : int = 3
|
|
278
|
+
Depthwise kernel size.
|
|
279
|
+
s : int | tuple[int, int] = 1
|
|
280
|
+
Depthwise stride.
|
|
281
|
+
p : int | None = None
|
|
282
|
+
Explicit depthwise padding. When `p is None`, `autopad` computes same-style padding [1].
|
|
283
|
+
activation : type[nn.Module] = nn.SiLU
|
|
284
|
+
Activation class instantiated after normalization when `act` is `True`.
|
|
285
|
+
norm_eps : float = 1e-8
|
|
286
|
+
Epsilon passed to `nn.InstanceNorm2d` [2].
|
|
287
|
+
act : bool = True
|
|
288
|
+
Whether to instantiate `activation` instead of `nn.Identity` [2].
|
|
289
|
+
norm_affine : bool = True
|
|
290
|
+
Whether instance normalization learns affine parameters.
|
|
291
|
+
bias : bool = False
|
|
292
|
+
Whether the depthwise and pointwise convolutions learn bias terms.
|
|
293
|
+
|
|
294
|
+
References
|
|
295
|
+
----------
|
|
296
|
+
[1] `autopad`
|
|
297
|
+
|
|
298
|
+
[2] PyTorch.
|
|
299
|
+
https://context7.com/pytorch/pytorch
|
|
300
|
+
"""
|
|
301
|
+
super().__init__()
|
|
302
|
+
self.dwconv = nn.Conv2d(c1, c1, k, s, autopad(k, p), groups=c1, bias=bias)
|
|
303
|
+
self.pwconv = nn.Conv2d(c1, c2, 1, 1, 0, bias=bias)
|
|
304
|
+
self.bn = nn.InstanceNorm2d(c2, affine=norm_affine, eps=norm_eps)
|
|
305
|
+
self.act: nn.Module = activation() if act else nn.Identity()
|
|
306
|
+
|
|
307
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
308
|
+
"""Transform one 2D feature tensor with depthwise-separable convolution.
|
|
309
|
+
|
|
310
|
+
(AI generated docstring)
|
|
311
|
+
|
|
312
|
+
You can use `forward` to apply the depthwise path, pointwise projection, instance
|
|
313
|
+
normalization, and activation stored in `DSConv`.
|
|
314
|
+
|
|
315
|
+
Parameters
|
|
316
|
+
----------
|
|
317
|
+
x : Tensor
|
|
318
|
+
Input tensor of shape `(batch, c1, height, width)`.
|
|
319
|
+
|
|
320
|
+
Returns
|
|
321
|
+
-------
|
|
322
|
+
transformedTensor : Tensor
|
|
323
|
+
Output tensor of shape `(batch, c2, height_out, width_out)`.
|
|
324
|
+
|
|
325
|
+
PyTorch
|
|
326
|
+
-------
|
|
327
|
+
depthwise-separable layout : grouped convolution followed by `1 × 1` projection
|
|
328
|
+
`forward` applies one grouped `nn.Conv2d` with `groups == c1`, one pointwise `nn.Conv2d`,
|
|
329
|
+
one `nn.InstanceNorm2d`, and one activation module in `NCHW` layout [1].
|
|
330
|
+
|
|
331
|
+
References
|
|
332
|
+
----------
|
|
333
|
+
[1] PyTorch.
|
|
334
|
+
https://context7.com/pytorch/pytorch
|
|
335
|
+
"""
|
|
336
|
+
return self.act(self.bn(self.pwconv(self.dwconv(x))))
|
|
337
|
+
|
|
338
|
+
class DS_Bottleneck(nn.Module):
|
|
339
|
+
"""Apply two depthwise-separable convolution blocks with an optional residual shortcut.
|
|
340
|
+
|
|
341
|
+
(AI generated docstring)
|
|
342
|
+
|
|
343
|
+
You can use `DS_Bottleneck` to stack one fixed `3 × 3` depthwise-separable block with one
|
|
344
|
+
configurable `k × k` depthwise-separable block [1]. When the input and output channel counts match
|
|
345
|
+
and `shortcut` is `True`, `DS_Bottleneck` adds the input feature map back to the output.
|
|
346
|
+
|
|
347
|
+
Mathematics
|
|
348
|
+
-----------
|
|
349
|
+
depthwise bottleneck [1 at Equation (15)] : equation
|
|
350
|
+
```
|
|
351
|
+
Let X ≜ `x`, D₁ ≜ `self.dsconv1`, D₂ ≜ `self.dsconv2`,
|
|
352
|
+
𝟙ₛ ≜ 1_{`self.shortcut`}
|
|
353
|
+
|
|
354
|
+
Z = D₂(D₁(X))
|
|
355
|
+
Y = Z + 𝟙ₛ X
|
|
356
|
+
|
|
357
|
+
where Y ≜ `refinedTensor`
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
References
|
|
361
|
+
----------
|
|
362
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
363
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
364
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
365
|
+
"""
|
|
366
|
+
|
|
367
|
+
def __init__(self, c1: int, c2: int, k: int = 3
|
|
368
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
369
|
+
, norm_eps: float = 1e-8
|
|
370
|
+
, *
|
|
371
|
+
, shortcut: bool = True
|
|
372
|
+
, norm_affine: bool = True, bias: bool = False) -> None:
|
|
373
|
+
"""Configure one depthwise-separable bottleneck block.
|
|
374
|
+
|
|
375
|
+
(AI generated docstring)
|
|
376
|
+
|
|
377
|
+
You can use `__init__` to choose the channel counts, large-kernel size, residual-shortcut
|
|
378
|
+
behavior, and activation family for one `DS_Bottleneck` block.
|
|
379
|
+
|
|
380
|
+
Parameters
|
|
381
|
+
----------
|
|
382
|
+
c1 : int
|
|
383
|
+
Input channel count.
|
|
384
|
+
c2 : int
|
|
385
|
+
Output channel count.
|
|
386
|
+
k : int = 3
|
|
387
|
+
Kernel size used by the second depthwise-separable block.
|
|
388
|
+
activation : type[nn.Module] = nn.SiLU
|
|
389
|
+
Activation class instantiated in both inner `DSConv` blocks.
|
|
390
|
+
norm_eps : float = 1e-8
|
|
391
|
+
Instance-normalization epsilon used in both inner `DSConv` blocks.
|
|
392
|
+
shortcut : bool = True
|
|
393
|
+
Whether to add the input feature map back when `c1 == c2`.
|
|
394
|
+
norm_affine : bool = True
|
|
395
|
+
Whether instance normalization learns affine parameters in both inner blocks.
|
|
396
|
+
bias : bool = False
|
|
397
|
+
Whether the inner convolutions learn bias terms.
|
|
398
|
+
"""
|
|
399
|
+
super().__init__()
|
|
400
|
+
c_: int = c1
|
|
401
|
+
self.dsconv1: DSConv = DSConv(c1, c_, k=3, s=1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=bias)
|
|
402
|
+
self.dsconv2: DSConv = DSConv(c_, c2, k=k, s=1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=bias)
|
|
403
|
+
self.shortcut: bool = shortcut and c1 == c2
|
|
404
|
+
|
|
405
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
406
|
+
"""Refine one 2D feature tensor with a two-step DS bottleneck.
|
|
407
|
+
|
|
408
|
+
(AI generated docstring)
|
|
409
|
+
|
|
410
|
+
You can use `forward` to pass one feature tensor `x` through the two stored `DSConv` blocks
|
|
411
|
+
and, when `self.shortcut` is enabled, add `x` back to the refined tensor.
|
|
412
|
+
|
|
413
|
+
Parameters
|
|
414
|
+
----------
|
|
415
|
+
x : Tensor
|
|
416
|
+
Input tensor of shape `(batch, c1, height, width)`.
|
|
417
|
+
|
|
418
|
+
Returns
|
|
419
|
+
-------
|
|
420
|
+
refinedTensor : Tensor
|
|
421
|
+
Output tensor of shape `(batch, c2, height, width)`.
|
|
422
|
+
|
|
423
|
+
PyTorch
|
|
424
|
+
-------
|
|
425
|
+
residual condition : channel-match shortcut
|
|
426
|
+
`forward` applies the residual addition only when `self.shortcut` is `True`, which
|
|
427
|
+
requires the channel counts chosen in `DS_Bottleneck.__init__` to make `x` and the refined
|
|
428
|
+
tensor shape-compatible.
|
|
429
|
+
|
|
430
|
+
References
|
|
431
|
+
----------
|
|
432
|
+
[1] `DSConv`
|
|
433
|
+
"""
|
|
434
|
+
return x + self.dsconv2(self.dsconv1(x)) if self.shortcut else self.dsconv2(self.dsconv1(x))
|
|
435
|
+
|
|
436
|
+
class DS_C3k(nn.Module):
|
|
437
|
+
"""Apply a CSP-style depthwise-separable feature block.
|
|
438
|
+
|
|
439
|
+
(AI generated docstring)
|
|
440
|
+
|
|
441
|
+
You can use `DS_C3k` to split one feature map into one transformed branch and one lateral branch,
|
|
442
|
+
process the transformed branch with repeated `DS_Bottleneck` blocks, concatenate both branches,
|
|
443
|
+
and fuse them back to the requested output width [1].
|
|
444
|
+
|
|
445
|
+
Mathematics
|
|
446
|
+
-----------
|
|
447
|
+
CSP depthwise branch [1 at Figure 4] : equation
|
|
448
|
+
```
|
|
449
|
+
Let X ≜ `x`, C₁ ≜ `self.cv1`, C₂ ≜ `self.cv2`,
|
|
450
|
+
C₃ ≜ `self.cv3`, B ≜ `self.m`
|
|
451
|
+
|
|
452
|
+
Y = C₃(Concat(B(C₁(X)), C₂(X)))
|
|
453
|
+
|
|
454
|
+
where Y ≜ `fusedTensor`
|
|
455
|
+
```
|
|
456
|
+
'Concat' _sic_ [1 at Figure 4]
|
|
457
|
+
|
|
458
|
+
References
|
|
459
|
+
----------
|
|
460
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
461
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
462
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
463
|
+
"""
|
|
464
|
+
|
|
465
|
+
def __init__(self, c1: int, c2: int, n: int = 1, k: int = 3, e: float = 0.5
|
|
466
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
467
|
+
, norm_eps: float = 1e-8
|
|
468
|
+
, *
|
|
469
|
+
, norm_affine: bool = True
|
|
470
|
+
, bias: bool = False) -> None:
|
|
471
|
+
"""Configure one `DS_C3k` block.
|
|
472
|
+
|
|
473
|
+
(AI generated docstring)
|
|
474
|
+
|
|
475
|
+
You can use `__init__` to choose the input width, output width, bottleneck repeat count,
|
|
476
|
+
large-kernel size, and expansion ratio for one `DS_C3k` block.
|
|
477
|
+
|
|
478
|
+
Parameters
|
|
479
|
+
----------
|
|
480
|
+
c1 : int
|
|
481
|
+
Input channel count.
|
|
482
|
+
c2 : int
|
|
483
|
+
Output channel count.
|
|
484
|
+
n : int = 1
|
|
485
|
+
Number of inner `DS_Bottleneck` blocks.
|
|
486
|
+
k : int = 3
|
|
487
|
+
Kernel size used by each inner `DS_Bottleneck` block.
|
|
488
|
+
e : float = 0.5
|
|
489
|
+
Expansion ratio used to compute the hidden width `int(c2 * e)`.
|
|
490
|
+
activation : type[nn.Module] = nn.SiLU
|
|
491
|
+
Activation class instantiated in all inner blocks.
|
|
492
|
+
norm_eps : float = 1e-8
|
|
493
|
+
Instance-normalization epsilon used in all inner blocks.
|
|
494
|
+
norm_affine : bool = True
|
|
495
|
+
Whether instance normalization learns affine parameters.
|
|
496
|
+
bias : bool = False
|
|
497
|
+
Whether the inner convolutions learn bias terms.
|
|
498
|
+
"""
|
|
499
|
+
super().__init__()
|
|
500
|
+
c_ = int(c2 * e)
|
|
501
|
+
self.cv1: Conv = Conv(c1, c_, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=bias)
|
|
502
|
+
self.cv2: Conv = Conv(c1, c_, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=bias)
|
|
503
|
+
self.cv3: Conv = Conv(2 * c_, c2, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=bias)
|
|
504
|
+
self.m = nn.Sequential(
|
|
505
|
+
*[
|
|
506
|
+
DS_Bottleneck(c_, c_, k=k, shortcut=True, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=bias)
|
|
507
|
+
for _index in loops(n)
|
|
508
|
+
]
|
|
509
|
+
)
|
|
510
|
+
|
|
511
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
512
|
+
"""Fuse one lateral branch with one repeated DS bottleneck branch.
|
|
513
|
+
|
|
514
|
+
(AI generated docstring)
|
|
515
|
+
|
|
516
|
+
You can use `forward` to split one feature tensor `x` into a transformed branch and a lateral
|
|
517
|
+
branch, concatenate both branches, and fuse the result back to the requested output width.
|
|
518
|
+
|
|
519
|
+
Parameters
|
|
520
|
+
----------
|
|
521
|
+
x : Tensor
|
|
522
|
+
Input tensor of shape `(batch, c1, height, width)`.
|
|
523
|
+
|
|
524
|
+
Returns
|
|
525
|
+
-------
|
|
526
|
+
fusedTensor : Tensor
|
|
527
|
+
Output tensor of shape `(batch, c2, height, width)`.
|
|
528
|
+
|
|
529
|
+
PyTorch
|
|
530
|
+
-------
|
|
531
|
+
channel fusion : `torch.cat(..., dim=1)`
|
|
532
|
+
`forward` keeps the spatial axes unchanged and concatenates the transformed branch with
|
|
533
|
+
the lateral branch along channel axis `1` before the final `1 × 1` fusion convolution [1].
|
|
534
|
+
|
|
535
|
+
References
|
|
536
|
+
----------
|
|
537
|
+
[1] PyTorch.
|
|
538
|
+
https://context7.com/pytorch/pytorch
|
|
539
|
+
"""
|
|
540
|
+
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), dim=1))
|
|
541
|
+
|
|
542
|
+
class DS_C3k2(nn.Module):
|
|
543
|
+
"""Apply a lightweight C3k-style refinement block with one inner `DS_C3k` stage.
|
|
544
|
+
|
|
545
|
+
(AI generated docstring)
|
|
546
|
+
|
|
547
|
+
You can use `DS_C3k2` when one feature map should first be projected to one hidden width, then
|
|
548
|
+
refined by one `DS_C3k` block, and finally fused back to the requested output width [1].
|
|
549
|
+
|
|
550
|
+
Mathematics
|
|
551
|
+
-----------
|
|
552
|
+
C3k2 refinement [1 at Figure 4] : equation
|
|
553
|
+
```
|
|
554
|
+
Let X ≜ `x`, C₁ ≜ `self.cv1`, B ≜ `self.m`, C₂ ≜ `self.cv2`
|
|
555
|
+
|
|
556
|
+
Z = C₁(X)
|
|
557
|
+
Y = C₂(B(Z))
|
|
558
|
+
|
|
559
|
+
where Y ≜ `refinedTensor`
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
References
|
|
563
|
+
----------
|
|
564
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
565
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
566
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
567
|
+
"""
|
|
568
|
+
|
|
569
|
+
def __init__(self, c1: int, c2: int, n: int = 1, k: int = 3, e: float = 0.5
|
|
570
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
571
|
+
, norm_eps: float = 1e-8
|
|
572
|
+
, *
|
|
573
|
+
, norm_affine: bool = True
|
|
574
|
+
, bias: bool = False) -> None:
|
|
575
|
+
"""Configure one `DS_C3k2` block.
|
|
576
|
+
|
|
577
|
+
(AI generated docstring)
|
|
578
|
+
|
|
579
|
+
You can use `__init__` to choose the input width, output width, inner repeat count,
|
|
580
|
+
large-kernel size, and expansion ratio for one `DS_C3k2` block.
|
|
581
|
+
|
|
582
|
+
Parameters
|
|
583
|
+
----------
|
|
584
|
+
c1 : int
|
|
585
|
+
Input channel count.
|
|
586
|
+
c2 : int
|
|
587
|
+
Output channel count.
|
|
588
|
+
n : int = 1
|
|
589
|
+
Repeat count used inside the inner `DS_C3k` block.
|
|
590
|
+
k : int = 3
|
|
591
|
+
Kernel size used inside the inner `DS_C3k` block.
|
|
592
|
+
e : float = 0.5
|
|
593
|
+
Expansion ratio used to compute the hidden width `int(c2 * e)`.
|
|
594
|
+
activation : type[nn.Module] = nn.SiLU
|
|
595
|
+
Activation class instantiated in all inner blocks.
|
|
596
|
+
norm_eps : float = 1e-8
|
|
597
|
+
Instance-normalization epsilon used in all inner blocks.
|
|
598
|
+
norm_affine : bool = True
|
|
599
|
+
Whether instance normalization learns affine parameters.
|
|
600
|
+
bias : bool = False
|
|
601
|
+
Whether the inner convolutions learn bias terms.
|
|
602
|
+
"""
|
|
603
|
+
super().__init__()
|
|
604
|
+
c_ = int(c2 * e)
|
|
605
|
+
self.cv1: Conv = Conv(c1, c_, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=bias)
|
|
606
|
+
self.m: DS_C3k = DS_C3k(c_, c_, n=n, k=k, e=1.0, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=bias)
|
|
607
|
+
self.cv2: Conv = Conv(c_, c2, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=bias)
|
|
608
|
+
|
|
609
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
610
|
+
"""Project, refine, and reproject one 2D feature tensor.
|
|
611
|
+
|
|
612
|
+
(AI generated docstring)
|
|
613
|
+
|
|
614
|
+
You can use `forward` to reduce the channel width of `x`, refine the reduced feature tensor
|
|
615
|
+
with the inner `DS_C3k` block, and project the refined tensor to the requested output width.
|
|
616
|
+
|
|
617
|
+
Parameters
|
|
618
|
+
----------
|
|
619
|
+
x : Tensor
|
|
620
|
+
Input tensor of shape `(batch, c1, height, width)`.
|
|
621
|
+
|
|
622
|
+
Returns
|
|
623
|
+
-------
|
|
624
|
+
refinedTensor : Tensor
|
|
625
|
+
Output tensor of shape `(batch, c2, height, width)`.
|
|
626
|
+
|
|
627
|
+
"""
|
|
628
|
+
x_ = self.cv1(x)
|
|
629
|
+
x_ = self.m(x_)
|
|
630
|
+
return self.cv2(x_)
|
|
631
|
+
|
|
632
|
+
class AdaptiveHyperedgeGeneration(nn.Module):
|
|
633
|
+
"""Generate a continuous vertex-to-hyperedge participation matrix from feature tokens.
|
|
634
|
+
|
|
635
|
+
(AI generated docstring)
|
|
636
|
+
|
|
637
|
+
You can use `AdaptiveHyperedgeGeneration` to summarize one spatial feature map into a set of
|
|
638
|
+
context-conditioned hyperedge prototypes and then compute soft vertex-to-hyperedge assignments for
|
|
639
|
+
every token [1]. The returned participation matrix is the adaptive incidence-like matrix used by
|
|
640
|
+
HyperACE before hypergraph message passing.
|
|
641
|
+
|
|
642
|
+
Mathematics
|
|
643
|
+
-----------
|
|
644
|
+
adaptive participation matrix [1 at Equations (1)-(4)] : equation
|
|
645
|
+
```
|
|
646
|
+
Let X ≜ `x`, B ≜ |X|₀, N ≜ |X|₁, C ≜ |X|₂,
|
|
647
|
+
M ≜ `self.num_hyperedges`, H ≜ `self.num_heads`, d ≜ C / H,
|
|
648
|
+
P⁰ ≜ `self.global_proto`,
|
|
649
|
+
Φ ≜ `self.context_mapper` (Φ : ℝ²ᶜ → ℝᴹˣᶜ),
|
|
650
|
+
Wᵠ ≜ `self.query_proj`
|
|
651
|
+
|
|
652
|
+
fᵃᵛᵍ[b, c] = (1 / N) ∑ᵢ X[b, i, c]
|
|
653
|
+
fᵐᵃˣ[b, c] = maxᵢ X[b, i, c]
|
|
654
|
+
fᶜᵗˣ[b] = [fᵃᵛᵍ[b] ‖ fᵐᵃˣ[b]]
|
|
655
|
+
P[b, m, :] = P⁰[m, :] + Φ(fᶜᵗˣ[b])[m, :]
|
|
656
|
+
Z[b, i, :] = Wᵠ X[b, i, :]
|
|
657
|
+
Sʰ[b, i, m] = ⟨Zʰ[b, i, :], Pʰ[b, m, :]⟩ / √d
|
|
658
|
+
S̄[b, i, m] = (1 / H) ∑ₕ Sʰ[b, i, m]
|
|
659
|
+
A[b, i, m] = exp(S̄[b, i, m]) / ∑ⱼ exp(S̄[b, j, m])
|
|
660
|
+
Aᵗ[b, m, i] = A[b, i, m]
|
|
661
|
+
|
|
662
|
+
where Aᵗ ≜ `participationMatrix`
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
References
|
|
666
|
+
----------
|
|
667
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
668
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
669
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
670
|
+
"""
|
|
671
|
+
|
|
672
|
+
def __init__(self, in_channels: int, num_hyperedges: int, num_heads: int = 8, *, linear_bias: bool = False) -> None:
|
|
673
|
+
"""Configure one adaptive hyperedge generator.
|
|
674
|
+
|
|
675
|
+
(AI generated docstring)
|
|
676
|
+
|
|
677
|
+
You can use `__init__` to choose the token width, the number of learned hyperedges, the number
|
|
678
|
+
of attention-style heads, and whether the internal linear layers learn bias terms.
|
|
679
|
+
|
|
680
|
+
Parameters
|
|
681
|
+
----------
|
|
682
|
+
in_channels : int
|
|
683
|
+
Token width `C`. `in_channels` should be divisible by `num_heads` because the projection
|
|
684
|
+
is reshaped into `num_heads` heads of width `C / num_heads`.
|
|
685
|
+
num_hyperedges : int
|
|
686
|
+
Number of adaptive hyperedges `M` used to summarize the token set.
|
|
687
|
+
num_heads : int = 8
|
|
688
|
+
Number of heads used to compute multi-head token-to-prototype similarity.
|
|
689
|
+
linear_bias : bool = False
|
|
690
|
+
Whether `context_mapper` and `query_proj` learn bias terms.
|
|
691
|
+
"""
|
|
692
|
+
super().__init__()
|
|
693
|
+
self.num_hyperedges: int = num_hyperedges
|
|
694
|
+
self.num_heads: int = num_heads
|
|
695
|
+
self.head_dim: int = in_channels // num_heads
|
|
696
|
+
|
|
697
|
+
self.global_proto: nn.Parameter = nn.Parameter(torch.randn(num_hyperedges, in_channels))
|
|
698
|
+
|
|
699
|
+
self.context_mapper: nn.Linear = nn.Linear(2 * in_channels, num_hyperedges * in_channels, bias=linear_bias)
|
|
700
|
+
|
|
701
|
+
self.query_proj: nn.Linear = nn.Linear(in_channels, in_channels, bias=linear_bias)
|
|
702
|
+
|
|
703
|
+
self.scale: float = self.head_dim**-0.5
|
|
704
|
+
|
|
705
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
706
|
+
"""Return adaptive hyperedge participation weights for one token set.
|
|
707
|
+
|
|
708
|
+
(AI generated docstring)
|
|
709
|
+
|
|
710
|
+
You can use `forward` to summarize one token tensor `x` into context-dependent hyperedge
|
|
711
|
+
prototypes and then compute one soft participation tensor that tells how strongly each token
|
|
712
|
+
participates in each hyperedge.
|
|
713
|
+
|
|
714
|
+
Parameters
|
|
715
|
+
----------
|
|
716
|
+
x : Tensor
|
|
717
|
+
Input tensor of shape `(batch, token_count, in_channels)`.
|
|
718
|
+
|
|
719
|
+
Returns
|
|
720
|
+
-------
|
|
721
|
+
participationMatrix : Tensor
|
|
722
|
+
Tensor of shape `(batch, num_hyperedges, token_count)`.
|
|
723
|
+
|
|
724
|
+
PyTorch
|
|
725
|
+
-------
|
|
726
|
+
returned layout : `torch.bmm`-ready storage
|
|
727
|
+
The HyperACE paper writes participation matrix `A` with vertex index first [1]. This
|
|
728
|
+
implementation permutes the result to `(batch, num_hyperedges, token_count)` so that
|
|
729
|
+
`torch.bmm(participationMatrix, x)` can aggregate one hyperedge feature tensor directly in
|
|
730
|
+
`HypergraphConvolution` [2].
|
|
731
|
+
|
|
732
|
+
References
|
|
733
|
+
----------
|
|
734
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
735
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
736
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
737
|
+
[2] `HypergraphConvolution`
|
|
738
|
+
"""
|
|
739
|
+
B, N, C = x.shape
|
|
740
|
+
|
|
741
|
+
f_avg: Tensor = F.adaptive_avg_pool1d(x.permute(0, 2, 1), 1).squeeze(-1)
|
|
742
|
+
f_max: Tensor = F.adaptive_max_pool1d(x.permute(0, 2, 1), 1).squeeze(-1)
|
|
743
|
+
f_ctx: Tensor = torch.cat((f_avg, f_max), dim=1)
|
|
744
|
+
|
|
745
|
+
delta_P = self.context_mapper(f_ctx).view(B, self.num_hyperedges, C)
|
|
746
|
+
P = self.global_proto.unsqueeze(0) + delta_P
|
|
747
|
+
|
|
748
|
+
z = self.query_proj(x)
|
|
749
|
+
|
|
750
|
+
z = z.view(B, N, self.num_heads, self.head_dim).permute(0, 2, 1, 3)
|
|
751
|
+
|
|
752
|
+
P = P.view(B, self.num_hyperedges, self.num_heads, self.head_dim).permute(0, 2, 3, 1)
|
|
753
|
+
|
|
754
|
+
sim = (z @ P) * self.scale
|
|
755
|
+
|
|
756
|
+
s_bar = sim.mean(dim=1)
|
|
757
|
+
|
|
758
|
+
A: Tensor = F.softmax(s_bar.permute(0, 2, 1), dim=-1)
|
|
759
|
+
|
|
760
|
+
return A
|
|
761
|
+
|
|
762
|
+
class HypergraphConvolution(nn.Module):
|
|
763
|
+
"""Propagate features through hyperedges and back to vertices.
|
|
764
|
+
|
|
765
|
+
(AI generated docstring)
|
|
766
|
+
|
|
767
|
+
You can use `HypergraphConvolution` after `AdaptiveHyperedgeGeneration` has produced a soft
|
|
768
|
+
vertex-to-hyperedge participation matrix. The block first aggregates vertex features into
|
|
769
|
+
hyperedge features and then projects those hyperedge features back to vertex space [1].
|
|
770
|
+
|
|
771
|
+
Mathematics
|
|
772
|
+
-----------
|
|
773
|
+
hypergraph message passing [1 at Equation (5)] : equation
|
|
774
|
+
```
|
|
775
|
+
Let X ≜ `x`, Aᵗ ≜ `A`, Wᵉ ≜ `self.W_e`,
|
|
776
|
+
Wᵛ ≜ `self.W_v`, σ ≜ `self.act`,
|
|
777
|
+
A[b, i, m] ≜ Aᵗ[b, m, i]
|
|
778
|
+
|
|
779
|
+
F[b, m, :] = σ(Wᵉ(∑ᵢ A[b, i, m] X[b, i, :]))
|
|
780
|
+
X̂[b, i, :] = σ(Wᵛ(∑ₘ A[b, i, m] F[b, m, :]))
|
|
781
|
+
Y = X + X̂
|
|
782
|
+
|
|
783
|
+
where Y ≜ `updatedTensor`
|
|
784
|
+
```
|
|
785
|
+
|
|
786
|
+
References
|
|
787
|
+
----------
|
|
788
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
789
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
790
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
791
|
+
"""
|
|
792
|
+
|
|
793
|
+
def __init__(self, in_channels: int, out_channels: int, activation: type[nn.Module] = nn.SiLU, *, linear_bias: bool = False) -> None:
|
|
794
|
+
"""Configure one hypergraph convolution block.
|
|
795
|
+
|
|
796
|
+
(AI generated docstring)
|
|
797
|
+
|
|
798
|
+
You can use `__init__` to choose the input width, output width, activation family, and bias
|
|
799
|
+
behavior for one `HypergraphConvolution` block.
|
|
800
|
+
|
|
801
|
+
Parameters
|
|
802
|
+
----------
|
|
803
|
+
in_channels : int
|
|
804
|
+
Input vertex-feature width.
|
|
805
|
+
out_channels : int
|
|
806
|
+
Output vertex-feature width produced by `W_v`. In the current residual implementation,
|
|
807
|
+
`out_channels` should match `in_channels` so that `x + x̂` is well-defined.
|
|
808
|
+
activation : type[nn.Module] = nn.SiLU
|
|
809
|
+
Activation class instantiated after both linear projections.
|
|
810
|
+
linear_bias : bool = False
|
|
811
|
+
Whether `W_e` and `W_v` learn bias terms.
|
|
812
|
+
"""
|
|
813
|
+
super().__init__()
|
|
814
|
+
self.W_e = nn.Linear(in_channels, in_channels, bias=linear_bias)
|
|
815
|
+
self.W_v = nn.Linear(in_channels, out_channels, bias=linear_bias)
|
|
816
|
+
self.act: nn.Module = activation()
|
|
817
|
+
|
|
818
|
+
def forward(self, x: Tensor, A: Tensor) -> Tensor:
|
|
819
|
+
"""Propagate one token set through hyperedges and back to vertices.
|
|
820
|
+
|
|
821
|
+
(AI generated docstring)
|
|
822
|
+
|
|
823
|
+
You can use `forward` to aggregate token features from `x` into hyperedge features using `A`,
|
|
824
|
+
project those hyperedge features back to token space, and add the projected update to `x`
|
|
825
|
+
residually.
|
|
826
|
+
|
|
827
|
+
Parameters
|
|
828
|
+
----------
|
|
829
|
+
x : Tensor
|
|
830
|
+
Input tensor of shape `(batch, token_count, in_channels)`.
|
|
831
|
+
A : Tensor
|
|
832
|
+
Participation tensor of shape `(batch, num_hyperedges, token_count)`.
|
|
833
|
+
|
|
834
|
+
Returns
|
|
835
|
+
-------
|
|
836
|
+
updatedTensor : Tensor
|
|
837
|
+
Tensor of shape `(batch, token_count, out_channels)`.
|
|
838
|
+
|
|
839
|
+
PyTorch
|
|
840
|
+
-------
|
|
841
|
+
batched matrix products : `torch.bmm`
|
|
842
|
+
`forward` uses `torch.bmm(A, x)` to gather one hyperedge feature tensor and uses
|
|
843
|
+
`torch.bmm(A.transpose(1, 2), f_m)` to distribute the hyperedge features back to token
|
|
844
|
+
space [1]. When `out_channels == in_channels`, the residual addition `x + x_out` is
|
|
845
|
+
shape-compatible.
|
|
846
|
+
|
|
847
|
+
References
|
|
848
|
+
----------
|
|
849
|
+
[1] PyTorch.
|
|
850
|
+
https://context7.com/pytorch/pytorch
|
|
851
|
+
"""
|
|
852
|
+
f_m: Tensor = torch.bmm(A, x)
|
|
853
|
+
f_m = self.act(self.W_e(f_m))
|
|
854
|
+
|
|
855
|
+
x_out: Tensor = torch.bmm(A.transpose(1, 2), f_m)
|
|
856
|
+
x_out = self.act(self.W_v(x_out))
|
|
857
|
+
|
|
858
|
+
return x + x_out
|
|
859
|
+
|
|
860
|
+
class AdaptiveHypergraphComputation(nn.Module):
|
|
861
|
+
"""Apply adaptive hypergraph computation to one spatial feature map.
|
|
862
|
+
|
|
863
|
+
(AI generated docstring)
|
|
864
|
+
|
|
865
|
+
You can use `AdaptiveHypergraphComputation` to flatten one spatial feature map into tokens,
|
|
866
|
+
compute adaptive hyperedge assignments with `AdaptiveHyperedgeGeneration`, apply
|
|
867
|
+
`HypergraphConvolution`, and then restore the original spatial layout.
|
|
868
|
+
|
|
869
|
+
Mathematics
|
|
870
|
+
-----------
|
|
871
|
+
spatial adaptive hypergraph [1 at Equations (1)-(5)] : transformation
|
|
872
|
+
```
|
|
873
|
+
Let X ≜ `x`, B ≜ |X|₀, C ≜ |X|₁, Hₛ ≜ |X|₂, Wₛ ≜ |X|₃,
|
|
874
|
+
G ≜ `self.adaptive_hyperedge_gen`, H ≜ `self.hypergraph_conv`
|
|
875
|
+
|
|
876
|
+
Xᵗ[b, i, c] = X[b, c, r, q], i = rWₛ + q
|
|
877
|
+
Aᵗ = G(Xᵗ)
|
|
878
|
+
Z = H(Xᵗ, Aᵗ)
|
|
879
|
+
Y[b, c, r, q] = Z[b, rWₛ + q, c]
|
|
880
|
+
|
|
881
|
+
where Y ≜ `refinedTensor`
|
|
882
|
+
```
|
|
883
|
+
|
|
884
|
+
References
|
|
885
|
+
----------
|
|
886
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
887
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
888
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
889
|
+
"""
|
|
890
|
+
|
|
891
|
+
def __init__(self, in_channels: int, out_channels: int, num_hyperedges: int = 8, num_heads: int = 8
|
|
892
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
893
|
+
, *
|
|
894
|
+
, linear_bias: bool = False) -> None:
|
|
895
|
+
"""Configure one spatial adaptive-hypergraph block.
|
|
896
|
+
|
|
897
|
+
(AI generated docstring)
|
|
898
|
+
|
|
899
|
+
You can use `__init__` to choose the token widths, the number of hyperedges, the number of
|
|
900
|
+
heads, and the activation family for one `AdaptiveHypergraphComputation` block.
|
|
901
|
+
|
|
902
|
+
Parameters
|
|
903
|
+
----------
|
|
904
|
+
in_channels : int
|
|
905
|
+
Input channel count before flattening.
|
|
906
|
+
out_channels : int
|
|
907
|
+
Output channel count after hypergraph propagation.
|
|
908
|
+
num_hyperedges : int = 8
|
|
909
|
+
Number of adaptive hyperedges used in `AdaptiveHyperedgeGeneration`.
|
|
910
|
+
num_heads : int = 8
|
|
911
|
+
Number of heads used in `AdaptiveHyperedgeGeneration`.
|
|
912
|
+
activation : type[nn.Module] = nn.SiLU
|
|
913
|
+
Activation class instantiated inside `HypergraphConvolution`.
|
|
914
|
+
linear_bias : bool = False
|
|
915
|
+
Whether the internal linear layers learn bias terms.
|
|
916
|
+
"""
|
|
917
|
+
super().__init__()
|
|
918
|
+
self.adaptive_hyperedge_gen = AdaptiveHyperedgeGeneration(in_channels, num_hyperedges, num_heads, linear_bias=linear_bias)
|
|
919
|
+
self.hypergraph_conv = HypergraphConvolution(in_channels, out_channels, activation=activation, linear_bias=linear_bias)
|
|
920
|
+
|
|
921
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
922
|
+
"""Apply adaptive hypergraph computation to one 2D feature tensor.
|
|
923
|
+
|
|
924
|
+
(AI generated docstring)
|
|
925
|
+
|
|
926
|
+
You can use `forward` to flatten one spatial feature tensor `x` into tokens, compute adaptive
|
|
927
|
+
hyperedge participation, propagate information through the hypergraph block, and restore the
|
|
928
|
+
original spatial layout.
|
|
929
|
+
|
|
930
|
+
Parameters
|
|
931
|
+
----------
|
|
932
|
+
x : Tensor
|
|
933
|
+
Input tensor of shape `(batch, in_channels, height, width)`.
|
|
934
|
+
|
|
935
|
+
Returns
|
|
936
|
+
-------
|
|
937
|
+
refinedTensor : Tensor
|
|
938
|
+
Output tensor of shape `(batch, out_channels, height, width)`.
|
|
939
|
+
|
|
940
|
+
PyTorch
|
|
941
|
+
-------
|
|
942
|
+
flatten-and-restore path : token view of one feature map
|
|
943
|
+
`forward` reshapes `x` from `NCHW` layout to `(batch, token_count, in_channels)` with
|
|
944
|
+
`token_count == height × width`, applies the adaptive hypergraph block, and then restores
|
|
945
|
+
`NCHW` layout by `permute` and `view` [1].
|
|
946
|
+
|
|
947
|
+
References
|
|
948
|
+
----------
|
|
949
|
+
[1] PyTorch.
|
|
950
|
+
https://context7.com/pytorch/pytorch
|
|
951
|
+
"""
|
|
952
|
+
B, _C, H, W = x.shape
|
|
953
|
+
x_flat: Tensor = x.flatten(2).permute(0, 2, 1)
|
|
954
|
+
|
|
955
|
+
A = self.adaptive_hyperedge_gen(x_flat)
|
|
956
|
+
|
|
957
|
+
x_out_flat = self.hypergraph_conv(x_flat, A)
|
|
958
|
+
|
|
959
|
+
return x_out_flat.permute(0, 2, 1).view(B, -1, H, W)
|
|
960
|
+
|
|
961
|
+
class C3AH(nn.Module):
|
|
962
|
+
"""Combine a CSP-style split with adaptive hypergraph computation.
|
|
963
|
+
|
|
964
|
+
(AI generated docstring)
|
|
965
|
+
|
|
966
|
+
You can use `C3AH` as the high-order correlation block inside `HyperACE`. `C3AH` splits one
|
|
967
|
+
feature map into one lateral branch and one adaptive-hypergraph branch, processes the second
|
|
968
|
+
branch with `AdaptiveHypergraphComputation`, concatenates both branches, and fuses them with a
|
|
969
|
+
final `1 × 1` projection [1].
|
|
970
|
+
|
|
971
|
+
Mathematics
|
|
972
|
+
-----------
|
|
973
|
+
C3AH split projection [1 at Equation (6)] : equation
|
|
974
|
+
```
|
|
975
|
+
Let X₀ ≜ `x`, C₁ ≜ `self.cv1`, C₂ ≜ `self.cv2`
|
|
976
|
+
|
|
977
|
+
Xˡ = C₁(X₀)
|
|
978
|
+
Xᵃ = C₂(X₀)
|
|
979
|
+
```
|
|
980
|
+
|
|
981
|
+
C3AH adaptive fusion [1 at Equation (7)] : equation
|
|
982
|
+
```
|
|
983
|
+
Let X₀ ≜ `x`, C₁ ≜ `self.cv1`, C₂ ≜ `self.cv2`,
|
|
984
|
+
H ≜ `self.ahc`, C₃ ≜ `self.cv3`
|
|
985
|
+
|
|
986
|
+
Xˡ = C₁(X₀)
|
|
987
|
+
Xᵃ = C₂(X₀)
|
|
988
|
+
Xʰ = H(Xᵃ)
|
|
989
|
+
Y = C₃(Concat(Xʰ, Xˡ))
|
|
990
|
+
|
|
991
|
+
where Y ≜ `fusedTensor`
|
|
992
|
+
```
|
|
993
|
+
'Concat' _sic_ [1 at Equations (9) and (11)]
|
|
994
|
+
|
|
995
|
+
References
|
|
996
|
+
----------
|
|
997
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
998
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
999
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
1000
|
+
"""
|
|
1001
|
+
|
|
1002
|
+
def __init__(
|
|
1003
|
+
self
|
|
1004
|
+
, c1: int
|
|
1005
|
+
, c2: int
|
|
1006
|
+
, num_hyperedges: int = 8
|
|
1007
|
+
, num_heads: int = 8
|
|
1008
|
+
, e: float = 0.5
|
|
1009
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
1010
|
+
, norm_eps: float = 1e-8
|
|
1011
|
+
, *
|
|
1012
|
+
, conv_bias: bool = False
|
|
1013
|
+
, linear_bias: bool = False
|
|
1014
|
+
, norm_affine: bool = True
|
|
1015
|
+
) -> None:
|
|
1016
|
+
"""Configure one `C3AH` block.
|
|
1017
|
+
|
|
1018
|
+
(AI generated docstring)
|
|
1019
|
+
|
|
1020
|
+
You can use `__init__` to choose the input width, output width, hypergraph size, expansion
|
|
1021
|
+
ratio, and normalization or bias settings for one `C3AH` block.
|
|
1022
|
+
|
|
1023
|
+
Parameters
|
|
1024
|
+
----------
|
|
1025
|
+
c1 : int
|
|
1026
|
+
Input channel count.
|
|
1027
|
+
c2 : int
|
|
1028
|
+
Output channel count after the final fusion projection.
|
|
1029
|
+
num_hyperedges : int = 8
|
|
1030
|
+
Number of adaptive hyperedges used inside `AdaptiveHypergraphComputation`.
|
|
1031
|
+
num_heads : int = 8
|
|
1032
|
+
Number of heads used inside `AdaptiveHyperedgeGeneration`.
|
|
1033
|
+
e : float = 0.5
|
|
1034
|
+
Expansion ratio used to compute the hidden branch width `int(c1 * e)`.
|
|
1035
|
+
activation : type[nn.Module] = nn.SiLU
|
|
1036
|
+
Activation class instantiated in all inner blocks.
|
|
1037
|
+
norm_eps : float = 1e-8
|
|
1038
|
+
Instance-normalization epsilon used in the inner `Conv` blocks.
|
|
1039
|
+
norm_affine : bool = True
|
|
1040
|
+
Whether instance normalization learns affine parameters.
|
|
1041
|
+
conv_bias : bool = False
|
|
1042
|
+
Whether the inner convolution layers learn bias terms.
|
|
1043
|
+
linear_bias : bool = False
|
|
1044
|
+
Whether the linear layers in `AdaptiveHypergraphComputation` learn bias terms.
|
|
1045
|
+
"""
|
|
1046
|
+
super().__init__()
|
|
1047
|
+
c_ = int(c1 * e)
|
|
1048
|
+
self.cv1: Conv = Conv(c1, c_, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1049
|
+
self.cv2: Conv = Conv(c1, c_, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1050
|
+
self.ahc = AdaptiveHypergraphComputation(c_, c_, num_hyperedges, num_heads, activation=activation, linear_bias=linear_bias)
|
|
1051
|
+
self.cv3: Conv = Conv(2 * c_, c2, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1052
|
+
|
|
1053
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
1054
|
+
"""Fuse one lateral branch with one adaptive-hypergraph branch.
|
|
1055
|
+
|
|
1056
|
+
(AI generated docstring)
|
|
1057
|
+
|
|
1058
|
+
You can use `forward` to split one feature tensor `x` into a lateral branch and an
|
|
1059
|
+
adaptive-hypergraph branch, refine the second branch with `AdaptiveHypergraphComputation`, and
|
|
1060
|
+
fuse both branches by channel concatenation and one final `1 × 1` projection.
|
|
1061
|
+
|
|
1062
|
+
Parameters
|
|
1063
|
+
----------
|
|
1064
|
+
x : Tensor
|
|
1065
|
+
Input tensor of shape `(batch, c1, height, width)`.
|
|
1066
|
+
|
|
1067
|
+
Returns
|
|
1068
|
+
-------
|
|
1069
|
+
fusedTensor : Tensor
|
|
1070
|
+
Output tensor of shape `(batch, c2, height, width)`.
|
|
1071
|
+
|
|
1072
|
+
"""
|
|
1073
|
+
x_lateral = self.cv1(x)
|
|
1074
|
+
x_ahc = self.ahc(self.cv2(x))
|
|
1075
|
+
return self.cv3(torch.cat((x_ahc, x_lateral), dim=1))
|
|
1076
|
+
|
|
1077
|
+
class HyperACE(nn.Module):
|
|
1078
|
+
"""Aggregate multi-scale encoder features with high-order and low-order branches.
|
|
1079
|
+
|
|
1080
|
+
(AI generated docstring)
|
|
1081
|
+
|
|
1082
|
+
You can use `HyperACE` to fuse multiple encoder stages, split the fused tensor into high-order,
|
|
1083
|
+
low-order, and shortcut channels, refine those branches independently, and fuse them back into one
|
|
1084
|
+
decoder-conditioning feature map [1]. In this audio adaptation, the block consumes four encoder
|
|
1085
|
+
stages rather than the three backbone stages used in the vision paper.
|
|
1086
|
+
|
|
1087
|
+
Mathematics
|
|
1088
|
+
-----------
|
|
1089
|
+
audio stage fusion : transformation
|
|
1090
|
+
```
|
|
1091
|
+
Let [B₂, B₃, B₄, B₅] ≜ `x`, Rᵢ ≜ Resize(· → size(B₄)),
|
|
1092
|
+
Cᶠ ≜ `self.fuse_conv`
|
|
1093
|
+
|
|
1094
|
+
Xᵇ = Cᶠ(Concat(R₂(B₂), R₃(B₃), B₄, R₅(B₅)))
|
|
1095
|
+
[Xᵇʰ, Xᵇˡ, Xˢ] = split_channel(Xᵇ; `self.c_h`, `self.c_l`, `self.c_s`)
|
|
1096
|
+
```
|
|
1097
|
+
|
|
1098
|
+
high-order branches [1 at Equation (8)] : equation
|
|
1099
|
+
```
|
|
1100
|
+
Let K ≜ |`self.high_order_branch`|,
|
|
1101
|
+
Hₖ ≜ `self.high_order_branch[k]`, k ∈ {0, …, K−1}
|
|
1102
|
+
|
|
1103
|
+
Xʰₖ = Hₖ(Xᵇʰ)
|
|
1104
|
+
```
|
|
1105
|
+
|
|
1106
|
+
high-order fusion [1 at Equation (9)] : equation
|
|
1107
|
+
```
|
|
1108
|
+
Let Cʰ ≜ `self.high_order_fuse`
|
|
1109
|
+
|
|
1110
|
+
Xʰ = Cʰ(Concat(Xʰ₀, …, Xʰ_{K−1}))
|
|
1111
|
+
```
|
|
1112
|
+
'Concat' _sic_ [1 at Equation (9)]
|
|
1113
|
+
|
|
1114
|
+
low-order branch [1 at Equation (10)] : equation
|
|
1115
|
+
```
|
|
1116
|
+
Let L ≜ `self.low_order_branch`
|
|
1117
|
+
|
|
1118
|
+
Xˡ = L(Xᵇˡ)
|
|
1119
|
+
```
|
|
1120
|
+
|
|
1121
|
+
final fusion [1 at Equation (11)] : equation
|
|
1122
|
+
```
|
|
1123
|
+
Let Cʸ ≜ `self.final_fuse`
|
|
1124
|
+
|
|
1125
|
+
Y = Cʸ(Concat(Xʰ, Xˡ, Xˢ))
|
|
1126
|
+
|
|
1127
|
+
where Y ≜ `conditioningTensor`
|
|
1128
|
+
```
|
|
1129
|
+
'Concat' _sic_ [1 at Equation (11)]
|
|
1130
|
+
|
|
1131
|
+
Implementation boundary
|
|
1132
|
+
-----------------------
|
|
1133
|
+
audio-stage adaptation : one fused spectrogram stage
|
|
1134
|
+
YOLOv13 applies HyperACE to three image-backbone stages [1]. This implementation first resizes
|
|
1135
|
+
four encoder stages `B2`, `B3`, `B4`, and `B5` to the spatial resolution of `B4`, fuses them
|
|
1136
|
+
with one `1 × 1` convolution, and then applies the same branch decomposition.
|
|
1137
|
+
|
|
1138
|
+
References
|
|
1139
|
+
----------
|
|
1140
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
1141
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
1142
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
1143
|
+
"""
|
|
1144
|
+
|
|
1145
|
+
def __init__(
|
|
1146
|
+
self
|
|
1147
|
+
, in_channels: list[int]
|
|
1148
|
+
, out_channels: int
|
|
1149
|
+
, num_hyperedges: int = 8
|
|
1150
|
+
, num_heads: int = 8
|
|
1151
|
+
, k: int = 2
|
|
1152
|
+
, l: int = 1
|
|
1153
|
+
, c_h: float = 0.5
|
|
1154
|
+
, c_l: float = 0.25
|
|
1155
|
+
, c3ah_expansion: float = 1.0
|
|
1156
|
+
, low_order_depth: int = 1
|
|
1157
|
+
, low_order_kernel: int = 3
|
|
1158
|
+
, low_order_expansion: float = 1.0
|
|
1159
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
1160
|
+
, norm_eps: float = 1e-8
|
|
1161
|
+
, *
|
|
1162
|
+
, conv_bias: bool = False
|
|
1163
|
+
, linear_bias: bool = False
|
|
1164
|
+
, norm_affine: bool = True
|
|
1165
|
+
) -> None:
|
|
1166
|
+
"""Configure one HyperACE aggregation block.
|
|
1167
|
+
|
|
1168
|
+
(AI generated docstring)
|
|
1169
|
+
|
|
1170
|
+
You can use `__init__` to choose the encoder-stage widths, output width, adaptive-hypergraph
|
|
1171
|
+
size, branch depths, branch width ratios, and normalization or bias settings for one
|
|
1172
|
+
`HyperACE` block.
|
|
1173
|
+
|
|
1174
|
+
Parameters
|
|
1175
|
+
----------
|
|
1176
|
+
in_channels : list[int]
|
|
1177
|
+
Channel counts for the four encoder stages `[B2, B3, B4, B5]`.
|
|
1178
|
+
out_channels : int
|
|
1179
|
+
Output channel count after the final fusion projection.
|
|
1180
|
+
num_hyperedges : int = 8
|
|
1181
|
+
Number of adaptive hyperedges used in every `C3AH` block.
|
|
1182
|
+
num_heads : int = 8
|
|
1183
|
+
Number of heads used in every `AdaptiveHyperedgeGeneration` block.
|
|
1184
|
+
k : int = 2
|
|
1185
|
+
Number of parallel high-order `C3AH` branches.
|
|
1186
|
+
l : int = 1
|
|
1187
|
+
Number of repeated low-order `DS_C3k` blocks.
|
|
1188
|
+
c_h : float = 0.5
|
|
1189
|
+
Fraction of the fused mid-level width assigned to the high-order branch.
|
|
1190
|
+
c_l : float = 0.25
|
|
1191
|
+
Fraction of the fused mid-level width assigned to the low-order branch.
|
|
1192
|
+
c3ah_expansion : float = 1.0
|
|
1193
|
+
Expansion ratio used inside every `C3AH` block.
|
|
1194
|
+
low_order_depth : int = 1
|
|
1195
|
+
Inner `DS_Bottleneck` repeat count used by each low-order `DS_C3k` block.
|
|
1196
|
+
low_order_kernel : int = 3
|
|
1197
|
+
Kernel size used by each low-order `DS_Bottleneck` block.
|
|
1198
|
+
low_order_expansion : float = 1.0
|
|
1199
|
+
Expansion ratio used by each low-order `DS_C3k` block.
|
|
1200
|
+
activation : type[nn.Module] = nn.SiLU
|
|
1201
|
+
Activation class instantiated in all inner blocks.
|
|
1202
|
+
norm_eps : float = 1e-8
|
|
1203
|
+
Instance-normalization epsilon used in the inner convolution blocks.
|
|
1204
|
+
norm_affine : bool = True
|
|
1205
|
+
Whether instance normalization learns affine parameters.
|
|
1206
|
+
conv_bias : bool = False
|
|
1207
|
+
Whether the convolution layers learn bias terms.
|
|
1208
|
+
linear_bias : bool = False
|
|
1209
|
+
Whether the linear layers inside the adaptive hypergraph blocks learn bias terms.
|
|
1210
|
+
|
|
1211
|
+
Raises
|
|
1212
|
+
------
|
|
1213
|
+
ValueError
|
|
1214
|
+
Raised when the channel split implied by `c_h` and `c_l` leaves no positive shortcut
|
|
1215
|
+
width.
|
|
1216
|
+
"""
|
|
1217
|
+
super().__init__()
|
|
1218
|
+
|
|
1219
|
+
c2, c3, c4, c5 = in_channels
|
|
1220
|
+
c_mid: int = c4
|
|
1221
|
+
|
|
1222
|
+
self.fuse_conv: Conv = Conv(
|
|
1223
|
+
c2 + c3 + c4 + c5, c_mid, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias
|
|
1224
|
+
)
|
|
1225
|
+
|
|
1226
|
+
self.c_h = int(c_mid * c_h)
|
|
1227
|
+
self.c_l = int(c_mid * c_l)
|
|
1228
|
+
self.c_s: int = c_mid - self.c_h - self.c_l
|
|
1229
|
+
if self.c_s <= 0:
|
|
1230
|
+
message: str = (
|
|
1231
|
+
f"I computed `{self.c_s = }`, indicative of a channel split problem, from `{c_mid = }`, `{c_h = }`, `{c_l = }`, "
|
|
1232
|
+
f"`{self.c_h = }`, and `{self.c_l = }`, but I need `self.c_s` to be greater than 0."
|
|
1233
|
+
)
|
|
1234
|
+
raise ValueError(message)
|
|
1235
|
+
|
|
1236
|
+
self.high_order_branch = nn.ModuleList(
|
|
1237
|
+
[
|
|
1238
|
+
C3AH(
|
|
1239
|
+
self.c_h
|
|
1240
|
+
, self.c_h
|
|
1241
|
+
, num_hyperedges
|
|
1242
|
+
, num_heads
|
|
1243
|
+
, e=c3ah_expansion
|
|
1244
|
+
, activation=activation
|
|
1245
|
+
, norm_eps=norm_eps
|
|
1246
|
+
, norm_affine=norm_affine
|
|
1247
|
+
, conv_bias=conv_bias
|
|
1248
|
+
, linear_bias=linear_bias
|
|
1249
|
+
)
|
|
1250
|
+
for _index in loops(k)
|
|
1251
|
+
]
|
|
1252
|
+
)
|
|
1253
|
+
self.high_order_fuse: Conv = Conv(
|
|
1254
|
+
self.c_h * k, self.c_h, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias
|
|
1255
|
+
)
|
|
1256
|
+
|
|
1257
|
+
self.low_order_branch = nn.Sequential(
|
|
1258
|
+
*[
|
|
1259
|
+
DS_C3k(
|
|
1260
|
+
self.c_l
|
|
1261
|
+
, self.c_l
|
|
1262
|
+
, n=low_order_depth
|
|
1263
|
+
, k=low_order_kernel
|
|
1264
|
+
, e=low_order_expansion
|
|
1265
|
+
, activation=activation
|
|
1266
|
+
, norm_eps=norm_eps
|
|
1267
|
+
, norm_affine=norm_affine
|
|
1268
|
+
, bias=conv_bias
|
|
1269
|
+
)
|
|
1270
|
+
for _index in loops(l)
|
|
1271
|
+
]
|
|
1272
|
+
)
|
|
1273
|
+
|
|
1274
|
+
self.final_fuse: Conv = Conv(
|
|
1275
|
+
self.c_h + self.c_l + self.c_s
|
|
1276
|
+
, out_channels
|
|
1277
|
+
, 1
|
|
1278
|
+
, 1
|
|
1279
|
+
, activation=activation
|
|
1280
|
+
, norm_eps=norm_eps
|
|
1281
|
+
, norm_affine=norm_affine
|
|
1282
|
+
, bias=conv_bias
|
|
1283
|
+
)
|
|
1284
|
+
|
|
1285
|
+
def forward(self, x: list[Tensor]) -> Tensor:
|
|
1286
|
+
"""Return one HyperACE-conditioned fusion of four encoder stages.
|
|
1287
|
+
|
|
1288
|
+
(AI generated docstring)
|
|
1289
|
+
|
|
1290
|
+
You can use `forward` to resize four encoder-stage tensors to one shared mid-level spatial
|
|
1291
|
+
resolution, split the fused tensor into high-order, low-order, and shortcut branches, and fuse
|
|
1292
|
+
the branch outputs into one conditioning tensor for the decoder.
|
|
1293
|
+
|
|
1294
|
+
Parameters
|
|
1295
|
+
----------
|
|
1296
|
+
x : list[Tensor]
|
|
1297
|
+
List of encoder tensors `[B2, B3, B4, B5]`.
|
|
1298
|
+
|
|
1299
|
+
Returns
|
|
1300
|
+
-------
|
|
1301
|
+
conditioningTensor : Tensor
|
|
1302
|
+
Tensor of shape `(batch, out_channels, B4_height, B4_width)`.
|
|
1303
|
+
|
|
1304
|
+
PyTorch
|
|
1305
|
+
-------
|
|
1306
|
+
stage alignment : `F.interpolate(..., mode='bilinear', align_corners=False)`
|
|
1307
|
+
`forward` resizes `B2`, `B3`, and `B5` to the spatial resolution of `B4` before channel
|
|
1308
|
+
concatenation [1]. The returned `conditioningTensor` therefore lives on the `B4` spatial
|
|
1309
|
+
grid.
|
|
1310
|
+
|
|
1311
|
+
References
|
|
1312
|
+
----------
|
|
1313
|
+
[1] PyTorch.
|
|
1314
|
+
https://context7.com/pytorch/pytorch
|
|
1315
|
+
"""
|
|
1316
|
+
B2, B3, B4, B5 = x
|
|
1317
|
+
|
|
1318
|
+
_B, _C, H4, W4 = B4.shape
|
|
1319
|
+
|
|
1320
|
+
B2_resized: Tensor = F.interpolate(B2, size=(H4, W4), mode='bilinear', align_corners=False)
|
|
1321
|
+
B3_resized: Tensor = F.interpolate(B3, size=(H4, W4), mode='bilinear', align_corners=False)
|
|
1322
|
+
B5_resized: Tensor = F.interpolate(B5, size=(H4, W4), mode='bilinear', align_corners=False)
|
|
1323
|
+
|
|
1324
|
+
x_b = self.fuse_conv(torch.cat((B2_resized, B3_resized, B4, B5_resized), dim=1))
|
|
1325
|
+
|
|
1326
|
+
x_h, x_l, x_s = torch.split(x_b, [self.c_h, self.c_l, self.c_s], dim=1)
|
|
1327
|
+
|
|
1328
|
+
x_h_outs: list[Tensor] = [m(x_h) for m in self.high_order_branch]
|
|
1329
|
+
x_h_fused = self.high_order_fuse(torch.cat(x_h_outs, dim=1))
|
|
1330
|
+
|
|
1331
|
+
x_l_out = self.low_order_branch(x_l)
|
|
1332
|
+
|
|
1333
|
+
return self.final_fuse(torch.cat((x_h_fused, x_l_out, x_s), dim=1))
|
|
1334
|
+
|
|
1335
|
+
class GatedFusion(nn.Module):
|
|
1336
|
+
"""Fuse one feature map with one resized HyperACE feature map.
|
|
1337
|
+
|
|
1338
|
+
(AI generated docstring)
|
|
1339
|
+
|
|
1340
|
+
You can use `GatedFusion` when one decoder feature map should absorb one resized HyperACE-guided
|
|
1341
|
+
conditioning feature map through a learnable residual gate [1].
|
|
1342
|
+
|
|
1343
|
+
Mathematics
|
|
1344
|
+
-----------
|
|
1345
|
+
FullPAD gated fusion [1 at Equation (13)] : equation
|
|
1346
|
+
```
|
|
1347
|
+
Let Fᵢ ≜ `f_in`, Hᵢ ≜ `h`, Γ ≜ `self.gamma`,
|
|
1348
|
+
Γ ∈ ℝ^{1×C×1×1}
|
|
1349
|
+
|
|
1350
|
+
F̃ᵢ = Fᵢ + Γ ⊙ Hᵢ
|
|
1351
|
+
|
|
1352
|
+
where F̃ᵢ ≜ `fusedTensor`
|
|
1353
|
+
```
|
|
1354
|
+
|
|
1355
|
+
References
|
|
1356
|
+
----------
|
|
1357
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
1358
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
1359
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
1360
|
+
"""
|
|
1361
|
+
|
|
1362
|
+
def __init__(self, in_channels: int) -> None:
|
|
1363
|
+
"""Configure one gated-fusion layer.
|
|
1364
|
+
|
|
1365
|
+
(AI generated docstring)
|
|
1366
|
+
|
|
1367
|
+
You can use `__init__` to choose the channel count of the broadcastable gate parameter.
|
|
1368
|
+
|
|
1369
|
+
Parameters
|
|
1370
|
+
----------
|
|
1371
|
+
in_channels : int
|
|
1372
|
+
Channel count shared by the decoder feature map and the resized HyperACE feature map.
|
|
1373
|
+
"""
|
|
1374
|
+
super().__init__()
|
|
1375
|
+
self.gamma = nn.Parameter(torch.zeros(1, in_channels, 1, 1))
|
|
1376
|
+
|
|
1377
|
+
def forward(self, f_in: Tensor, h: Tensor) -> Tensor:
|
|
1378
|
+
"""Add one gated conditioning tensor to one decoder tensor.
|
|
1379
|
+
|
|
1380
|
+
(AI generated docstring)
|
|
1381
|
+
|
|
1382
|
+
You can use `forward` to inject one conditioning tensor `h` into one decoder feature tensor
|
|
1383
|
+
`f_in` through the broadcastable gate stored in `self.gamma`.
|
|
1384
|
+
|
|
1385
|
+
Parameters
|
|
1386
|
+
----------
|
|
1387
|
+
f_in : Tensor
|
|
1388
|
+
Decoder feature tensor of shape `(batch, channels, height, width)`.
|
|
1389
|
+
h : Tensor
|
|
1390
|
+
Conditioning tensor of shape `(batch, channels, height, width)`.
|
|
1391
|
+
|
|
1392
|
+
Returns
|
|
1393
|
+
-------
|
|
1394
|
+
fusedTensor : Tensor
|
|
1395
|
+
Tensor of shape `(batch, channels, height, width)`.
|
|
1396
|
+
|
|
1397
|
+
Raises
|
|
1398
|
+
------
|
|
1399
|
+
ValueError
|
|
1400
|
+
Raised when `f_in.shape[1]` and `h.shape[1]` differ.
|
|
1401
|
+
|
|
1402
|
+
PyTorch
|
|
1403
|
+
-------
|
|
1404
|
+
broadcast gate : parameter tensor of shape `(1, channels, 1, 1)`
|
|
1405
|
+
`forward` evaluates `f_in + self.gamma * h`, so `self.gamma` is broadcast across the batch
|
|
1406
|
+
axis and both spatial axes [1].
|
|
1407
|
+
|
|
1408
|
+
References
|
|
1409
|
+
----------
|
|
1410
|
+
[1] PyTorch.
|
|
1411
|
+
https://context7.com/pytorch/pytorch
|
|
1412
|
+
[2] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
1413
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
1414
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
1415
|
+
""" # noqa: DOC502
|
|
1416
|
+
if f_in.shape[1] != h.shape[1]:
|
|
1417
|
+
message: str = (
|
|
1418
|
+
f"I received `{f_in.shape = }` and `{h.shape = }`, but I need the number of channels to match, so "
|
|
1419
|
+
f"`{f_in.shape[1] = }` to equal `{h.shape[1] = }`."
|
|
1420
|
+
)
|
|
1421
|
+
raise ValueError(message)
|
|
1422
|
+
return f_in + self.gamma * h
|
|
1423
|
+
|
|
1424
|
+
class Backbone(nn.Module):
|
|
1425
|
+
"""Encode one spectrogram-like feature map into four progressively coarser stages.
|
|
1426
|
+
|
|
1427
|
+
(AI generated docstring)
|
|
1428
|
+
|
|
1429
|
+
You can use `Backbone` to convert one `(channels, time, bands)` feature map into four encoder
|
|
1430
|
+
stages with progressively smaller spatial support and progressively larger channel width. The
|
|
1431
|
+
block uses the DS-series convolution blocks that HyperACE adopts from YOLOv13 [1].
|
|
1432
|
+
|
|
1433
|
+
Mathematics
|
|
1434
|
+
-----------
|
|
1435
|
+
encoder recurrence : equation
|
|
1436
|
+
```
|
|
1437
|
+
Let X₀ ≜ `x`, S ≜ `self.stem`, P₂ ≜ `self.p2`,
|
|
1438
|
+
P₃ ≜ `self.p3`, P₄ ≜ `self.p4`, P₅ ≜ `self.p5`
|
|
1439
|
+
|
|
1440
|
+
X₁ = S(X₀)
|
|
1441
|
+
B₂ = P₂(X₁)
|
|
1442
|
+
B₃ = P₃(B₂)
|
|
1443
|
+
B₄ = P₄(B₃)
|
|
1444
|
+
B₅ = P₅(B₄)
|
|
1445
|
+
|
|
1446
|
+
where [B₂, B₃, B₄, B₅] ≜ `encoderStages`
|
|
1447
|
+
```
|
|
1448
|
+
|
|
1449
|
+
References
|
|
1450
|
+
----------
|
|
1451
|
+
[1] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
1452
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
1453
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
1454
|
+
"""
|
|
1455
|
+
|
|
1456
|
+
def __init__(
|
|
1457
|
+
self
|
|
1458
|
+
, in_channels: int = 256
|
|
1459
|
+
, base_channels: int = 64
|
|
1460
|
+
, base_depth: int = 3
|
|
1461
|
+
, channels: tuple[int, int, int, int, int] | None = None
|
|
1462
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
1463
|
+
, norm_eps: float = 1e-8
|
|
1464
|
+
, *
|
|
1465
|
+
, norm_affine: bool = True
|
|
1466
|
+
, conv_bias: bool = False
|
|
1467
|
+
) -> None:
|
|
1468
|
+
"""Configure one encoder backbone.
|
|
1469
|
+
|
|
1470
|
+
(AI generated docstring)
|
|
1471
|
+
|
|
1472
|
+
You can use `__init__` to choose the input width, the stage widths, the per-stage refinement
|
|
1473
|
+
depth, and the shared normalization or bias settings for one `Backbone` block.
|
|
1474
|
+
|
|
1475
|
+
Parameters
|
|
1476
|
+
----------
|
|
1477
|
+
in_channels : int = 256
|
|
1478
|
+
Input channel count of the spectrogram feature map.
|
|
1479
|
+
base_channels : int = 64
|
|
1480
|
+
Stem output width used when `channels` is `None`.
|
|
1481
|
+
base_depth : int = 3
|
|
1482
|
+
Base refinement depth. The second and third encoder stages use `2 × base_depth`.
|
|
1483
|
+
channels : tuple[int, int, int, int, int] | None = None
|
|
1484
|
+
Explicit stage widths `(c2, c3, c4, c5, c6)`. When omitted, `Backbone` uses the default
|
|
1485
|
+
progression derived from `base_channels`.
|
|
1486
|
+
activation : type[nn.Module] = nn.SiLU
|
|
1487
|
+
Activation class instantiated in all inner blocks.
|
|
1488
|
+
norm_eps : float = 1e-8
|
|
1489
|
+
Instance-normalization epsilon used in all inner convolution blocks.
|
|
1490
|
+
norm_affine : bool = True
|
|
1491
|
+
Whether instance normalization learns affine parameters.
|
|
1492
|
+
conv_bias : bool = False
|
|
1493
|
+
Whether the inner convolution layers learn bias terms.
|
|
1494
|
+
"""
|
|
1495
|
+
super().__init__()
|
|
1496
|
+
if channels is None:
|
|
1497
|
+
c2: int = base_channels
|
|
1498
|
+
# TODO Ought these values be user-configured, computed, or hardcoded?
|
|
1499
|
+
c3: int = 256
|
|
1500
|
+
c4: int = 384
|
|
1501
|
+
c5: int = 512
|
|
1502
|
+
c6: int = 768
|
|
1503
|
+
else:
|
|
1504
|
+
c2, c3, c4, c5, c6 = channels
|
|
1505
|
+
|
|
1506
|
+
self.stem: DSConv = DSConv(
|
|
1507
|
+
in_channels, c2, k=3, s=(2, 1), p=1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias
|
|
1508
|
+
)
|
|
1509
|
+
|
|
1510
|
+
self.p2 = nn.Sequential(
|
|
1511
|
+
DSConv(c2, c3, k=3, s=(2, 1), p=1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias),
|
|
1512
|
+
DS_C3k2(c3, c3, n=base_depth, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias),
|
|
1513
|
+
)
|
|
1514
|
+
|
|
1515
|
+
self.p3 = nn.Sequential(
|
|
1516
|
+
DSConv(c3, c4, k=3, s=(2, 1), p=1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias),
|
|
1517
|
+
DS_C3k2(c4, c4, n=base_depth * 2, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias),
|
|
1518
|
+
)
|
|
1519
|
+
|
|
1520
|
+
self.p4 = nn.Sequential(
|
|
1521
|
+
DSConv(c4, c5, k=3, s=2, p=1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias),
|
|
1522
|
+
DS_C3k2(c5, c5, n=base_depth * 2, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias),
|
|
1523
|
+
)
|
|
1524
|
+
|
|
1525
|
+
self.p5 = nn.Sequential(
|
|
1526
|
+
DSConv(c5, c6, k=3, s=2, p=1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias),
|
|
1527
|
+
DS_C3k2(c6, c6, n=base_depth, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias),
|
|
1528
|
+
)
|
|
1529
|
+
|
|
1530
|
+
self.out_channels: list[int] = [c3, c4, c5, c6]
|
|
1531
|
+
|
|
1532
|
+
def forward(self, x: Tensor) -> list[Tensor]:
|
|
1533
|
+
"""Return four progressively coarser encoder stages.
|
|
1534
|
+
|
|
1535
|
+
(AI generated docstring)
|
|
1536
|
+
|
|
1537
|
+
You can use `forward` to encode one band-split feature tensor `x` into the four stage tensors
|
|
1538
|
+
consumed later by `HyperACE` and `Decoder`.
|
|
1539
|
+
|
|
1540
|
+
Parameters
|
|
1541
|
+
----------
|
|
1542
|
+
x : Tensor
|
|
1543
|
+
Input tensor of shape `(batch, in_channels, time_frames, in_bands)`.
|
|
1544
|
+
|
|
1545
|
+
Returns
|
|
1546
|
+
-------
|
|
1547
|
+
encoderStages : list[Tensor]
|
|
1548
|
+
List `[p2, p3, p4, p5]` of tensors in `NCHW` layout.
|
|
1549
|
+
|
|
1550
|
+
PyTorch
|
|
1551
|
+
-------
|
|
1552
|
+
stage order : `p2`, `p3`, `p4`, `p5`
|
|
1553
|
+
`forward` returns the stage tensors in shallow-to-deep order so downstream code can unpack
|
|
1554
|
+
the list deterministically as `[p2, p3, p4, p5]`.
|
|
1555
|
+
|
|
1556
|
+
References
|
|
1557
|
+
----------
|
|
1558
|
+
[1] `HyperACE`
|
|
1559
|
+
|
|
1560
|
+
[2] `Decoder`
|
|
1561
|
+
"""
|
|
1562
|
+
x = self.stem(x)
|
|
1563
|
+
x2 = self.p2(x)
|
|
1564
|
+
x3 = self.p3(x2)
|
|
1565
|
+
x4 = self.p4(x3)
|
|
1566
|
+
x5 = self.p5(x4)
|
|
1567
|
+
return [x2, x3, x4, x5]
|
|
1568
|
+
|
|
1569
|
+
class Decoder(nn.Module):
|
|
1570
|
+
"""Fuse encoder stages with HyperACE features and decode them toward the input resolution.
|
|
1571
|
+
|
|
1572
|
+
(AI generated docstring)
|
|
1573
|
+
|
|
1574
|
+
You can use `Decoder` to combine the four encoder stages with one shared HyperACE feature map,
|
|
1575
|
+
decode those features from coarse to fine resolution, and return one refined feature map at the
|
|
1576
|
+
`p2` stage resolution. The block mixes DTTNet-style multi-stage decoding [1] with HyperACE's
|
|
1577
|
+
FullPAD-style gated feature injection [2].
|
|
1578
|
+
|
|
1579
|
+
Mathematics
|
|
1580
|
+
-----------
|
|
1581
|
+
FullPAD projection [2 at Equation (12)] : equation
|
|
1582
|
+
```
|
|
1583
|
+
Let Y ≜ `h_ace`, Rᵢ ≜ Resize(· → size(Fᵢ)),
|
|
1584
|
+
Pᵢ ∈ {`self.h_to_d5`, `self.h_to_d4`, `self.h_to_d3`, `self.h_to_d2`}
|
|
1585
|
+
|
|
1586
|
+
Hᵢ = Pᵢ(Rᵢ(Y))
|
|
1587
|
+
```
|
|
1588
|
+
'Resize' _sic_ [2 at Equation (12)]
|
|
1589
|
+
|
|
1590
|
+
FullPAD gate [2 at Equation (13)] : equation
|
|
1591
|
+
```
|
|
1592
|
+
Let Γᵢ ∈ {`self.fusion_d5.gamma`, `self.fusion_d4.gamma`,
|
|
1593
|
+
`self.fusion_d3.gamma`, `self.fusion_d2.gamma`}
|
|
1594
|
+
|
|
1595
|
+
F̃ᵢ = Fᵢ + Γᵢ ⊙ Hᵢ
|
|
1596
|
+
```
|
|
1597
|
+
|
|
1598
|
+
coarse-to-fine decoder : recurrence
|
|
1599
|
+
```
|
|
1600
|
+
Let [P₂, P₃, P₄, P₅] ≜ `enc_feats`,
|
|
1601
|
+
Sᵢ ∈ {`self.skip_p5`, `self.skip_p4`, `self.skip_p3`, `self.skip_p2`},
|
|
1602
|
+
Uᵢ ∈ {`self.up_d5`, `self.up_d4`, `self.up_d3`},
|
|
1603
|
+
Gᵢ ∈ {`self.fusion_d5`, `self.fusion_d4`, `self.fusion_d3`, `self.fusion_d2`}
|
|
1604
|
+
|
|
1605
|
+
D₅ = G₅(S₅(P₅), H₅)
|
|
1606
|
+
D₄ = G₄(U₅(Resize(D₅ → size(P₄))) + S₄(P₄), H₄)
|
|
1607
|
+
D₃ = G₃(U₄(Resize(D₄ → size(P₃))) + S₃(P₃), H₃)
|
|
1608
|
+
D₂ = G₂(U₃(Resize(D₃ → size(P₂))) + S₂(P₂), H₂)
|
|
1609
|
+
Y = `self.final_d2`(D₂)
|
|
1610
|
+
|
|
1611
|
+
where Y ≜ `decodedTensor`
|
|
1612
|
+
```
|
|
1613
|
+
'Resize' _sic_ [2 at Equation (12)]
|
|
1614
|
+
|
|
1615
|
+
References
|
|
1616
|
+
----------
|
|
1617
|
+
[1] Chen, J., Wu, L., Suryotrisongko, H., and Kim, M. (2024). Music source separation
|
|
1618
|
+
based on a lightweight deep learning framework (DTTNET: DUAL-PATH TFC-TDF UNET).
|
|
1619
|
+
https://arxiv.org/abs/2309.08684
|
|
1620
|
+
[2] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
1621
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
1622
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
1623
|
+
"""
|
|
1624
|
+
|
|
1625
|
+
def __init__(
|
|
1626
|
+
self
|
|
1627
|
+
, encoder_channels: list[int]
|
|
1628
|
+
, hyperace_out_c: int
|
|
1629
|
+
, decoder_channels: list[int]
|
|
1630
|
+
, block_depth: int = 1
|
|
1631
|
+
, block_kernel: int = 3
|
|
1632
|
+
, block_expansion: float = 0.5
|
|
1633
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
1634
|
+
, norm_eps: float = 1e-8
|
|
1635
|
+
, *
|
|
1636
|
+
, conv_bias: bool = False
|
|
1637
|
+
, norm_affine: bool = True
|
|
1638
|
+
) -> None:
|
|
1639
|
+
"""Configure one coarse-to-fine decoder.
|
|
1640
|
+
|
|
1641
|
+
(AI generated docstring)
|
|
1642
|
+
|
|
1643
|
+
You can use `__init__` to choose the encoder widths, HyperACE conditioning width, decoder
|
|
1644
|
+
widths, inner DS-C3k refinement depth, and shared normalization or bias settings for one
|
|
1645
|
+
`Decoder` block.
|
|
1646
|
+
|
|
1647
|
+
Parameters
|
|
1648
|
+
----------
|
|
1649
|
+
encoder_channels : list[int]
|
|
1650
|
+
Channel counts of the four encoder stages `[p2, p3, p4, p5]`.
|
|
1651
|
+
hyperace_out_c : int
|
|
1652
|
+
Channel count of the HyperACE conditioning feature map.
|
|
1653
|
+
decoder_channels : list[int]
|
|
1654
|
+
Decoder stage widths `[d2, d3, d4, d5]`.
|
|
1655
|
+
block_depth : int = 1
|
|
1656
|
+
Inner `DS_Bottleneck` repeat count used by each decoder `DS_C3k2` block.
|
|
1657
|
+
block_kernel : int = 3
|
|
1658
|
+
Kernel size used by each decoder `DS_Bottleneck` block.
|
|
1659
|
+
block_expansion : float = 0.5
|
|
1660
|
+
Expansion ratio used by each decoder `DS_C3k2` block.
|
|
1661
|
+
activation : type[nn.Module] = nn.SiLU
|
|
1662
|
+
Activation class instantiated in all inner blocks.
|
|
1663
|
+
norm_eps : float = 1e-8
|
|
1664
|
+
Instance-normalization epsilon used in the inner convolution blocks.
|
|
1665
|
+
norm_affine : bool = True
|
|
1666
|
+
Whether instance normalization learns affine parameters.
|
|
1667
|
+
conv_bias : bool = False
|
|
1668
|
+
Whether the inner convolution layers learn bias terms.
|
|
1669
|
+
"""
|
|
1670
|
+
super().__init__()
|
|
1671
|
+
c_p2, c_p3, c_p4, c_p5 = encoder_channels
|
|
1672
|
+
c_d2, c_d3, c_d4, c_d5 = decoder_channels
|
|
1673
|
+
|
|
1674
|
+
self.h_to_d5: Conv = Conv(hyperace_out_c, c_d5, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1675
|
+
self.h_to_d4: Conv = Conv(hyperace_out_c, c_d4, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1676
|
+
self.h_to_d3: Conv = Conv(hyperace_out_c, c_d3, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1677
|
+
self.h_to_d2: Conv = Conv(hyperace_out_c, c_d2, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1678
|
+
|
|
1679
|
+
self.fusion_d5: GatedFusion = GatedFusion(c_d5)
|
|
1680
|
+
self.fusion_d4: GatedFusion = GatedFusion(c_d4)
|
|
1681
|
+
self.fusion_d3: GatedFusion = GatedFusion(c_d3)
|
|
1682
|
+
self.fusion_d2: GatedFusion = GatedFusion(c_d2)
|
|
1683
|
+
|
|
1684
|
+
self.skip_p5: Conv = Conv(c_p5, c_d5, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1685
|
+
self.skip_p4: Conv = Conv(c_p4, c_d4, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1686
|
+
self.skip_p3: Conv = Conv(c_p3, c_d3, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1687
|
+
self.skip_p2: Conv = Conv(c_p2, c_d2, 1, 1, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias)
|
|
1688
|
+
|
|
1689
|
+
self.up_d5: DS_C3k2 = DS_C3k2(
|
|
1690
|
+
c_d5
|
|
1691
|
+
, c_d4
|
|
1692
|
+
, n=block_depth
|
|
1693
|
+
, k=block_kernel
|
|
1694
|
+
, e=block_expansion
|
|
1695
|
+
, activation=activation
|
|
1696
|
+
, norm_eps=norm_eps
|
|
1697
|
+
, norm_affine=norm_affine
|
|
1698
|
+
, bias=conv_bias
|
|
1699
|
+
)
|
|
1700
|
+
self.up_d4: DS_C3k2 = DS_C3k2(
|
|
1701
|
+
c_d4
|
|
1702
|
+
, c_d3
|
|
1703
|
+
, n=block_depth
|
|
1704
|
+
, k=block_kernel
|
|
1705
|
+
, e=block_expansion
|
|
1706
|
+
, activation=activation
|
|
1707
|
+
, norm_eps=norm_eps
|
|
1708
|
+
, norm_affine=norm_affine
|
|
1709
|
+
, bias=conv_bias
|
|
1710
|
+
)
|
|
1711
|
+
self.up_d3: DS_C3k2 = DS_C3k2(
|
|
1712
|
+
c_d3
|
|
1713
|
+
, c_d2
|
|
1714
|
+
, n=block_depth
|
|
1715
|
+
, k=block_kernel
|
|
1716
|
+
, e=block_expansion
|
|
1717
|
+
, activation=activation
|
|
1718
|
+
, norm_eps=norm_eps
|
|
1719
|
+
, norm_affine=norm_affine
|
|
1720
|
+
, bias=conv_bias
|
|
1721
|
+
)
|
|
1722
|
+
|
|
1723
|
+
self.final_d2: DS_C3k2 = DS_C3k2(
|
|
1724
|
+
c_d2
|
|
1725
|
+
, c_d2
|
|
1726
|
+
, n=block_depth
|
|
1727
|
+
, k=block_kernel
|
|
1728
|
+
, e=block_expansion
|
|
1729
|
+
, activation=activation
|
|
1730
|
+
, norm_eps=norm_eps
|
|
1731
|
+
, norm_affine=norm_affine
|
|
1732
|
+
, bias=conv_bias
|
|
1733
|
+
)
|
|
1734
|
+
|
|
1735
|
+
def forward(self, enc_feats: list[Tensor], h_ace: Tensor) -> Tensor:
|
|
1736
|
+
"""Decode four encoder stages with shared HyperACE conditioning.
|
|
1737
|
+
|
|
1738
|
+
(AI generated docstring)
|
|
1739
|
+
|
|
1740
|
+
You can use `forward` to combine the stage tensors in `enc_feats` with the conditioning tensor
|
|
1741
|
+
`h_ace`, decode the result from coarse to fine resolution, and return one refined tensor on
|
|
1742
|
+
the `p2` spatial grid.
|
|
1743
|
+
|
|
1744
|
+
Parameters
|
|
1745
|
+
----------
|
|
1746
|
+
enc_feats : list[Tensor]
|
|
1747
|
+
List `[p2, p3, p4, p5]` of encoder-stage tensors.
|
|
1748
|
+
h_ace : Tensor
|
|
1749
|
+
Conditioning tensor produced by `HyperACE`.
|
|
1750
|
+
|
|
1751
|
+
Returns
|
|
1752
|
+
-------
|
|
1753
|
+
decodedTensor : Tensor
|
|
1754
|
+
Tensor on the `p2` spatial grid with `decoder_channels[0]` channels.
|
|
1755
|
+
|
|
1756
|
+
PyTorch
|
|
1757
|
+
-------
|
|
1758
|
+
resolution matching : bilinear interpolation toward skip-feature grids
|
|
1759
|
+
`forward` resizes coarse decoder states and the shared conditioning tensor to the spatial
|
|
1760
|
+
sizes of the skip tensors with `F.interpolate(..., mode='bilinear')` before each fusion
|
|
1761
|
+
step [1].
|
|
1762
|
+
|
|
1763
|
+
References
|
|
1764
|
+
----------
|
|
1765
|
+
[1] PyTorch.
|
|
1766
|
+
https://context7.com/pytorch/pytorch
|
|
1767
|
+
"""
|
|
1768
|
+
p2, p3, p4, p5 = enc_feats
|
|
1769
|
+
|
|
1770
|
+
d5 = self.skip_p5(p5)
|
|
1771
|
+
h_d5 = self.h_to_d5(F.interpolate(h_ace, size=d5.shape[2:], mode='bilinear'))
|
|
1772
|
+
d5 = self.fusion_d5(d5, h_d5)
|
|
1773
|
+
|
|
1774
|
+
d5_up: Tensor = F.interpolate(d5, size=p4.shape[2:], mode='bilinear')
|
|
1775
|
+
d4_skip = self.skip_p4(p4)
|
|
1776
|
+
d4 = self.up_d5(d5_up) + d4_skip
|
|
1777
|
+
|
|
1778
|
+
h_d4 = self.h_to_d4(F.interpolate(h_ace, size=d4.shape[2:], mode='bilinear'))
|
|
1779
|
+
d4 = self.fusion_d4(d4, h_d4)
|
|
1780
|
+
|
|
1781
|
+
d4_up: Tensor = F.interpolate(d4, size=p3.shape[2:], mode='bilinear')
|
|
1782
|
+
d3_skip = self.skip_p3(p3)
|
|
1783
|
+
d3 = self.up_d4(d4_up) + d3_skip
|
|
1784
|
+
|
|
1785
|
+
h_d3 = self.h_to_d3(F.interpolate(h_ace, size=d3.shape[2:], mode='bilinear'))
|
|
1786
|
+
d3 = self.fusion_d3(d3, h_d3)
|
|
1787
|
+
|
|
1788
|
+
d3_up: Tensor = F.interpolate(d3, size=p2.shape[2:], mode='bilinear')
|
|
1789
|
+
d2_skip = self.skip_p2(p2)
|
|
1790
|
+
d2 = self.up_d3(d3_up) + d2_skip
|
|
1791
|
+
|
|
1792
|
+
h_d2 = self.h_to_d2(F.interpolate(h_ace, size=d2.shape[2:], mode='bilinear'))
|
|
1793
|
+
d2 = self.fusion_d2(d2, h_d2)
|
|
1794
|
+
|
|
1795
|
+
return self.final_d2(d2)
|
|
1796
|
+
|
|
1797
|
+
class TFC_TDF(nn.Module):
|
|
1798
|
+
"""Refine one feature map with residual TFC-TDF blocks.
|
|
1799
|
+
|
|
1800
|
+
(AI generated docstring)
|
|
1801
|
+
|
|
1802
|
+
You can use `TFC_TDF` to stack the TFC-TDF v3 residual block described in the DTTNet and
|
|
1803
|
+
TFC-TDF-UNet v3 literature [1][2]. Each repeated block applies one TFC path, one bottlenecked TDF
|
|
1804
|
+
path over the frequency axis, one second TFC path, and one residual shortcut.
|
|
1805
|
+
|
|
1806
|
+
Mathematics
|
|
1807
|
+
-----------
|
|
1808
|
+
TFC-TDF residual stack : recurrence
|
|
1809
|
+
```
|
|
1810
|
+
Let X₀ ≜ `x`, L ≜ |`self.blocks`|,
|
|
1811
|
+
Aₗ ≜ `self.blocks[ℓ].tfc1`, Bₗ ≜ `self.blocks[ℓ].tdf`,
|
|
1812
|
+
Cₗ ≜ `self.blocks[ℓ].tfc2`, Sₗ ≜ `self.blocks[ℓ].shortcut`
|
|
1813
|
+
|
|
1814
|
+
Uₗ = Aₗ(Xₗ)
|
|
1815
|
+
Vₗ = Uₗ + Bₗ(Uₗ)
|
|
1816
|
+
Xₗ₊₁ = Cₗ(Vₗ) + Sₗ(Xₗ)
|
|
1817
|
+
Y = X_L
|
|
1818
|
+
|
|
1819
|
+
where Y ≜ `refinedTensor`
|
|
1820
|
+
```
|
|
1821
|
+
|
|
1822
|
+
Architecture
|
|
1823
|
+
------------
|
|
1824
|
+
per-block structure : TFC-TDF v3
|
|
1825
|
+
DTTNet describes one TFC-TDF v3 block as one TFC block, one residual TDF that compresses and
|
|
1826
|
+
restores the frequency axis, one second TFC block, and one residual convolution shortcut [1].
|
|
1827
|
+
|
|
1828
|
+
References
|
|
1829
|
+
----------
|
|
1830
|
+
[1] Chen, J., Wu, L., Suryotrisongko, H., and Kim, M. (2024). Music source separation
|
|
1831
|
+
based on a lightweight deep learning framework (DTTNET: DUAL-PATH TFC-TDF UNET).
|
|
1832
|
+
https://arxiv.org/abs/2309.08684
|
|
1833
|
+
[2] Kim, M., Lee, J. H., and Jung, S. (2023). Sound Demixing Challenge 2023 Music
|
|
1834
|
+
Demixing Track Technical Report: TFC-TDF-UNet v3. https://arxiv.org/abs/2306.09382
|
|
1835
|
+
"""
|
|
1836
|
+
|
|
1837
|
+
def __init__(self, in_c: int, c: int, l: int, f: int, bn: int = 4
|
|
1838
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
1839
|
+
, norm_eps: float = 1e-8
|
|
1840
|
+
, *
|
|
1841
|
+
, norm_affine: bool = True
|
|
1842
|
+
, conv_bias: bool = False
|
|
1843
|
+
, linear_bias: bool = False) -> None:
|
|
1844
|
+
"""Configure one repeated TFC-TDF stack.
|
|
1845
|
+
|
|
1846
|
+
(AI generated docstring)
|
|
1847
|
+
|
|
1848
|
+
You can use `__init__` to choose the input width, hidden width, repeat count, frequency-axis
|
|
1849
|
+
width, TDF bottleneck factor, and normalization or bias settings for one `TFC_TDF` block.
|
|
1850
|
+
|
|
1851
|
+
Parameters
|
|
1852
|
+
----------
|
|
1853
|
+
in_c : int
|
|
1854
|
+
Input channel count of the first repeated block.
|
|
1855
|
+
c : int
|
|
1856
|
+
Channel count used inside the repeated blocks and at the output.
|
|
1857
|
+
l : int
|
|
1858
|
+
Number of repeated TFC-TDF residual blocks.
|
|
1859
|
+
f : int
|
|
1860
|
+
Frequency-axis width expected by the TDF linear layers.
|
|
1861
|
+
bn : int = 4
|
|
1862
|
+
Bottleneck factor used to reduce the frequency axis from `f` to `f // bn` inside TDF.
|
|
1863
|
+
activation : type[nn.Module] = nn.SiLU
|
|
1864
|
+
Activation class instantiated in all inner blocks.
|
|
1865
|
+
norm_eps : float = 1e-8
|
|
1866
|
+
Instance-normalization epsilon used in the inner normalization layers.
|
|
1867
|
+
norm_affine : bool = True
|
|
1868
|
+
Whether instance normalization learns affine parameters.
|
|
1869
|
+
conv_bias : bool = False
|
|
1870
|
+
Whether the convolution layers learn bias terms.
|
|
1871
|
+
linear_bias : bool = False
|
|
1872
|
+
Whether the TDF linear layers learn bias terms.
|
|
1873
|
+
"""
|
|
1874
|
+
super().__init__()
|
|
1875
|
+
|
|
1876
|
+
self.blocks = nn.ModuleList()
|
|
1877
|
+
for _index in loops(l):
|
|
1878
|
+
block = nn.Module()
|
|
1879
|
+
|
|
1880
|
+
block.tfc1 = nn.Sequential(
|
|
1881
|
+
nn.InstanceNorm2d(in_c, affine=norm_affine, eps=norm_eps), activation(), nn.Conv2d(in_c, c, 3, 1, 1, bias=conv_bias)
|
|
1882
|
+
)
|
|
1883
|
+
block.tdf = nn.Sequential(
|
|
1884
|
+
nn.InstanceNorm2d(c, affine=norm_affine, eps=norm_eps)
|
|
1885
|
+
, activation()
|
|
1886
|
+
, nn.Linear(f, f // bn, bias=linear_bias)
|
|
1887
|
+
, nn.InstanceNorm2d(c, affine=norm_affine, eps=norm_eps)
|
|
1888
|
+
, activation()
|
|
1889
|
+
, nn.Linear(f // bn, f, bias=linear_bias)
|
|
1890
|
+
)
|
|
1891
|
+
block.tfc2 = nn.Sequential(
|
|
1892
|
+
nn.InstanceNorm2d(c, affine=norm_affine, eps=norm_eps), activation(), nn.Conv2d(c, c, 3, 1, 1, bias=conv_bias)
|
|
1893
|
+
)
|
|
1894
|
+
block.shortcut = nn.Conv2d(in_c, c, 1, 1, 0, bias=conv_bias)
|
|
1895
|
+
|
|
1896
|
+
self.blocks.append(block)
|
|
1897
|
+
in_c = c
|
|
1898
|
+
|
|
1899
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
1900
|
+
"""Refine one feature tensor with repeated TFC-TDF residual blocks.
|
|
1901
|
+
|
|
1902
|
+
(AI generated docstring)
|
|
1903
|
+
|
|
1904
|
+
You can use `forward` to pass one feature tensor `x` through each stored TFC-TDF residual
|
|
1905
|
+
block in sequence and return the final refined tensor.
|
|
1906
|
+
|
|
1907
|
+
Parameters
|
|
1908
|
+
----------
|
|
1909
|
+
x : Tensor
|
|
1910
|
+
Input tensor of shape `(batch, in_c_or_c, time_frames, frequency_bins)`.
|
|
1911
|
+
|
|
1912
|
+
Returns
|
|
1913
|
+
-------
|
|
1914
|
+
refinedTensor : Tensor
|
|
1915
|
+
Tensor of shape `(batch, c, time_frames, frequency_bins)`.
|
|
1916
|
+
|
|
1917
|
+
PyTorch
|
|
1918
|
+
-------
|
|
1919
|
+
frequency-axis linear layers : `nn.Linear` acts on the last dimension
|
|
1920
|
+
Each inner `tdf` stack applies `nn.Linear(f, f // bn)` and `nn.Linear(f // bn, f)`
|
|
1921
|
+
directly to one tensor in `NCHW` layout. In PyTorch, `nn.Linear` transforms the last
|
|
1922
|
+
dimension, so the TDF path operates along the frequency axis without permuting the tensor
|
|
1923
|
+
first [1].
|
|
1924
|
+
|
|
1925
|
+
References
|
|
1926
|
+
----------
|
|
1927
|
+
[1] PyTorch.
|
|
1928
|
+
https://context7.com/pytorch/pytorch
|
|
1929
|
+
[2] Chen, J., Wu, L., Suryotrisongko, H., and Kim, M. (2024). Music source separation
|
|
1930
|
+
based on a lightweight deep learning framework (DTTNET: DUAL-PATH TFC-TDF UNET).
|
|
1931
|
+
https://arxiv.org/abs/2309.08684
|
|
1932
|
+
"""
|
|
1933
|
+
for block in self.blocks:
|
|
1934
|
+
s: Tensor = cast('Callable[[Tensor], Tensor]', block.shortcut)(x)
|
|
1935
|
+
x = cast('Callable[[Tensor], Tensor]', block.tfc1)(x)
|
|
1936
|
+
x += cast('Callable[[Tensor], Tensor]', block.tdf)(x)
|
|
1937
|
+
x = cast('Callable[[Tensor], Tensor]', block.tfc2)(x)
|
|
1938
|
+
x += s
|
|
1939
|
+
return x
|
|
1940
|
+
|
|
1941
|
+
class FreqPixelShuffle(nn.Module):
|
|
1942
|
+
"""Expand frequency resolution with a channel-to-frequency rearrangement.
|
|
1943
|
+
|
|
1944
|
+
(AI generated docstring)
|
|
1945
|
+
|
|
1946
|
+
You can use `FreqPixelShuffle` to project one feature map to `out_channels × scale` channels,
|
|
1947
|
+
rearrange those channels into a wider frequency axis, and refine the result with one `TFC_TDF`
|
|
1948
|
+
block stack.
|
|
1949
|
+
|
|
1950
|
+
Mathematics
|
|
1951
|
+
-----------
|
|
1952
|
+
channel-to-frequency rearrangement : transformation
|
|
1953
|
+
```
|
|
1954
|
+
Let X⁰ ≜ `x`, C ≜ `self.conv`, T ≜ `self.out_conv`,
|
|
1955
|
+
r ≜ `self.scale`, X ≜ C(X⁰), Cʳ ≜ |X|₁, Cᵒ ≜ Cʳ / r
|
|
1956
|
+
|
|
1957
|
+
Z[b, c, ρ, t, f] = X[b, cr + ρ, t, f]
|
|
1958
|
+
U[b, c, t, fr + ρ] = Z[b, c, ρ, t, f]
|
|
1959
|
+
Y = T(U)
|
|
1960
|
+
|
|
1961
|
+
where Y ≜ `upsampledTensor`
|
|
1962
|
+
```
|
|
1963
|
+
|
|
1964
|
+
"""
|
|
1965
|
+
|
|
1966
|
+
def __init__(
|
|
1967
|
+
self
|
|
1968
|
+
, in_channels: int
|
|
1969
|
+
, out_channels: int
|
|
1970
|
+
, scale: int
|
|
1971
|
+
, f: int
|
|
1972
|
+
, tfc_tdf_depth: int = 2
|
|
1973
|
+
, tfc_tdf_bn: int = 4
|
|
1974
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
1975
|
+
, norm_eps: float = 1e-8
|
|
1976
|
+
, *
|
|
1977
|
+
, conv_bias: bool = False
|
|
1978
|
+
, linear_bias: bool = False
|
|
1979
|
+
, norm_affine: bool = True
|
|
1980
|
+
) -> None:
|
|
1981
|
+
"""Configure one frequency-axis pixel-shuffle stage.
|
|
1982
|
+
|
|
1983
|
+
(AI generated docstring)
|
|
1984
|
+
|
|
1985
|
+
You can use `__init__` to choose the input width, output width, frequency upsampling factor,
|
|
1986
|
+
post-shuffle frequency width, TFC-TDF depth, and normalization or bias settings for one
|
|
1987
|
+
`FreqPixelShuffle` block.
|
|
1988
|
+
|
|
1989
|
+
Parameters
|
|
1990
|
+
----------
|
|
1991
|
+
in_channels : int
|
|
1992
|
+
Input channel count.
|
|
1993
|
+
out_channels : int
|
|
1994
|
+
Output channel count after channel-to-frequency rearrangement.
|
|
1995
|
+
scale : int
|
|
1996
|
+
Frequency upsampling factor.
|
|
1997
|
+
f : int
|
|
1998
|
+
Frequency-axis width after rearrangement. `FreqPixelShuffle` forwards `f` to `TFC_TDF`.
|
|
1999
|
+
tfc_tdf_depth : int = 2
|
|
2000
|
+
Number of repeated TFC-TDF blocks used after rearrangement.
|
|
2001
|
+
tfc_tdf_bn : int = 4
|
|
2002
|
+
TDF bottleneck factor forwarded to `TFC_TDF`.
|
|
2003
|
+
activation : type[nn.Module] = nn.SiLU
|
|
2004
|
+
Activation class instantiated in the inner `DSConv` and `TFC_TDF` blocks.
|
|
2005
|
+
norm_eps : float = 1e-8
|
|
2006
|
+
Instance-normalization epsilon used in the inner blocks.
|
|
2007
|
+
norm_affine : bool = True
|
|
2008
|
+
Whether instance normalization learns affine parameters.
|
|
2009
|
+
conv_bias : bool = False
|
|
2010
|
+
Whether the convolution layers learn bias terms.
|
|
2011
|
+
linear_bias : bool = False
|
|
2012
|
+
Whether the TDF linear layers learn bias terms.
|
|
2013
|
+
"""
|
|
2014
|
+
super().__init__()
|
|
2015
|
+
self.scale: int = scale
|
|
2016
|
+
self.conv: DSConv = DSConv(
|
|
2017
|
+
in_channels, out_channels * scale, activation=activation, norm_eps=norm_eps, norm_affine=norm_affine, bias=conv_bias
|
|
2018
|
+
)
|
|
2019
|
+
self.out_conv: TFC_TDF = TFC_TDF(
|
|
2020
|
+
out_channels
|
|
2021
|
+
, out_channels
|
|
2022
|
+
, tfc_tdf_depth
|
|
2023
|
+
, f
|
|
2024
|
+
, bn=tfc_tdf_bn
|
|
2025
|
+
, activation=activation
|
|
2026
|
+
, norm_eps=norm_eps
|
|
2027
|
+
, norm_affine=norm_affine
|
|
2028
|
+
, conv_bias=conv_bias
|
|
2029
|
+
, linear_bias=linear_bias
|
|
2030
|
+
)
|
|
2031
|
+
|
|
2032
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
2033
|
+
"""Increase frequency resolution by rearranging channels into bins.
|
|
2034
|
+
|
|
2035
|
+
(AI generated docstring)
|
|
2036
|
+
|
|
2037
|
+
You can use `forward` to expand the last spatial axis of `x` by `self.scale`, reinterpret
|
|
2038
|
+
projected channels as frequency positions, and refine the expanded tensor with the stored
|
|
2039
|
+
`TFC_TDF` block.
|
|
2040
|
+
|
|
2041
|
+
Parameters
|
|
2042
|
+
----------
|
|
2043
|
+
x : Tensor
|
|
2044
|
+
Input tensor of shape `(batch, in_channels, time_frames, frequency_bins)`.
|
|
2045
|
+
|
|
2046
|
+
Returns
|
|
2047
|
+
-------
|
|
2048
|
+
upsampledTensor : Tensor
|
|
2049
|
+
Tensor of shape `(batch, out_channels, time_frames, frequency_bins × scale)`.
|
|
2050
|
+
|
|
2051
|
+
PyTorch
|
|
2052
|
+
-------
|
|
2053
|
+
channel-to-frequency rearrangement : `view`, `permute`, `contiguous`, `view`
|
|
2054
|
+
`forward` first projects `x` to `out_channels × scale` channels, then reshapes and
|
|
2055
|
+
permutes the tensor so that the extra channel factor becomes additional positions on the
|
|
2056
|
+
last spatial axis. `contiguous()` makes the memory layout safe for the final `view` [1].
|
|
2057
|
+
|
|
2058
|
+
References
|
|
2059
|
+
----------
|
|
2060
|
+
[1] PyTorch.
|
|
2061
|
+
https://context7.com/pytorch/pytorch
|
|
2062
|
+
"""
|
|
2063
|
+
x = self.conv(x)
|
|
2064
|
+
B, C_r, H, W = x.shape
|
|
2065
|
+
out_c: int = C_r // self.scale
|
|
2066
|
+
|
|
2067
|
+
x = x.view(B, out_c, self.scale, H, W)
|
|
2068
|
+
|
|
2069
|
+
x = x.permute(0, 1, 3, 4, 2).contiguous()
|
|
2070
|
+
x = x.view(B, out_c, H, W * self.scale)
|
|
2071
|
+
|
|
2072
|
+
return self.out_conv(x)
|
|
2073
|
+
|
|
2074
|
+
class ProgressiveUpsampleHead(nn.Module):
|
|
2075
|
+
"""Recover target frequency resolution through repeated frequency-axis upsampling.
|
|
2076
|
+
|
|
2077
|
+
(AI generated docstring)
|
|
2078
|
+
|
|
2079
|
+
You can use `ProgressiveUpsampleHead` to convert the decoder output at the band-split frequency
|
|
2080
|
+
resolution into one mask tensor defined on the requested number of STFT bins. The head applies
|
|
2081
|
+
four `FreqPixelShuffle` stages and one final `3 × 3` projection.
|
|
2082
|
+
|
|
2083
|
+
Mathematics
|
|
2084
|
+
-----------
|
|
2085
|
+
progressive frequency expansion : recurrence
|
|
2086
|
+
```
|
|
2087
|
+
Let X₀ ≜ `x`, C ≜ `self.final_conv`, T ≜ `self.target_bins`,
|
|
2088
|
+
Bᵢ ∈ {`self.block1`, `self.block2`, `self.block3`, `self.block4`}
|
|
2089
|
+
|
|
2090
|
+
X₁ = B₁(X₀)
|
|
2091
|
+
X₂ = B₂(X₁)
|
|
2092
|
+
X₃ = B₃(X₂)
|
|
2093
|
+
X₄ = B₄(X₃)
|
|
2094
|
+
Z = ⎧ Resize(X₄ → (|X₄|₂, T)), |X₄|₃ ≠ T
|
|
2095
|
+
⎨
|
|
2096
|
+
⎩ X₄, |X₄|₃ = T
|
|
2097
|
+
Y = C(Z)
|
|
2098
|
+
|
|
2099
|
+
where Y ≜ `maskTensor`
|
|
2100
|
+
```
|
|
2101
|
+
|
|
2102
|
+
"""
|
|
2103
|
+
|
|
2104
|
+
def __init__(
|
|
2105
|
+
self
|
|
2106
|
+
, in_channels: int
|
|
2107
|
+
, out_channels: int
|
|
2108
|
+
, target_bins: int = 1025
|
|
2109
|
+
, in_bands: int = 62
|
|
2110
|
+
, upsample_scales: tuple[int, int, int, int] = (2, 2, 2, 2)
|
|
2111
|
+
, tfc_tdf_depth: int = 2
|
|
2112
|
+
, tfc_tdf_bn: int = 4
|
|
2113
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
2114
|
+
, norm_eps: float = 1e-8
|
|
2115
|
+
, *
|
|
2116
|
+
, conv_bias: bool = False
|
|
2117
|
+
, linear_bias: bool = False
|
|
2118
|
+
, norm_affine: bool = True
|
|
2119
|
+
) -> None:
|
|
2120
|
+
"""Configure one progressive frequency upsampling head.
|
|
2121
|
+
|
|
2122
|
+
(AI generated docstring)
|
|
2123
|
+
|
|
2124
|
+
You can use `__init__` to choose the decoder width, output mask width, target STFT-bin count,
|
|
2125
|
+
frequency upsampling schedule, and shared TFC-TDF settings for one `ProgressiveUpsampleHead`.
|
|
2126
|
+
|
|
2127
|
+
Parameters
|
|
2128
|
+
----------
|
|
2129
|
+
in_channels : int
|
|
2130
|
+
Input channel count from the decoder.
|
|
2131
|
+
out_channels : int
|
|
2132
|
+
Output channel count of the final mask tensor.
|
|
2133
|
+
target_bins : int = 1025
|
|
2134
|
+
Target number of STFT bins at the output.
|
|
2135
|
+
in_bands : int = 62
|
|
2136
|
+
Frequency-bin count of the decoder input before progressive upsampling.
|
|
2137
|
+
upsample_scales : tuple[int, int, int, int] = (2, 2, 2, 2)
|
|
2138
|
+
Per-stage frequency upsampling factors used by the four `FreqPixelShuffle` blocks.
|
|
2139
|
+
tfc_tdf_depth : int = 2
|
|
2140
|
+
Number of repeated TFC-TDF blocks used inside every `FreqPixelShuffle` block.
|
|
2141
|
+
tfc_tdf_bn : int = 4
|
|
2142
|
+
TDF bottleneck factor used inside every `FreqPixelShuffle` block.
|
|
2143
|
+
activation : type[nn.Module] = nn.SiLU
|
|
2144
|
+
Activation class instantiated in all inner blocks.
|
|
2145
|
+
norm_eps : float = 1e-8
|
|
2146
|
+
Instance-normalization epsilon used in all inner blocks.
|
|
2147
|
+
norm_affine : bool = True
|
|
2148
|
+
Whether instance normalization learns affine parameters.
|
|
2149
|
+
conv_bias : bool = False
|
|
2150
|
+
Whether the convolution layers learn bias terms.
|
|
2151
|
+
linear_bias : bool = False
|
|
2152
|
+
Whether the TDF linear layers learn bias terms.
|
|
2153
|
+
"""
|
|
2154
|
+
super().__init__()
|
|
2155
|
+
self.target_bins: int = target_bins
|
|
2156
|
+
|
|
2157
|
+
c: int = in_channels
|
|
2158
|
+
scale1, scale2, scale3, scale4 = upsample_scales
|
|
2159
|
+
f1: int = in_bands * scale1
|
|
2160
|
+
f2: int = f1 * scale2
|
|
2161
|
+
f3: int = f2 * scale3
|
|
2162
|
+
f4: int = f3 * scale4
|
|
2163
|
+
|
|
2164
|
+
self.block1: FreqPixelShuffle = FreqPixelShuffle(
|
|
2165
|
+
c
|
|
2166
|
+
, c // 2
|
|
2167
|
+
, scale=scale1
|
|
2168
|
+
, f=f1
|
|
2169
|
+
, tfc_tdf_depth=tfc_tdf_depth
|
|
2170
|
+
, tfc_tdf_bn=tfc_tdf_bn
|
|
2171
|
+
, activation=activation
|
|
2172
|
+
, norm_eps=norm_eps
|
|
2173
|
+
, norm_affine=norm_affine
|
|
2174
|
+
, conv_bias=conv_bias
|
|
2175
|
+
, linear_bias=linear_bias
|
|
2176
|
+
)
|
|
2177
|
+
self.block2: FreqPixelShuffle = FreqPixelShuffle(
|
|
2178
|
+
c // 2
|
|
2179
|
+
, c // 4
|
|
2180
|
+
, scale=scale2
|
|
2181
|
+
, f=f2
|
|
2182
|
+
, tfc_tdf_depth=tfc_tdf_depth
|
|
2183
|
+
, tfc_tdf_bn=tfc_tdf_bn
|
|
2184
|
+
, activation=activation
|
|
2185
|
+
, norm_eps=norm_eps
|
|
2186
|
+
, norm_affine=norm_affine
|
|
2187
|
+
, conv_bias=conv_bias
|
|
2188
|
+
, linear_bias=linear_bias
|
|
2189
|
+
)
|
|
2190
|
+
self.block3: FreqPixelShuffle = FreqPixelShuffle(
|
|
2191
|
+
c // 4
|
|
2192
|
+
, c // 8
|
|
2193
|
+
, scale=scale3
|
|
2194
|
+
, f=f3
|
|
2195
|
+
, tfc_tdf_depth=tfc_tdf_depth
|
|
2196
|
+
, tfc_tdf_bn=tfc_tdf_bn
|
|
2197
|
+
, activation=activation
|
|
2198
|
+
, norm_eps=norm_eps
|
|
2199
|
+
, norm_affine=norm_affine
|
|
2200
|
+
, conv_bias=conv_bias
|
|
2201
|
+
, linear_bias=linear_bias
|
|
2202
|
+
)
|
|
2203
|
+
self.block4: FreqPixelShuffle = FreqPixelShuffle(
|
|
2204
|
+
c // 8
|
|
2205
|
+
, c // 16
|
|
2206
|
+
, scale=scale4
|
|
2207
|
+
, f=f4
|
|
2208
|
+
, tfc_tdf_depth=tfc_tdf_depth
|
|
2209
|
+
, tfc_tdf_bn=tfc_tdf_bn
|
|
2210
|
+
, activation=activation
|
|
2211
|
+
, norm_eps=norm_eps
|
|
2212
|
+
, norm_affine=norm_affine
|
|
2213
|
+
, conv_bias=conv_bias
|
|
2214
|
+
, linear_bias=linear_bias
|
|
2215
|
+
)
|
|
2216
|
+
|
|
2217
|
+
self.final_conv = nn.Conv2d(c // 16, out_channels, kernel_size=3, stride=1, padding='same', bias=conv_bias)
|
|
2218
|
+
|
|
2219
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
2220
|
+
"""Return one full-resolution mask tensor from decoder features.
|
|
2221
|
+
|
|
2222
|
+
(AI generated docstring)
|
|
2223
|
+
|
|
2224
|
+
You can use `forward` to increase the frequency resolution of decoder tensor `x` through the
|
|
2225
|
+
four stored `FreqPixelShuffle` stages and then project the result to the final mask channels.
|
|
2226
|
+
|
|
2227
|
+
Parameters
|
|
2228
|
+
----------
|
|
2229
|
+
x : Tensor
|
|
2230
|
+
Input tensor of shape `(batch, in_channels, time_frames, in_bands)`.
|
|
2231
|
+
|
|
2232
|
+
Returns
|
|
2233
|
+
-------
|
|
2234
|
+
maskTensor : Tensor
|
|
2235
|
+
Tensor of shape `(batch, out_channels, time_frames, target_bins)`.
|
|
2236
|
+
|
|
2237
|
+
PyTorch
|
|
2238
|
+
-------
|
|
2239
|
+
final bin alignment : exact last-axis correction with `F.interpolate`
|
|
2240
|
+
Each `FreqPixelShuffle` block increases only the last spatial axis. If the accumulated
|
|
2241
|
+
scale factors do not land exactly on `self.target_bins`, `forward` uses `F.interpolate`
|
|
2242
|
+
with `size=(x.shape[2], self.target_bins)` to preserve the time axis and correct only the
|
|
2243
|
+
frequency axis [1].
|
|
2244
|
+
|
|
2245
|
+
References
|
|
2246
|
+
----------
|
|
2247
|
+
[1] PyTorch.
|
|
2248
|
+
https://context7.com/pytorch/pytorch
|
|
2249
|
+
"""
|
|
2250
|
+
x = self.block1(x)
|
|
2251
|
+
x = self.block2(x)
|
|
2252
|
+
x = self.block3(x)
|
|
2253
|
+
x = self.block4(x)
|
|
2254
|
+
|
|
2255
|
+
if x.shape[-1] != self.target_bins:
|
|
2256
|
+
x = F.interpolate(x, size=(x.shape[2], self.target_bins), mode='bilinear', align_corners=False)
|
|
2257
|
+
|
|
2258
|
+
return self.final_conv(x)
|
|
2259
|
+
|
|
2260
|
+
class SegmModel(nn.Module):
|
|
2261
|
+
"""Assemble the full segmentation-style HyperACE branch used by the mask estimator.
|
|
2262
|
+
|
|
2263
|
+
You can use `SegmModel` to encode one band-split feature tensor, aggregate the encoder stages with
|
|
2264
|
+
`HyperACE`, decode the aggregated representation, restore the time resolution, and upsample the
|
|
2265
|
+
frequency axis to the target STFT-bin count. `mask.MaskEstimator` uses this branch when
|
|
2266
|
+
`use_hyperACE` is enabled [1]. This assembly follows unwa's BS-Roformer-HyperACE implementation [5].
|
|
2267
|
+
|
|
2268
|
+
Mathematics
|
|
2269
|
+
-----------
|
|
2270
|
+
HyperACE mask branch : equation
|
|
2271
|
+
```
|
|
2272
|
+
Let X ≜ `x`, B ≜ `self.backbone`, H ≜ `self.hyperace`,
|
|
2273
|
+
D ≜ `self.decoder`, U ≜ `self.upsample_head`, T₀ ≜ |X|₂
|
|
2274
|
+
|
|
2275
|
+
E = B(X)
|
|
2276
|
+
A = H(E)
|
|
2277
|
+
Z = D(E, A)
|
|
2278
|
+
Z′ = Resize(Z → (T₀, |Z|₃))
|
|
2279
|
+
Y = U(Z′)
|
|
2280
|
+
|
|
2281
|
+
where Y ≜ `mask_tensor`
|
|
2282
|
+
```
|
|
2283
|
+
|
|
2284
|
+
References
|
|
2285
|
+
----------
|
|
2286
|
+
[1] `hunterFormsBS.mask.MaskEstimator`
|
|
2287
|
+
|
|
2288
|
+
[2] Lei, M., Li, S., Wu, Y., Hu, H., Zhou, Y., Zheng, X., Ding, G., Du, S., Wu, Z.,
|
|
2289
|
+
and Gao, Y. (2025). YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive
|
|
2290
|
+
Visual Perception. https://arxiv.org/abs/2506.17733
|
|
2291
|
+
[3] Lu, W.-T., Wang, J.-C., Kong, Q., and Hung, Y.-N. (2023). Music Source Separation
|
|
2292
|
+
with Band-Split RoPE Transformer. https://arxiv.org/abs/2309.02612
|
|
2293
|
+
[4] Wang, J.-C., Lu, W.-T., and Chen, J. (2024). Mel-RoFormer for Vocal Separation and
|
|
2294
|
+
Vocal Melody Transcription. https://arxiv.org/abs/2409.04702
|
|
2295
|
+
[5] unwa (pcunwa). (2025). BS-Roformer-HyperACE.
|
|
2296
|
+
https://huggingface.co/pcunwa/BS-Roformer-HyperACE/resolve/main/v2_voc/bs_roformer.py
|
|
2297
|
+
"""
|
|
2298
|
+
|
|
2299
|
+
def __init__(
|
|
2300
|
+
self
|
|
2301
|
+
, activation: type[nn.Module] = nn.SiLU
|
|
2302
|
+
, backbone_channels: tuple[int, int, int, int, int] | None = None
|
|
2303
|
+
, base_channels: int = 64
|
|
2304
|
+
, base_depth: int = 2
|
|
2305
|
+
, decoder_block_depth: int = 1
|
|
2306
|
+
, decoder_block_expansion: float = 0.5
|
|
2307
|
+
, decoder_block_kernel: int = 3
|
|
2308
|
+
, decoder_channels: list[int] | tuple[int, int, int, int] | None = None
|
|
2309
|
+
, hyperace_c_h: float = 0.5
|
|
2310
|
+
, hyperace_c_l: float = 0.25
|
|
2311
|
+
, hyperace_c3ah_expansion: float = 1.0
|
|
2312
|
+
, hyperace_k: int = 2
|
|
2313
|
+
, hyperace_l: int = 1
|
|
2314
|
+
, hyperace_low_order_depth: int = 1
|
|
2315
|
+
, hyperace_low_order_expansion: float = 1.0
|
|
2316
|
+
, hyperace_low_order_kernel: int = 3
|
|
2317
|
+
, hyperace_out_channels: int | None = None
|
|
2318
|
+
, in_bands: int = 62
|
|
2319
|
+
, in_dim: int = 256
|
|
2320
|
+
, norm_eps: float = 1e-8
|
|
2321
|
+
, num_heads: int = 8
|
|
2322
|
+
, num_hyperedges: int = 32
|
|
2323
|
+
, out_bins: int = 1025
|
|
2324
|
+
, out_channels: int = 4
|
|
2325
|
+
, upsample_scales: tuple[int, int, int, int] = (2, 2, 2, 2)
|
|
2326
|
+
, upsample_tfc_tdf_bn: int = 4
|
|
2327
|
+
, upsample_tfc_tdf_depth: int = 2
|
|
2328
|
+
, *
|
|
2329
|
+
, conv_bias: bool = False
|
|
2330
|
+
, linear_bias: bool = False
|
|
2331
|
+
, norm_affine: bool = True
|
|
2332
|
+
) -> None:
|
|
2333
|
+
"""Configure one full HyperACE segmentation branch.
|
|
2334
|
+
|
|
2335
|
+
(AI generated docstring)
|
|
2336
|
+
|
|
2337
|
+
You can use `__init__` to choose the input band geometry, the encoder width schedule, the
|
|
2338
|
+
adaptive-hypergraph size, the decoder width schedule, and the final frequency upsampling plan
|
|
2339
|
+
for one `SegmModel` instance.
|
|
2340
|
+
|
|
2341
|
+
Parameters
|
|
2342
|
+
----------
|
|
2343
|
+
in_bands : int = 62
|
|
2344
|
+
Input number of band-split frequency bins presented to `SegmModel`.
|
|
2345
|
+
in_dim : int = 256
|
|
2346
|
+
Input channel count of the band-split feature tensor.
|
|
2347
|
+
out_bins : int = 1025
|
|
2348
|
+
Target number of STFT bins produced by `ProgressiveUpsampleHead`.
|
|
2349
|
+
out_channels : int = 4
|
|
2350
|
+
Output channel count of the final mask tensor.
|
|
2351
|
+
activation : type[nn.Module] = nn.SiLU
|
|
2352
|
+
Activation class shared by submodules that expose `activation`.
|
|
2353
|
+
norm_eps : float = 1e-8
|
|
2354
|
+
Instance-normalization epsilon shared by submodules that expose `norm_eps`.
|
|
2355
|
+
norm_affine : bool = True
|
|
2356
|
+
Whether instance normalization learns affine parameters in submodules that expose
|
|
2357
|
+
`norm_affine`.
|
|
2358
|
+
conv_bias : bool = False
|
|
2359
|
+
Whether convolution layers learn bias terms in submodules that expose `conv_bias`.
|
|
2360
|
+
linear_bias : bool = False
|
|
2361
|
+
Whether linear layers learn bias terms in submodules that expose `linear_bias`.
|
|
2362
|
+
|
|
2363
|
+
Other Parameters
|
|
2364
|
+
----------------
|
|
2365
|
+
backbone parameters : forwarded parameter family
|
|
2366
|
+
See `Backbone.__init__`. `SegmModel` forwards `base_channels`, `base_depth`, and
|
|
2367
|
+
`backbone_channels` to `Backbone`.
|
|
2368
|
+
hypergraph size : forwarded parameter family
|
|
2369
|
+
See `HyperACE.__init__`. `SegmModel` forwards `num_hyperedges` and `num_heads` to
|
|
2370
|
+
`HyperACE`.
|
|
2371
|
+
hyperace_* : forwarded parameter family
|
|
2372
|
+
See `HyperACE.__init__`.
|
|
2373
|
+
decoder_channels : forwarded parameter
|
|
2374
|
+
See `Decoder.__init__`. When `decoder_channels` is `None`, `SegmModel` mirrors the encoder
|
|
2375
|
+
widths `[c2, c3, c4, c5]` before calling `Decoder`.
|
|
2376
|
+
decoder_* : forwarded parameter family
|
|
2377
|
+
See `Decoder.__init__`.
|
|
2378
|
+
upsample_* : forwarded parameter family
|
|
2379
|
+
See `ProgressiveUpsampleHead.__init__`.
|
|
2380
|
+
"""
|
|
2381
|
+
super().__init__()
|
|
2382
|
+
|
|
2383
|
+
self.backbone = Backbone(
|
|
2384
|
+
in_channels=in_dim
|
|
2385
|
+
, base_channels=base_channels
|
|
2386
|
+
, base_depth=base_depth
|
|
2387
|
+
, channels=backbone_channels
|
|
2388
|
+
, activation=activation
|
|
2389
|
+
, norm_eps=norm_eps
|
|
2390
|
+
, norm_affine=norm_affine
|
|
2391
|
+
, conv_bias=conv_bias
|
|
2392
|
+
)
|
|
2393
|
+
enc_channels: list[int] = self.backbone.out_channels
|
|
2394
|
+
c2, c3, c4, c5 = enc_channels
|
|
2395
|
+
|
|
2396
|
+
hyperace_in_channels: list[int] = enc_channels
|
|
2397
|
+
hyperace_out_channels = c4 if hyperace_out_channels is None else hyperace_out_channels
|
|
2398
|
+
self.hyperace = HyperACE(
|
|
2399
|
+
hyperace_in_channels
|
|
2400
|
+
, hyperace_out_channels
|
|
2401
|
+
, num_hyperedges
|
|
2402
|
+
, num_heads
|
|
2403
|
+
, k=hyperace_k
|
|
2404
|
+
, l=hyperace_l
|
|
2405
|
+
, c_h=hyperace_c_h
|
|
2406
|
+
, c_l=hyperace_c_l
|
|
2407
|
+
, c3ah_expansion=hyperace_c3ah_expansion
|
|
2408
|
+
, low_order_depth=hyperace_low_order_depth
|
|
2409
|
+
, low_order_kernel=hyperace_low_order_kernel
|
|
2410
|
+
, low_order_expansion=hyperace_low_order_expansion
|
|
2411
|
+
, activation=activation
|
|
2412
|
+
, norm_eps=norm_eps
|
|
2413
|
+
, norm_affine=norm_affine
|
|
2414
|
+
, conv_bias=conv_bias
|
|
2415
|
+
, linear_bias=linear_bias
|
|
2416
|
+
)
|
|
2417
|
+
|
|
2418
|
+
decoder_channels = list(decoder_channels or (c2, c3, c4, c5))
|
|
2419
|
+
self.decoder = Decoder(
|
|
2420
|
+
enc_channels
|
|
2421
|
+
, hyperace_out_channels
|
|
2422
|
+
, decoder_channels
|
|
2423
|
+
, block_depth=decoder_block_depth
|
|
2424
|
+
, block_kernel=decoder_block_kernel
|
|
2425
|
+
, block_expansion=decoder_block_expansion
|
|
2426
|
+
, activation=activation
|
|
2427
|
+
, norm_eps=norm_eps
|
|
2428
|
+
, norm_affine=norm_affine
|
|
2429
|
+
, conv_bias=conv_bias
|
|
2430
|
+
)
|
|
2431
|
+
|
|
2432
|
+
self.upsample_head = ProgressiveUpsampleHead(
|
|
2433
|
+
in_channels=decoder_channels[0]
|
|
2434
|
+
, out_channels=out_channels
|
|
2435
|
+
, target_bins=out_bins
|
|
2436
|
+
, in_bands=in_bands
|
|
2437
|
+
, upsample_scales=upsample_scales
|
|
2438
|
+
, tfc_tdf_depth=upsample_tfc_tdf_depth
|
|
2439
|
+
, tfc_tdf_bn=upsample_tfc_tdf_bn
|
|
2440
|
+
, activation=activation
|
|
2441
|
+
, norm_eps=norm_eps
|
|
2442
|
+
, norm_affine=norm_affine
|
|
2443
|
+
, conv_bias=conv_bias
|
|
2444
|
+
, linear_bias=linear_bias
|
|
2445
|
+
)
|
|
2446
|
+
|
|
2447
|
+
def forward(self, x: Tensor) -> Tensor:
|
|
2448
|
+
"""Predict full-resolution masks from one band-split feature tensor.
|
|
2449
|
+
|
|
2450
|
+
(AI generated docstring)
|
|
2451
|
+
|
|
2452
|
+
You can use `forward` to encode one band-split feature tensor `x`, aggregate multi-scale
|
|
2453
|
+
correlations with `HyperACE`, decode the aggregated tensor back to finer time resolution, and
|
|
2454
|
+
emit one full-resolution mask tensor.
|
|
2455
|
+
|
|
2456
|
+
Parameters
|
|
2457
|
+
----------
|
|
2458
|
+
x : Tensor
|
|
2459
|
+
Input tensor of shape `(batch, in_dim, time_frames, in_bands)`.
|
|
2460
|
+
|
|
2461
|
+
Returns
|
|
2462
|
+
-------
|
|
2463
|
+
mask_tensor : Tensor
|
|
2464
|
+
Tensor of shape `(batch, out_channels, time_frames, out_bins)`.
|
|
2465
|
+
|
|
2466
|
+
PyTorch
|
|
2467
|
+
-------
|
|
2468
|
+
time-axis restoration : bilinear interpolation before frequency upsampling
|
|
2469
|
+
`Backbone` reduces spatial support before `Decoder` reconstructs one tensor on the `p2`
|
|
2470
|
+
grid. `forward` then uses `F.interpolate` with `size=(H, dec_feat.shape[-1])` to restore
|
|
2471
|
+
the original time-axis length before `ProgressiveUpsampleHead` expands the frequency axis
|
|
2472
|
+
[1].
|
|
2473
|
+
|
|
2474
|
+
References
|
|
2475
|
+
----------
|
|
2476
|
+
[1] PyTorch.
|
|
2477
|
+
https://context7.com/pytorch/pytorch
|
|
2478
|
+
[2] `Backbone`
|
|
2479
|
+
|
|
2480
|
+
[3] `HyperACE`
|
|
2481
|
+
|
|
2482
|
+
[4] `Decoder`
|
|
2483
|
+
|
|
2484
|
+
[5] `ProgressiveUpsampleHead`
|
|
2485
|
+
"""
|
|
2486
|
+
H, _W = x.shape[2:]
|
|
2487
|
+
|
|
2488
|
+
enc_feats = self.backbone(x)
|
|
2489
|
+
|
|
2490
|
+
h_ace_feats = self.hyperace(enc_feats)
|
|
2491
|
+
|
|
2492
|
+
dec_feat = self.decoder(enc_feats, h_ace_feats)
|
|
2493
|
+
|
|
2494
|
+
feat_time_restored: Tensor = F.interpolate(dec_feat, size=(H, dec_feat.shape[-1]), mode='bilinear', align_corners=False)
|
|
2495
|
+
|
|
2496
|
+
return self.upsample_head(feat_time_restored)
|