-
Notifications
You must be signed in to change notification settings - Fork 16
/
sample.py
194 lines (169 loc) · 6.01 KB
/
sample.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
import os
import torch
import argparse
from multiprocessing import Pool
from model.code import CodeModel
from model.decoder import SketchDecoder, EXTDecoder
from model.encoder import PARAMEncoder, CMDEncoder, EXTEncoder
import sys
sys.path.insert(0, 'utils')
from utils import CADparser, write_obj_sample
NUM_TRHEADS = 36
NUM_SAMPLE = 20000
BS = 1024
def sample(args):
# Initialize gpu device
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.device)
device = torch.device("cuda:0")
cmd_encoder = CMDEncoder(
config={
'hidden_dim': 512,
'embed_dim': 256,
'num_layers': 4,
'num_heads': 8,
'dropout_rate': 0.1
},
max_len=200,
code_len = 4,
num_code = 500,
)
cmd_encoder.load_state_dict(torch.load(os.path.join(args.sketch_weight, 'cmdenc_epoch_300.pt')))
cmd_encoder = cmd_encoder.to(device).eval()
param_encoder = PARAMEncoder(
config={
'hidden_dim': 512,
'embed_dim': 256,
'num_layers': 4,
'num_heads': 8,
'dropout_rate': 0.1
},
quantization_bits=args.bit,
max_len=200,
code_len = 2,
num_code = 1000,
)
param_encoder.load_state_dict(torch.load(os.path.join(args.sketch_weight, 'paramenc_epoch_300.pt')))
param_encoder = param_encoder.to(device).eval()
sketch_decoder = SketchDecoder(
config={
'hidden_dim': 512,
'embed_dim': 256,
'num_layers': 4,
'num_heads': 8,
'dropout_rate': 0.1
},
pix_len=200,
cmd_len=124,
quantization_bits=args.bit,
)
sketch_decoder.load_state_dict(torch.load(os.path.join(args.sketch_weight, 'sketchdec_epoch_300.pt')))
sketch_decoder = sketch_decoder.to(device).eval()
ext_encoder = EXTEncoder(
config={
'hidden_dim': 512,
'embed_dim': 256,
'num_layers': 4,
'num_heads': 8,
'dropout_rate': 0.1
},
quantization_bits=args.bit,
max_len=96,
code_len = 4,
num_code = 1000,
)
ext_encoder.load_state_dict(torch.load(os.path.join(args.ext_weight, 'extenc_epoch_200.pt')))
ext_encoder = ext_encoder.to(device).eval()
ext_decoder = EXTDecoder(
config={
'hidden_dim': 512,
'embed_dim': 256,
'num_layers': 4,
'num_heads': 8,
'dropout_rate': 0.1
},
max_len=96,
quantization_bits=args.bit,
)
ext_decoder.load_state_dict(torch.load(os.path.join(args.ext_weight, 'extdec_epoch_200.pt')))
ext_decoder = ext_decoder.to(device).eval()
code_model = CodeModel(
config={
'hidden_dim': 512,
'embed_dim': 256,
'num_layers': 8,
'num_heads': 8,
'dropout_rate': 0.1
},
max_len=10,
classes=1000,
)
code_model.load_state_dict(torch.load(os.path.join(args.code_weight, 'code_epoch_800.pt')))
code_model = code_model.to(device).eval()
print('Random Generation...')
if not os.path.exists(args.output):
os.makedirs(args.output)
cad = []
cmd_codebook = cmd_encoder.vq_vae._embedding
param_codebook = param_encoder.vq_vae._embedding
ext_codebook = ext_encoder.vq_vae._embedding
while len(cad) < NUM_SAMPLE:
with torch.no_grad():
codes = code_model.sample(n_samples=BS)
cmd_code = codes[:,:4]
param_code = codes[:,4:6]
ext_code = codes[:,6:]
cmd_codes = []
param_codes = []
ext_codes = []
for cmd, param, ext in zip(cmd_code, param_code, ext_code):
if torch.max(cmd) >= 500:
continue
else:
cmd_codes.append(cmd)
param_codes.append(param)
ext_codes.append(ext)
cmd_codes = torch.vstack(cmd_codes)
param_codes = torch.vstack(param_codes)
ext_codes = torch.vstack(ext_codes)
latent_cmd = cmd_encoder.up(cmd_codebook(cmd_codes))
latent_param = param_encoder.up(param_codebook(param_codes))
latent_ext = ext_encoder.up(ext_codebook(ext_codes))
latent_sketch = torch.cat((latent_cmd, latent_param), 1)
# Parallel Sample Sketches
sample_pixels, latent_ext_samples = sketch_decoder.sample(n_samples=latent_sketch.shape[0], \
latent_z=latent_sketch, latent_ext=latent_ext)
_latent_ext_ = torch.vstack(latent_ext_samples)
# Parallel Sample Extrudes
sample_merges = ext_decoder.sample(n_samples=len(sample_pixels), latent_z=_latent_ext_, sample_pixels=sample_pixels)
cad += sample_merges
print(f'cad:{len(cad)}')
# # Parallel raster OBJ
gen_data = []
load_iter = Pool(NUM_TRHEADS).imap(raster_cad, cad)
for data_sample in load_iter:
gen_data += data_sample
print(len(gen_data))
print('Saving...')
print('Writting OBJ...')
for index, value in enumerate(gen_data):
output = os.path.join(args.output, str(index).zfill(5))
if not os.path.exists(output):
os.makedirs(output)
write_obj_sample(output, value)
def raster_cad(pixels):
try:
parser = CADparser(args.bit)
parsed_data = parser.perform(pixels)
return [parsed_data]
except Exception as error_msg:
return []
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--output", type=str, required=True)
parser.add_argument("--sketch_weight", type=str, required=True)
parser.add_argument("--ext_weight", type=str, required=True)
parser.add_argument("--code_weight", type=str, required=True)
parser.add_argument("--device", type=int, required=True)
parser.add_argument("--bit", type=int, required=True)
args = parser.parse_args()
sample(args)