-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmodel.py
479 lines (391 loc) · 18.1 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
from __future__ import print_function
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from misc import render_block
class BlockOuterNet(nn.Module):
"""
predict block-level programs and parameters
block-LSTM
"""
def __init__(self, opt):
super(BlockOuterNet, self).__init__()
self.feat_size = opt.shape_feat_size
self.input_size = opt.outer_input_size
self.rnn_size = opt.outer_rnn_size
self.num_layers = opt.outer_num_layers
self.drop_prob = opt.outer_drop_prob
self.seq_length = opt.outer_seq_length
self.is_cuda = opt.is_cuda
self.shape_feat = Conv3DNet(self.feat_size, input_channel=2, power=2)
self.core = nn.LSTM(self.input_size,
self.rnn_size,
self.num_layers,
bias=False,
dropout=self.drop_prob)
self.inner_net = BlockInnerNet(opt)
def init_hidden(self, bsz):
weight = next(self.parameters()).data
return (Variable(weight.new(self.num_layers, bsz, self.rnn_size).zero_()),
Variable(weight.new(self.num_layers, bsz, self.rnn_size).zero_()))
def forward(self, x, y, sample_prob=None):
batch_size = x.size(0)
state = self.init_hidden(batch_size)
outputs_pgm = []
outputs_param = []
rendered_shapes = np.zeros((batch_size, 32, 32, 32), dtype=np.uint8)
def combine(x, y):
y = torch.from_numpy(np.copy(y).astype(np.float32))
y = Variable(torch.unsqueeze(y, 1), requires_grad=False)
if self.is_cuda:
y = y.cuda()
return torch.cat([x, y], dim=1)
fc_feats = self.shape_feat(combine(x, rendered_shapes))
seq = y
for i in range(seq.size(1)):
if i == 0:
xt = fc_feats
else:
prob_pre = torch.exp(outputs_pgm[-1].data)
_, it1 = torch.max(prob_pre, dim=2)
it2 = outputs_param[-1].data.clone()
it1 = it1.cpu().numpy()
it2 = it2.cpu().numpy()
rendered_shapes = render_block(rendered_shapes, it1, it2)
xt = self.shape_feat(combine(x, rendered_shapes))
output, state = self.core(xt.unsqueeze(0), state)
output = F.relu(output)
pgm, param = self.inner_net(output.squeeze(), y[:, i], sample_prob)
outputs_pgm.append(pgm)
outputs_param.append(param)
return [torch.cat([_.unsqueeze(1) for _ in outputs_pgm], 1).contiguous(),
torch.cat([_.unsqueeze(1) for _ in outputs_param], 1).contiguous()]
def decode(self, x):
batch_size = x.size(0)
state = self.init_hidden(batch_size)
outputs_pgm = []
outputs_param = []
rendered_shapes = np.zeros((batch_size, 32, 32, 32), dtype=np.uint8)
def combine(x, y):
y = torch.from_numpy(np.copy(y).astype(np.float32))
y = Variable(torch.unsqueeze(y, 1), requires_grad=False)
if self.is_cuda:
y = y.cuda()
return torch.cat([x, y], dim=1)
fc_feats = self.shape_feat(combine(x, rendered_shapes))
for i in range(self.seq_length):
if i == 0:
xt = fc_feats
else:
prob_pre = torch.exp(outputs_pgm[-1].data)
_, it1 = torch.max(prob_pre, dim=2)
it2 = outputs_param[-1].data.clone()
it1 = it1.cpu().numpy()
it2 = it2.cpu().numpy()
rendered_shapes = render_block(rendered_shapes, it1, it2)
xt = self.shape_feat(combine(x, rendered_shapes))
output, state = self.core(xt.unsqueeze(0), state)
output = F.relu(output)
pgm, param = self.inner_net.decode(output.squeeze())
outputs_pgm.append(pgm)
outputs_param.append(param)
return [torch.cat([_.unsqueeze(1) for _ in outputs_pgm], 1).contiguous(),
torch.cat([_.unsqueeze(1) for _ in outputs_param], 1).contiguous()]
class BlockInnerNet(nn.Module):
"""
Inner Block Net
use last pgm as input for each time step
step-LSTM
"""
def __init__(self, opt):
super(BlockInnerNet, self).__init__()
self.vocab_size = opt.program_size
self.max_param = opt.max_param
self.input_size = opt.inner_input_size
self.rnn_size = opt.inner_rnn_size
self.num_layers = opt.inner_num_layers
self.drop_prob = opt.inner_drop_prob
self.seq_length = opt.inner_seq_length
self.cls_feat_size = opt.inner_cls_feat_size
self.reg_feat_size = opt.inner_reg_feat_size
self.sample_prob = opt.inner_sample_prob
self.is_cuda = opt.is_cuda
self.pgm_embed = nn.Embedding(self.vocab_size + 1, self.input_size)
self.core = nn.LSTM(self.input_size,
self.rnn_size,
self.num_layers,
bias=False,
dropout=self.drop_prob)
self.logit1 = nn.Linear(self.rnn_size, self.cls_feat_size)
self.logit2 = nn.Linear(self.cls_feat_size, self.vocab_size + 1)
self.regress1 = nn.Linear(self.rnn_size, self.reg_feat_size)
self.regress2 = nn.Linear(self.vocab_size + 1 + self.reg_feat_size, (self.vocab_size + 1) * self.max_param)
self.init_weights()
def init_weights(self):
initrange = 0.1
self.pgm_embed.weight.data.uniform_(-initrange, initrange)
self.logit1.bias.data.fill_(0)
self.logit1.weight.data.uniform_(-initrange, initrange)
self.logit2.bias.data.fill_(0)
self.logit2.weight.data.uniform_(-initrange, initrange)
self.regress1.bias.data.fill_(0)
self.regress1.weight.data.uniform_(-initrange, initrange)
self.regress2.bias.data.fill_(0)
self.regress2.weight.data.uniform_(-initrange, initrange)
def init_hidden(self, bsz):
weight = next(self.parameters()).data
return (Variable(weight.new(self.num_layers, bsz, self.rnn_size).zero_()),
Variable(weight.new(self.num_layers, bsz, self.rnn_size).zero_()))
def forward(self, x, y, sample_prob=None):
if sample_prob is not None:
self.sample_prob = sample_prob
batch_size = x.size(0)
state = self.init_hidden(batch_size)
outputs_pgm = []
outputs_param = []
seq = y
for i in range(seq.size(1)):
if i == 0:
xt = x
else:
if i >= 1 and self.sample_prob > 0:
sample_prob = x.data.new(batch_size).uniform_(0, 1)
sample_mask = sample_prob < self.sample_prob
if sample_mask.sum() == 0:
it1 = seq[:, i-1].clone()
else:
sample_ind = sample_mask.nonzero().view(-1)
it1 = seq[:, i-1].data.clone()
prob_prev = torch.exp(outputs_pgm[-1].data)
it1.index_copy_(0, sample_ind, torch.multinomial(prob_prev, 1).view(-1).index_select(0, sample_ind))
it1 = Variable(it1, requires_grad=False)
else:
print("obtain last ground truth")
it1 = seq[:, i-1].clone()
xt = self.pgm_embed(it1)
output, state = self.core(xt.unsqueeze(0), state)
pgm_feat1 = F.relu(self.logit1(output.squeeze(0)))
pgm_feat2 = self.logit2(pgm_feat1)
pgm_score = F.log_softmax(pgm_feat2, dim=1)
trans_prob = F.softmax(pgm_feat2, dim=1).detach()
param_feat1 = F.relu(self.regress1(output.squeeze(0)))
param_feat2 = torch.cat([trans_prob, param_feat1], dim=1)
param_score = self.regress2(param_feat2)
param_score = param_score.view(batch_size, self.vocab_size + 1, self.max_param)
index = seq[:, i].unsqueeze(1).unsqueeze(2).expand(batch_size, 1, self.max_param).detach()
param_score = param_score.gather(1, index).squeeze(1)
outputs_pgm.append(pgm_score)
outputs_param.append(param_score)
return [torch.cat([_.unsqueeze(1) for _ in outputs_pgm], 1).contiguous(),
torch.cat([_.unsqueeze(1) for _ in outputs_param], 1).contiguous()]
def decode(self, x):
batch_size = x.size(0)
state = self.init_hidden(batch_size)
outputs_pgm = []
outputs_param = []
for i in range(self.seq_length):
if i == 0:
xt = x
else:
prob_pre = torch.exp(outputs_pgm[-1].data)
_, it1 = torch.max(prob_pre, dim=1)
it1 = Variable(it1, requires_grad=False)
xt = self.pgm_embed(it1)
output, state = self.core(xt.unsqueeze(0), state)
pgm_feat1 = F.relu(self.logit1(output.squeeze(0)))
pgm_feat2 = self.logit2(pgm_feat1)
pgm_score = F.log_softmax(pgm_feat2, dim=1)
trans_prob = F.softmax(pgm_feat2, dim=1).detach()
param_feat1 = F.relu(self.regress1(output.squeeze(0)))
param_feat2 = torch.cat([trans_prob, param_feat1], dim=1)
param_score = self.regress2(param_feat2)
param_score = param_score.view(batch_size, self.vocab_size + 1, self.max_param)
_, index = torch.max(trans_prob, dim=1)
index = index.unsqueeze(1).unsqueeze(2).expand(batch_size, 1, self.max_param).detach()
param_score = param_score.gather(1, index).squeeze(1)
outputs_pgm.append(pgm_score)
outputs_param.append(param_score)
return [torch.cat([_.unsqueeze(1) for _ in outputs_pgm], 1).contiguous(),
torch.cat([_.unsqueeze(1) for _ in outputs_param], 1).contiguous()]
class Conv3DNet(nn.Module):
"""
encode 3D voxelized shape into a vector
"""
def __init__(self, feat_size, input_channel=1, power=1):
super(Conv3DNet, self).__init__()
power = int(power)
self.conv1 = nn.Conv3d(input_channel, 8*power, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
self.conv2 = nn.Conv3d(8*power, 16*power, kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1))
self.conv3 = nn.Conv3d(16*power, 16*power, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
self.conv4 = nn.Conv3d(16*power, 32*power, kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1))
self.conv5 = nn.Conv3d(32*power, 32*power, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
self.conv6 = nn.Conv3d(32*power, 64*power, kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1))
self.conv7 = nn.Conv3d(64*power, 64*power, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
self.conv8 = nn.Conv3d(64*power, 64*power, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
# self.BN1 = nn.BatchNorm3d(8*power)
# self.BN2 = nn.BatchNorm3d(16*power)
# self.BN3 = nn.BatchNorm3d(16*power)
# self.BN4 = nn.BatchNorm3d(32*power)
# self.BN5 = nn.BatchNorm3d(32*power)
# self.BN6 = nn.BatchNorm3d(64*power)
# self.BN7 = nn.BatchNorm3d(64*power)
# self.BN8 = nn.BatchNorm3d(64*power)
self.BN1 = nn.BatchNorm3d(8 * power, track_running_stats=False)
self.BN2 = nn.BatchNorm3d(16 * power, track_running_stats=False)
self.BN3 = nn.BatchNorm3d(16 * power, track_running_stats=False)
self.BN4 = nn.BatchNorm3d(32 * power, track_running_stats=False)
self.BN5 = nn.BatchNorm3d(32 * power, track_running_stats=False)
self.BN6 = nn.BatchNorm3d(64 * power, track_running_stats=False)
self.BN7 = nn.BatchNorm3d(64 * power, track_running_stats=False)
self.BN8 = nn.BatchNorm3d(64 * power, track_running_stats=False)
self.avgpool = nn.AvgPool3d(kernel_size=(4, 4, 4))
self.fc = nn.Linear(64*power, feat_size)
def forward(self, x):
x = F.relu(self.BN1(self.conv1(x)))
x = F.relu(self.BN2(self.conv2(x)))
x = F.relu(self.BN3(self.conv3(x)))
x = F.relu(self.BN4(self.conv4(x)))
x = F.relu(self.BN5(self.conv5(x)))
x = F.relu(self.BN6(self.conv6(x)))
x = F.relu(self.BN7(self.conv7(x)))
x = F.relu(self.BN8(self.conv8(x)))
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = F.relu(self.fc(x))
return x
class RenderNet(nn.Module):
"""
Multiple Step Render
"""
def __init__(self, opt):
super(RenderNet, self).__init__()
# program LSTM parameter
self.vocab_size = opt.program_size
self.max_param = opt.max_param
self.input_encoding_size = opt.input_encoding_size
self.rnn_size = opt.rnn_size
self.num_layers = opt.num_layers
self.drop_prob_lm = opt.drop_prob_lm
self.seq_length = opt.seq_length
self.program_vector_size = opt.program_vector_size
self.nc = opt.nc
self.pgm_embed = nn.Linear(self.vocab_size + 1, int(self.input_encoding_size / 2))
self.param_embed = nn.Linear(self.max_param, self.input_encoding_size - int(self.input_encoding_size / 2))
self.lstm = nn.LSTM(self.input_encoding_size, self.rnn_size, self.num_layers, bias=False,
dropout=self.drop_prob_lm)
self.pgm_param_feat = nn.Linear(self.rnn_size, self.program_vector_size)
self.decoder = nn.Sequential(
nn.ConvTranspose3d(self.program_vector_size, 64, 4, 1, 0, bias=False),
nn.BatchNorm3d(64),
nn.ReLU(True),
nn.Conv3d(64, 64, 3, 1, 1, bias=False),
nn.BatchNorm3d(64),
nn.ReLU(True),
# 4 x 4 x 4
nn.ConvTranspose3d(64, 16, 4, 2, 1, bias=False),
nn.BatchNorm3d(16),
nn.ReLU(True),
nn.Conv3d(16, 16, 3, 1, 1, bias=False),
nn.BatchNorm3d(16),
nn.ReLU(True),
# 8 x 8 x 8
nn.ConvTranspose3d(16, 4, 4, 2, 1, bias=False),
nn.BatchNorm3d(4),
nn.ReLU(True),
nn.Conv3d(4, 4, 3, 1, 1, bias=False),
nn.BatchNorm3d(4),
nn.ReLU(True),
# 16 x 16 x 16
nn.ConvTranspose3d(4, self.nc, 4, 2, 1, bias=False),
)
def init_hidden(self, bsz):
weight = next(self.parameters()).data
return (Variable(weight.new(self.num_layers, bsz, self.rnn_size).zero_()),
Variable(weight.new(self.num_layers, bsz, self.rnn_size).zero_()))
def forward(self, program, parameters, index):
program = program.permute(1, 0, 2)
parameters = parameters.permute(1, 0, 2)
bsz = program.size(1)
init = self.init_hidden(bsz)
# program linear transform
dim1 = program.size()
program = program.contiguous().view(-1, self.vocab_size + 1)
x1 = F.relu(self.pgm_embed(program))
x1 = x1.view(dim1[0], dim1[1], -1)
# parameter linear transform
dim2 = parameters.size()
parameters = parameters.contiguous().view(-1, self.max_param)
x2 = F.relu(self.param_embed(parameters))
x2 = x2.view(dim2[0], dim2[1], -1)
# LSTM to aggregate programs and parameters
x = torch.cat((x1, x2), dim=2)
out, hidden = self.lstm(x, init)
# select desired step aggregated features
index = index.unsqueeze(1).expand(-1, out.size(2)).unsqueeze(0)
pgm_param_feat = out.gather(dim=0, index=index).squeeze()
pgm_param_feat = F.relu(self.pgm_param_feat(pgm_param_feat))
pgm_param_feat = pgm_param_feat.view(bsz, self.program_vector_size, 1, 1, 1)
shape = self.decoder(pgm_param_feat)
return shape
def compute_LSTM_feat(self, program, parameters, index):
program = program.permute(1, 0, 2)
parameters = parameters.permute(1, 0, 2)
bsz = program.size(1)
init = self.init_hidden(bsz)
# program linear transform
dim1 = program.size()
program = program.contiguous().view(-1, self.vocab_size + 1)
x1 = F.relu(self.pgm_embed(program))
x1 = x1.view(dim1[0], dim1[1], -1)
# parameter linear transform
dim2 = parameters.size()
parameters = parameters.contiguous().view(-1, self.max_param)
x2 = F.relu(self.param_embed(parameters))
x2 = x2.view(dim2[0], dim2[1], -1)
# LSTM to aggregate programs and parameters
x = torch.cat((x1, x2), dim=2)
out, hidden = self.lstm(x, init)
# select desired step aggregated features
index = index.unsqueeze(1).expand(-1, out.size(2)).unsqueeze(0)
pgm_param_feat = out.gather(dim=0, index=index).squeeze()
# pgm_param_feat = F.relu(self.pgm_param_feat(pgm_param_feat))
return pgm_param_feat
def compute_shape_from_feat(self, pgm_param_feat):
bsz = pgm_param_feat.size(0)
pgm_param_feat = F.relu(self.pgm_param_feat(pgm_param_feat))
pgm_param_feat = pgm_param_feat.view(bsz, self.program_vector_size, 1, 1, 1)
shape = self.decoder(pgm_param_feat)
return shape
if __name__ == '__main__':
from easydict import EasyDict as edict
from programs.label_config import stop_id, max_param
opt = edict()
opt.shape_feat_size = 64
opt.outer_input_size = 64
opt.outer_rnn_size = 64
opt.outer_num_layers = 1
opt.outer_drop_prob = 0
opt.outer_seq_length = 6
opt.is_cuda = False
opt.program_size = stop_id - 1
opt.max_param = max_param - 1
opt.inner_input_size = 64
opt.inner_rnn_size = 64
opt.inner_num_layers = 1
opt.inner_drop_prob = 0
opt.inner_seq_length = 3
opt.inner_cls_feat_size = 64
opt.inner_reg_feat_size = 64
opt.inner_sample_prob = 1.0
net = BlockOuterNet(opt)
x = torch.zeros((10, 1, 32, 32, 32))
x = Variable(x, requires_grad=False)
y = torch.zeros((10, 6, 3)).long()
pgms, params = net(x, y)
print(pgms.size())
print(params.size())
pgms, params = net.decode(x)
print(pgms.size())
print(params.size())