-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmodel_modules.py
252 lines (201 loc) · 8.75 KB
/
model_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
import os
import math
import time
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from models_kp import SpatialSoftmax
from data import denormalize, normalize
from utils import load_data, count_parameters
class GRUNet(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, n_layers=2, drop_prob=0.2):
super(GRUNet, self).__init__()
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.output_dim = output_dim
self.gru = nn.GRU(input_dim, hidden_dim, n_layers, batch_first=True, dropout=drop_prob)
self.fc = nn.Linear(hidden_dim, output_dim)
self.relu = nn.ReLU()
def forward(self, x, h=None):
# x: B x T x nf
# h: n_layers x B x nf
B, T, nf = x.size()
if h is None:
h = self.init_hidden(B)
out, h = self.gru(x, h)
# out: B x T x nf
# h: n_layers x B x nf
out = self.fc(self.relu(out.contiguous().view(B * T, self.hidden_dim)))
out = out.view(B, T, self.output_dim)
# out: B x output_dim
return out[:, -1]
def init_hidden(self, batch_size):
weight = next(self.parameters()).data
hidden = weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda()
return hidden
class CNNet(nn.Module):
def __init__(self, ks, nf_in, nf_hidden, nf_out, do_prob=0.):
super(CNNet, self).__init__()
self.pool = nn.MaxPool1d(
kernel_size=2, stride=None, padding=0,
dilation=1, return_indices=False,
ceil_mode=False)
self.conv1 = nn.Conv1d(nf_in, nf_hidden, kernel_size=ks, stride=1, padding=0)
self.bn1 = nn.BatchNorm1d(nf_hidden)
self.conv2 = nn.Conv1d(nf_hidden, nf_hidden, kernel_size=ks, stride=1, padding=0)
self.bn2 = nn.BatchNorm1d(nf_hidden)
self.conv3 = nn.Conv1d(nf_hidden, nf_hidden, kernel_size=ks, stride=1, padding=0)
self.bn3 = nn.BatchNorm1d(nf_hidden)
self.conv_predict = nn.Conv1d(nf_hidden, nf_out, kernel_size=1)
self.conv_attention = nn.Conv1d(nf_hidden, 1, kernel_size=1)
self.dropout_prob = do_prob
def forward(self, inputs):
# inputs: B x T x nf_in
inputs = inputs.transpose(1, 2)
# inputs: B x nf_in x T
x = F.relu(self.conv1(inputs))
x = self.bn1(x)
x = F.dropout(x, self.dropout_prob, training=self.training)
x = self.pool(x)
x = F.relu(self.conv2(x))
x = self.bn2(x)
x = F.dropout(x, self.dropout_prob, training=self.training)
x = self.pool(x)
x = F.relu(self.conv3(x))
x = self.bn3(x)
pred = self.conv_predict(x)
# ret: B x nf_out
ret = pred.max(dim=2)[0]
return ret
class PropNet(nn.Module):
def __init__(self, node_dim_in, edge_dim_in, nf_hidden, node_dim_out, edge_dim_out,
edge_type_num=1, pstep=2, batch_norm=1, use_gpu=True):
super(PropNet, self).__init__()
self.node_dim_in = node_dim_in
self.edge_dim_in = edge_dim_in
self.nf_hidden = nf_hidden
self.node_dim_out = node_dim_out
self.edge_dim_out = edge_dim_out
self.edge_type_num = edge_type_num
self.pstep = pstep
# node encoder
modules = [
nn.Linear(node_dim_in, nf_hidden),
nn.ReLU()]
if batch_norm == 1:
modules.append(nn.BatchNorm1d(nf_hidden))
self.node_encoder = nn.Sequential(*modules)
# edge encoder
self.edge_encoders = nn.ModuleList()
for i in range(edge_type_num):
modules = [
nn.Linear(node_dim_in * 2 + edge_dim_in, nf_hidden),
nn.ReLU()]
if batch_norm == 1:
modules.append(nn.BatchNorm1d(nf_hidden))
self.edge_encoders.append(nn.Sequential(*modules))
# node propagator
modules = [
# input: node_enc, node_rep, edge_agg
nn.Linear(nf_hidden * 3, nf_hidden),
nn.ReLU(),
nn.Linear(nf_hidden, nf_hidden),
nn.ReLU()]
if batch_norm == 1:
modules.append(nn.BatchNorm1d(nf_hidden))
self.node_propagator = nn.Sequential(*modules)
# edge propagator
self.edge_propagators = nn.ModuleList()
for i in range(pstep):
edge_propagator = nn.ModuleList()
for j in range(edge_type_num):
modules = [
# input: node_rep * 2, edge_enc, edge_rep
nn.Linear(nf_hidden * 3, nf_hidden),
nn.ReLU(),
nn.Linear(nf_hidden, nf_hidden),
nn.ReLU()]
if batch_norm == 1:
modules.append(nn.BatchNorm1d(nf_hidden))
edge_propagator.append(nn.Sequential(*modules))
self.edge_propagators.append(edge_propagator)
# node predictor
modules = [
nn.Linear(nf_hidden * 2, nf_hidden),
nn.ReLU()]
if batch_norm == 1:
modules.append(nn.BatchNorm1d(nf_hidden))
modules.append(nn.Linear(nf_hidden, node_dim_out))
self.node_predictor = nn.Sequential(*modules)
# edge predictor
modules = [
nn.Linear(nf_hidden * 2, nf_hidden),
nn.ReLU()]
if batch_norm == 1:
modules.append(nn.BatchNorm1d(nf_hidden))
modules.append(nn.Linear(nf_hidden, edge_dim_out))
self.edge_predictor = nn.Sequential(*modules)
def forward(self, node_rep, edge_rep=None, edge_type=None, start_idx=0,
ignore_node=False, ignore_edge=False):
# node_rep: B x N x node_dim_in
# edge_rep: B x N x N x edge_dim_in
# edge_type: B x N x N x edge_type_num
# start_idx: whether to ignore the first edge type
B, N, _ = node_rep.size()
# node_enc
node_enc = self.node_encoder(node_rep.view(-1, self.node_dim_in)).view(B, N, self.nf_hidden)
# edge_enc
node_rep_r = node_rep[:, :, None, :].repeat(1, 1, N, 1)
node_rep_s = node_rep[:, None, :, :].repeat(1, N, 1, 1)
if edge_rep is not None:
tmp = torch.cat([node_rep_r, node_rep_s, edge_rep], 3)
else:
tmp = torch.cat([node_rep_r, node_rep_s], 3)
edge_encs = []
for i in range(start_idx, self.edge_type_num):
edge_enc = self.edge_encoders[i](tmp.view(B * N * N, -1)).view(B, N, N, 1, self.nf_hidden)
edge_encs.append(edge_enc)
# edge_enc: B x N x N x edge_type_num x nf
edge_enc = torch.cat(edge_encs, 3)
if edge_type is not None:
edge_enc = edge_enc * edge_type.view(B, N, N, self.edge_type_num, 1)[:, :, :, start_idx:]
# edge_enc: B x N x N x nf
edge_enc = edge_enc.sum(3)
for i in range(self.pstep):
if i == 0:
node_effect = node_enc
edge_effect = edge_enc
# calculate edge_effect
node_effect_r = node_effect[:, :, None, :].repeat(1, 1, N, 1)
node_effect_s = node_effect[:, None, :, :].repeat(1, N, 1, 1)
tmp = torch.cat([node_effect_r, node_effect_s, edge_effect], 3)
edge_effects = []
for j in range(start_idx, self.edge_type_num):
edge_effect = self.edge_propagators[i][j](tmp.view(B * N * N, -1))
edge_effect = edge_effect.view(B, N, N, 1, self.nf_hidden)
edge_effects.append(edge_effect)
# edge_effect: B x N x N x edge_type_num x nf
edge_effect = torch.cat(edge_effects, 3)
if edge_type is not None:
edge_effect = edge_effect * edge_type.view(B, N, N, self.edge_type_num, 1)[:, :, :, start_idx:]
# edge_effect: B x N x N x nf
edge_effect = edge_effect.sum(3)
# calculate node_effect
edge_effect_agg = edge_effect.sum(2)
tmp = torch.cat([node_enc, node_effect, edge_effect_agg], 2)
node_effect = self.node_propagator(tmp.view(B * N, -1)).view(B, N, self.nf_hidden)
node_effect = torch.cat([node_effect, node_enc], 2).view(B * N, -1)
edge_effect = torch.cat([edge_effect, edge_enc], 3).view(B * N * N, -1)
# node_pred: B x N x node_dim_out
# edge_pred: B x N x N x edge_dim_out
if ignore_node:
edge_pred = self.edge_predictor(edge_effect)
return edge_pred.view(B, N, N, -1)
if ignore_edge:
node_pred = self.node_predictor(node_effect)
return node_pred.view(B, N, -1)
node_pred = self.node_predictor(node_effect).view(B, N, -1)
edge_pred = self.edge_predictor(edge_effect).view(B, N, N, -1)
return node_pred, edge_pred