-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassembly.py
787 lines (642 loc) · 30.6 KB
/
assembly.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
import pickle
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import random
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import utils
from model.connect import Model as ConnectModel
from model.retrieval import Model as RetrievalModel, gaussian_mixture_loss
from model.contact import Model as ContactModel
from data.dataset import *
import copy
from pyod.models.ocsvm import OCSVM
from train_od import get_features
import argparse
import time
import yaml
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
def get_obj(node, partnet_root_dir):
objs = node.objs
all_verts = []
all_faces = []
offset = 0
for obj in objs:
verts, faces = utils.load_obj(f"{partnet_root_dir}/{node.partnet_shape_dir.split('/')[-1]}/objs/{obj}.obj")
faces -= 1
used_vs = set()
for fidx in range(faces.shape[0]):
face = faces[fidx]
for vidx in range(3):
used_vs.add(face[vidx])
used_vs = sorted(list(used_vs))
verts = verts[used_vs]
vert_map = {used_vs[a]:a for a in range(len(used_vs))}
for fidx in range(faces.shape[0]):
for vidx in range(3):
faces[fidx][vidx] = vert_map[faces[fidx][vidx]]
faces += offset
offset += verts.shape[0]
all_verts.append(verts)
all_faces.append(faces)
verts = np.concatenate(all_verts, axis=0)
faces = np.concatenate(all_faces, axis=0)
return verts, faces
class Part():
def __init__(self):
pass
class Assembly():
def __init__(self, config):
self.__dict__.update(config) #I hate typing quotation marks
with open(f"{self.model_contact_dir}/config.yaml", 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
model_params = config['model_params']
self.model_contact = ContactModel(
feature_size=13,
edge_size=1,
**model_params
)
with open(f"{self.model_retrieval_dir}/config.yaml", 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
model_params = config['model_params']
self.model_retrieval = RetrievalModel(
feature_size=13,
edge_size=1,
**model_params
)
with open(f"{self.model_connect_dir}/config.yaml", 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
model_params = config['model_params']
self.model_connect = ConnectModel(
feature_size=13,
edge_size=1,
**model_params
)
self.model_contact.load_state_dict(torch.load(self.model_contact_checkpoint))
self.model_retrieval.load_state_dict(torch.load(self.model_retrieval_checkpoint))
self.model_connect.load_state_dict(torch.load(self.model_connect_checkpoint))
self.model_ods = {}
for i in range(2, self.node_threshold):
try:
with open(f"{self.model_od_dir}/svm_{i}.pkl", 'rb') as f:
self.model_ods[i] = pickle.load(f)
except:
self.model_ods[i] = None
self.model_retrieval.to(self.device).eval()
self.model_contact.to(self.device).eval()
self.model_connect.to(self.device).eval()
assembly_dataset = SPSMDataset(data_dir = data_dir, data_root_dir = data_root_dir, task='assembly')
all_imgs = []
all_embeddings = []
all_attachments = []
all_nodes = []
all_shapes = []
with torch.no_grad():
for i in range(self.test_size):
print(f"Loading shapes {i}/{self.test_size}", end="\r")
offset = len(all_nodes)
boxes, edges, N, splits, vis_img, bboxes, nodes = assembly_dataset.get_data(i)
if len(nodes) > self.node_threshold:
continue
for node in nodes:
node.sym_pair_idxs = []
node.sym_idxs = {}
node.sym_idxs['trans'] = []
node.sym_idxs['ref'] = []
node.sym_idxs['rot'] = []
for sym_part, sym_info in node.sym_parts:
if sym_info == 'trans':
sym_type = sym_info
else:
sym_type, sym_params = sym_info
if sym_type == 'refr':
sym_type = 'ref'
if sym_type == 'rotr':
sym_type = 'rot'
sym_part_idx = nodes.index(sym_part)
node.sym_idxs[sym_type].append(sym_part_idx + offset)
for syma, symb, params in node.sym_pairs:
idxa = node.adj.index(syma)
idxb = node.adj.index(symb)
node.sym_pair_idxs.append((idxa, idxb, params))
shape_pc = torch.cat([vis_img[k] + bboxes[k] for k in range(len(vis_img))], dim=0)
all_shapes.append(shape_pc.unsqueeze(0).cuda())
boxes = boxes.to(self.device)
if edges.shape[0] < 1:
continue
edge_label = torch.zeros(len(edges)).to(self.device)
embeddings = self.model_retrieval.predict_embedding(boxes, edges, edge_label, splits)
all_imgs += vis_img
all_embeddings.append(embeddings)
all_nodes += nodes
all_embeddings = torch.cat(all_embeddings, dim=0)
total_rank = 0
all_imgs = torch.stack(all_imgs, dim=0).to(self.device)
print(f"Loaded {all_imgs.shape} parts")
self.all_nodes = all_nodes
self.all_embeddings = all_embeddings
self.all_imgs = all_imgs
self.all_shapes = all_shapes
self.N_data = len(all_nodes)
self.trans_syms = []
self.rot_syms = []
self.ref_syms = []
def global_idx_to_node_adj_idxs(self, idx):
i = 0
while i < self.N - 1 and idx >= self.cum_adj_counts[i+1]:
i += 1
return i, idx-self.cum_adj_counts[i]
def update_info(self):
self.adj_counts = [len(node.adj) for node in self.nodes]
self.total_contacts = sum(self.adj_counts)
self.cum_adj_counts = [sum(self.adj_counts[:i]) for i in range(len(self.nodes))]
self.N = len(self.nodes)
self.unattached = []
for (i, node) in enumerate(self.nodes):
for (j, adj) in enumerate(node.adj):
if adj is None:
self.unattached.append(self.cum_adj_counts[i] + j)
self.remaining_adjs = len(self.unattached)
self.boxes = [node.adj_descriptor for node in self.nodes]
self.boxes = torch.cat(self.boxes, dim=0).float().to(self.device)
self.boxes[self.boxes.isnan()] = 0
self.part_edges = get_node_edges(self.nodes)
self.adj_edges = get_adj_edges(self.nodes, return_adj_count=False)
self.all_edges = self.part_edges + self.adj_edges
self.edges = torch.LongTensor(self.all_edges)
self.edge_label = torch.zeros(len(self.all_edges)).to(self.device)
self.edge_label[len(self.part_edges):] = 1
self.dists = None
def get_contact_probability(self, input_label, predict_idxs):
input_label = input_label.unsqueeze(1)
input_features = torch.cat([input_label, self.boxes], dim=1)
p, logits = self.model_contact.predict_likelihood(input_features, self.edges, self.edge_label, torch.LongTensor([self.total_contacts]), input_label.detach(), predict_idxs)
return p, logits
def predict_contact(self, first_contact=None, continue_temp=2, select_temp=2):
if first_contact is not None:
assert first_contact in self.unattached
self.unattached.remove(first_contact)
target_contacts = [first_contact]
else:
target_contacts = []
unattached = copy.copy(self.unattached)
random.shuffle(unattached)
should_continue = True
while should_continue:
input_label = torch.zeros(self.total_contacts).to(self.device)
input_label[target_contacts] = 1
predict_idxs = torch.LongTensor(self.unattached).unsqueeze(0)
p, logits = self.get_contact_probability(input_label, predict_idxs)
continue_p = float(F.softmax(p*continue_temp, dim=1)[:,1])
if len(target_contacts) == 0 and continue_p < 0.5:
return None
if (0.5 < continue_p) and len(self.unattached) > 0:
next_slot_idx = int(torch.argmax(logits))
next_slot = self.unattached[next_slot_idx]
self.unattached.remove(next_slot)
target_contacts.append(next_slot)
else:
should_continue = False
return target_contacts
def get_od_score(self):
input_label = torch.zeros(self.total_contacts).to(self.device)
predict_idxs = torch.LongTensor(self.unattached).unsqueeze(0)
input_label = input_label.unsqueeze(1)
input_features = torch.cat([input_label, self.boxes], dim=1)
h = self.model_contact.get_latent(input_features, self.edges, self.edge_label, torch.LongTensor([self.total_contacts]), input_label.detach(), predict_idxs)
od_score = self.model_od.decision_function(h.detach().cpu().numpy())
return od_score
def get_retrieval_gmm(self, target_contacts):
input_label = torch.zeros(self.total_contacts).to(self.device)
input_label[target_contacts] = 1
input_label = input_label.unsqueeze(1)
input_features = torch.cat([input_label, self.boxes], dim=1)
log_logits, mus, sigmas = self.model_retrieval.predict_retrieval(input_features, self.edges, self.edge_label, torch.LongTensor([self.total_contacts]), input_label.detach())
return log_logits, mus, sigmas
def compute_retrieval_distances(self, target_contacts):
log_logits, mus, sigmas = self.get_retrieval_gmm(target_contacts)
dists = gaussian_mixture_loss(self.all_embeddings, log_logits, mus, sigmas)
dists = list(dists.cpu().numpy())
dists = list(enumerate(dists))
dists = sorted(dists, key=lambda x:x[1])
self.dists = dists
def compute_retrieval_order(self, sym_candidates):
min_dist = self.dists[0][1]
cur_dist = self.dists[0][1]
cur_idx = 0
candidate_idxs = []
non_candidate_idxs = []
while cur_dist - min_dist < 60 and cur_idx < 200: #Hardcoded for now
next_idx = self.dists[cur_idx][0]
if next_idx in sym_candidates:
candidate_idxs.append((next_idx, cur_idx))
else:
non_candidate_idxs.append((next_idx, cur_idx))
cur_idx += 1
cur_dist = self.dists[cur_idx][1]
while cur_dist - min_dist < 100 and cur_idx < 200:
next_idx = self.dists[cur_idx][0]
if next_idx in sym_candidates:
candidate_idxs.append((next_idx, cur_idx))
cur_idx += 1
cur_dist = self.dists[cur_idx][1]
return candidate_idxs, non_candidate_idxs
def connect_edges(self, target_contacts, new_node):
non_target_contacts = [i for i in self.unattached if i not in target_contacts]
if len(non_target_contacts) > 0:
noise_contact = [random.choice(non_target_contacts)]
else:
noise_contact = []
a = len(target_contacts)
b = len(new_node.adj)
u_indices = [[i for _ in range(b)] for i in target_contacts]
u_indices = torch.LongTensor([item for sublist in u_indices for item in sublist])
v_indices = [list(range(b)) for _ in range(a)]
v_indices = torch.LongTensor([item for sublist in v_indices for item in sublist])
boxes_p = new_node.adj_descriptor.float().to(self.device)
boxes_p[boxes_p.isnan()] = 0
edges_p = get_node_edges([new_node])
edge_label_p = torch.zeros(len(edges_p)).to(self.device)
edges_p = torch.LongTensor(edges_p)
boxes = self.boxes.clone()
input_label = torch.zeros(self.total_contacts).to(self.device).unsqueeze(1)
input_label[target_contacts] = 1
input_label[noise_contact] = 1
input_features = torch.cat([input_label, self.boxes], dim=1)
output = self.model_connect.predict_likelihood(input_features, self.edges, self.edge_label, torch.LongTensor([self.total_contacts]), u_indices, v_indices, boxes_p, edges_p, edge_label_p, torch.LongTensor([len(new_node.adj)]))
output = F.softmax(output, dim=1)[:,1]
attached_u = []
attached_v = []
output = list(output.cpu().numpy())
output = [(i, output[i]) for i in range(len(output))]
output = sorted(output, key=lambda x:-x[1])
c = len(self.unattached)
p_threshold = 1/(max(a+1,b)+0.5)
for max_idx, p in output:
u_idx = int(u_indices[max_idx])
v_idx = int(v_indices[max_idx])
if p > p_threshold and (u_idx not in attached_u) and (v_idx not in attached_v):
attached_u.append(u_idx)
attached_v.append(v_idx)
assert len(attached_u) == len(attached_v)
if len(attached_u) == len(target_contacts):
return attached_u, attached_v
else:
return None, None
def add_node(self, node, update_original_adj=True):
new_node = copy.deepcopy(node)
if update_original_adj:
new_node.original_adj = new_node.adj
new_node.adj = [None for _ in range(len(new_node.adj))]
new_node.original_node = node
self.nodes.append(new_node)
def optimize(self, num_epochs = 1500):
num_epochs = num_epochs
n_parts = len(self.nodes)
initial_offset = torch.randn(n_parts, 3) * 0.2
initial_scale = torch.ones(n_parts, 3) * 1
scale = initial_scale.cuda()
offset = initial_offset.cuda()
offset.requires_grad = True
scale.requires_grad = True
points_opt = torch.stack([node.points - node.box[0:3] for node in self.nodes]).cuda()
optimizer = optim.Adam([offset, scale], lr=0.005, eps=1e-4)
indices = []
adj_edges = self.adj_edges
prev_loss = 100000000000
skip_first = False
skip_second = False
ods = []
for u,v in adj_edges:
u1, u2 = self.global_idx_to_node_adj_idxs(u)
v1, v2 = self.global_idx_to_node_adj_idxs(v)
od1 = self.nodes[u1].adj_dists[u2].cuda()
od2 = self.nodes[v1].adj_dists[v2].cuda()
od1.requires_grad = False
od2.requires_grad = False
ods.append((u,v,od1,od2))
for iters in range(num_epochs):
optimizer.zero_grad()
if iters < 1000:
if skip_first:
continue
points_translated = points_opt + offset.unsqueeze(1)
else:
if skip_second:
continue
if iters % 100 == 0:
offset.requires_grad = False
scale.requires_grad = True
elif iters % 50 == 0:
offset.requires_grad = True
scale.requires_grad = False
points_translated = points_opt * scale.unsqueeze(1) + offset.unsqueeze(1)
loss = 0
for u,v,od1,od2 in ods:
u1, u2 = self.global_idx_to_node_adj_idxs(u)
v1, v2 = self.global_idx_to_node_adj_idxs(v)
p1 = points_translated[u1][self.nodes[u1].adj_idxs[u2]]
p2 = points_translated[v1][self.nodes[v1].adj_idxs[v2]]
dists_cur = utils.pairwise_dist(p1, p2) ** 0.5
d1 = dists_cur.min(axis=1)[0]
d2 = dists_cur.min(axis=0)[0]
loss += ((d1-od1) ** 2).sum()
loss += ((d2-od2) ** 2).sum()
loss_reg = ((1-scale) ** 4 * 100).sum()
if iters % (50) == 0:
print("Optimizing...")
print(f"Iteration {iters}, loss {float(loss.item())}")
if iters < 1000 or (iters > 1099 and iters % 100 == 0):
if abs(loss-prev_loss) < 1:
if iters < 1000:
skip_first = True
else:
skip_second = True
prev_loss = loss
loss.backward()
optimizer.step()
if iters == 1000:
offset.requires_grad=False
for g in optimizer.param_groups:
g['lr'] = 0.001
voffset = 0
all_verts = []
all_faces = []
all_source = []
for (i, node) in enumerate(self.nodes):
verts, faces = get_obj(node, partnet_root_dir)
verts = verts - node.box[0:3].numpy()
verts = verts * scale[i].detach().cpu().numpy()
verts = verts + offset[i].detach().cpu().numpy()
faces += voffset
voffset += verts.shape[0]
all_verts.append(verts)
all_faces.append(faces)
all_source.append(node.partnet_shape_dir)
verts = np.concatenate(all_verts, axis=0)
faces = np.concatenate(all_faces, axis=0)
return verts, faces
def check_for_dataset_pairs(self, new_node, target_contacts):
n_psd = new_node.partnet_shape_dir
n_id = new_node.id
for contact in target_contacts:
n_idx, _ = self.global_idx_to_node_adj_idxs(contact)
for adj in self.nodes[n_idx].original_adj:
if n_psd == adj.partnet_shape_dir and n_id == adj.id:
return True
return False
def check_symmetry(self, target_contacts):
all_candidates = []
geo_checks = []
for contact in target_contacts:
n_idx, adj_idx = self.global_idx_to_node_adj_idxs(contact)
node = self.nodes[n_idx]
for a, b, edge in node.sym_pair_idxs:
if edge == 'trans':
sym_type, sym_params = edge, None
else:
sym_type, sym_params = edge
if a == adj_idx:
if node.adj[b] is not None:
all_candidates += (node.adj[b].sym_idxs[sym_type])
geo_checks.append((node.adj[b], sym_type, sym_params))
elif b == adj_idx:
if node.adj[a] is not None:
all_candidates += (node.adj[a].sym_idxs[sym_type])
geo_checks.append((node.adj[a], sym_type, sym_params))
return list(set(all_candidates)), geo_checks
def check_longest_path(self, nodes):
dists = []
degrees = []
degrees2 = []
for node in nodes:
visited = []
cur_level = [node]
next_level = []
dist = -1
count = 0
while len(visited) != len(nodes):
count += 1
if count > 50:
raise NotImplementedError
for cur in cur_level:
for adj in cur.adj:
if (adj in nodes) and (adj not in visited) and (adj not in next_level) and (adj not in cur_level):
next_level.append(adj)
visited += cur_level
cur_level = next_level
next_level = []
dist += 1
if dist == 1:
assert len(visited) - 1 == len(node.adj)
if dist == 2:
two_hop = len(visited) - 1
dists.append(dist)
assert dist > 0
if dist < 2:
two_hop = len(nodes)
degrees.append(len(node.adj))
degrees2.append(two_hop)
return dists, degrees, degrees2
def synth(self, idx, start_node=None):
all_nodes = self.all_nodes
self.idx = idx
self.nodes = []
if start_node is None:
start_node = random.choice(all_nodes)
self.add_node(start_node)
self.update_info()
try:
self.failure = False
self.od_score = 9999999
with torch.no_grad():
while self.remaining_adjs > 0:
print(f"Total number of parts: {len(self.nodes)}, number of unattached slots: {self.remaining_adjs}")
print('---------------------')
if len(self.nodes) >= self.node_threshold:
print(f"Failure: more than {self.node_threshold} nodes")
self.failure = True
break
self.od_score = 0
target_contacts = self.predict_contact()
if target_contacts is None:
print("No viable contacts identified")
return None
if self.use_symmetry:
sym_candidates, geo_checks = self.check_symmetry(target_contacts)
else:
sym_candidates, geo_checks = [], []
saved_is = []
self.compute_retrieval_distances(target_contacts)
candidate_idxs, non_candidate_idxs = self.compute_retrieval_order(sym_candidates)
attached_u = None
for next_idx, rank in candidate_idxs:
new_node = self.all_nodes[next_idx]
if (self.no_duplicate or self.duplicate_last) and self.check_for_dataset_pairs(new_node, target_contacts) is True:
print("Retrieved an original attached part from the dataset")
if self.duplicate_last:
saved_is.append(i)
continue
attached_u, attached_v = self.connect_edges(target_contacts, new_node)
if attached_u is not None:
break
if attached_u is None:
no_sym_saves = []
check_pcs = []
if len(geo_checks) > 0:
for node, sym_type, sym_params in geo_checks:
if sym_type == 'rot':
continue
pc = node.points.float()
pc -= pc.mean(dim=0)
if sym_type == 'ref':
_, direction = sym_params
www = torch.ones_like(direction).float()
if direction.abs()[0] > 0.9:
www[0] = -1
elif direction.abs()[1] > 0.9:
www[1] = -1
elif direction.abs()[2] > 0.9:
www[2] = -1
pc = pc * www.unsqueeze(0)
check_pcs.append(pc)
if len(check_pcs) > 0:
check_chamfer = True
else:
check_chamfer = False
for next_idx, rank in non_candidate_idxs:
new_node = self.all_nodes[next_idx]
if check_chamfer:
min_error = 10000
pc = new_node.points.float()
pc -= pc.mean(dim=0)
for check_pc in check_pcs:
error = utils.get_chamfer_distance(pc, check_pc)
min_error = min(error, min_error)
if min_error > 0.02:
no_sym_saves.append((next_idx, rank))
continue
if (self.no_duplicate or self.duplicate_last) and self.check_for_dataset_pairs(new_node, target_contacts) is True:
continue
attached_u, attached_v = self.connect_edges(target_contacts, new_node)
if attached_u is not None:
break
if attached_u is None:
for next_idx, rank in no_sym_saves:
new_node = self.all_nodes[next_idx]
attached_u, attached_v = self.connect_edges(target_contacts, new_node)
if attached_u is not None:
break
if (attached_u is None) and self.duplicate_last:
for i in saved_is:
new_node = self.retrieve_node(target_contacts, nth=i)
attached_u, attached_v = self.connect_edges(target_contacts, new_node)
if attached_u is not None:
break
if attached_u is None:
print("Failure, can't connect...")
self.failure = True
break
self.add_node(new_node)
for i in range(len(attached_u)):
u_idx = attached_u[i]
v_idx = attached_v[i]
n_idx, a_idx = self.global_idx_to_node_adj_idxs(u_idx)
self.nodes[n_idx].adj[a_idx] = self.nodes[-1]
self.nodes[-1].adj[v_idx] = self.nodes[n_idx]
self.update_info()
if len(self.nodes) < 20 and not self.failure:
dists, degrees, degrees2 = self.check_longest_path(self.nodes)
features = get_features(self.nodes, degrees, degrees2, dists)
if self.model_ods[len(self.nodes)] is None: #No dataset shape contains this many parts
self.od_score = 10000000
self.od_threshold = 0
self.failure = True
else:
self.od_score = self.model_ods[len(self.nodes)].decision_function(features.unsqueeze(0).numpy())
self.od_threshold = self.model_ods[len(self.nodes)].threshold_
self.od_score = round(float(self.od_score), 2)
self.od_threshold = round(float(self.od_threshold), 2)
else:
self.od_score = 10000000
self.od_threshold = 0
self.failure = True
if not self.failure:
shape_dirs = [node.partnet_shape_dir for node in self.nodes]
shape_dirs_set = list(set(shape_dirs))
if self.failure and self.skip_failure:
return None
for i in range(self.opt_attempts):
try:
verts, faces = self.optimize()
break
except Exception as e:
print(e)
print("Optimize Error")
if i+1 == self.opt_attempts:
return None
utils.write_obj(verts, faces+1, f"{vis_dir}/{attempts}_{self.failure}_{len(self.nodes)}_{self.od_score}_{self.od_threshold}.obj")
except:
raise
if __name__ == "__main__":
from datetime import datetime
parser = argparse.ArgumentParser(description='retrieval')
parser.add_argument('--vis-root-dir', type=str, default='assembly_outputs', metavar='N')
parser.add_argument('--synth-category', type=str, default='chair', metavar='N')
parser.add_argument('--data-root-dir', type=str, default="/data_hdd/part-data", metavar='N')
parser.add_argument('--partnet-root-dir', type=str, default='/data_hdd/data_v0', metavar='N')
parser.add_argument('--config-dir', type=str, default="config/assembly.yaml", metavar='N')
args = parser.parse_args()
synth_category = args.synth_category
partnet_root_dir = args.partnet_root_dir
data_root_dir = args.data_root_dir
data_dir = f"graph_{synth_category}_test_final"
vis_root_dir = args.vis_root_dir
vis_dir = f"{vis_root_dir}/gen_{synth_category}/{str(datetime.now())}"
utils.ensuredir(vis_dir)
config_dir = args.config_dir
with open(config_dir, 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
assert 'model_root_dir' in config
model_root_dir = config['model_root_dir']
config['synth_category'] = synth_category
if not 'model_contact_dir' in config:
config['model_contact_dir'] = f"{model_root_dir}/{synth_category}_contact"
if not 'model_retrieval_dir' in config:
config['model_retrieval_dir'] = f"{model_root_dir}/{synth_category}_retrieval"
if not 'model_connect_dir' in config:
config['model_connect_dir'] = f"{model_root_dir}/{synth_category}_connect"
if not 'model_od_dir' in config:
config['model_od_dir'] = f"{model_root_dir}/{synth_category}_od"
if not 'model_contact_checkpoint' in config:
contact_epoch = 'best'
config['model_contact_checkpoint'] = f"{config['model_contact_dir']}/{contact_epoch}.pt"
if not 'model_retrieval_checkpoint' in config:
retrieval_epoch = 'best'
config['model_retrieval_checkpoint'] = f"{config['model_retrieval_dir']}/{retrieval_epoch}.pt"
if not 'model_connect_checkpoint' in config:
connect_epoch = 'best'
config['model_connect_checkpoint'] = f"{config['model_connect_dir']}/{connect_epoch}.pt"
training_size = 0
idxs = []
for (i,filename) in enumerate(Path(f"{data_root_dir}/{data_dir}").glob('*.pkl')):
if "full" not in str(filename):
idxs.append(int(str(filename).split("/")[-1][:-4]))
test_set_size = len(idxs)
with open(f"{vis_dir}/config.yaml", 'w') as f:
yaml.dump(config, f, default_flow_style=False)
if 'test_size' not in config or config['test_size'] == -1:
config['test_size'] = test_set_size
else:
assert config['test_size'] <= test_set_size
assembly = Assembly(config)
for attempts in range(0,10000):
print('=========================================')
print(f"Assembly attempt {attempts}")
assembly.synth(attempts)
print('=========================================')