-
Notifications
You must be signed in to change notification settings - Fork 23
/
example_overdrive-random.py
250 lines (208 loc) · 7.87 KB
/
example_overdrive-random.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
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
# This code is based on the following repository written by Christian J. Steinmetz
# https://github.com/csteinmetz1/micro-tcn
import logging
import os
from argparse import ArgumentParser
from pathlib import Path
from typing import Dict, List
import torch
import torch.nn as nn
from torch import Tensor
from neutone_sdk import WaveformToWaveformBase, NeutoneParameter, ContinuousNeutoneParameter
from neutone_sdk.tcn_1d import FiLM
from neutone_sdk.utils import save_neutone_model
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(level=os.environ.get("LOGLEVEL", "INFO"))
# TODO(christhetree): integrate this into tcn_1d.py
class PaddingCached(nn.Module): # to maintain signal continuity over sample windows
def __init__(self, padding: int, channels: int) -> None:
super().__init__()
self.padding = padding
self.channels = channels
pad = torch.zeros(1, self.channels, self.padding)
self.register_buffer("pad", pad)
def forward(self, x: Tensor) -> Tensor:
padded_x = torch.cat([self.pad, x], -1) # concat input signal to the cache
self.pad = padded_x[..., -self.padding :] # discard old cache
return padded_x
# TODO(christhetree): integrate this into tcn_1d.py
class Conv1dCached(nn.Module): # Conv1d with cache
def __init__(
self,
in_chan: int,
out_chan: int,
kernel: int,
stride: int,
padding: int,
dilation: int = 1,
weight_norm: bool = False,
bias: bool = False,
) -> None:
super().__init__()
self.pad = PaddingCached(padding * 2, in_chan)
self.conv = nn.Conv1d(
in_chan, out_chan, kernel, stride, dilation=dilation, bias=bias
)
nn.init.normal_(self.conv.weight) # random initialization
if weight_norm:
self.conv = nn.utils.weight_norm(self.conv)
def forward(self, x: Tensor) -> Tensor:
x = self.pad(x) # get (cached input + current input)
x = self.conv(x)
return x
# TODO(christhetree): integrate this into tcn_1d.py
class TCNBlock(nn.Module):
def __init__(
self,
in_ch: int,
out_ch: int,
kernel_size: int = 3,
dilation: int = 1,
cond_dim: int = 32,
) -> None:
super(TCNBlock, self).__init__()
self.in_ch = in_ch
self.out_ch = out_ch
self.kernel_size = kernel_size
padding = kernel_size // 2 * dilation
self.conv1 = Conv1dCached(
in_ch,
out_ch,
kernel=kernel_size,
stride=1,
padding=padding,
dilation=dilation,
bias=True,
)
self.res = nn.Conv1d(
in_ch, out_ch, kernel_size=1, groups=1, bias=False
) # residual connection
self.bn = nn.BatchNorm1d(out_ch)
self.film = FiLM(out_ch, cond_dim)
self.relu = nn.PReLU(out_ch)
def forward(self, x: Tensor, p: Tensor) -> Tensor:
x_in = x
x = self.conv1(x)
x = self.film(x, p)
x = self.bn(x)
x = self.relu(x)
# residual
x_res = self.res(x_in)
start = (x_res.shape[-1] - x.shape[-1]) // 2
stop = start + x.shape[-1]
x = x + x_res[..., start:stop]
return x
class OverdriveModel(nn.Module):
def __init__(
self,
ninputs: int = 1,
noutputs: int = 1,
nblocks: int = 4,
channel_growth: int = 0,
channel_width: int = 32,
kernel_size: int = 13,
dilation_growth: int = 2,
ncondition: int = 2,
) -> None:
super().__init__()
# MLP layers for conditioning
self.ncondition = ncondition
self.condition = torch.nn.Sequential(
torch.nn.Linear(ncondition, 16),
torch.nn.ReLU(),
torch.nn.Linear(16, 32),
torch.nn.ReLU(),
torch.nn.Linear(32, 32), # cond_dim = 32
torch.nn.ReLU(),
)
# main model
self.blocks = torch.nn.ModuleList()
for n in range(nblocks):
in_ch = out_ch if n > 0 else ninputs
out_ch = in_ch * channel_growth if channel_growth > 1 else channel_width
dilation = dilation_growth**n
self.blocks.append(
TCNBlock(in_ch, out_ch, kernel_size, dilation, cond_dim=32)
)
self.output = nn.Conv1d(out_ch, noutputs, kernel_size=1)
# random initialization
self.initialize_random()
def forward(self, x: Tensor, c: Tensor) -> Tensor:
p = self.condition(c) # conditioning
for _, block in enumerate(self.blocks):
x = block(x, p)
y = torch.tanh(self.output(x)) # clipping
return y
def weights_init(self, m: nn.Module) -> None:
classname = m.__class__.__name__
if classname == "Linear":
nn.init.normal_(m.weight, 0, 0.40)
def initialize_random(self) -> None:
for n in self.blocks:
nn.init.normal_(n.conv1.conv.weight, 0, 0.7)
# nn.init.normal_(self.output.weight, 0, 0.25)
self.condition.apply(self.weights_init)
class OverdriveModelWrapper(WaveformToWaveformBase):
def get_model_name(self) -> str:
return "conv1d-overdrive.random"
def get_model_authors(self) -> List[str]:
return ["Nao Tokui"]
def get_model_short_description(self) -> str:
return "Neural distortion/overdrive effect"
def get_model_long_description(self) -> str:
return "Neural distortion/overdrive effect through randomly initialized Convolutional Neural Network"
def get_technical_description(self) -> str:
return "Random distortion/overdrive effect through randomly initialized Temporal-1D-convolution layers. Based on the idea proposed by Steinmetz et al."
def get_tags(self) -> List[str]:
return ["distortion", "overdrive"]
def get_model_version(self) -> str:
return "1.0.0"
def is_experimental(self) -> bool:
return False
def get_technical_links(self) -> Dict[str, str]:
return {
"Paper": "https://arxiv.org/abs/2010.04237",
"Code": "https://github.com/csteinmetz1/micro-tcn",
}
def get_citation(self) -> str:
return "Steinmetz, C. J., & Reiss, J. D. (2020). Randomized overdrive neural networks. arXiv preprint arXiv:2010.04237."
def get_neutone_parameters(self) -> List[NeutoneParameter]:
return [
ContinuousNeutoneParameter("depth", "Effect Depth", 0.0),
ContinuousNeutoneParameter("P1", "Feature modulation 1", 0.0),
ContinuousNeutoneParameter("P2", "Feature modulation 2", 0.0),
]
@torch.jit.export
def is_input_mono(self) -> bool:
return False
@torch.jit.export
def is_output_mono(self) -> bool:
return False
@torch.jit.export
def get_native_sample_rates(self) -> List[int]:
return [] # Supports all sample rates
@torch.jit.export
def get_native_buffer_sizes(self) -> List[int]:
return [] # Supports all buffer sizes
def do_forward_pass(self, x: Tensor, params: Dict[str, Tensor]) -> Tensor:
# conditioning for FiLM layer
p1 = params["P1"]
p2 = params["P2"]
depth = params["depth"]
condition = torch.hstack([p1, p2]).reshape((1, -1)) * depth
# main process
for ch in range(x.shape[0]): # process channel by channel
x_ = x[ch].reshape(1, 1, -1)
x_ = self.model(x_, condition)
x[ch] = x_.squeeze()
return x
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-o", "--output", default="export_model")
args = parser.parse_args()
root_dir = Path(args.output)
model = OverdriveModel()
wrapper = OverdriveModelWrapper(model)
metadata = wrapper.to_metadata()
save_neutone_model(wrapper, root_dir, dump_samples=True, submission=True)