-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
375 lines (308 loc) · 14.3 KB
/
test.py
File metadata and controls
375 lines (308 loc) · 14.3 KB
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
# Author : Doyu Lim (2024)
# Simulate reconstruction process through PIT-NBV
import argparse
import os
import csv
import time
import random
import subprocess
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
import open3d as o3d
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from dataloader_poisson import Poisson_dataset
import util.poisson_util as poisson_util
from network.pct_v import PointTransformer_View
def print_data(i_batch, model_id, gt_pcd, partial_pcd, nbv, score, iter, poseList, scoreList, nbvC, sDif):
print('\n--------------------------------- data view ---------------------------------\n')
print('batch idx \t', i_batch)
print('model_id \t', model_id.shape, model_id) # Batch X 1
print('gt_pcd \t', gt_pcd.shape, gt_pcd[0][0]) # Batch X 5000 X 3
print('partial_pcd \t', partial_pcd.shape) # Batch X (# of gt points) X 6
print('nbv_pose \t', nbv.shape, nbv) # Batch X 6
print('nbv scroe \t', score.shape, score) # Batch X 1
print('iteration \t', iter.shape, iter) # Batch X 10
print('all pose\t', poseList.shape, poseList) # Batch X 11 X 6
print('all score\t', scoreList.shape, scoreList) # Batch X 11 X 1
print('nbv candidate\t', nbvC.shape) # Batch X 10 X 6
print('score Diff\t', sDif.shape, sDif) # Batch X 1
print('\n-----------------------------------------------------------------------------\n\n')
def testShapeNet(args, model, device, test_dataloader, logDir, nbvPositions, scaleFactor):
idx = 0
model.eval()
with torch.no_grad():
for i_batch, batch_data in enumerate(test_dataloader):
if (i_batch % args.test_term) != 0:
continue
idx += 1
i_batch = int(i_batch)+1
batch_model_id = np.array(batch_data[0])
batch_gt_pcd = np.array(batch_data[1])
batch_partial_pcd = np.array(batch_data[2])
batch_nbv = np.array(batch_data[3])
batch_score = np.array(batch_data[4])
batch_iter = np.array(batch_data[5])
batch_poseList = np.array(batch_data[6])
batch_scoreList = np.array(batch_data[7])
batch_nbvC = np.array(batch_data[8])
batch_sDif = np.array(batch_data[9])
batch_dist = np.array(batch_data[10])
batch_objPath = np.array(batch_data[11])
print_data(i_batch, batch_model_id, batch_gt_pcd, batch_partial_pcd, batch_nbv, \
batch_score, batch_iter, batch_poseList, batch_scoreList, batch_nbvC, batch_sDif)
target = batch_model_id[0]
objPath = batch_objPath[0]
gtPoints = batch_gt_pcd[0]
simFlag = False
while not simFlag:
simFlag = simulation(args, target, model, device, logDir, nbvPositions, objPath, gtPoints, scaleFactor)
def testMesh(args, model, device, logDir, nbvPositions, meshPath, scaleFactor):
target=args.data
gtPoints = np.array([])
model.eval()
with torch.no_grad():
for i in range(args.Iter):
simFlag = False
while not simFlag:
simFlag = simulation(args, target, model, device, logDir, nbvPositions, meshPath, gtPoints, scaleFactor)
def simulation(args, target, model, device, logDir, nbvPositions, meshPath, gtPoints, scaleFactor):
if args.save :
savePath = os.path.join(logDir, args.test_file)
isExist = os.path.exists(savePath)
csv_file = open(savePath, 'a+')
csv_writer = csv.writer(csv_file)
if not isExist:
header = ['model_id', 'simulation time', 'inference time'] + [str(i) for i in range(1, args.Round + 1)]
csv_writer.writerow(header)
print(f'save file {savePath}')
if args.saveNpIt :
npitPath = os.path.join(logDir, 'npit_'+args.test_file)
ititPath = os.path.join(logDir, 'itit_'+args.test_file)
npit_isExist = os.path.exists(npitPath)
itit_isExist = os.path.exists(ititPath)
npit_file = open(npitPath, 'a+')
itit_file = open(ititPath, 'a+')
npit_writer = csv.writer(npit_file)
itit_writer = csv.writer(itit_file)
if not npit_isExist:
header = ['model_id', 'points num', 'inference time']
npit_writer.writerow(header)
if not itit_isExist:
header = ['model_id', 'simulation time', 'inference time'] + [str(i) for i in range(1, args.Round + 1)]
itit_writer.writerow(header)
mesh = o3d.io.read_triangle_mesh(meshPath)
center, mesh = poisson_util.meshCenter(mesh)
if gtPoints.size == 0: # except shapenet
gtCloud = mesh.sample_points_uniformly(number_of_points=500000)
else : # shapenet
gtCloud = o3d.geometry.PointCloud()
gtCloud.points = o3d.utility.Vector3dVector(gtPoints)
gtCloud.translate(-center)
aabb = gtCloud.get_axis_aligned_bounding_box()
size = aabb.get_max_bound() - aabb.get_min_bound()
print(f'Before preprocessing object size (x,y,z) {size}')
if args.data == 'shapenet': # rotate
R = mesh.get_rotation_matrix_from_xyz((np.pi / 2, 0, 0))
center = mesh.get_center()
mesh.rotate(R, center=center)
gtCloud.rotate(R, center=center)
# scailing
if scaleFactor != False :
maxL = np.max(size)
scaleFactor = 2*args.sphereRadius*scaleFactor / maxL
#scaleFactor = 2*scaleFactor / maxL
center = mesh.get_center()
mesh.scale(scaleFactor, center=center)
gtCloud.scale(scaleFactor, center=center)
aabb = gtCloud.get_axis_aligned_bounding_box()
size = aabb.get_max_bound() - aabb.get_min_bound()
print(f'After preprocessing object size (x,y,z) {size}')
print(f'After preprocessing AABB {aabb}')
print(f'After preprocessing center {mesh.get_center()}')
if args.viz :
mesh.compute_vertex_normals()
#o3d.visualization.draw_geometries([mesh])
#o3d.visualization.draw_geometries([gtCloud])
#o3d.visualization.draw_geometries([gtCloud, mesh])
# initial viewpoint
isAvailable = False
trial = 0
while trial < 50:
trial += 1
camera, direction = poisson_util.genInitialPose(args.sphereRadius)
initCloud = poisson_util.rayCasting(mesh, camera, direction, \
yaw=0.0, sfov=args.sfov, minDist=args.RCminDist, maxDist=args.RCmaxDist)
if initCloud is not None:
isAvailable = True
break
if not isAvailable:
print("No available point!")
if args.save : csv_writer.writerow([target, 0.0, 0.0] + [0.0]*20)
return True
# simulation process
prevCloud = o3d.geometry.PointCloud()
cover = []
infTimes = []
prevCamera = []
prevDirection = []
prevCube = None
simFlag = True
startTime = time.time()
yaw = 0.0
for iter in range(args.Round):
print('\niter', int(iter)+1)
print('camera parameter (nbv) :', camera)
if simFlag == False :
print('Invalid view! Skip this iteration')
curCloud = prevCloud
simFlag = True
else: curCloud = poisson_util.rayCasting(mesh, camera, direction, \
yaw=yaw, sfov=args.sfov, minDist=args.RCminDist, maxDist=args.RCmaxDist)
if args.viz :
if curCloud == None: poisson_util.vizCloud(prevCloud, prevCloud)
else : poisson_util.vizCloud(curCloud, prevCloud)
# accCloud
accCloud = o3d.geometry.PointCloud()
if curCloud == None:
curCloud = prevCloud
accCloud = prevCloud
else : accCloud = prevCloud + curCloud
print('accCloud', accCloud)
# downsampling
voxel_size = 0.01
curCloud = curCloud.voxel_down_sample(voxel_size)
accCloud = accCloud.voxel_down_sample(voxel_size)
prevCloud = accCloud
print('downsampled', accCloud)
# compute coverage score
score = poisson_util.getCoverageScore(gtCloud, accCloud, threshold=0.01)
cover.append(score)
print(f'[Coverage Score] {score}%')
# get NBV through network
infStartTime = time.time()
accPoints = np.asarray(accCloud.points).astype(np.float64) # (original, 3)
accPoints = poisson_util.resample(accPoints, args.sample_input)
accPoints = np.expand_dims(accPoints, axis=0)
accPoints = torch.from_numpy(accPoints).to(device).float()
Snet = model(accPoints, device=device)
camera = np.array([Snet[0][0], Snet[0][1], Snet[0][2]])
direction = np.array([Snet[0][3], Snet[0][4], Snet[0][5]])
print(f'Before VCB : {camera} {direction}')
# VCB
camera, direction = poisson_util.getNBV_VCB_5DOF_all(camera, direction, accCloud, args.VCBoptDist)
camera, direction, yaw = poisson_util.getNBV_VCB_6DOF_nearDensity(camera, direction, accCloud, h_fov=args.h_fov, w_fov=args.w_fov, optDist=args.VCBoptDist, yaw_range_deg=180, num_yaw_steps=90)
print(f'After VCB : {camera} {direction} / yaw {yaw}')
infTime = time.time() - infStartTime
infTimes.append(infTime)
print(f'[Inference Time] {infTime} sec')
if camera is None:
print('[Err] camera is None!')
return False
# view constraint
if poisson_util.isCameraOnObj(gtCloud, camera, threshold=0.01):
print('camera is on the surface')
simFlag = False
# if poisson_util.isCameraInObj(mesh, camera, threshold=0.01):
# print('camera is in the object')
if poisson_util.isCameraInAABB(aabb, camera):
print('camera is in the bbox')
if poisson_util.isVisitedBefore(camera, prevCamera, threshold=0.00):
print('visited before')
simFlag = False
prevCamera.append(camera)
prevDirection.append(direction)
if args.saveNpIt : npit_writer.writerow([target, len(accCloud.points), infTime])
if args.viz:
print('viz acc cloud and nbv')
poisson_util.viz_cloud_wCams(accCloud, prevCamera, prevDirection, sphere_radius=0.02, arrow_length=0.3)
# save data
simTime = time.time() - startTime
print(f'\n[Simulation Time] {simTime} sec\n')
avgInfTime = sum(infTimes)/len(infTimes)
if args.save : csv_writer.writerow([target, simTime, avgInfTime] + cover)
if args.saveNpIt : itit_writer.writerow([target, simTime, avgInfTime] + infTimes)
if args.viz:
print('Viz cloud with NBV points')
poisson_util.viz_cloud_wCams(accCloud, prevCamera, prevDirection, sphere_radius=0.02, arrow_length=0.3)
return True
def main(args):
# Dataset
shapenet_dataset_test = Poisson_dataset(dataset_path=args.data_path, mode='test', \
inputSample=args.sample_input*5, gtSample=args.sample_gt)
test_dataloader = DataLoader(dataset=shapenet_dataset_test, batch_size=args.batch_size, shuffle=False, num_workers=0)
device = args.gpu
print(f'[device] {device}')
print(f'[small FOV] {args.sfov}')
print(f'[limited depth] {args.ldepth}')
print(f'[dataset] {args.data}')
print(f'[test file] {args.test_file}')
# Set parameters
if args.ldepth:
args.RCminDist = 0.2
args.RCmaxDist = 0.3
args.VCBoptDist = 0.25
else:
args.RCminDist = 0
args.RCmaxDist = 100
args.VCBoptDist = 1.0
if args.sfov:
args.h_fov=30
args.w_fov=22.73
else :
args.h_fov=67.82
args.w_fov=53.51
# Set model parameters
nbvPositions = None
model = PointTransformer_View(in_dim=3, out=6).to(device)
logDir = args.log_dir
ptPath = logDir+'/model/epoch_'+str(args.loadEpoch)+'.pt'
input_shape = (args.sample_input, 3)
# trained model load
weightPath = os.path.join(ptPath)
print(f'Load trained model {weightPath}')
model.load_state_dict(torch.load(weightPath, map_location=torch.device(device)))
if args.data == 'shapenet':
scaleFactor = False
args.test_file =f'shapenet_{args.loadEpoch}_{args.test_file}'
testShapeNet(args, model, device, test_dataloader, logDir, nbvPositions, scaleFactor)
elif args.data == 'stanford':
scaleFactor = 0.7
testTerm = f'_{args.loadEpoch}_{args.test_file}'
args.test_file = 'bunny'+testTerm
meshPath = 'data/bunny.ply'
testMesh(args, model, device, logDir, nbvPositions, meshPath, scaleFactor)
elif args.data == 'csail':
scaleFactor = 0.7
testTerm = f'_{args.loadEpoch}_{args.test_file}'
args.test_file = 'bird'+testTerm
meshPath = 'data/bird.obj'
testMesh(args, model, device, logDir, nbvPositions, meshPath, scaleFactor)
print('Done Successfully')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', default='/media/owner/CoCEL/nbv_dataset/owner_poisson_ShapeNet_nbv_dataset_scale_10.hdf5')
parser.add_argument('--log_dir', default='log/240902')
parser.add_argument('--loadEpoch', type=int, default=500)
parser.add_argument('--data', type=str, default='stanford') # test obejct (shapenet/stanford/csail)
parser.add_argument('--test_term', type=int, default=10) # One simulation for every 10 (i.e., only one sim per object)
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--sample_input', type=int, default=1024)
parser.add_argument('--sample_gt', type=int, default=5000)
parser.add_argument('--gpu', default='cpu')
parser.add_argument('--viz', type=bool, default=True)
# save
parser.add_argument('--save', type=bool, default=True)
parser.add_argument('--test_file', type=str, default='test.csv')
parser.add_argument('--saveNpIt', type=bool, default=False) # save relationship btw # of points and inference time
parser.add_argument('--Round', type=int, default=20)
parser.add_argument('--Iter', type=int, default=20)
# sensor specification
parser.add_argument('--sfov', type=bool, default=True) # FOV
parser.add_argument('--ldepth', type=bool, default=False) # working range
parser.add_argument('--sphereRadius', type=float, default=1.0) # Sphere radius for initial viewpoint
args = parser.parse_args()
main(args)