-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcounting_model.py
More file actions
204 lines (170 loc) · 7.41 KB
/
counting_model.py
File metadata and controls
204 lines (170 loc) · 7.41 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
###########################################
# https://github.com/Cyanogenoid/vqa-counting
###########################################
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from torch.autograd import Variable
from torch.nn.utils import weight_norm
from torch.nn.utils.rnn import pack_padded_sequence
import config
import counting
class Net(nn.Module):
""" Based on ``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
objects = 10
self.text = TextProcessor(
embedding_tokens=len(embedding_tokens)+1,
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=glimpses,
drop=0.5,
)
self.classifier = Classifier(
in_features=(glimpses * vision_features, question_features),
mid_features=1024,
out_features=config.max_answers,
count_features=objects + 1,
drop=0.5,
)
self.counter = counting.Counter(objects)
for m in self.modules():
if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d):
init.xavier_uniform_(m.weight)
if m.bias is not None:
m.bias.data.zero_()
def forward(self, v, b, q, v_mask, q_mask, q_len):
'''
v: visual feature [batch, num_obj, 2048]
b: bounding box [batch, num_obj, 4]
q: question [batch, max_q_len]
v_mask: number of obj [batch, max_obj] 1 is obj, 0 is none
q_mask: question length [batch, max_len] 1 is word, 0 is none
answer: predict logits [batch, config.max_answers]
'''
# question embedding
q = self.text(q, list(q_len.data)) # [batch, 1024]
# normalized visual feature
v = v.transpose(1,2).unsqueeze(2) # [batch, 2048, 1, num_obj]
v = v / (v.norm(p=2, dim=1, keepdim=True) + 1e-12).expand_as(v) # [batch, 2048, 1, num_obj]
# attention
a = self.attention(v, q) # [batch, num_glimpse, 1, num_obj]
v = apply_attention(v, a) # [batch, 4096]
# this is where the counting component is used
# pick out the first attention map
a1 = a[:, 0, :, :].contiguous().view(a.size(0), -1) # [batch, num_obj]
# give it and the bounding boxes to the component
count = self.counter(b.transpose(1,2), a1) # [batch, 11]
answer = self.classifier(v, q, count) # [batch, 3000]
return answer
class Fusion(nn.Module):
""" Crazy multi-modal fusion: negative squared difference minus relu'd sum
"""
def __init__(self):
super().__init__()
def forward(self, x, y):
# found through grad student descent ;)
return - (x - y)**2 + F.relu(x + y)
class Classifier(nn.Sequential):
def __init__(self, in_features, mid_features, count_features, out_features, drop=0.0):
super(Classifier, self).__init__()
self.drop = nn.Dropout(drop)
self.relu = nn.ReLU()
self.fusion = Fusion()
self.lin11 = nn.Linear(in_features[0], mid_features)
self.lin12 = nn.Linear(in_features[1], mid_features)
self.lin2 = nn.Linear(mid_features, out_features)
self.lin_c = nn.Linear(count_features, mid_features)
self.bn = nn.BatchNorm1d(mid_features)
self.bn2 = nn.BatchNorm1d(mid_features)
def forward(self, x, y, c):
x = self.fusion(self.lin11(self.drop(x)), self.lin12(self.drop(y)))
x = x + self.bn2(self.relu(self.lin_c(c)))
x = self.lin2(self.drop(self.bn(x)))
return x
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.GRU(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(3, 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)
_, h = self.lstm(packed)
return h.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)
self.fusion = Fusion()
def forward(self, v, q):
q_in = q
v = self.v_conv(self.drop(v))
q = self.q_lin(self.drop(q))
q = tile_2d_over_nd(q, v)
x = self.fusion(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, dim=1)
# 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, keepdim=True)
# 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_sizes = feature_map.size()[2:]
tiled = feature_vector.view(n, c, *([1] * len(spatial_sizes))).expand(n, c, *spatial_sizes)
return tiled