This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
generate_training_meshes.py
executable file
·139 lines (101 loc) · 3.88 KB
/
generate_training_meshes.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
#!/usr/bin/env python3
# Copyright 2004-present Facebook. All Rights Reserved.
import argparse
import json
import numpy as np
import os
import torch
import deep_sdf
import deep_sdf.workspace as ws
def code_to_mesh(experiment_directory, checkpoint, keep_normalized=False):
specs_filename = os.path.join(experiment_directory, "specs.json")
if not os.path.isfile(specs_filename):
raise Exception(
'The experiment directory does not include specifications file "specs.json"'
)
specs = json.load(open(specs_filename))
arch = __import__("networks." + specs["NetworkArch"], fromlist=["Decoder"])
latent_size = specs["CodeLength"]
decoder = arch.Decoder(latent_size, **specs["NetworkSpecs"])
decoder = torch.nn.DataParallel(decoder)
saved_model_state = torch.load(
os.path.join(experiment_directory, ws.model_params_subdir, checkpoint + ".pth")
)
saved_model_epoch = saved_model_state["epoch"]
decoder.load_state_dict(saved_model_state["model_state_dict"])
decoder = decoder.module.cuda()
decoder.eval()
latent_vectors = ws.load_latent_vectors(experiment_directory, checkpoint)
train_split_file = specs["TrainSplit"]
with open(train_split_file, "r") as f:
train_split = json.load(f)
data_source = specs["DataSource"]
instance_filenames = deep_sdf.data.get_instance_filenames(data_source, train_split)
print(len(instance_filenames), " vs ", len(latent_vectors))
for i, latent_vector in enumerate(latent_vectors):
dataset_name, class_name, instance_name = instance_filenames[i].split("/")
instance_name = instance_name.split(".")[0]
print("{} {} {}".format(dataset_name, class_name, instance_name))
mesh_dir = os.path.join(
experiment_directory,
ws.training_meshes_subdir,
str(saved_model_epoch),
dataset_name,
class_name,
)
print(mesh_dir)
if not os.path.isdir(mesh_dir):
os.makedirs(mesh_dir)
mesh_filename = os.path.join(mesh_dir, instance_name)
print(instance_filenames[i])
offset = None
scale = None
if not keep_normalized:
normalization_params = np.load(
ws.get_normalization_params_filename(
data_source, dataset_name, class_name, instance_name
)
)
offset = normalization_params["offset"]
scale = normalization_params["scale"]
with torch.no_grad():
deep_sdf.mesh.create_mesh(
decoder,
latent_vector,
mesh_filename,
N=256,
max_batch=int(2 ** 18),
offset=offset,
scale=scale,
)
if __name__ == "__main__":
arg_parser = argparse.ArgumentParser(
description="Use a trained DeepSDF decoder to generate a mesh given a latent code."
)
arg_parser.add_argument(
"--experiment",
"-e",
dest="experiment_directory",
required=True,
help="The experiment directory which includes specifications and saved model "
+ "files to use for reconstruction",
)
arg_parser.add_argument(
"--checkpoint",
"-c",
dest="checkpoint",
default="latest",
help="The checkpoint weights to use. This can be a number indicated an epoch "
+ "or 'latest' for the latest weights (this is the default)",
)
arg_parser.add_argument(
"--keep_normalization",
dest="keep_normalized",
default=False,
action="store_true",
help="If set, keep the meshes in the normalized scale.",
)
deep_sdf.add_common_args(arg_parser)
args = arg_parser.parse_args()
deep_sdf.configure_logging(args)
code_to_mesh(args.experiment_directory, args.checkpoint, args.keep_normalized)