-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
155 lines (126 loc) · 5.46 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from torch.nn.utils.rnn import pack_padded_sequence
import config
class Net(nn.Module):
""" Re-implementation of ``Show, Ask, Attend, and Answer: A Strong Baseline For Visual Question Answering'' [0]
[0]: https://arxiv.org/abs/1704.03162
"""
def __init__(self, embedding_tokens):
super(Net, self).__init__()
question_features = 1024
vision_features = config.output_features
glimpses = 2
self.text = TextProcessor(
embedding_tokens=embedding_tokens,
embedding_features=300,
lstm_features=question_features,
drop=0.5,
)
self.attention = Attention(
v_features=vision_features,
q_features=question_features,
mid_features=512,
glimpses=2,
drop=0.5,
)
self.classifier = Classifier(
in_features=glimpses * vision_features + question_features,
mid_features=1024,
out_features=config.max_answers,
drop=0.5,
)
for m in self.modules():
if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d):
init.kaiming_uniform(m.weight,nonlinearity='relu')
if m.bias is not None:
m.bias.data.zero_()
def forward(self, v, q, q_len):
q = self.text(q, list(q_len.data))
v = v / (v.norm(p=2, dim=1, keepdim=True).expand_as(v) + 1e-8)
a = self.attention(v, q)
v = apply_attention(v, a)
combined = torch.cat([v, q], dim=1)
answer = self.classifier(combined)
return answer
class Classifier(nn.Sequential):
def __init__(self, in_features, mid_features, out_features, drop=0.0):
super(Classifier, self).__init__()
self.add_module('drop1', nn.Dropout(drop))
self.add_module('lin1', nn.Linear(in_features, mid_features))
self.add_module('relu', nn.ReLU())
self.add_module('drop2', nn.Dropout(drop))
self.add_module('lin2', nn.Linear(mid_features, out_features))
class TextProcessor(nn.Module):
def __init__(self, embedding_tokens, embedding_features, lstm_features, drop=0.0):
super(TextProcessor, self).__init__()
self.embedding = nn.Embedding(embedding_tokens, embedding_features, padding_idx=0)
self.drop = nn.Dropout(drop)
self.tanh = nn.Tanh()
self.lstm = nn.LSTM(input_size=embedding_features,
hidden_size=lstm_features,
num_layers=1)
self.features = lstm_features
self._init_lstm(self.lstm.weight_ih_l0)
self._init_lstm(self.lstm.weight_hh_l0)
self.lstm.bias_ih_l0.data.zero_()
self.lstm.bias_hh_l0.data.zero_()
init.xavier_uniform(self.embedding.weight)
def _init_lstm(self, weight):
for w in weight.chunk(4, 0):
init.xavier_uniform(w)
def forward(self, q, q_len):
embedded = self.embedding(q)
tanhed = self.tanh(self.drop(embedded))
packed = pack_padded_sequence(tanhed, q_len, batch_first=True)
_, (_, c) = self.lstm(packed)
return c.squeeze(0)
class Attention(nn.Module):
def __init__(self, v_features, q_features, mid_features, glimpses, drop=0.0):
super(Attention, self).__init__()
self.v_conv = nn.Conv2d(v_features, mid_features, 1, bias=False) # let self.lin take care of bias
self.q_lin = nn.Linear(q_features, mid_features)
self.x_conv = nn.Conv2d(mid_features, glimpses, 1)
self.drop = nn.Dropout(drop)
self.relu = nn.ReLU(inplace=True)
def forward(self, v, q):
v = self.v_conv(self.drop(v))
q = self.q_lin(self.drop(q))
q = tile_2d_over_nd(q, v)
x = self.relu(v + q)
x = self.x_conv(self.drop(x))
return x
def apply_attention(input, attention):
""" Apply any number of attention maps over the input.
The attention map has to have the same size in all dimensions except dim=1.
"""
n, c = input.size()[:2]
glimpses = attention.size(1)
# flatten the spatial dims into the third dim, since we don't need to care about how they are arranged
input = input.view(n, c, -1)
attention = attention.view(n, glimpses, -1)
s = input.size(2)
# apply a softmax to each attention map separately
# since softmax only takes 2d inputs, we have to collapse the first two dimensions together
# so that each glimpse is normalized separately
attention = attention.view(n * glimpses, -1)
attention = F.softmax(attention)
# apply the weighting by creating a new dim to tile both tensors over
target_size = [n, glimpses, c, s]
input = input.view(n, 1, c, s).expand(*target_size)
attention = attention.view(n, glimpses, 1, s).expand(*target_size)
weighted = input * attention
# sum over only the spatial dimension
weighted_mean = weighted.sum(dim=3)
# the shape at this point is (n, glimpses, c, 1)
return weighted_mean.view(n, -1)
def tile_2d_over_nd(feature_vector, feature_map):
""" Repeat the same feature vector over all spatial positions of a given feature map.
The feature vector should have the same batch size and number of features as the feature map.
"""
n, c = feature_vector.size()
spatial_size = feature_map.dim() - 2
tiled = feature_vector.view(n, c, *([1] * spatial_size)).expand_as(feature_map)
return tiled