File size: 25,688 Bytes
bb70eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# Third-party
import torch
import torch.nn as nn

# Local
from vocal_isolation.short_time_fourier_transform import STFT
from vocal_isolation.constants import COMPUTATION_DEVICE

dim_s = 4
dim_c = 4

class TFCC(nn.Module):
    def __init__(self, c, l, k):
        super(TFCC, self).__init__()

        self.H = nn.ModuleList()
        for i in range(l):
            self.H.append(
                nn.Sequential(
                    nn.Conv2d(in_channels=c, out_channels=c, kernel_size=k, stride=1, padding=k // 2),
                    nn.GroupNorm(4, c),
                    nn.ReLU(),
                )
            )

    def forward(self, x):
        for h in self.H:
            x = h(x)
        return x


class DenseTFCC(nn.Module):
    def __init__(self, c, l, k):
        super(DenseTFCC, self).__init__()

        self.conv = nn.ModuleList()
        for i in range(l):
            self.conv.append(
                nn.Sequential(
                    nn.Conv2d(in_channels=c, out_channels=c, kernel_size=k, stride=1, padding=k // 2),
                    nn.GroupNorm(4, c),
                    nn.ReLU(),
                )
            )

    def forward(self, x):
        for layer in self.conv[:-1]:
            x = torch.cat([layer(x), x], 1)
        return self.conv[-1](x)


class TFC_TDFF(nn.Module):
    def __init__(self, c, l, f, k, bn, dense=False, bias=True):

        super(TFC_TDFF, self).__init__()

        self.use_tdf = bn is not None

        self.tfc = DenseTFCC(c, l, k) if dense else TFCC(c, l, k)

        if self.use_tdf:
            if bn == 0:
                self.tdf = nn.Sequential(
                    nn.Linear(f, f, bias=bias),
                    nn.GroupNorm(4, c),
                    nn.ReLU()
                )
            else:
                self.tdf = nn.Sequential(
                    nn.Linear(f, f // bn, bias=bias),
                    nn.GroupNorm(4, c),
                    nn.ReLU(),
                    nn.Linear(f // bn, f, bias=bias),
                    nn.GroupNorm(4, c),
                    nn.ReLU()
                )

    def forward(self, x):
        x = self.tfc(x)
        return x + self.tdf(x) if self.use_tdf else x


class TFC(nn.Module):
    def __init__(self, c, l, k):
        super(TFC, self).__init__()

        self.H = nn.ModuleList()
        for i in range(l):
            self.H.append(
                nn.Sequential(
                    nn.Conv2d(in_channels=c, out_channels=c, kernel_size=k, stride=1, padding=k // 2),
                    nn.GroupNorm(2, c),
                    nn.ReLU(),
                )
            )

    def forward(self, x):
        for h in self.H:
            x = h(x)
        return x


class DenseTFC(nn.Module):
    def __init__(self, c, l, k):
        super(DenseTFC, self).__init__()

        self.conv = nn.ModuleList()
        for i in range(l):
            self.conv.append(
                nn.Sequential(
                    nn.Conv2d(in_channels=c, out_channels=c, kernel_size=k, stride=1, padding=k // 2),
                    nn.GroupNorm(2, c),
                    nn.ReLU(),
                )
            )

    def forward(self, x):
        for layer in self.conv[:-1]:
            x = torch.cat([layer(x), x], 1)
        return self.conv[-1](x)


class TFC_TDF(nn.Module):
    def __init__(self, c, l, f, k, bn, dense=False, bias=True):

        super(TFC_TDF, self).__init__()

        self.use_tdf = bn is not None

        self.tfc = DenseTFC(c, l, k) if dense else TFC(c, l, k)

        if self.use_tdf:
            if bn == 0:
                self.tdf = nn.Sequential(
                    nn.Linear(f, f, bias=bias),
                    nn.GroupNorm(2, c),
                    nn.ReLU()
                )
            else:
                self.tdf = nn.Sequential(
                    nn.Linear(f, f // bn, bias=bias),
                    nn.GroupNorm(2, c),
                    nn.ReLU(),
                    nn.Linear(f // bn, f, bias=bias),
                    nn.GroupNorm(2, c),
                    nn.ReLU()
                )

    def forward(self, x):
        x = self.tfc(x)
        return x + self.tdf(x) if self.use_tdf else x


class Conv_TDF(nn.Module):
    """
    Convolutional Time-Domain Filter (TDF) Module.

    Args:
        c (int): The number of input and output channels for the convolutional layers.
        l (int): The number of convolutional layers within the module.
        f (int): The number of features (or units) in the time-domain filter.
        k (int): The size of the convolutional kernels (filters).
        bn (int or None): Batch normalization factor (controls TDF behavior). If None, TDF is not used.
        bias (bool): A boolean flag indicating whether bias terms are included in the linear layers.

    Attributes:
        use_tdf (bool): Flag indicating whether TDF is used.

    Methods:
        forward(x): Forward pass through the TDF module.
    """

    def __init__(self, c, l, f, k, bn, bias=True):
        super(Conv_TDF, self).__init__()

        # Determine whether to use TDF (Time-Domain Filter)
        self.use_tdf = bn is not None

        # Define a list of convolutional layers within the module
        self.H = nn.ModuleList()
        for i in range(l):
            self.H.append(
                nn.Sequential(
                    nn.Conv2d(
                        in_channels=c,
                        out_channels=c,
                        kernel_size=k,
                        stride=1,
                        padding=k // 2,
                    ),
                    nn.GroupNorm(2, c),
                    nn.ReLU(),
                )
            )

        # Define the Time-Domain Filter (TDF) layers if enabled
        if self.use_tdf:
            if bn == 0:
                self.tdf = nn.Sequential(
                    nn.Linear(f, f, bias=bias), nn.GroupNorm(2, c), nn.ReLU()
                )
            else:
                self.tdf = nn.Sequential(
                    nn.Linear(f, f // bn, bias=bias),
                    nn.GroupNorm(2, c),
                    nn.ReLU(),
                    nn.Linear(f // bn, f, bias=bias),
                    nn.GroupNorm(2, c),
                    nn.ReLU(),
                )

    def forward(self, x):
        # Apply the convolutional layers sequentially
        for h in self.H:
            x = h(x)

        # Apply the Time-Domain Filter (TDF) if enabled, and add the result to the orignal input
        return x + self.tdf(x) if self.use_tdf else x


class Conv_TDF_net_trimm(nn.Module):
    """
    Convolutional Time-Domain Filter (TDF) Network with Trimming.
    Used for Vocals.

    Args:
        L (int): This parameter controls the number of down-sampling (DS) blocks in the network.
                 It's divided by 2 to determine how many DS blocks should be created.
        l (int): This parameter represents the number of convolutional layers (or filters) within each dense (fully connected) block.
        g (int): This parameter specifies the number of output channels for the first convolutional layer and is also used to determine the number of channels for subsequent layers in the network.
        dim_f (int): This parameter represents the number of frequency bins (spectrogram columns) in the input audio data.
        dim_t (int): This parameter represents the number of time frames (spectrogram rows) in the input audio data.
        k (int): This parameter specifies the size of convolutional kernels (filters) used in the network's convolutional layers.
        bn (int or None): This parameter controls whether batch normalization is used in the network.
                         If it's None, batch normalization may or may not be used based on other conditions in the code.
        bias (bool): This parameter is a boolean flag that controls whether bias terms are included in the convolutional layers.
        overlap (int): This parameter specifies the amount of overlap between consecutive chunks of audio data during processing.

    Attributes:
        n (int): The calculated number of down-sampling (DS) blocks.
        dim_f (int): The number of frequency bins (spectrogram columns) in the input audio data.
        dim_t (int): The number of time frames (spectrogram rows) in the input audio data.
        n_fft (int): The size of the Fast Fourier Transform (FFT) window.
        hop (int): The hop size used in the STFT calculations.
        n_bins (int): The number of bins in the frequency domain.
        chunk_size (int): The size of each chunk of audio data.
        target_name (str): The name of the target instrument being separated.
        overlap (int): The amount of overlap between consecutive chunks of audio data during processing.

    Methods:
        forward(x): Forward pass through the Conv_TDF_net_trimm network.
    """

    def __init__(
        self,
        model_path,
        use_onnx,
        target_name,
        L,
        l,
        g,
        dim_f,
        dim_t,
        k=3,
        hop=1024,
        bn=None,
        bias=True,
        overlap=1500,
    ):
        super(Conv_TDF_net_trimm, self).__init__()
        # Dictionary specifying the scale for the number of FFT bins for different target names
        n_fft_scale = {"vocals": 3, "*": 2}

        # Number of input and output channels for the initial and final convolutional layers
        out_c = in_c = 4

        # Number of down-sampling (DS) blocks
        self.n = L // 2

        # Dimensions of the frequency and time axes of the input data
        self.dim_f = 3072
        self.dim_t = 256

        # Number of FFT bins (frequencies) and hop size for the Short-Time Fourier Transform (STFT)
        self.n_fft = 7680
        self.hop = hop
        self.n_bins = self.n_fft // 2 + 1

        # Chunk size used for processing
        self.chunk_size = hop * (self.dim_t - 1)

        # Target name for the model
        self.target_name = target_name

        # Overlap between consecutive chunks of audio data during processing
        self.overlap = overlap

        # STFT module for audio processing
        self.stft = STFT(self.n_fft, self.hop, self.dim_f)

        # Check if ONNX representation of the model should be used
        if not use_onnx:
            # First convolutional layer
            self.first_conv = nn.Sequential(
                nn.Conv2d(in_channels=in_c, out_channels=g, kernel_size=1, stride=1),
                nn.BatchNorm2d(g),
                nn.ReLU(),
            )

            # Initialize variables for dense (fully connected) blocks and downsampling (DS) blocks
            f = self.dim_f
            c = g
            self.ds_dense = nn.ModuleList()
            self.ds = nn.ModuleList()

            # Loop through down-sampling (DS) blocks
            for i in range(self.n):
                # Create dense (fully connected) block for down-sampling
                self.ds_dense.append(Conv_TDF(c, l, f, k, bn, bias=bias))

                # Create down-sampling (DS) block
                scale = (2, 2)
                self.ds.append(
                    nn.Sequential(
                        nn.Conv2d(
                            in_channels=c,
                            out_channels=c + g,
                            kernel_size=scale,
                            stride=scale,
                        ),
                        nn.BatchNorm2d(c + g),
                        nn.ReLU(),
                    )
                )
                f = f // 2
                c += g

            # Middle dense (fully connected block)
            self.mid_dense = Conv_TDF(c, l, f, k, bn, bias=bias)

            # If batch normalization is not specified and mid_tdf is True, use Conv_TDF with bn=0 and bias=False
            if bn is None and mid_tdf:
                self.mid_dense = Conv_TDF(c, l, f, k, bn=0, bias=False)

            # Initialize variables for up-sampling (US) blocks
            self.us_dense = nn.ModuleList()
            self.us = nn.ModuleList()

            # Loop through up-sampling (US) blocks
            for i in range(self.n):
                scale = (2, 2)
                # Create up-sampling (US) block
                self.us.append(
                    nn.Sequential(
                        nn.ConvTranspose2d(
                            in_channels=c,
                            out_channels=c - g,
                            kernel_size=scale,
                            stride=scale,
                        ),
                        nn.BatchNorm2d(c - g),
                        nn.ReLU(),
                    )
                )
                f = f * 2
                c -= g

                # Create dense (fully connected) block for up-sampling
                self.us_dense.append(Conv_TDF(c, l, f, k, bn, bias=bias))

            # Final convolutional layer
            self.final_conv = nn.Sequential(
                nn.Conv2d(in_channels=c, out_channels=out_c, kernel_size=1, stride=1),
            )

            try:
                # Load model state from a file
                self.load_state_dict(
                    torch.load(
                        f"{model_path}/{target_name}.pt",
                        map_location=COMPUTATION_DEVICE,
                    )
                )
                print(f"Loading model ({target_name})")
            except FileNotFoundError:
                print(f"Random init ({target_name})")

    def forward(self, x):
        """
        Forward pass through the Conv_TDF_net_trimm network.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            torch.Tensor: Output tensor after passing through the network.
        """
        x = self.first_conv(x)

        x = x.transpose(-1, -2)

        ds_outputs = []
        for i in range(self.n):
            x = self.ds_dense[i](x)
            ds_outputs.append(x)
            x = self.ds[i](x)

        x = self.mid_dense(x)

        for i in range(self.n):
            x = self.us[i](x)
            x *= ds_outputs[-i - 1]
            x = self.us_dense[i](x)

        x = x.transpose(-1, -2)

        x = self.final_conv(x)

        return x


class Conv_TDF_net_trim(nn.Module):
    """
    Convolutional Time-Domain Filter (TDF) Network with Trimming.
    Used for drums and other.

    Args:
        L (int): This parameter controls the number of down-sampling (DS) blocks in the network.
                It's divided by 2 to determine how many DS blocks should be created.
        l (int): This parameter represents the number of convolutional layers (or filters) within each dense (fully connected) block.
        g (int): This parameter specifies the number of output channels for the first convolutional layer and is also used to determine the number of channels for subsequent layers in the network.
        dim_f (int): This parameter represents the number of frequency bins (spectrogram columns) in the input audio data.
        dim_t (int): This parameter represents the number of time frames (spectrogram rows) in the input audio data.
        k (int): This parameter specifies the size of convolutional kernels (filters) used in the network's convolutional layers.
        bn (int or None): This parameter controls whether batch normalization is used in the network.
                        If it's None, batch normalization may or may not be used based on other conditions in the code.
        bias (bool): This parameter is a boolean flag that controls whether bias terms are included in the convolutional layers.
        overlap (int): This parameter specifies the amount of overlap between consecutive chunks of audio data during processing.

    Attributes:
        n (int): The calculated number of down-sampling (DS) blocks.
        dim_f (int): The number of frequency bins (spectrogram columns) in the input audio data.
        dim_t (int): The number of time frames (spectrogram rows) in the input audio data.
        n_fft (int): The size of the Fast Fourier Transform (FFT) window.
        hop (int): The hop size used in the STFT calculations.
        n_bins (int): The number of bins in the frequency domain.
        chunk_size (int): The size of each chunk of audio data.
        target_name (str): The name of the target instrument being separated.
        overlap (int): The amount of overlap between consecutive chunks of audio data during processing.

    Methods:
        forward(x): Forward pass through the Conv_TDF_net_trimm network.
    """
    def __init__(self, model_path, use_onnx, target_name, 
                 L, l, g, dim_f, dim_t, k=3, hop=1024, bn=None, bias=False, overlap=1754):
        
        super(Conv_TDF_net_trim, self).__init__()
        
        n_fft_scale = {'drums', 'other'}
        
        out_c = in_c = 4
        self.n = L//2
        self.dim_f = 3072
        self.dim_t = 256
        self.n_fft = 7680
        self.hop = hop
        self.n_bins = self.n_fft//2+1
        self.chunk_size = hop * (self.dim_t-1)
        self.use_onnx = use_onnx
        self.target_name = target_name
        self.overlap = overlap
        self.window = nn.Parameter(torch.hann_window(window_length=self.n_fft, periodic=True), requires_grad=False)
        self.freq_pad = nn.Parameter(torch.zeros([1, dim_c, self.n_bins - self.dim_f, self.dim_t]), requires_grad=False)
               
        self.stft = STFT(self.n_fft, self.hop, self.dim_f)
                   
        if not use_onnx:
            
            scale = (2, 2)
            self.first_conv = nn.Sequential(
                nn.Conv2d(in_channels=4, out_channels=g, kernel_size=(1, 1)),
                nn.GroupNorm(2, g),
                nn.ReLU(),
            )

            f = self.dim_f
            c = g
            self.encoding_blocks = nn.ModuleList()
            self.ds = nn.ModuleList()
            for i in range(self.n):
                self.encoding_blocks.append(TFC_TDF(c, l, f, k, bn, bias=bias))
                self.ds.append(
                    nn.Sequential(
                        nn.Conv2d(in_channels=c, out_channels=c + g, kernel_size=scale, stride=scale),
                        nn.GroupNorm(2, c + g),
                        nn.ReLU()
                    )
                )
                f = f // 2
                c += g

            self.bottleneck_block = TFC_TDF(c, l, f, k, bn, bias=bias)

            self.decoding_blocks = nn.ModuleList()
            self.us = nn.ModuleList()
            for i in range(self.n):
                self.us.append(
                    nn.Sequential(
                        nn.ConvTranspose2d(in_channels=c, out_channels=c - g, kernel_size=scale, stride=scale),
                        nn.GroupNorm(2, c - g),
                        nn.ReLU()
                    )
                )
                f = f * 2
                c -= g

                self.decoding_blocks.append(TFC_TDF(c, l, f, k, bn, bias=bias))

            self.final_conv = nn.Sequential(
                nn.Conv2d(in_channels=c, out_channels=4, kernel_size=(1, 1)),
            )

            try:
                self.load_state_dict(
                    torch.load(f'{model_path}/{target_name}.pt', map_location=lambda storage, loc: storage)
                )
                print(f'Loading model ({target_name})')
            except FileNotFoundError:
                print(f'Random init ({target_name})') 
        
    
    def forward(self, x):

        x = self.first_conv(x)

        x = x.transpose(-1, -2)

        ds_outputs = []
        for i in range(self.n):
            x = self.encoding_blocks[i](x)
            ds_outputs.append(x)
            x = self.ds[i](x)

        x = self.bottleneck_block(x)

        for i in range(self.n):
            x = self.us[i](x)
            x *= ds_outputs[-i - 1]
            x = self.decoding_blocks[i](x)

        x = x.transpose(-1, -2)

        x = self.final_conv(x)

        return x


class Conv_TDF_net_trimmmm(nn.Module):
    """
    Convolutional Time-Domain Filter (TDF) Network with Trimming.
    Used for bass.

    Args:
        L (int): This parameter controls the number of down-sampling (DS) blocks in the network.
                 It's divided by 2 to determine how many DS blocks should be created.
        l (int): This parameter represents the number of convolutional layers (or filters) within each dense (fully connected) block.
        g (int): This parameter specifies the number of output channels for the first convolutional layer and is also used to determine the number of channels for subsequent layers in the network.
        dim_f (int): This parameter represents the number of frequency bins (spectrogram columns) in the input audio data.
        dim_t (int): This parameter represents the number of time frames (spectrogram rows) in the input audio data.
        k (int): This parameter specifies the size of convolutional kernels (filters) used in the network's convolutional layers.
        bn (int or None): This parameter controls whether batch normalization is used in the network.
                         If it's None, batch normalization may or may not be used based on other conditions in the code.
        bias (bool): This parameter is a boolean flag that controls whether bias terms are included in the convolutional layers.
        overlap (int): This parameter specifies the amount of overlap between consecutive chunks of audio data during processing.

    Attributes:
        n (int): The calculated number of down-sampling (DS) blocks.
        dim_f (int): The number of frequency bins (spectrogram columns) in the input audio data.
        dim_t (int): The number of time frames (spectrogram rows) in the input audio data.
        n_fft (int): The size of the Fast Fourier Transform (FFT) window.
        hop (int): The hop size used in the STFT calculations.
        n_bins (int): The number of bins in the frequency domain.
        chunk_size (int): The size of each chunk of audio data.
        target_name (str): The name of the target instrument being separated.
        overlap (int): The amount of overlap between consecutive chunks of audio data during processing.

    Methods:
        forward(x): Forward pass through the Conv_TDF_net_trimm network.
    """
    def __init__(self, model_path, use_onnx, target_name, 
                 L, l, g, dim_f, dim_t, k=3, hop=1024, bn=None, bias=False, overlap=1350):
        
        super(Conv_TDF_net_trimmmm, self).__init__()
        
        n_fft_scale = {'bass'}
        
        out_c = in_c = 4
        self.n = L//2
        self.dim_f = 2048
        self.dim_t = 256
        self.n_fft = 16384
        self.hop = hop
        self.n_bins = self.n_fft//2+1
        self.chunk_size = hop * (self.dim_t-1)
        self.use_onnx = use_onnx
        self.target_name = target_name
        self.overlap = overlap
        self.window = nn.Parameter(torch.hann_window(window_length=self.n_fft, periodic=True), requires_grad=False)
        self.freq_pad = nn.Parameter(torch.zeros([1, dim_c, self.n_bins - self.dim_f, self.dim_t]), requires_grad=False)
               
        self.stft = STFT(self.n_fft, self.hop, self.dim_f)
                   
        if not use_onnx:
            
            scale = (2, 2)
            self.first_conv = nn.Sequential(
                nn.Conv2d(in_channels=4, out_channels=g, kernel_size=(1, 1)),
                nn.GroupNorm(4, g),
                nn.ReLU(),
            )

            f = self.dim_f
            c = g
            self.encoding_blocks = nn.ModuleList()
            self.ds = nn.ModuleList()
            for i in range(self.n):
                self.encoding_blocks.append(TFC_TDFF(c, l, f, k, bn, bias=bias))
                self.ds.append(
                    nn.Sequential(
                        nn.Conv2d(in_channels=c, out_channels=c + g, kernel_size=scale, stride=scale),
                        nn.GroupNorm(4, c + g),
                        nn.ReLU()
                    )
                )
                f = f // 2
                c += g

            self.bottleneck_block = TFC_TDFF(c, l, f, k, bn, bias=bias)

            self.decoding_blocks = nn.ModuleList()
            self.us = nn.ModuleList()
            for i in range(self.n):
                self.us.append(
                    nn.Sequential(
                        nn.ConvTranspose2d(in_channels=c, out_channels=c - g, kernel_size=scale, stride=scale),
                        nn.GroupNorm(4, c - g),
                        nn.ReLU()
                    )
                )
                f = f * 2
                c -= g

                self.decoding_blocks.append(TFC_TDFF(c, l, f, k, bn, bias=bias))

            self.final_conv = nn.Sequential(
                nn.Conv2d(in_channels=c, out_channels=4, kernel_size=(1, 1)),
            )

            try:
                self.load_state_dict(
                    torch.load(f'{model_path}/{target_name}.pt', map_location=lambda storage, loc: storage)
                )
                print(f'Loading model ({target_name})')
            except FileNotFoundError:
                print(f'Random init ({target_name})') 
        
    
    def forward(self, x):

        x = self.first_conv(x)

        x = x.transpose(-1, -2)

        ds_outputs = []
        for i in range(self.n):
            x = self.encoding_blocks[i](x)
            ds_outputs.append(x)
            x = self.ds[i](x)

        x = self.bottleneck_block(x)

        for i in range(self.n):
            x = self.us[i](x)
            x *= ds_outputs[-i - 1]
            x = self.decoding_blocks[i](x)

        x = x.transpose(-1, -2)

        x = self.final_conv(x)

        return x