-
Notifications
You must be signed in to change notification settings - Fork 6
/
denoising_model_small.py
160 lines (123 loc) · 5.57 KB
/
denoising_model_small.py
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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary
from math import log as ln
import leaf_audio_pytorch.frontend as frontend
class Conv1d(nn.Conv1d):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.reset_parameters()
def reset_parameters(self):
nn.init.kaiming_normal_(self.weight)
nn.init.zeros_(self.bias)
class PositionalEncoding(nn.Module):
def __init__(self, dim):
super().__init__()
self.dim = dim
def forward(self, noise_level):
noise_level=noise_level.view(-1)
count = self.dim // 2
step = torch.arange(count, dtype=noise_level.dtype,
device=noise_level.device) / count
encoding = noise_level.unsqueeze(
1) * torch.exp(-ln(1e4) * step.unsqueeze(0))
encoding = torch.cat(
[torch.sin(encoding), torch.cos(encoding)], dim=-1)
return encoding
class FeatureWiseAffine(nn.Module):
def __init__(self, in_channels, out_channels, use_affine_level=False):
super().__init__()
self.use_affine_level = use_affine_level
self.noise_func = nn.Sequential(
nn.Linear(in_channels, out_channels*(1+self.use_affine_level))
)
def forward(self, x, noise_embed):
batch = x.shape[0]
if self.use_affine_level:
gamma, beta = self.noise_func(noise_embed).view(
batch, -1, 1).chunk(2, dim=1)
x = (1 + gamma) * x + beta
else:
x = x + self.noise_func(noise_embed).view(batch, -1, 1)
return x
class HNFBlock(nn.Module):
def __init__(self, input_size, hidden_size, dilation):
super().__init__()
self.filters = nn.ModuleList([
Conv1d(input_size, hidden_size//4, 3, dilation=dilation, padding=1*dilation, padding_mode='reflect'),
Conv1d(hidden_size, hidden_size//4, 5, dilation=dilation, padding=2*dilation, padding_mode='reflect'),
Conv1d(hidden_size, hidden_size//4, 9, dilation=dilation, padding=4*dilation, padding_mode='reflect'),
Conv1d(hidden_size, hidden_size//4, 15, dilation=dilation, padding=7*dilation, padding_mode='reflect'),
])
self.conv_1 = Conv1d(hidden_size, hidden_size, 9, padding=4, padding_mode='reflect')
self.norm = nn.InstanceNorm1d(hidden_size//2)
self.conv_2 = Conv1d(hidden_size, hidden_size, 9, padding=4, padding_mode='reflect')
def forward(self, x):
residual = x
filts = []
for layer in self.filters:
filts.append(layer(x))
filts = torch.cat(filts, dim=1)
nfilts, filts = self.conv_1(filts).chunk(2, dim=1)
filts = F.leaky_relu(torch.cat([self.norm(nfilts), filts], dim=1), 0.2)
filts = F.leaky_relu(self.conv_2(filts), 0.2)
return filts + residual
class Bridge(nn.Module):
def __init__(self, input_size, hidden_size):
super().__init__()
self.encoding = FeatureWiseAffine(input_size, hidden_size, use_affine_level=1)
self.input_conv = Conv1d(input_size, input_size, 3, padding=1, padding_mode='reflect')
self.output_conv = Conv1d(input_size, hidden_size, 3, padding=1, padding_mode='reflect')
def forward(self, x, noise_embed):
x = self.input_conv(x)
x = self.encoding(x, noise_embed)
return self.output_conv(x)
class ConditionalModel(nn.Module):
def __init__(self, feats=64):
super(ConditionalModel, self).__init__()
self.stream_x = nn.ModuleList([
nn.Sequential(Conv1d(1, feats, 9, padding=4, padding_mode='reflect'),
nn.LeakyReLU(0.2)),
HNFBlock(feats, feats, 1),
HNFBlock(feats, feats, 2),
HNFBlock(feats, feats, 4),
HNFBlock(feats, feats, 2),
HNFBlock(feats, feats, 1),
])
self.stream_cond = nn.ModuleList([
nn.Sequential(Conv1d(1, feats, 9, padding=4, padding_mode='reflect'),
nn.LeakyReLU(0.2)),
HNFBlock(feats, feats, 1),
HNFBlock(feats, feats, 2),
HNFBlock(feats, feats, 4),
HNFBlock(feats, feats, 2),
HNFBlock(feats, feats, 1),
])
self.embed = PositionalEncoding(feats)
self.bridge = nn.ModuleList([
Bridge(feats, feats),
Bridge(feats, feats),
Bridge(feats, feats),
Bridge(feats, feats),
Bridge(feats, feats),
])
self.conv_out = Conv1d(feats, 1, 9, padding=4, padding_mode='reflect')
def forward(self, x, cond, noise_scale):
noise_embed = self.embed(noise_scale)
xs = []
for layer, br in zip(self.stream_x, self.bridge):
x = layer(x)
xs.append(br(x, noise_embed))
for x, layer in zip(xs, self.stream_cond):
cond = layer(cond)+x
return self.conv_out(cond)
if __name__ == "__main__":
net = ConditionalModel(80).cuda()
#leaf = frontend.Leaf(sample_rate=400, n_filters=128, window_len=65, window_stride=40).cuda()
x = torch.randn(10,1,512).cuda()
y = torch.randn(10,1).cuda()
z = net(x,x,y)
#print(z.shape)
summary(net)