-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_n_est_multi_scale.py
284 lines (237 loc) · 13.6 KB
/
test_n_est_multi_scale.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
# test_n_est.py run a pretrained DeepFit model and export the normal estimation output
# Author:Itzik Ben Sabat sitzikbs[at]gmail.com
# If you use this code,see LICENSE.txt file and cite our work
from __future__ import print_function
import argparse
import os
import sys
import random
import numpy as np
import torch
import torch.nn.parallel
import torch.utils.data
import importlib.util
import time
from pathlib import Path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR_PATH = Path(BASE_DIR)
sys.path.append(BASE_DIR)
sys.path.append(os.path.join(BASE_DIR_PATH, 'models'))
sys.path.append(os.path.join(BASE_DIR, 'utils'))
from dataset_multi_scale import PointcloudPatchDataset, SequentialShapeRandomPointcloudPatchSampler, RandomPointcloudPatchSampler, SequentialPointcloudPatchSampler
# Execution
# python3 test_n_est_multi_scale.py --models 'Deepfit_simple_sigmoid_cr_log_d1_p64_Lsin' 'Deepfit_simple_sigmoid_cr_log_d2_p64_Lsin' 'Deepfit_simple_sigmoid_cr_log_d3_p64_Lsin' 'Deepfit_simple_sigmoid_cr_log_d4_p64_Lsin' 'Deepfit_simple_sigmoid_cr_log_d4_p128_Lsin' 'Deepfit_simple_sigmoid_cr_log_d3_p128_Lsin' 'Deepfit_simple_sigmoid_cr_log_d2_p128_Lsin' --logdir './log/jetnet_nci_new3/ablations/' --sparse_patches 1 --testset 'testset_all.txt'
def parse_arguments():
parser = argparse.ArgumentParser()
# naming / file handling
parser.add_argument('--indir', type=str, default='./data/pcpnet/', help='input folder (point clouds)')
parser.add_argument('--testset', type=str, default='testset_high_noise.txt', help='shape set file name')
parser.add_argument('--models', type=str, default='my_experiment', help='names of trained models, can evaluate multiple models')
parser.add_argument('--modelpostfix', type=str, default='_model.pth', help='model file postfix')
parser.add_argument('--logdir', type=str, default='./log_v3/my_experiments/', help='model folder')
parser.add_argument('--parmpostfix', type=str, default='_params.pth', help='parameter file postfix')
parser.add_argument('--gpu_idx', type=int, default=1, help='set < 0 to use CPU')
parser.add_argument('--sparse_patches', type=int, default=True, help='evaluate on a sparse set of patches, given by a .pidx file containing the patch center point indices.')
parser.add_argument('--sampling', type=str, default='full', help='sampling strategy, any of:\n'
'full: evaluate all points in the dataset\n'
'sequential_shapes_random_patches: pick n random points from each shape as patch centers, shape order is not randomized')
parser.add_argument('--patches_per_shape', type=int, default=1000, help='number of patches evaluated in each shape (only for sequential_shapes_random_patches)')
parser.add_argument('--seed', type=int, default=40938661, help='manual seed')
parser.add_argument('--batchSize', type=int, default=0, help='batch size, if 0 the training batch size is used')
parser.add_argument('--workers', type=int, default=0, help='number of data loading workers - 0 means same thread as main execution')
parser.add_argument('--cache_capacity', type=int, default=100, help='Max. number of dataset elements (usually shapes) to hold in the cache at the same time.')
return parser.parse_args()
def test_n_est(opt):
opt.models = opt.models.split()
if opt.seed < 0:
opt.seed = random.randint(1, 10000)
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
#os.environ["CUDA_VISIBLE_DEVICES"] = str(opt.gpu_idx)
#device = torch.device("cpu" if opt.gpu_idx < 0 else "cuda:%d" % 0)
device = torch.device("cuda:0")
for model_name in opt.models:
# fetch the model from the log dir
# append model name to output directory and create directory if necessary
model_log_dir = os.path.join(opt.logdir , model_name,'trained_models')
model_filename = os.path.join(model_log_dir, model_name+opt.modelpostfix)
param_filename = os.path.join(model_log_dir, model_name+opt.parmpostfix)
output_dir = os.path.join(opt.logdir, model_name, 'results_debug')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print("Random Seed: %d" % (opt.seed))
random.seed(opt.seed)
torch.manual_seed(opt.seed)
# load model and training parameters
trainopt = torch.load(param_filename)
if not hasattr(trainopt, 'arch'):
trainopt.arch = 'simple'
if opt.batchSize == 0:
opt.batchSize = trainopt.batchSize*4
# get indices in targets and predictions corresponding to each output
target_features, output_target_ind, output_pred_ind, output_loss_weight, pred_dim = get_target_features((trainopt))
dataloader, dataset, datasampler = get_data_loaders(opt, trainopt, target_features)
if trainopt.arch == 'simple':
spec = importlib.util.spec_from_file_location("AdaFit_multi_scale", os.path.join(
os.path.join( "models/", "AdaFit_multi_scale.py")))
DeepFit = importlib.util.module_from_spec(spec)
spec.loader.exec_module(DeepFit)
regressor = DeepFit.DeepFit(1, num_points=trainopt.points_per_patch,
use_point_stn=trainopt.use_point_stn,
use_feat_stn=trainopt.use_feat_stn, point_tuple=1,
sym_op=trainopt.sym_op, jet_order=trainopt.jet_order,
weight_mode=trainopt.weight_mode).cuda()
elif trainopt.arch == '3dmfv':
spec = importlib.util.spec_from_file_location("DeepFitNormals", os.path.join(
os.path.join(opt.logdir, model_name, "DeepFitNormals.py")))
DeepFit = importlib.util.module_from_spec(spec)
spec.loader.exec_module(DeepFit)
regressor = DeepFit.DeepFit(1, num_points=trainopt.points_per_patch,
use_point_stn=trainopt.use_point_stn,
use_feat_stn=trainopt.use_feat_stn, point_tuple=trainopt.point_tuple,
sym_op=trainopt.sym_op, arch=trainopt.arch,
n_gaussians=trainopt.n_gaussians, jet_order=trainopt.jet_order,
weight_mode=trainopt.weight_mode).cuda()
else:
raise ValueError("unsupported architecture")
regressor.load_state_dict(torch.load(model_filename))
regressor.to(device)
regressor.eval()
shape_ind = 0
shape_patch_offset = 0
if opt.sampling == 'full':
shape_patch_count = dataset.shape_patch_count[shape_ind]
elif opt.sampling == 'sequential_shapes_random_patches':
shape_patch_count = min(opt.patches_per_shape, dataset.shape_patch_count[shape_ind])
else:
raise ValueError('Unknown sampling strategy: %s' % opt.sampling)
num_batch = len(dataloader)
batch_enum = enumerate(dataloader, 0)
shape_ind = 0
normal_prop = torch.zeros([shape_patch_count, 3])
for batchind, data in batch_enum:
# get batch and upload to GPU
points = data[0]
target = data[1:-2]
data_trans = data[-2]
n_effective_points = data[-1].squeeze()
points = points.transpose(2, 1)
points = points.to(device)
data_trans = data_trans.to(device)
target = tuple(t.to(device) for t in target)
start_time = 0
end_time = 0
with torch.no_grad():
if trainopt.arch == 'simple' or trainopt.arch == 'res' or trainopt.arch == '3dmfv':
start_time = time.time()
#n_est, beta_pred, weights, trans, trans2, neighbor_normals = regressor(points)
n_est, beta_pred, weights, trans, trans2, neighbor_normals,_ = regressor(points)
end_time = time.time()
print("elapsed_time per point: {} ms".format(1000*(end_time-start_time) / opt.batchSize))
if trainopt.use_point_stn:
# transform predictions with inverse transform
# since we know the transform to be a rotation (QSTN), the transpose is the inverse
n_est[:, :] = torch.bmm(n_est.unsqueeze(1), trans.transpose(2, 1)).squeeze(dim=1)
if trainopt.use_pca:
# transform predictions with inverse pca rotation (back to world space)
n_est[:, :] = torch.bmm(n_est.unsqueeze(1), data_trans.transpose(2, 1)).squeeze(dim=1)
print('[%s %d/%d] shape %s' % (model_name, batchind, num_batch-1, dataset.shape_names[shape_ind]))
# Save estimated normals to file
batch_offset = 0
while batch_offset < n_est.shape[0] and shape_ind + 1 <= len(dataset.shape_names):
shape_patches_remaining = shape_patch_count - shape_patch_offset
batch_patches_remaining = n_est.shape[0] - batch_offset
# append estimated patch properties batch to properties for the current shape on the CPU
normal_prop[shape_patch_offset:shape_patch_offset + min(shape_patches_remaining,
batch_patches_remaining), :] = \
n_est[batch_offset:batch_offset + min(shape_patches_remaining, batch_patches_remaining), :]
batch_offset = batch_offset + min(shape_patches_remaining, batch_patches_remaining)
shape_patch_offset = shape_patch_offset + min(shape_patches_remaining, batch_patches_remaining)
if shape_patches_remaining <= batch_patches_remaining:
normals_to_write = normal_prop.cpu().numpy()
eps=1e-6
normals_to_write[np.logical_and(normals_to_write < eps, normals_to_write > -eps)] = 0.0
np.savetxt(os.path.join(output_dir, dataset.shape_names[shape_ind] + '.normals'),
normals_to_write)
print('saved normals for ' + dataset.shape_names[shape_ind])
sys.stdout.flush()
shape_patch_offset = 0
shape_ind += 1
if shape_ind < len(dataset.shape_names):
shape_patch_count = dataset.shape_patch_count[shape_ind]
normal_prop = torch.zeros([shape_patch_count, 3])
def get_data_loaders(opt, trainopt, target_features):
# create dataset loader
if opt.batchSize == 0:
model_batchSize = trainopt.batchSize
else:
model_batchSize = opt.batchSize
test_dataset = PointcloudPatchDataset(
root=opt.indir,
shape_list_filename=opt.testset,
patch_radius=trainopt.patch_radius,
points_per_patch=trainopt.points_per_patch,
patch_features=target_features,
point_count_std=trainopt.patch_point_count_std,
seed=opt.seed,
identical_epochs=trainopt.identical_epochs,
use_pca=trainopt.use_pca,
center=trainopt.patch_center,
point_tuple=trainopt.point_tuple,
sparse_patches=opt.sparse_patches,
cache_capacity=opt.cache_capacity,
neighbor_search_method=trainopt.neighbor_search,
final_patch_size=opt.points_per_patch//4)
if opt.sampling == 'full':
test_datasampler = SequentialPointcloudPatchSampler(test_dataset)
elif opt.sampling == 'sequential_shapes_random_patches':
test_datasampler = SequentialShapeRandomPointcloudPatchSampler(
test_dataset,
patches_per_shape=opt.patches_per_shape,
seed=opt.seed,
sequential_shapes=True,
identical_epochs=False)
else:
raise ValueError('Unknown sampling strategy: %s' % opt.sampling)
test_dataloader = torch.utils.data.DataLoader(
test_dataset,
sampler=test_datasampler,
batch_size=model_batchSize,
num_workers=int(opt.workers))
return test_dataloader, test_dataset, test_datasampler
def get_target_features(opt):
# get indices in targets and predictions corresponding to each output
target_features = []
output_target_ind = []
output_pred_ind = []
output_loss_weight = []
pred_dim = 0
for o in opt.outputs:
if o == 'unoriented_normals' or o == 'oriented_normals':
if 'normal' not in target_features:
target_features.append('normal')
output_target_ind.append(target_features.index('normal'))
output_pred_ind.append(pred_dim)
output_loss_weight.append(1.0)
pred_dim += 3
elif o == 'max_curvature' or o == 'min_curvature':
if o not in target_features:
target_features.append(o)
output_target_ind.append(target_features.index(o))
output_pred_ind.append(pred_dim)
if o == 'max_curvature':
output_loss_weight.append(0.7)
else:
output_loss_weight.append(0.3)
pred_dim += 1
elif o == 'neighbor_normals':
target_features.append(o)
output_target_ind.append(target_features.index(o))
output_pred_ind.append(pred_dim)
else:
raise ValueError('Unknown output: %s' % (o))
if pred_dim <= 0:
raise ValueError('Prediction is empty for the given outputs.')
return target_features, output_target_ind, output_pred_ind, output_loss_weight, pred_dim
if __name__ == '__main__':
eval_opt = parse_arguments()
test_n_est(eval_opt)