-
Notifications
You must be signed in to change notification settings - Fork 62
/
model.py
60 lines (48 loc) · 2.26 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
import torch
import torch.nn as nn
from torch.autograd import Variable
from MobileNetV2 import MobileNetV2
class EventDetector(nn.Module):
def __init__(self, pretrain, width_mult, lstm_layers, lstm_hidden, bidirectional=True, dropout=True):
super(EventDetector, self).__init__()
self.width_mult = width_mult
self.lstm_layers = lstm_layers
self.lstm_hidden = lstm_hidden
self.bidirectional = bidirectional
self.dropout = dropout
net = MobileNetV2(width_mult=width_mult)
state_dict_mobilenet = torch.load('mobilenet_v2.pth.tar')
if pretrain:
net.load_state_dict(state_dict_mobilenet)
self.cnn = nn.Sequential(*list(net.children())[0][:19])
self.rnn = nn.LSTM(int(1280*width_mult if width_mult > 1.0 else 1280),
self.lstm_hidden, self.lstm_layers,
batch_first=True, bidirectional=bidirectional)
if self.bidirectional:
self.lin = nn.Linear(2*self.lstm_hidden, 9)
else:
self.lin = nn.Linear(self.lstm_hidden, 9)
if self.dropout:
self.drop = nn.Dropout(0.5)
def init_hidden(self, batch_size):
if self.bidirectional:
return (Variable(torch.zeros(2*self.lstm_layers, batch_size, self.lstm_hidden).cuda(), requires_grad=True),
Variable(torch.zeros(2*self.lstm_layers, batch_size, self.lstm_hidden).cuda(), requires_grad=True))
else:
return (Variable(torch.zeros(self.lstm_layers, batch_size, self.lstm_hidden).cuda(), requires_grad=True),
Variable(torch.zeros(self.lstm_layers, batch_size, self.lstm_hidden).cuda(), requires_grad=True))
def forward(self, x, lengths=None):
batch_size, timesteps, C, H, W = x.size()
self.hidden = self.init_hidden(batch_size)
# CNN forward
c_in = x.view(batch_size * timesteps, C, H, W)
c_out = self.cnn(c_in)
c_out = c_out.mean(3).mean(2)
if self.dropout:
c_out = self.drop(c_out)
# LSTM forward
r_in = c_out.view(batch_size, timesteps, -1)
r_out, states = self.rnn(r_in, self.hidden)
out = self.lin(r_out)
out = out.view(batch_size*timesteps,9)
return out