-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVGGVox.py
94 lines (72 loc) · 3 KB
/
VGGVox.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
#! /usr/bin/python
# -*- encoding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Parameter
class VGGVox(nn.Module):
def __init__(self, nOut=251, encoder_type='SAP', log_input=False, **kwargs):
super(VGGVox, self).__init__()
print('Embedding size is %d, encoder %s.'%(nOut, encoder_type))
self.encoder_type = encoder_type
self.log_input = log_input
self.netcnn = nn.Sequential(
nn.Conv2d(1, 96, kernel_size=(5,7), stride=(1,2), padding=(2,2)),
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1,3), stride=(1,2)),
nn.Conv2d(96, 256, kernel_size=(5,5), stride=(2,2), padding=(1,1)),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(3,3), stride=(2,2)),
nn.Conv2d(256, 384, kernel_size=(3,3), padding=(1,1)),
nn.BatchNorm2d(384),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=(3,3), padding=(1,1)),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=(3,3), padding=(1,1)),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(3,3), stride=(2,2)),
nn.Conv2d(256, 512, kernel_size=(4,1), padding=(0,0)),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
)
if self.encoder_type == "MAX":
self.encoder = nn.AdaptiveMaxPool2d((1,1))
out_dim = 512
elif self.encoder_type == "TAP":
self.encoder = nn.AdaptiveAvgPool2d((1,1))
out_dim = 512
elif self.encoder_type == "SAP":
self.sap_linear = nn.Linear(512, 512)
self.attention = self.new_parameter(512, 1)
out_dim = 512
else:
raise ValueError('Undefined encoder')
self.fc = nn.Linear(out_dim, nOut)
self.instancenorm = nn.InstanceNorm1d(40)
def new_parameter(self, *size):
out = nn.Parameter(torch.FloatTensor(*size))
nn.init.xavier_normal_(out)
return out
def forward(self, x):
with torch.no_grad():
x = x + 1e-6
if self.log_input: x = x.log()
x = self.instancenorm(x).unsqueeze(1)
x = self.netcnn(x)
if self.encoder_type == "MAX" or self.encoder_type == "TAP":
x = self.encoder(x)
x = x.view((x.size()[0], -1))
elif self.encoder_type == "SAP":
x = x.permute(0, 2, 1, 3)
x = x.squeeze(dim=1).permute(0, 2, 1) # batch * L * D
h = torch.tanh(self.sap_linear(x))
w = torch.matmul(h, self.attention).squeeze(dim=2)
w = F.softmax(w, dim=1).view(x.size(0), x.size(1), 1)
x = torch.sum(x * w, dim=1)
x = self.fc(x)
return x
net = VGGVox(nOut=251, encoder_type="SAP", log_input=False)