-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
94 lines (70 loc) · 3.19 KB
/
model.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
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import math
import numpy as np
class LipNet(torch.nn.Module):
def __init__(self, dropout_p=0.5):
super(LipNet, self).__init__()
self.conv1 = nn.Conv3d(3, 32, (3, 5, 5), (1, 2, 2), (1, 2, 2))
self.pool1 = nn.MaxPool3d((1, 2, 2), (1, 2, 2))
self.conv2 = nn.Conv3d(32, 64, (3, 5, 5), (1, 1, 1), (1, 2, 2))
self.pool2 = nn.MaxPool3d((1, 2, 2), (1, 2, 2))
self.conv3 = nn.Conv3d(64, 96, (3, 3, 3), (1, 1, 1), (1, 1, 1))
self.pool3 = nn.MaxPool3d((1, 2, 2), (1, 2, 2))
self.gru1 = nn.GRU(96*4*8, 256, 1, bidirectional=True)
self.gru2 = nn.GRU(512, 256, 1, bidirectional=True)
self.FC = nn.Linear(512, 27+1)
self.dropout_p = dropout_p
self.relu = nn.ReLU(inplace=True)
self.dropout = nn.Dropout(self.dropout_p)
self.dropout3d = nn.Dropout3d(self.dropout_p)
self._init()
def _init(self):
init.kaiming_normal_(self.conv1.weight, nonlinearity='relu')
init.constant_(self.conv1.bias, 0)
init.kaiming_normal_(self.conv2.weight, nonlinearity='relu')
init.constant_(self.conv2.bias, 0)
init.kaiming_normal_(self.conv3.weight, nonlinearity='relu')
init.constant_(self.conv3.bias, 0)
init.kaiming_normal_(self.FC.weight, nonlinearity='sigmoid')
init.constant_(self.FC.bias, 0)
for m in (self.gru1, self.gru2):
stdv = math.sqrt(2 / (96 * 3 * 6 + 256))
for i in range(0, 256 * 3, 256):
init.uniform_(m.weight_ih_l0[i: i + 256],
-math.sqrt(3) * stdv, math.sqrt(3) * stdv)
init.orthogonal_(m.weight_hh_l0[i: i + 256])
init.constant_(m.bias_ih_l0[i: i + 256], 0)
init.uniform_(m.weight_ih_l0_reverse[i: i + 256],
-math.sqrt(3) * stdv, math.sqrt(3) * stdv)
init.orthogonal_(m.weight_hh_l0_reverse[i: i + 256])
init.constant_(m.bias_ih_l0_reverse[i: i + 256], 0)
def forward(self, x, return_video_features=False):
x = self.conv1(x)
x = self.relu(x)
x = self.dropout3d(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.relu(x)
x = self.dropout3d(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.relu(x)
x = self.dropout3d(x)
x = self.pool3(x)
# (B, C, T, H, W)->(T, B, C, H, W)
x = x.permute(2, 0, 1, 3, 4).contiguous()
# (B, C, T, H, W)->(T, B, C*H*W)
x = x.view(x.size(0), x.size(1), -1)
self.gru1.flatten_parameters()
self.gru2.flatten_parameters()
x, h = self.gru1(x)
x = self.dropout(x)
x, h = self.gru2(x)
video_features = x
x = self.dropout(x)
x = self.FC(x)
x = x.permute(1, 0, 2).contiguous()
return video_features if return_video_features else x