-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmodules.py
78 lines (65 loc) · 2.28 KB
/
modules.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
import math
import torch
from torch import nn
from torch.nn import functional as F
import commons
class ResidualStack(nn.Module):
def __init__(self, channels):
super().__init__()
self.channels = channels
self.res_1 = nn.Sequential(
nn.LeakyReLU(),
nn.Conv1d(channels, channels, 3, padding=commons.get_same_padding(3)),
nn.LeakyReLU(),
nn.Conv1d(channels, channels, 3, padding=commons.get_same_padding(3))
)
self.res_2 = nn.Sequential(
nn.LeakyReLU(),
nn.Conv1d(channels, channels, 3, dilation=3, padding=commons.get_same_padding(3, 3)),
nn.LeakyReLU(),
nn.Conv1d(channels, channels, 3, padding=commons.get_same_padding(3))
)
self.res_3 = nn.Sequential(
nn.LeakyReLU(),
nn.Conv1d(channels, channels, 3, dilation=9, padding=commons.get_same_padding(3, 9)),
nn.LeakyReLU(),
nn.Conv1d(channels, channels, 3, padding=commons.get_same_padding(3))
)
nn.utils.weight_norm(self.res_1[1])
nn.utils.weight_norm(self.res_1[3])
nn.utils.weight_norm(self.res_2[1])
nn.utils.weight_norm(self.res_2[3])
nn.utils.weight_norm(self.res_3[1])
nn.utils.weight_norm(self.res_3[3])
def forward(self, x):
for l in [self.res_1, self.res_2, self.res_3]:
x_ = l(x)
x = x + x_
return x
def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.res_1[1])
nn.utils.remove_weight_norm(self.res_1[3])
nn.utils.remove_weight_norm(self.res_2[1])
nn.utils.remove_weight_norm(self.res_2[3])
nn.utils.remove_weight_norm(self.res_3[1])
nn.utils.remove_weight_norm(self.res_3[3])
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = stride
self.pre = nn.Sequential(
nn.LeakyReLU(),
nn.ConvTranspose1d(in_channels, out_channels, kernel_size, stride, padding=stride//2)
)
self.res_stack = ResidualStack(out_channels)
nn.utils.weight_norm(self.pre[1])
def forward(self, x):
x = self.pre(x)
x = self.res_stack(x)
return x
def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.pre[1])
self.res_stack.remove_weight_norm()