-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmodules.py
141 lines (124 loc) · 5.58 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
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
import torch
import torch.nn as nn
def set_fr_module(args):
"""
Create a frequency-representation module
"""
net = None
if args.fr_module_type == 'psnet':
net = PSnet(signal_dim=args.signal_dim, fr_size=args.fr_size, n_filters=args.fr_n_filters,
inner_dim=args.fr_inner_dim, n_layers=args.fr_n_layers, kernel_size=args.fr_kernel_size)
elif args.fr_module_type == 'fr':
assert args.fr_size == args.fr_inner_dim * args.fr_upsampling, \
'The desired size of the frequency representation (fr_size) must be equal to inner_dim*upsampling'
net = FrequencyRepresentationModule(signal_dim=args.signal_dim, n_filters=args.fr_n_filters,
inner_dim=args.fr_inner_dim, n_layers=args.fr_n_layers,
upsampling=args.fr_upsampling, kernel_size=args.fr_kernel_size,
kernel_out=args.fr_kernel_out)
else:
raise NotImplementedError('Frequency representation module type not implemented')
if args.use_cuda:
net.cuda()
return net
def set_fc_module(args):
"""
Create a frequency-counting module
"""
assert args.fr_size % args.fc_downsampling == 0, \
'The downsampling factor (fc_downsampling) does not divide the frequency representation size (fr_size)'
net = None
if args.fc_module_type == 'regression':
net = FrequencyCountingModule(n_output=1, n_layers=args.fc_n_layers, n_filters=args.fc_n_filters,
kernel_size=args.fc_kernel_size, fr_size=args.fr_size,
downsampling=args.fc_downsampling, kernel_in=args.fc_kernel_in)
elif args.fc_module_type == 'classification':
net = FrequencyCountingModule(n_output=args.max_num_freq, n_layers=args.fc_n_layers,
n_filters=args.fc_n_filters)
else:
NotImplementedError('Counter module type not implemented')
if args.use_cuda:
net.cuda()
return net
class PSnet(nn.Module):
def __init__(self, signal_dim=50, fr_size=1000, n_filters=8, inner_dim=100, n_layers=3, kernel_size=3):
super().__init__()
self.fr_size = fr_size
self.num_filters = n_filters
self.in_layer = nn.Linear(2 * signal_dim, inner_dim, bias=False)
mod = []
if torch.__version__ >= "1.7.0":
conv_padding = "same"
elif torch.__version__ >= "1.5.0":
conv_padding = kernel_size // 2
else:
conv_padding = kernel_size - 1
for n in range(n_layers):
in_filters = n_filters if n > 0 else 1
mod += [
nn.Conv1d(in_channels=in_filters, out_channels=n_filters, kernel_size=kernel_size,
stride=1, padding=conv_padding, bias=False),
nn.BatchNorm1d(n_filters),
nn.ReLU()
]
self.mod = nn.Sequential(*mod)
self.out_layer = nn.Linear(inner_dim * n_filters, fr_size, bias=True)
def forward(self, inp):
bsz = inp.size(0)
inp = inp.view(bsz, -1)
x = self.in_layer(inp).view(bsz, 1, -1)
x = self.mod(x).view(bsz, -1)
output = self.out_layer(x)
return output
class FrequencyRepresentationModule(nn.Module):
def __init__(self, signal_dim=50, n_filters=8, n_layers=3, inner_dim=125,
kernel_size=3, upsampling=8, kernel_out=25):
super().__init__()
self.fr_size = inner_dim * upsampling
self.n_filters = n_filters
self.in_layer = nn.Linear(2 * signal_dim, inner_dim * n_filters, bias=False)
mod = []
if torch.__version__ >= "1.7.0":
conv_padding = "same"
elif torch.__version__ >= "1.5.0":
conv_padding = kernel_size // 2
else:
conv_padding = kernel_size - 1
for n in range(n_layers):
mod += [
nn.Conv1d(n_filters, n_filters, kernel_size=kernel_size, padding=conv_padding, bias=False,
padding_mode='circular'),
nn.BatchNorm1d(n_filters),
nn.ReLU(),
]
self.mod = nn.Sequential(*mod)
self.out_layer = nn.ConvTranspose1d(n_filters, 1, kernel_out, stride=upsampling,
padding=(kernel_out - upsampling + 1) // 2, output_padding=1, bias=False)
def forward(self, inp):
bsz = inp.size(0)
inp = inp.view(bsz, -1)
x = self.in_layer(inp).view(bsz, self.n_filters, -1)
x = self.mod(x)
x = self.out_layer(x).view(bsz, -1)
return x
class FrequencyCountingModule(nn.Module):
def __init__(self, n_output, n_layers, n_filters, kernel_size, fr_size, downsampling, kernel_in):
super().__init__()
mod = [nn.Conv1d(1, n_filters, kernel_in, stride=downsampling, padding=kernel_in - downsampling,
padding_mode='circular')]
for i in range(n_layers):
mod += [
nn.Conv1d(n_filters, n_filters, kernel_size=kernel_size, padding=kernel_size - 1, bias=False,
padding_mode='circular'),
nn.BatchNorm1d(n_filters),
nn.ReLU(),
]
mod += [nn.Conv1d(n_filters, 1, 1)]
self.mod = nn.Sequential(*mod)
self.out_layer = nn.Linear(fr_size // downsampling, n_output)
def forward(self, inp):
bsz = inp.size(0)
inp = inp[:, None]
x = self.mod(inp)
x = x.view(bsz, -1)
y = self.out_layer(x)
return y