-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodules.py
322 lines (263 loc) · 11.3 KB
/
modules.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import ipdb
import torch.utils.model_zoo as model_zoo
from torch.autograd import Variable
class FC(nn.Module):
def __init__(self, in_features, out_features, relu=True):
super(FC, self).__init__()
self.fc = nn.Linear(in_features, out_features)
self.relu = nn.ReLU(inplace=True) if relu else None
def forward(self, x):
x = self.fc(x)
if self.relu is not None:
x = self.relu(x)
return x
class MLP(nn.Module):
"""Two-layer fully-connected ELU net with batch norm."""
def __init__(self, n_in, n_hid, n_out, do_prob=0.):
super(MLP, self).__init__()
self.fc1 = nn.Linear(n_in, n_hid)
self.fc2 = nn.Linear(n_hid, n_out)
self.bn = nn.BatchNorm1d(n_out)
self.dropout_prob = do_prob
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight.data)
m.bias.data.fill_(0.1)
elif isinstance(m, nn.BatchNorm1d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def batch_norm(self, inputs):
x = inputs.view(inputs.size(0) * inputs.size(1), -1)
x = self.bn(x)
return x.view(inputs.size(0), inputs.size(1), -1)
def forward(self, inputs):
# Input shape: [num_sims, num_things, num_features]
x = F.elu(self.fc1(inputs))
x = F.dropout(x, self.dropout_prob, training=self.training)
x = F.elu(self.fc2(x))
return self.batch_norm(x)
class SimpleEncoder(nn.Module):
def __init__(self, n_hid, edge_types=71, node_types=101, do_prob=0., use_vis=True, use_spatial=True, use_sem=True, use_loc=False, use_cls=False):
super(SimpleEncoder, self).__init__()
self.use_vis = use_vis
self.use_spatial = use_spatial
self.use_sem = use_sem
self.use_loc = use_loc
self.use_cls = use_cls
# self.vis_hid = int(n_hid/2)
self.vis_hid = n_hid
self.sem_hid = n_hid
self.spatial_hid = n_hid
self.loc_hid = 64
self.cls_hid = 64
self.fc_vis = FC(4096, self.vis_hid)
self.fc_spatial = FC(512, self.spatial_hid)
self.fc_sem = FC(300, self.sem_hid)
self.fc_loc = FC(20, self.loc_hid)
n_fusion = 0
if self.use_vis:
n_fusion += self.vis_hid
if self.use_cls:
n_fusion += self.cls_hid
if self.use_spatial:
n_fusion += self.spatial_hid
if self.use_sem:
n_fusion += self.sem_hid
if self.use_loc:
n_fusion += self.loc_hid
# ---- sub obj concat ---------#
self.fc_so_vis = FC(self.vis_hid*2, self.vis_hid)
# ---- sub obj concat ---------#
self.fc_so_sem = FC(self.sem_hid*2, self.sem_hid)
# ---- all the feature into hidden space -------#
self.fc_fusion = FC(n_fusion, n_hid)
self.fc_rel = FC(n_hid, edge_types, relu=False)
if self.use_vis:
self.fc_cls = FC(4096, node_types, relu=False)
else:
self.fc_cls = FC(300, node_types, relu=False)
self.fc_so_cls = FC(node_types*2, self.cls_hid)
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight.data)
m.bias.data.fill_(0.1)
def node2edge(self, x, rel_rec, rel_send):
receivers = torch.matmul(rel_rec, x)
senders = torch.matmul(rel_send, x)
edges = torch.cat([receivers, senders], dim=2)
return edges
def forward(self, inputs, spatial_feats, rel_rec, rel_send, bbox_loc):
inputs = inputs[:, :, :].contiguous()
x = inputs.view(inputs.size(0), inputs.size(1), -1)
# New shape: [batch_size, num_nodes, num_dims]
if self.use_vis:
x_v = self.fc_vis(x[:, :, :4096]) #[batch_size, num_nodes, n_hid]
e_hid_v = self.node2edge(x_v, rel_rec, rel_send) #[batch_size, num_edges, n_hid*2]
e_v = self.fc_so_vis(e_hid_v) #[batch_size, num_edges, n_hid]
edge_feats = e_v #[batch_size, num_edges, n_hid]
x_cls = self.fc_cls(x[:, :, :4096])
if self.use_cls:
e_hid_cls = self.node2edge(x_cls, rel_rec, rel_send)
e_cls = self.fc_so_cls(e_hid_cls)
edge_feats = torch.cat([edge_feats, e_cls], -1)
if self.use_sem:
if self.use_vis:
x_s = self.fc_sem(x[:, :, 4096:]) #[batch_size, num_nodes, n_hid]
else:
x_s = self.fc_sem(x)
x_cls = self.fc_cls(x)
e_hid_s = self.node2edge(x_s, rel_rec, rel_send) #[batch_size, num_edges, n_hid*2]
e_s = self.fc_so_sem(e_hid_s) #[batch_size, num_edges, n_hid]
if self.use_vis:
edge_feats = torch.cat([edge_feats, e_s], -1) #[batch_size, num_edges, n_hid*2]
else:
edge_feats = e_s
if self.use_spatial:
e_l = self.fc_spatial(spatial_feats) #[batch_size, bun_edges, n_hid]
if self.use_vis or self.use_sem:
edge_feats = torch.cat([edge_feats, e_l], -1) #[batch_size, num_edges, n_hid*3]
else:
edge_feats = e_l
if self.use_loc:
e_loc = self.fc_loc(bbox_loc)
edge_feats = torch.cat([edge_feats, e_loc], -1)
self.edge_feats_final = self.fc_fusion(edge_feats)
output = self.fc_rel(self.edge_feats_final)
return output, x_cls
class NMPEncoder(nn.Module):
def __init__(self, n_hid, edge_types=71, node_types=101, n_iter=2, do_prob=0., use_vis=True, use_spatial=False, use_sem=True, use_loc=False, use_cls=False):
super(MLPEncoder, self).__init__()
self.use_vis = use_vis
self.use_spatial = use_spatial
self.use_sem = use_sem
self.use_loc = use_loc
self.use_cls = use_cls
self.n_iter = n_iter
self.vis_hid = 128
self.sem_hid = n_hid
self.spatial_hid = n_hid
self.loc_hid = 64
self.cls_hid = 64
self.mlp1 = MLP(n_hid * 2, n_hid, n_hid, do_prob)
self.mlp2 = MLP(n_hid, n_hid, n_hid, do_prob)
self.mlp3 = MLP(n_hid * 3, n_hid, n_hid, do_prob)
self.mlp4 = MLP(n_hid * 2, n_hid, n_hid, do_prob)
self.mlp5 = MLP(n_hid * 2, n_hid, n_hid, do_prob)
self.mlp_e2n = MLP(n_hid * 2, n_hid, n_hid, do_prob)
# ------- visual feature ---------#
# self.fc_vis = FC(4096, n_hid)
self.fc_vis = MLP(4096, self.vis_hid, self.vis_hid, do_prob)
# ------ spatial feature ---------#
# self.fc_spatial = FC(512, n_hid)
self.fc_spatial = MLP(512, self.spatial_hid, self.spatial_hid, do_prob)
# ------- semantic feature -------#
# self.fc_sem = FC(300, n_hid)
self.fc_sem = MLP(300, self.sem_hid, self.sem_hid, do_prob)
# ------- location feature -------#
self.fc_loc = MLP(20, self.loc_hid, self.loc_hid, do_prob)
n_fusion = 0
if self.use_vis:
n_fusion += self.vis_hid
if self.use_cls:
n_fusion += self.cls_hid
if self.use_sem:
n_fusion += self.sem_hid
final_fusion = n_hid
if self.use_loc:
final_fusion += self.loc_hid
# # ---- sub obj concat ---------#
# self.fc_so_vis = FC(n_hid*2, n_hid)
# # ---- sub obj concat ---------#
# self.fc_so_sem = FC(n_hid*2, n_hid)
# ---- all the feature into hidden space -------#
self.fc_fusion = FC(n_fusion, n_hid)
self.fc_rel = FC(final_fusion, edge_types, relu=False)
if self.use_vis:
self.fc_cls = FC(4096, node_types, relu=False)
else:
self.fc_cls = FC(300, node_types, relu=False)
self.fc_cls_feat = FC(node_types, self.cls_hid)
self.dropout_prob = do_prob
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight.data)
m.bias.data.fill_(0.1)
def node2edge(self, x, rel_rec, rel_send):
receivers = torch.matmul(rel_rec, x)
senders = torch.matmul(rel_send, x)
edges = torch.cat([receivers, senders], dim=2)
return edges
def edge2node(self, x, rel_rec, rel_send):
new_rec_rec = rel_rec.permute(0,2,1)
weight_rec = torch.sum(new_rec_rec, -1).float()
weight_rec = weight_rec + (weight_rec==0).float()
weight_rec = torch.unsqueeze(weight_rec, -1).expand(weight_rec.size(0), weight_rec.size(1), x.size(-1))
incoming = torch.matmul(new_rec_rec, x)
incoming = incoming / weight_rec
new_rec_send = rel_send.permute(0,2,1)
weight_send = torch.sum(new_rec_send, -1).float()
weight_send = weight_send + (weight_send==0).float()
weight_send = torch.unsqueeze(weight_send, -1).expand(weight_send.size(0), weight_send.size(1), x.size(-1))
outgoing = torch.matmul(new_rec_send, x)
outgoing = outgoing / weight_send
nodes = torch.cat([incoming, outgoing], -1)
# nodes = (incoming + outgoing) * 0.5
# nodes = incoming + outgoing
# nodes = outgoing
# nodes = incoming
return nodes
def forward(self, inputs, spatial_feats, rel_rec, rel_send, bbox_loc):
x = inputs.view(inputs.size(0), inputs.size(1), -1)
batch_size = inputs.size(0)
n_atoms = inputs.size(1)
n_edges = rel_rec.size(1)
if self.use_vis:
x_v = self.fc_vis(x[:, :, :4096]) #[batch_size, num_nodes, n_hid]
node_feats = x_v
if self.use_sem:
x_s = self.fc_sem(x[:, :, 4096:]) #[batch_size, num_nodes, n_hid]
node_feats = torch.cat([node_feats, x_s], -1)
x_cls = self.fc_cls(x[:, :, :4096])
if self.use_cls:
e_cls = self.fc_cls_feat(x_cls)
node_feats = torch.cat([node_feats, e_cls], -1)
else:
x_s = self.fc_sem(x)
node_feats = x_s
x_cls = self.fc_cls(x)
node_feats = self.fc_fusion(node_feats)
if self.use_spatial:
x_l = self.fc_spatial(spatial_feats)
edge_feats = x_l
else:
edge_feats = self.mlp1(self.node2edge(node_feats, rel_rec, rel_send))
x = edge_feats
x = self.mlp_e2n(self.edge2node(x, rel_rec, rel_send))
x = self.mlp2(x)
self.node_feats = x
x = self.node2edge(x, rel_rec, rel_send)
# # n2e
# x = self.mlp4(x)
# x = self.edge2node(x, rel_rec, rel_send)
# x = self.mlp5(x)
# x = self.node2edge(x, rel_rec, rel_send)
# [e_{ij}^1; e_{ij}^2]
x = torch.cat((x, edge_feats), dim=2) # Skip connection
self.edge_feats = self.mlp3(x)
# e_{ij}^2
# self.edge_feats = self.mlp4(x)
if self.use_loc:
e_loc = self.fc_loc(bbox_loc)
self.edge_feats = torch.cat([self.edge_feats, e_loc], -1)
output = self.fc_rel(self.edge_feats)
return output, x_cls