-
Notifications
You must be signed in to change notification settings - Fork 74
/
models.py
136 lines (111 loc) · 4.43 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
import torch.nn as nn
import torch.nn.functional as F
import torch
import numpy as np
from torch.autograd import Variable
from torchvision.models import resnet152
##############################
# Encoder
##############################
class Encoder(nn.Module):
def __init__(self, latent_dim):
super(Encoder, self).__init__()
resnet = resnet152(pretrained=True)
self.feature_extractor = nn.Sequential(*list(resnet.children())[:-1])
self.final = nn.Sequential(
nn.Linear(resnet.fc.in_features, latent_dim), nn.BatchNorm1d(latent_dim, momentum=0.01)
)
def forward(self, x):
with torch.no_grad():
x = self.feature_extractor(x)
x = x.view(x.size(0), -1)
return self.final(x)
##############################
# LSTM
##############################
class LSTM(nn.Module):
def __init__(self, latent_dim, num_layers, hidden_dim, bidirectional):
super(LSTM, self).__init__()
self.lstm = nn.LSTM(latent_dim, hidden_dim, num_layers, batch_first=True, bidirectional=bidirectional)
self.hidden_state = None
def reset_hidden_state(self):
self.hidden_state = None
def forward(self, x):
x, self.hidden_state = self.lstm(x, self.hidden_state)
return x
##############################
# Attention Module
##############################
class Attention(nn.Module):
def __init__(self, latent_dim, hidden_dim, attention_dim):
super(Attention, self).__init__()
self.latent_attention = nn.Linear(latent_dim, attention_dim)
self.hidden_attention = nn.Linear(hidden_dim, attention_dim)
self.joint_attention = nn.Linear(attention_dim, 1)
def forward(self, latent_repr, hidden_repr):
if hidden_repr is None:
hidden_repr = [
Variable(
torch.zeros(latent_repr.size(0), 1, self.hidden_attention.in_features), requires_grad=False
).float()
]
h_t = hidden_repr[0]
latent_att = self.latent_attention(latent_att)
hidden_att = self.hidden_attention(h_t)
joint_att = self.joint_attention(F.relu(latent_att + hidden_att)).squeeze(-1)
attention_w = F.softmax(joint_att, dim=-1)
return attention_w
##############################
# ConvLSTM
##############################
class ConvLSTM(nn.Module):
def __init__(
self, num_classes, latent_dim=512, lstm_layers=1, hidden_dim=1024, bidirectional=True, attention=True
):
super(ConvLSTM, self).__init__()
self.encoder = Encoder(latent_dim)
self.lstm = LSTM(latent_dim, lstm_layers, hidden_dim, bidirectional)
self.output_layers = nn.Sequential(
nn.Linear(2 * hidden_dim if bidirectional else hidden_dim, hidden_dim),
nn.BatchNorm1d(hidden_dim, momentum=0.01),
nn.ReLU(),
nn.Linear(hidden_dim, num_classes),
nn.Softmax(dim=-1),
)
self.attention = attention
self.attention_layer = nn.Linear(2 * hidden_dim if bidirectional else hidden_dim, 1)
def forward(self, x):
batch_size, seq_length, c, h, w = x.shape
x = x.view(batch_size * seq_length, c, h, w)
x = self.encoder(x)
x = x.view(batch_size, seq_length, -1)
x = self.lstm(x)
if self.attention:
attention_w = F.softmax(self.attention_layer(x).squeeze(-1), dim=-1)
x = torch.sum(attention_w.unsqueeze(-1) * x, dim=1)
else:
x = x[:, -1]
return self.output_layers(x)
##############################
# Conv2D Classifier
# (Baseline)
##############################
class ConvClassifier(nn.Module):
def __init__(self, num_classes, latent_dim):
super(ConvClassifier, self).__init__()
resnet = resnet152(pretrained=True)
self.feature_extractor = nn.Sequential(*list(resnet.children())[:-1])
self.final = nn.Sequential(
nn.Linear(resnet.fc.in_features, latent_dim),
nn.BatchNorm1d(latent_dim, momentum=0.01),
nn.Linear(latent_dim, num_classes),
nn.Softmax(dim=-1),
)
def forward(self, x):
batch_size, seq_length, c, h, w = x.shape
x = x.view(batch_size * seq_length, c, h, w)
x = self.feature_extractor(x)
x = x.view(batch_size * seq_length, -1)
x = self.final(x)
x = x.view(batch_size, seq_length, -1)
return x