File size: 6,708 Bytes
35a4689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
DeepFilterNet 的原生实现不直接支持流式推理

社区开发者(如 Rikorose)提供了基于 Torch 的流式推理实现
https://github.com/grazder/DeepFilterNet/tree/1097015d53ced78fb234e7d7071a5dd4446e3952/torchDF

此文件试图实现一个支持流式推理的 dfnet

"""
import os
import math
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union

import numpy as np
import torch
import torch.nn as nn
from torch.nn import functional as F

from toolbox.torchaudio.configuration_utils import CONFIG_FILE
from toolbox.torchaudio.models.dfnet.configuration_dfnet import DfNetConfig
from toolbox.torchaudio.modules.conv_stft import ConvSTFT, ConviSTFT
from toolbox.torchaudio.modules.local_snr_target import LocalSnrTarget
from toolbox.torchaudio.modules.freq_bands.erb_bands import ErbBands


MODEL_FILE = "model.pt"


norm_layer_dict = {
    "batch_norm_2d": torch.nn.BatchNorm2d
}


activation_layer_dict = {
    "relu": torch.nn.ReLU,
    "identity": torch.nn.Identity,
    "sigmoid": torch.nn.Sigmoid,
}


class CausalConv2d(nn.Module):
    def __init__(self,
                 in_channels: int,
                 out_channels: int,
                 kernel_size: Union[int, Iterable[int]],
                 fstride: int = 1,
                 dilation: int = 1,
                 pad_f_dim: bool = True,
                 bias: bool = True,
                 separable: bool = False,
                 norm_layer: str = "batch_norm_2d",
                 activation_layer: str = "relu",
                 ):
        super(CausalConv2d, self).__init__()
        kernel_size = (kernel_size, kernel_size) if isinstance(kernel_size, int) else tuple(kernel_size)

        if pad_f_dim:
            fpad = kernel_size[1] // 2 + dilation - 1
        else:
            fpad = 0

        # for last 2 dim, pad (left, right, top, bottom).
        self.lookback = kernel_size[0] - 1
        if self.lookback > 0:
            self.tpad = nn.ConstantPad2d(padding=(0, 0, self.lookback, 0), value=0.0)
        else:
            self.tpad = nn.Identity()

        groups = math.gcd(in_channels, out_channels) if separable else 1
        if groups == 1:
            separable = False
        if max(kernel_size) == 1:
            separable = False

        self.conv = nn.Conv2d(
            in_channels,
            out_channels,
            kernel_size=kernel_size,
            padding=(0, fpad),
            stride=(1, fstride),  # stride over time is always 1
            dilation=(1, dilation),  # dilation over time is always 1
            groups=groups,
            bias=bias,
        )

        if separable:
            self.convp = nn.Conv2d(
                out_channels,
                out_channels,
                kernel_size=1,
                bias=False,
            )
        else:
            self.convp = nn.Identity()

        if norm_layer is not None:
            norm_layer = norm_layer_dict[norm_layer]
            self.norm = norm_layer(out_channels)
        else:
            self.norm = nn.Identity()

        if activation_layer is not None:
            activation_layer = activation_layer_dict[activation_layer]
            self.activation = activation_layer()
        else:
            self.activation = nn.Identity()

        super().__init__()

    def forward(self, inputs: torch.Tensor, cache: Tuple[torch.Tensor, torch.Tensor] = None):
        """
        :param inputs: shape: [b, c, t, f]
        :param cache: shape: [b, c, lookback, f];
        :return:
        """
        x = inputs

        if cache is None:
            x = self.tpad(x)
        else:
            x = torch.concat(tensors=[cache, x], dim=2)
        new_cache = x[:, :, -self.lookback:, :]

        x = self.conv(x)

        x = self.convp(x)
        x = self.norm(x)
        x = self.activation(x)

        return x, new_cache


class CausalConvTranspose2d(nn.Module):
    def __init__(self,
                 in_channels: int,
                 out_channels: int,
                 kernel_size: Union[int, Iterable[int]],
                 fstride: int = 1,
                 dilation: int = 1,
                 pad_f_dim: bool = True,
                 bias: bool = True,
                 separable: bool = False,
                 norm_layer: str = "batch_norm_2d",
                 activation_layer: str = "relu",
                 ):
        super(CausalConvTranspose2d, self).__init__()

        kernel_size = (kernel_size, kernel_size) if isinstance(kernel_size, int) else kernel_size

        if pad_f_dim:
            fpad = kernel_size[1] // 2
        else:
            fpad = 0

        # for last 2 dim, pad (left, right, top, bottom).
        self.lookback = kernel_size[0] - 1

        groups = math.gcd(in_channels, out_channels) if separable else 1
        if groups == 1:
            separable = False

        self.convt = nn.ConvTranspose2d(
            in_channels,
            out_channels,
            kernel_size=kernel_size,
            padding=(0, fpad),
            output_padding=(0, 0),
            stride=(1, fstride),  # stride over time is always 1
            dilation=(1, dilation),  # dilation over time is always 1
            groups=groups,
            bias=bias,
        )

        if separable:
            self.convp = nn.Conv2d(
                out_channels,
                out_channels,
                kernel_size=1,
                bias=False,
            )
        else:
            self.convp = nn.Identity()

        if norm_layer is not None:
            norm_layer = norm_layer_dict[norm_layer]
            self.norm = norm_layer(out_channels)
        else:
            self.norm = nn.Identity()

        if activation_layer is not None:
            activation_layer = activation_layer_dict[activation_layer]
            self.activation = activation_layer()
        else:
            self.activation = nn.Identity()

    def forward(self, inputs: torch.Tensor, cache: Tuple[torch.Tensor, torch.Tensor] = None):
        """
        :param inputs: shape: [b, c, t, f]
        :param cache: shape: [b, c, lookback, f];
        :return:
        """
        x = inputs

        # x shape: [b, c, t, f]
        x = self.convt(x)
        # x shape: [b, c, t+lookback, f]

        if cache is not None:
            x = torch.concat(tensors=[
                x[:, :, :self.lookback, :] + cache,
                x[:, :, self.lookback:, :]
            ], dim=2)
        x = x[:, :, :-self.lookback, :]
        new_cache = x[:, :, -self.lookback:, :]

        x = self.convp(x)
        x = self.norm(x)
        x = self.activation(x)

        return x, new_cache


if __name__ == "__main__":
    pass