File size: 11,607 Bytes
b6c45cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from scipy.signal import get_window
# from asteroid_test.losses import PITLossWrapper
from torch import nn

'''

class LambdaOverlapAdd(torch.nn.Module):

    """Overlap-add with lambda transform on segments.



    Segment input signal, apply lambda function (a neural network for example)

    and combine with OLA.



    Args:

        nnet (callable): Function to apply to each segment.

        n_src (int): Number of sources in the output of nnet.

        window_size (int): Size of segmenting window.

        hop_size (int): Segmentation hop size.

        window (str): Name of the window (see scipy.signal.get_window) used

            for the synthesis.

        reorder_chunks (bool): Whether to reorder each consecutive segment.

            This might be useful when `nnet` is permutation invariant, as

            source assignements might change output channel from one segment

            to the next (in classic speech separation for example).

            Reordering is performed based on the correlation between

            the overlapped part of consecutive segment.



     Examples:

        >>> from asteroid_test import ConvTasNet

        >>> nnet = ConvTasNet(n_src=2)

        >>> continuous_nnet = LambdaOverlapAdd(

        >>>     nnet=nnet,

        >>>     n_src=2,

        >>>     window_size=64000,

        >>>     hop_size=None,

        >>>     window="hanning",

        >>>     reorder_chunks=True,

        >>>     enable_grad=False,

        >>> )

        >>> wav = torch.randn(1, 1, 500000)

        >>> out_wavs = continuous_nnet.forward(wav)

    """



    def __init__(

        self,

        nnet,

        n_src,

        window_size,

        hop_size=None,

        window="hanning",

        reorder_chunks=True,

        enable_grad=False,

    ):

        super().__init__()

        assert window_size % 2 == 0, "Window size must be even"



        self.nnet = nnet

        self.window_size = window_size

        self.hop_size = hop_size if hop_size is not None else window_size // 2

        self.n_src = n_src



        if window:

            window = get_window(window, self.window_size).astype("float32")

            window = torch.from_numpy(window)

            self.use_window = True

        else:

            self.use_window = False



        self.register_buffer("window", window)

        self.reorder_chunks = reorder_chunks

        self.enable_grad = enable_grad



    def ola_forward(self, x):

        """Heart of the class: segment signal, apply func, combine with OLA."""



        assert x.ndim == 3



        batch, channels, n_frames = x.size()

        # Overlap and add:

        # [batch, chans, n_frames] -> [batch, chans, win_size, n_chunks]

        unfolded = torch.nn.functional.unfold(

            x.unsqueeze(-1),

            kernel_size=(self.window_size, 1),

            padding=(self.window_size, 0),

            stride=(self.hop_size, 1),

        )



        out = []

        n_chunks = unfolded.shape[-1]

        for frame_idx in range(n_chunks):  # for loop to spare memory

            frame = self.nnet(unfolded[..., frame_idx])

            # user must handle multichannel by reshaping to batch

            if frame_idx == 0:

                assert frame.ndim == 3, "nnet should return (batch, n_src, time)"

                assert frame.shape[1] == self.n_src, "nnet should return (batch, n_src, time)"

            frame = frame.reshape(batch * self.n_src, -1)



            if frame_idx != 0 and self.reorder_chunks:

                # we determine best perm based on xcorr with previous sources

                frame = _reorder_sources(

                    frame, out[-1], self.n_src, self.window_size, self.hop_size

                )



            if self.use_window:

                frame = frame * self.window

            else:

                frame = frame / (self.window_size / self.hop_size)

            out.append(frame)



        out = torch.stack(out).reshape(n_chunks, batch * self.n_src, self.window_size)

        out = out.permute(1, 2, 0)



        out = torch.nn.functional.fold(

            out,

            (n_frames, 1),

            kernel_size=(self.window_size, 1),

            padding=(self.window_size, 0),

            stride=(self.hop_size, 1),

        )

        return out.squeeze(-1).reshape(batch, self.n_src, -1)



    def forward(self, x):

        """Forward module: segment signal, apply func, combine with OLA.



        Args:

            x (:class:`torch.Tensor`): waveform signal of shape (batch, 1, time).



        Returns:

            :class:`torch.Tensor`: The output of the lambda OLA.

        """

        # Here we can do the reshaping

        with torch.autograd.set_grad_enabled(self.enable_grad):

            olad = self.ola_forward(x)

            return olad





def _reorder_sources(

    current: torch.FloatTensor,

    previous: torch.FloatTensor,

    n_src: int,

    window_size: int,

    hop_size: int,

):

    """

     Reorder sources in current chunk to maximize correlation with previous chunk.

     Used for Continuous Source Separation. Standard dsp correlation is used

     for reordering.





    Args:

        current (:class:`torch.Tensor`): current chunk, tensor

                                        of shape (batch, n_src, window_size)

        previous (:class:`torch.Tensor`): previous chunk, tensor

                                        of shape (batch, n_src, window_size)

        n_src (:class:`int`): number of sources.

        window_size (:class:`int`): window_size, equal to last dimension of

                                    both current and previous.

        hop_size (:class:`int`): hop_size between current and previous tensors.



    Returns:

        current:



    """

    batch, frames = current.size()

    current = current.reshape(-1, n_src, frames)

    previous = previous.reshape(-1, n_src, frames)



    overlap_f = window_size - hop_size



    def reorder_func(x, y):

        x = x[..., :overlap_f]

        y = y[..., -overlap_f:]

        # Mean normalization

        x = x - x.mean(-1, keepdim=True)

        y = y - y.mean(-1, keepdim=True)

        # Negative mean Correlation

        return -torch.sum(x.unsqueeze(1) * y.unsqueeze(2), dim=-1)



    # We maximize correlation-like between previous and current.

    pit = PITLossWrapper(reorder_func)

    current = pit(current, previous, return_est=True)[1]

    return current.reshape(batch, frames)

'''


