forked from jrgillick/laughter-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
289 lines (247 loc) · 10.1 KB
/
models.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os, sys, pickle, time, librosa, torch, numpy as np
from torch import nn
import torch.nn.functional as F
from torch import optim
from torch.distributions.categorical import Categorical
class MLPModel(nn.Module):
def __init__(self, linear_layer_size=101*40, hid_dim1=600, hid_dim2=100, dropout_rate=0.5,filter_sizes=None):
super().__init__()
print(f"training with dropout={dropout_rate}")
self.input_dim = linear_layer_size
self.hid_dim1 = hid_dim1
self.hid_dim2 = hid_dim2
self.dropout = nn.Dropout(dropout_rate)
self.linear1 = nn.Linear(self.input_dim, hid_dim1)
self.linear2 = nn.Linear(hid_dim1, hid_dim2)
self.linear3 = nn.Linear(hid_dim2, 1)
self.bn1 = nn.BatchNorm1d(num_features=hid_dim1)
self.bn2 = nn.BatchNorm1d(num_features=hid_dim2)
self.global_step = 0
self.epoch = 0
self.best_val_loss = np.inf
def forward(self, src):
src = src.view((-1,self.input_dim))
hidden1 = self.linear1(src)
hidden1 = self.bn1(hidden1)
hidden1 = self.dropout(hidden1)
hidden1 = F.relu(hidden1)
hidden2 = self.linear2(hidden1)
hidden2 = self.bn2(hidden2)
hidden2 = self.dropout(hidden2)
hidden2 = F.relu(hidden2)
output = self.linear3(hidden2)
output = torch.sigmoid(output)
return output
def set_device(self, device):
self.to(device)
class ResidualBlockNoBN(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(ResidualBlockNoBN, self).__init__()
# Conv Layer 1
self.conv1 = nn.Conv2d(
in_channels=in_channels, out_channels=out_channels,
kernel_size=(3, 3), stride=stride, padding=1, bias=True
)
# Conv Layer 2
self.conv2 = nn.Conv2d(
in_channels=out_channels, out_channels=out_channels,
kernel_size=(3, 3), stride=1, padding=1, bias=True
)
#self.bn2 = nn.BatchNorm2d(out_channels)
self.shortcut = nn.Sequential()
if stride != 1 or in_channels != out_channels:
self.shortcut = nn.Sequential(
nn.Conv2d(
in_channels=in_channels, out_channels=out_channels,
kernel_size=(1, 1), stride=stride, bias=False
)#,
) #nn.BatchNorm2d(out_channels)
#)
def forward(self, x):
out = nn.ReLU()(self.conv1(x))#out = nn.ReLU()(self.bn1(self.conv1(x)))
out = self.conv2(out)#out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = nn.ReLU()(out)
return out
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(ResidualBlock, self).__init__()
# Conv Layer 1
self.conv1 = nn.Conv2d(
in_channels=in_channels, out_channels=out_channels,
kernel_size=(3, 3), stride=stride, padding=1, bias=True
)
self.bn1 = nn.BatchNorm2d(out_channels)
# Conv Layer 2
self.conv2 = nn.Conv2d(
in_channels=out_channels, out_channels=out_channels,
kernel_size=(3, 3), stride=1, padding=1, bias=True
)
self.bn2 = nn.BatchNorm2d(out_channels)
self.shortcut = nn.Sequential()
if stride != 1 or in_channels != out_channels:
self.shortcut = nn.Sequential(
nn.Conv2d(
in_channels=in_channels, out_channels=out_channels,
kernel_size=(1, 1), stride=stride, bias=False
),
nn.BatchNorm2d(out_channels)
)
def forward(self, x):
out = nn.ReLU()(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = nn.ReLU()(out)
return out
class ResNet(nn.Module):
def __init__(self, num_classes=1, dropout_rate=0.5):
super(ResNet, self).__init__()
print(f"training with dropout={dropout_rate}")
# Initial input conv
self.conv1 = nn.Conv2d(
in_channels=1, out_channels=32, kernel_size=(3, 3),
stride=1, padding=1, bias=False
)
self.bn1 = nn.BatchNorm2d(32)
self.block1 = self._create_block(32, 32, stride=1)
self.block2 = self._create_block(32, 16, stride=2)
self.block3 = self._create_block(16, 16, stride=2)
self.block4 = self._create_block(16, 16, stride=2)
self.bn2 = nn.BatchNorm1d(192)
self.bn3 = nn.BatchNorm1d(32)
self.linear1 = nn.Linear(192, 32)
self.linear2 = nn.Linear(32, num_classes)
self.dropout = nn.Dropout(dropout_rate)
self.global_step = 0
self.epoch = 0
self.best_val_loss = np.inf
# A block is just two residual blocks for ResNet18
def _create_block(self, in_channels, out_channels, stride):
return nn.Sequential(
ResidualBlock(in_channels, out_channels, stride),
ResidualBlock(out_channels, out_channels, 1)
)
def forward(self, x):
# Output of one layer becomes input to the next
out = nn.ReLU()(self.bn1(self.conv1(x)))
out = self.block1(out)
out = self.block2(out)
out = self.block3(out)
out = self.block4(out)
out = nn.AvgPool2d(4)(out)
out = out.view(out.size(0), -1)
out = self.bn2(out)
out = self.dropout(out)
out = self.linear1(out)
out = self.bn3(out)
out = self.dropout(out)
out = F.relu(out)
out = self.linear2(out)
out = torch.sigmoid(out)
return out
def set_device(self, device):
for b in [self.block1, self.block2, self.block3, self.block4]:
b.to(device)
self.to(device)
class ResNetBigger(nn.Module):
def __init__(self, num_classes=1,dropout_rate=0.5,linear_layer_size=192,filter_sizes=[64,32,16,16]):
super(ResNetBigger, self).__init__()
print(f"training with dropout={dropout_rate}")
# Initial input conv
self.conv1 = nn.Conv2d(
in_channels=1, out_channels=64, kernel_size=(3, 3),
stride=1, padding=1, bias=False
)
self.bn1 = nn.BatchNorm2d(64)
self.linear_layer_size=linear_layer_size
self.filter_sizes = filter_sizes
self.block1 = self._create_block(64, filter_sizes[0], stride=1)
self.block2 = self._create_block(filter_sizes[0], filter_sizes[1], stride=2)
self.block3 = self._create_block(filter_sizes[1], filter_sizes[2], stride=2)
self.block4 = self._create_block(filter_sizes[2], filter_sizes[3], stride=2)
self.bn2 = nn.BatchNorm1d(linear_layer_size)
self.bn3 = nn.BatchNorm1d(32)
self.linear1 = nn.Linear(linear_layer_size, 32)
self.linear2 = nn.Linear(32, num_classes)
self.dropout = nn.Dropout(dropout_rate)
self.global_step = 0
self.epoch = 0
self.best_val_loss = np.inf
# A block is just two residual blocks for ResNet18
def _create_block(self, in_channels, out_channels, stride):
return nn.Sequential(
ResidualBlock(in_channels, out_channels, stride),
ResidualBlock(out_channels, out_channels, 1)
)
def forward(self, x):
# Output of one layer becomes input to the next
out = nn.ReLU()(self.bn1(self.conv1(x)))
out = self.block1(out)
out = self.block2(out)
out = self.block3(out)
out = self.block4(out)
out = nn.AvgPool2d(4)(out)
out = out.view(out.size(0), -1)
out = self.bn2(out)
out = self.dropout(out)
out = self.linear1(out)
out = self.bn3(out)
out = self.dropout(out)
out = F.relu(out)
out = self.linear2(out)
out = torch.sigmoid(out)
return out
def set_device(self, device):
for b in [self.block1, self.block2, self.block3, self.block4]:
b.to(device)
self.to(device)
class ResNetNoBN(nn.Module):
def __init__(self, num_classes=1,dropout_rate=0.5,linear_layer_size=192):
super(ResNetNoBN, self).__init__()
print(f"training with dropout={dropout_rate}")
# Initial input conv
self.conv1 = nn.Conv2d(
in_channels=1, out_channels=64, kernel_size=(3, 3),
stride=1, padding=1, bias=False
)
#self.bn1 = nn.BatchNorm2d(64)
self.linear_layer_size=linear_layer_size
# Create blocks
self.block1 = self._create_block(64, 64, stride=1)
self.block2 = self._create_block(64, 32, stride=2)
self.block3 = self._create_block(32, 16, stride=2)
self.block4 = self._create_block(16, 16, stride=2)
self.linear1 = nn.Linear(linear_layer_size, 32)
self.linear2 = nn.Linear(32, num_classes)
self.dropout = nn.Dropout(dropout_rate)
self.global_step = 0
self.epoch = 0
self.best_val_loss = np.inf
# A block is just two residual blocks for ResNet18
def _create_block(self, in_channels, out_channels, stride):
return nn.Sequential(
ResidualBlockNoBN(in_channels, out_channels, stride),
ResidualBlockNoBN(out_channels, out_channels, 1)
)
def forward(self, x):
# Output of one layer becomes input to the next
out=nn.ReLU()(self.conv1(x))#out = nn.ReLU()(self.bn1(self.conv1(x)))
out = self.block1(out)
out = self.block2(out)
out = self.block3(out)
out = self.block4(out)
out = nn.AvgPool2d(4)(out)
out = out.view(out.size(0), -1)
#out = self.bn2(out)
out = self.dropout(out)
out = self.linear1(out)
#out = self.bn3(out)
out = self.dropout(out)
out = F.relu(out)
out = self.linear2(out)
out = torch.sigmoid(out)
return out
def set_device(self, device):
for b in [self.block1, self.block2, self.block3, self.block4]:
b.to(device)
self.to(device)