class DualPathProcessing(nn.Module):
    """Perform Dual-Path processing via overlap-add as in DPRNN [1].



     Args:

        chunk_size (int): Size of segmenting window.

        hop_size (int): segmentation hop size.



    References:

        [1] "Dual-path RNN: efficient long sequence modeling for

            time-domain single-channel speech separation", Yi Luo, Zhuo Chen

            and Takuya Yoshioka. https://arxiv.org/abs/1910.06379

    """

    def __init__(self, chunk_size, hop_size):
        super(DualPathProcessing, self).__init__()
        self.chunk_size = chunk_size
        self.hop_size = hop_size
        self.n_orig_frames = None

    def unfold(self, x):
        """Unfold the feature tensor from



        (batch, channels, time) to (batch, channels, chunk_size, n_chunks).



        Args:

            x: (:class:`torch.Tensor`): feature tensor of shape (batch, channels, time).



        Returns:

            x: (:class:`torch.Tensor`): spliced feature tensor of shape

                (batch, channels, chunk_size, n_chunks).



        """
        # x is (batch, chan, frames)
        batch, chan, frames = x.size()
        assert x.ndim == 3
        self.n_orig_frames = x.shape[-1]
        unfolded = torch.nn.functional.unfold(
            x.unsqueeze(-1),
            kernel_size=(self.chunk_size, 1),
            padding=(self.chunk_size, 0),
            stride=(self.hop_size, 1),
        )

        return unfolded.reshape(
            batch, chan, self.chunk_size, -1
        )  # (batch, chan, chunk_size, n_chunks)

    def fold(self, x, output_size=None):
        """Folds back the spliced feature tensor.



        Input shape (batch, channels, chunk_size, n_chunks) to original shape

        (batch, channels, time) using overlap-add.



        Args:

            x: (:class:`torch.Tensor`): spliced feature tensor of shape

                (batch, channels, chunk_size, n_chunks).

            output_size: (int, optional): sequence length of original feature tensor.

                If None, the original length cached by the previous call of `unfold`

                will be used.



        Returns:

            x: (:class:`torch.Tensor`):  feature tensor of shape (batch, channels, time).



        .. note:: `fold` caches the original length of the pr



        """
        output_size = output_size if output_size is not None else self.n_orig_frames
        # x is (batch, chan, chunk_size, n_chunks)
        batch, chan, chunk_size, n_chunks = x.size()
        to_unfold = x.reshape(batch, chan * self.chunk_size, n_chunks)
        x = torch.nn.functional.fold(
            to_unfold,
            (output_size, 1),
            kernel_size=(self.chunk_size, 1),
            padding=(self.chunk_size, 0),
            stride=(self.hop_size, 1),
        )

        x /= self.chunk_size / self.hop_size

        return x.reshape(batch, chan, self.n_orig_frames)

    @staticmethod
    def intra_process(x, module):
        """Performs intra-chunk processing.



        Args:

            x (:class:`torch.Tensor`): spliced feature tensor of shape

                (batch, channels, chunk_size, n_chunks).

            module (:class:`torch.nn.Module`): module one wish to apply to each chunk

                of the spliced feature tensor.





        Returns:

            x (:class:`torch.Tensor`): processed spliced feature tensor of shape

                (batch, channels, chunk_size, n_chunks).



        .. note:: the module should have the channel first convention and accept

            a 3D tensor of shape (batch, channels, time).

        """

        # x is (batch, channels, chunk_size, n_chunks)
        batch, channels, chunk_size, n_chunks = x.size()
        # we reshape to batch*chunk_size, channels, n_chunks
        x = x.transpose(1, -1).reshape(batch * n_chunks, chunk_size, channels).transpose(1, -1)
        x = module(x)
        x = x.reshape(batch, n_chunks, channels, chunk_size).transpose(1, -1).transpose(1, 2)
        return x

    @staticmethod
    def inter_process(x, module):
        """Performs inter-chunk processing.



        Args:

            x (:class:`torch.Tensor`): spliced feature tensor of shape

                (batch, channels, chunk_size, n_chunks).

            module (:class:`torch.nn.Module`): module one wish to apply between

                each chunk of the spliced feature tensor.





        Returns:

            x (:class:`torch.Tensor`): processed spliced feature tensor of shape

                (batch, channels, chunk_size, n_chunks).



        .. note:: the module should have the channel first convention and accept

            a 3D tensor of shape (batch, channels, time).

        """

        batch, channels, chunk_size, n_chunks = x.size()
        x = x.transpose(1, 2).reshape(batch * chunk_size, channels, n_chunks)
        x = module(x)
        x = x.reshape(batch, chunk_size, channels, n_chunks).transpose(1, 2)
        return x