def main(): args = parse_args() inputs = tf.placeholder(tf.float32, (1, None, 3)) gt = tf.placeholder(tf.float32, (1, args.num_gt_points, 3)) npts = tf.placeholder(tf.int32, (1,)) model_module = importlib.import_module('.%s' % args.model_type, 'models') model = model_module.Model(inputs, npts, gt, tf.constant(1.0)) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) saver = tf.train.Saver() saver.restore(sess, args.checkpoint) partial = read_pcd(args.input_path) complete = sess.run(model.outputs, feed_dict={inputs: [partial], npts: [partial.shape[0]]})[0] create_plots(partial, complete) if args.output_path is None: show_pcd(complete) plt.show() else: os.makedirs(args.output_path, exist_ok=True) filename = os.path.splitext(os.path.basename(args.input_path))[0] output_file = os.path.join(args.output_path, filename + '.pcd') save_pcd(output_file, complete) output_file = os.path.join(args.output_path, filename + '.png') plt.savefig(output_file)
def test(args): inputs = tf.placeholder(tf.float32, (1, None, 3)) npts = tf.placeholder(tf.int32, (1, )) print('AAAAAAAAAAAAAAAA') print(args.num_gt_points) print('AAAAAAAAAAAAAAAAAAAAAAAAAa') gt = tf.placeholder(tf.float32, (1, args.num_gt_points, 3)) model_module = importlib.import_module('.%s' % args.model_type, 'models') model = model_module.Model(inputs, npts, gt, tf.constant(1.0)) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) saver = tf.train.Saver() saver.restore(sess, args.checkpoint) t0 = time.time() partial = read_pcd(args.pcd_file) bbox = np.loadtxt(args.bbox_file) # Calculate center, rotation and scale center = (bbox.min(0) + bbox.max(0)) / 2 bbox -= center yaw = np.arctan2(bbox[3, 1] - bbox[0, 1], bbox[3, 0] - bbox[0, 0]) rotation = np.array([[np.cos(yaw), -np.sin(yaw), 0], [np.sin(yaw), np.cos(yaw), 0], [0, 0, 1]]) bbox = np.dot(bbox, rotation) scale = bbox[3, 0] - bbox[0, 0] bbox /= scale partial = np.dot(partial - center, rotation) / scale partial = np.dot(partial, [[1, 0, 0], [0, 0, 1], [0, 1, 0]]) start = time.time() completion = sess.run(model.outputs, feed_dict={ inputs: [partial], npts: [partial.shape[0]] }) completion = completion[0] completion_w = np.dot(completion, [[1, 0, 0], [0, 0, 1], [0, 1, 0]]) completion_w = np.dot(completion_w * scale, rotation.T) + center dummy = args.pcd_file.split('/')[-1] result_path = os.path.join(args.results_dir, dummy) save_pcd(result_path, completion_w) plot_path = os.path.join(args.results_dir, 'plots.png') plot_pcd_three_views(plot_path, [partial, completion], ['input', 'output'], '%d input points' % partial.shape[0], [5, 0.5]) sess.close() tf = time.time() print('Total time: {}'.format(tf - t0))
def test(args): inputs = tf.placeholder(tf.float32, (1, None, 3)) npts = tf.placeholder(tf.int32, (1,)) gt = tf.placeholder(tf.float32, (1, args.num_gt_points, 3)) model_module = importlib.import_module('.%s' % args.model_type, 'models') model = model_module.Model(inputs, npts, gt, tf.constant(1.0)) os.makedirs(os.path.join(args.results_dir, 'plots'), exist_ok=True) os.makedirs(os.path.join(args.results_dir, 'completions'), exist_ok=True) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) saver = tf.train.Saver() saver.restore(sess, args.checkpoint) car_ids = [filename.split('.')[0] for filename in os.listdir(args.pcd_dir)] total_time = 0 total_points = 0 for i, car_id in enumerate(car_ids): partial = read_pcd(os.path.join(args.pcd_dir, '%s.pcd' % car_id)) bbox = np.loadtxt(os.path.join(args.bbox_dir, '%s.txt' % car_id)) total_points += partial.shape[0] # Calculate center, rotation and scale center = (bbox.min(0) + bbox.max(0)) / 2 bbox -= center yaw = np.arctan2(bbox[3, 1] - bbox[0, 1], bbox[3, 0] - bbox[0, 0]) rotation = np.array([[np.cos(yaw), -np.sin(yaw), 0], [np.sin(yaw), np.cos(yaw), 0], [0, 0, 1]]) bbox = np.dot(bbox, rotation) scale = bbox[3, 0] - bbox[0, 0] bbox /= scale partial = np.dot(partial - center, rotation) / scale partial = np.dot(partial, [[1, 0, 0], [0, 0, 1], [0, 1, 0]]) start = time.time() completion = sess.run(model.outputs, feed_dict={inputs: [partial], npts: [partial.shape[0]]}) total_time += time.time() - start completion = completion[0] completion_w = np.dot(completion, [[1, 0, 0], [0, 0, 1], [0, 1, 0]]) completion_w = np.dot(completion_w * scale, rotation.T) + center pcd_path = os.path.join(args.results_dir, 'completions', '%s.pcd' % car_id) save_pcd(pcd_path, completion_w) if i % args.plot_freq == 0: plot_path = os.path.join(args.results_dir, 'plots', '%s.png' % car_id) plot_pcd_three_views(plot_path, [partial, completion], ['input', 'output'], '%d input points' % partial.shape[0], [5, 0.5]) print('Average # input points:', total_points / len(car_ids)) print('Average time:', total_time / len(car_ids)) sess.close()
def read(args): df = dataflow.LMDBSerializer.load(args.lmdb_path, shuffle=False) size = df.size() df.reset_state() data_gen = df.get_data() i = 0 for _, data in enumerate(data_gen): i += 1 name, inputs, gt = data synset_id = name.split('_')[0] os.system('mkdir -p data/shapenet_pcd/train/partial/' + synset_id) os.system('mkdir -p data/shapenet_pcd/train/gt/' + synset_id) name = name.split('_')[1] save_pcd( 'data/shapenet_pcd/train/partial/' + synset_id + '/' + name + '.pcd', np.array(inputs)) save_pcd( 'data/shapenet_pcd/train/gt/' + synset_id + '/' + name + '.pcd', np.array(gt)) print(i)
def test(args): inputs = tf.placeholder(tf.float32, (1, None, 3)) npts = tf.placeholder(tf.int32, (1, )) gt = tf.placeholder(tf.float32, (1, args.num_gt_points, 3)) model_module = importlib.import_module('.%s' % args.model_type, 'models') model = model_module.Model(inputs, npts, gt, tf.constant(1.0)) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) saver = tf.train.Saver() saver.restore(sess, args.checkpoint) an = Analyzer(dr.get_PLIDAR_predicted_path) argument_list = an.get_datatype_trackno_carno(data_types=["train"]) for i in range(len(argument_list)): data_type, track_no, car_no = argument_list[i] # Input - partial point cloud .pcd pcd_file = \ dr.get_pcn_lidar_reference_partial_path(data_type, track_no, car_no, extension='pcd') # Input - bbox 8 corners .txt bbox_file = dr.get_pcn_bbox_lidar_path(data_type, track_no, car_no) # Result - complete point cloud .pcd result_path = dr.get_pcn_lidar_reference_complete_path( data_type, track_no, car_no) # Result - plot of input and output of pointcloud plot_path = dr.get_pcn_plot_lidar_path(data_type, track_no, car_no) partial = read_pcd(pcd_file) bbox = np.loadtxt(bbox_file) # Calculate center, rotation and scale center = (bbox.min(0) + bbox.max(0)) / 2 bbox -= center yaw = np.arctan2(bbox[3, 1] - bbox[0, 1], bbox[3, 0] - bbox[0, 0]) rotation = np.array([[np.cos(yaw), -np.sin(yaw), 0], [np.sin(yaw), np.cos(yaw), 0], [0, 0, 1]]) bbox = np.dot(bbox, rotation) scale = bbox[3, 0] - bbox[0, 0] bbox /= scale partial = np.dot(partial - center, rotation) / scale partial = np.dot(partial, [[1, 0, 0], [0, 0, 1], [0, 1, 0]]) completion = sess.run(model.outputs, feed_dict={ inputs: [partial], npts: [partial.shape[0]] }) completion = completion[0] completion_w = np.dot(completion, [[1, 0, 0], [0, 0, 1], [0, 1, 0]]) completion_w = np.dot(completion_w * scale, rotation.T) + center save_pcd(result_path, completion_w) plot_pcd_three_views(plot_path, [partial, completion], ['input', 'output'], '%d input points' % partial.shape[0], [5, 0.5]) print('Finish {}/{}'.format(i + 1, len(argument_list))) sess.close()
def test(args): inputs = tf.placeholder(tf.float32, (1, None, 3)) gt = tf.placeholder(tf.float32, (1, args.num_gt_points, 3)) model_module = importlib.import_module('.%s' % args.model_type, 'models') model = model_module.Model(inputs, gt, tf.constant(1.0)) output = tf.placeholder(tf.float32, (1, args.num_gt_points, 3)) cd_op = chamfer(output, gt) emd_op = earth_mover(output, gt) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) saver = tf.train.Saver() saver.restore(sess, args.checkpoint) os.makedirs(args.results_dir, exist_ok=True) csv_file = open(os.path.join(args.results_dir, 'results.csv'), 'w') writer = csv.writer(csv_file) writer.writerow(['id', 'cd', 'emd']) with open(args.list_path) as file: model_list = file.read().splitlines() total_time = 0 total_cd = 0 total_emd = 0 cd_per_cat = {} emd_per_cat = {} for i, model_id in enumerate(model_list): partial = read_pcd( os.path.join(args.data_dir, 'partial', '%s.pcd' % model_id)) complete = read_pcd( os.path.join(args.data_dir, 'complete', '%s.pcd' % model_id)) start = time.time() completion = sess.run(model.outputs, feed_dict={inputs: [partial]}) total_time += time.time() - start cd, emd = sess.run([cd_op, emd_op], feed_dict={ output: completion, gt: [complete] }) total_cd += cd total_emd += emd writer.writerow([model_id, cd, emd]) synset_id, model_id = model_id.split('/') if not cd_per_cat.get(synset_id): cd_per_cat[synset_id] = [] if not emd_per_cat.get(synset_id): emd_per_cat[synset_id] = [] cd_per_cat[synset_id].append(cd) emd_per_cat[synset_id].append(emd) if i % args.plot_freq == 0: os.makedirs(os.path.join(args.results_dir, 'plots', synset_id), exist_ok=True) plot_path = os.path.join(args.results_dir, 'plots', synset_id, '%s.png' % model_id) plot_pcd_three_views(plot_path, [partial, completion[0], complete], ['input', 'output', 'ground truth'], 'CD %.4f EMD %.4f' % (cd, emd), [5, 0.5, 0.5]) if args.save_pcd: os.makedirs(os.path.join(args.results_dir, 'pcds', synset_id), exist_ok=True) save_pcd( os.path.join(args.results_dir, 'pcds', '%s.pcd' % model_id), completion[0]) csv_file.close() sess.close() print('Average time: %f' % (total_time / len(model_list))) print('Average Chamfer distance: %f' % (total_cd / len(model_list))) print('Average Earth mover distance: %f' % (total_emd / len(model_list))) print('Chamfer distance per category') for synset_id in cd_per_cat.keys(): print(synset_id, '%f' % np.mean(cd_per_cat[synset_id])) print('Earth mover distance per category') for synset_id in emd_per_cat.keys(): print(synset_id, '%f' % np.mean(emd_per_cat[synset_id]))
def evaluate(batch_size, checknum, mode, discriminative): n_vox = cfg.CONST.N_VOX dim = cfg.NET.DIM vox_shape = [n_vox[0], n_vox[1], n_vox[2], dim[4]] com_shape = [n_vox[0], n_vox[1], n_vox[2], 2] dim_z = cfg.NET.DIM_Z start_vox_size = cfg.NET.START_VOX kernel = cfg.NET.KERNEL stride = cfg.NET.STRIDE dilations = cfg.NET.DILATIONS freq = cfg.CHECK_FREQ save_path = cfg.DIR.EVAL_PATH if discriminative is True: model_path = cfg.DIR.CHECK_POINT_PATH + '-d' else: model_path = cfg.DIR.CHECK_POINT_PATH chckpt_path = model_path + '/checkpoint' + str(checknum) depvox_gan_model = depvox_gan(batch_size=batch_size, vox_shape=vox_shape, com_shape=com_shape, dim_z=dim_z, dim=dim, start_vox_size=start_vox_size, kernel=kernel, stride=stride, dilations=dilations, discriminative=discriminative, is_train=False) Z_tf, z_enc_tf, surf_tf, full_tf, full_gen_tf, surf_dec_tf, full_dec_tf,\ gen_loss_tf, discrim_loss_tf, recons_ssc_loss_tf, recons_com_loss_tf, recons_sem_loss_tf, encode_loss_tf, refine_loss_tf, summary_tf,\ part_tf, part_dec_tf, comp_gt_tf, comp_gen_tf, comp_dec_tf, ssc_tf, scores_tf = depvox_gan_model.build_model() if discriminative is True: Z_tf_samp, comp_tf_samp, surf_tf_samp, full_tf_samp, part_tf_samp, scores_tf_samp = depvox_gan_model.samples_generator( visual_size=batch_size) sess = tf.InteractiveSession() saver = tf.train.Saver() # Restore variables from disk. saver.restore(sess, chckpt_path) print("...Weights restored.") if mode == 'recons': # evaluation for reconstruction voxel_test, surf_test, part_test, num, data_paths = scene_model_id_pair_test( dataset_portion=cfg.TRAIN.DATASET_PORTION) # Evaluation masks if cfg.TYPE_TASK == 'scene': # occluded region """ space_effective = np.where(voxel_test > -1, 1, 0) * np.where( part_test > -1, 1, 0) voxel_test *= space_effective part_test *= space_effective """ # occluded region part_test[part_test < -1] = 0 surf_test[surf_test < 0] = 0 voxel_test[voxel_test < 0] = 0 num = voxel_test.shape[0] print("test voxels loaded") from progressbar import ProgressBar pbar = ProgressBar() for i in pbar(np.arange(int(num / batch_size))): bth_tsdf = part_test[i * batch_size:i * batch_size + batch_size] bth_surf = surf_test[i * batch_size:i * batch_size + batch_size] bth_voxel = voxel_test[i * batch_size:i * batch_size + batch_size] bth_pd_surf, bth_pd_full, bth_pd_part, bth_part_enc_Z, bth_comp_gt, bth_pd_comp, bth_ssc = sess.run( [ surf_dec_tf, full_dec_tf, part_dec_tf, z_enc_tf, comp_gt_tf, comp_dec_tf, ssc_tf ], feed_dict={ part_tf: bth_tsdf, surf_tf: bth_surf, full_tf: bth_voxel }) if i == 0: pd_part = bth_pd_part pd_surf = bth_pd_surf pd_full = bth_pd_full pd_ssc = bth_ssc part_enc_Z = bth_part_enc_Z comp_gt = bth_comp_gt pd_comp = bth_pd_comp else: pd_part = np.concatenate((pd_part, bth_pd_part), axis=0) pd_surf = np.concatenate((pd_surf, bth_pd_surf), axis=0) pd_full = np.concatenate((pd_full, bth_pd_full), axis=0) pd_ssc = np.concatenate((pd_ssc, bth_ssc), axis=0) part_enc_Z = np.concatenate((part_enc_Z, bth_part_enc_Z), axis=0) comp_gt = np.concatenate((comp_gt, bth_comp_gt), axis=0) pd_comp = np.concatenate((pd_comp, bth_pd_comp), axis=0) print("forwarded") # For visualization bin_file = np.uint8(voxel_test) bin_file.tofile(save_path + '/scene.bin') sdf_volume = np.round(10 * np.abs(np.array(part_test))) observed = np.array(part_test) if cfg.TYPE_TASK == 'scene': observed = np.abs(observed) observed *= 10 observed -= 7 observed = np.round(observed) pd_part = np.abs(pd_part) pd_part *= 10 pd_part -= 7 elif cfg.TYPE_TASK == 'object': observed = np.clip(observed, 0, 1) pd_part = np.clip(pd_part, 0, 1) sdf_volume.astype('uint8').tofile(save_path + '/surface.bin') pd_part.astype('uint8').tofile(save_path + '/dec_part.bin') depsem_gt = np.multiply(voxel_test, np.clip(observed, 0, 1)) if cfg.TYPE_TASK == 'scene': depsem_gt[depsem_gt < 0] = 0 depsem_gt.astype('uint8').tofile(save_path + '/depth_seg_scene.bin') # decoded do_save_pcd = True if do_save_pcd is True: results_pcds = np.argmax(pd_ssc, axis=4) for i in range(np.shape(results_pcds)[0]): pcd_idx = np.where(results_pcds[i] > 0) pts_coord = np.float32(np.transpose(pcd_idx)) / 80 - 0.5 pts_color = matplotlib.cm.Paired( np.float32(results_pcds[i][pcd_idx]) / 11 - 0.5 / 11) output_name = os.path.join('results_pcds', '%s.pcd' % data_paths[i][1][:-4]) output_pcds = np.concatenate((pts_coord, pts_color[:, 0:3]), -1) save_pcd(output_name, output_pcds) np.argmax(pd_ssc, axis=4).astype('uint8').tofile(save_path + '/dec_ssc.bin') error = np.array( np.clip(np.argmax(pd_ssc, axis=4), 0, 1) + np.argmax(comp_gt, axis=4) * 2) error.astype('uint8').tofile(save_path + '/dec_ssc_error.bin') np.argmax(pd_surf, axis=4).astype('uint8').tofile(save_path + '/dec_surf.bin') error = np.array( np.clip(np.argmax(pd_surf, axis=4), 0, 1) + np.argmax(comp_gt, axis=4) * 2) error.astype('uint8').tofile(save_path + '/dec_surf_error.bin') np.argmax(pd_full, axis=4).astype('uint8').tofile(save_path + '/dec_full.bin') error = np.array( np.clip(np.argmax(pd_full, axis=4), 0, 1) + np.argmax(comp_gt, axis=4) * 2) error.astype('uint8').tofile(save_path + '/dec_full_error.bin') np.argmax(pd_comp, axis=4).astype('uint8').tofile(save_path + '/dec_complete.bin') np.argmax(comp_gt, axis=4).astype('uint8').tofile(save_path + '/complete_gt.bin') # reconstruction and generation from normal distribution evaluation # generator from random distribution if discriminative is True: np.save(save_path + '/decode_z.npy', part_enc_Z) sample_times = 10 for j in np.arange(sample_times): gaussian_samp = np.random.normal( size=(batch_size, start_vox_size[0], start_vox_size[1], start_vox_size[2], dim_z)).astype(np.float32) z_comp_rnd, z_surf_rnd, z_full_rnd, z_part_rnd, scores_samp = sess.run( [ comp_tf_samp, surf_tf_samp, full_tf_samp, part_tf_samp, scores_tf_samp ], feed_dict={Z_tf_samp: gaussian_samp}) if j == 0: z_comp_rnd_all = z_comp_rnd z_part_rnd_all = z_part_rnd z_surf_rnd_all = z_surf_rnd z_full_rnd_all = z_full_rnd else: z_comp_rnd_all = np.concatenate( [z_comp_rnd_all, z_comp_rnd], axis=0) z_part_rnd_all = np.concatenate( [z_part_rnd_all, z_part_rnd], axis=0) z_surf_rnd_all = np.concatenate( [z_surf_rnd_all, z_surf_rnd], axis=0) z_full_rnd_all = np.concatenate( [z_full_rnd_all, z_full_rnd], axis=0) print('Discrim score: ' + colored(np.mean(scores_samp), 'blue')) gaussian_samp.astype('float32').tofile(save_path + '/sample_z.bin') np.argmax(z_comp_rnd_all, axis=4).astype('uint8').tofile(save_path + '/gen_comp.bin') np.argmax(z_surf_rnd_all, axis=4).astype('uint8').tofile(save_path + '/gen_surf.bin') np.argmax(z_full_rnd_all, axis=4).astype('uint8').tofile(save_path + '/gen_full.bin') if cfg.TYPE_TASK == 'scene': z_part_rnd_all = np.abs(z_part_rnd_all) z_part_rnd_all *= 10 z_part_rnd_all -= 7 elif cfg.TYPE_TASK == 'object': z_part_rnd_all[z_part_rnd_all <= 0.4] = 0 z_part_rnd_all[z_part_rnd_all > 0.4] = 1 z_part_rnd = np.squeeze(z_part_rnd) z_part_rnd_all.astype('uint8').tofile(save_path + '/gen_part.bin') print("voxels saved") # numerical evalutation iou_eval = True if iou_eval is True: # completion print(colored("Completion:", 'red')) on_gt = comp_gt pd_max = np.argmax(pd_comp, axis=4) on_pd = onehot(pd_max, 2) IoU_comp = np.zeros([2 + 1]) AP_comp = np.zeros([2 + 1]) IoU_comp = IoU(on_gt, on_pd, [vox_shape[0], vox_shape[1], vox_shape[2], 2]) # depth segmentation print(colored("Segmentation:", 'red')) print(colored("encoded", 'cyan')) on_gt = onehot(depsem_gt, vox_shape[3]) on_pd = np.multiply( onehot(np.argmax(pd_ssc, axis=4), vox_shape[3]), np.expand_dims(np.clip(observed, 0, 1), -1)) # IoUs = np.zeros([vox_shape[3] + 1]) IoU_temp = IoU(on_gt, on_pd, vox_shape) IoU_all = np.expand_dims(IoU_temp, axis=1) print(colored("decoded", 'cyan')) on_pd = np.multiply( onehot(np.argmax(pd_surf, axis=4), vox_shape[3]), np.expand_dims(np.clip(observed, 0, 1), -1)) IoU_temp = IoU(on_gt, on_pd, vox_shape, IoU_compared=IoU_all[:, -1]) IoU_all = np.concatenate( (IoU_all, np.expand_dims(IoU_temp, axis=1)), axis=1) print(colored("solidly decoded", 'cyan')) on_pd = np.multiply( onehot(np.argmax(pd_full, axis=4), vox_shape[3]), np.expand_dims(np.clip(observed, 0, 1), -1)) IoU_temp = IoU(on_gt, on_pd, vox_shape, IoU_compared=IoU_all[:, -1]) IoU_all = np.concatenate( (IoU_all, np.expand_dims(IoU_temp, axis=1)), axis=1) # volume segmentation print(colored("Semantic Completion:", 'red')) on_surf_gt = onehot(surf_test, vox_shape[3]) on_gt = onehot(voxel_test, vox_shape[3]) print(colored("encoded", 'cyan')) on_pd = onehot(np.argmax(pd_ssc, axis=4), vox_shape[3]) IoU_temp = IoU(on_gt, on_pd, vox_shape) IoU_all = np.concatenate( (IoU_all, np.expand_dims(IoU_temp, axis=1)), axis=1) print(colored("decoded", 'cyan')) on_pd = onehot(np.argmax(pd_surf, axis=4), vox_shape[3]) IoU_temp = IoU(on_gt, on_pd, vox_shape, IoU_compared=IoU_all[:, -1]) IoU_all = np.concatenate( (IoU_all, np.expand_dims(IoU_temp, axis=1)), axis=1) print(colored("solidly decoded", 'cyan')) on_pd = onehot(np.argmax(pd_full, axis=4), vox_shape[3]) IoU_temp = IoU(on_gt, on_pd, vox_shape, IoU_compared=IoU_all[:, -1]) IoU_all = np.concatenate( (IoU_all, np.expand_dims(IoU_temp, axis=1)), axis=1) np.savetxt(save_path + '/IoU.csv', np.transpose(IoU_all[1:] * 100), delimiter=" & ", fmt='%2.1f') # interpolation evaluation if mode == 'interpolate': interpolate_num = 8 #interpolatioin latent vectores decode_z = np.load(save_path + '/decode_z.npy') print(save_path) decode_z = decode_z[20:20 + batch_size] for l in np.arange(batch_size): for r in np.arange(batch_size): if l != r: print l, r base_num_left = l base_num_right = r left = np.reshape(decode_z[base_num_left], [ 1, start_vox_size[0], start_vox_size[1], start_vox_size[2], dim_z ]) right = np.reshape(decode_z[base_num_right], [ 1, start_vox_size[0], start_vox_size[1], start_vox_size[2], dim_z ]) duration = (right - left) / (interpolate_num - 1) # left is the reference sample and Z_np_samp is the remaining samples if base_num_left == 0: Z_np_samp = decode_z[1:] elif base_num_left == batch_size - 1: Z_np_samp = decode_z[:batch_size - 1] else: Z_np_samp_before = np.reshape( decode_z[:base_num_left], [ base_num_left, start_vox_size[0], start_vox_size[1], start_vox_size[2], dim_z ]) Z_np_samp_after = np.reshape( decode_z[base_num_left + 1:], [ batch_size - base_num_left - 1, start_vox_size[0], start_vox_size[1], start_vox_size[2], dim_z ]) Z_np_samp = np.concatenate( [Z_np_samp_before, Z_np_samp_after], axis=0) for i in np.arange(interpolate_num): if i == 0: Z = copy.copy(left) interpolate_z = copy.copy(Z) else: Z = Z + duration interpolate_z = np.concatenate([interpolate_z, Z], axis=0) # Z_np_samp is used to fill up the batch gaussian_samp = np.concatenate([Z, Z_np_samp], axis=0) pd_full_rnd, pd_part_rnd = sess.run( [full_tf_samp, part_tf_samp], feed_dict={Z_tf_samp: gaussian_samp}) interpolate_vox = np.reshape(pd_full_rnd[0], [ 1, vox_shape[0], vox_shape[1], vox_shape[2], vox_shape[3] ]) interpolate_part = np.reshape(pd_part_rnd[0], [ 1, vox_shape[0], vox_shape[1], vox_shape[2], com_shape[3] ]) if i == 0: pd_full = interpolate_vox pd_part = interpolate_part else: pd_full = np.concatenate( [pd_full, interpolate_vox], axis=0) pd_part = np.concatenate( [pd_part, interpolate_part], axis=0) interpolate_z.astype('uint8').tofile( save_path + '/interpolate/interpolation_z' + str(l) + '-' + str(r) + '.bin') full_models_cat = np.argmax(pd_full, axis=4) full_models_cat.astype('uint8').tofile( save_path + '/interpolate/interpolation_f' + str(l) + '-' + str(r) + '.bin') if cfg.TYPE_TASK == 'scene': pd_part = np.abs(pd_part) pd_part *= 10 pd_part -= 7 elif cfg.TYPE_TASK == 'object': pd_part = np.argmax(pd_part, axis=4) pd_part.astype('uint8').tofile( save_path + '/interpolate/interpolation_p' + str(l) + '-' + str(r) + '.bin') print("voxels saved") # add noise evaluation if mode == 'noise': decode_z = np.load(save_path + '/decode_z.npy') decode_z = decode_z[:batch_size] noise_num = 10 for base_num in np.arange(batch_size): print base_num base = np.reshape(decode_z[base_num], [ 1, start_vox_size[0], start_vox_size[1], start_vox_size[2], dim_z ]) eps = np.random.normal(size=(noise_num - 1, dim_z)).astype(np.float32) if base_num == 0: Z_np_samp = decode_z[1:] elif base_num == batch_size - 1: Z_np_samp = decode_z[:batch_size - 1] else: Z_np_samp_before = np.reshape(decode_z[:base_num], [ base_num, start_vox_size[0], start_vox_size[1], start_vox_size[2], dim_z ]) Z_np_samp_after = np.reshape(decode_z[base_num + 1:], [ batch_size - base_num - 1, start_vox_size[0], start_vox_size[1], start_vox_size[2], dim_z ]) Z_np_samp = np.concatenate([Z_np_samp_before, Z_np_samp_after], axis=0) for c in np.arange(start_vox_size[0]): for l in np.arange(start_vox_size[1]): for d in np.arange(start_vox_size[2]): for i in np.arange(noise_num): if i == 0: Z = copy.copy(base) noise_z = copy.copy(Z) else: Z = copy.copy(base) Z[0, c, l, d, :] += eps[i - 1] noise_z = np.concatenate([noise_z, Z], axis=0) gaussian_samp = np.concatenate([Z, Z_np_samp], axis=0) pd_full_rnd = sess.run( full_tf_samp, feed_dict={Z_tf_samp: gaussian_samp}) """ refined_voxs_rnd = sess.run( sample_refine_full_tf, feed_dict={ sample_full_tf: pd_full_rnd }) """ noise_vox = np.reshape(pd_full_rnd[0], [ 1, vox_shape[0], vox_shape[1], vox_shape[2], vox_shape[3] ]) if i == 0: pd_full = noise_vox else: pd_full = np.concatenate([pd_full, noise_vox], axis=0) np.save( save_path + '/noise_z' + str(base_num) + '_' + str(c) + str(l) + str(d) + '.npy', noise_z) full_models_cat = np.argmax(pd_full, axis=4) np.save( save_path + '/noise' + str(base_num) + '_' + str(c) + str(l) + str(d) + '.npy', full_models_cat) print("voxels saved")
saver = tf.train.Saver() saver.restore(sess, args.checkpoint) # partial = read_point_cloud(args.input_path) # partial = np.array(partial.points) print('Processing', args.input_path) if args.input_path.endswith('.pcd'): partial = read_pcd(args.input_path) elif args.input_path.endswith('.ply'): scene, _ = loadPLY(args.input_path) partial = scene[:, :3].copy() center = 0.5 * (partial.min(axis=0) + partial.max(axis=0)) partial -= center scale = (partial.max(axis=0) - partial.min(axis=0)).max() partial /= scale save_pcd('scaled_partial.pcd', partial) mean_color = scene[:, 3:6].mean(axis=0) print('partial', partial.shape) complete = sess.run(model.outputs, feed_dict={inputs: [partial], npts: [partial.shape[0]]})[0] print('complete', complete.shape) save_pcd('scaled_complete.pcd', complete) out = np.zeros((len(complete), 6)) out[:, :3] = complete*scale + center out[:, 3:6] = mean_color # flann = pyflann.FLANN() # q,_ = flann.nn(scene[:, :3], out[:, :3], 1, algorithm='kdtree_simple') # for i in range(len(q)): # out[i, 3:6] = scene[q[i], 3:6] savePLY(args.output_path, out) # save_pcd(args.output_path, complete)
def test(args): inputs = tf.placeholder(tf.float32, (1, None, 3)) npts = tf.placeholder(tf.int32, (1, )) gt = tf.placeholder(tf.float32, (1, args.num_gt_points, 3)) model_module = importlib.import_module('.%s' % args.model_type, 'models') model = model_module.Model(inputs, npts, gt, tf.constant(1.0)) #os.makedirs(os.path.join(args.results_dir, args.drive, 'plots'), exist_ok=True) #os.makedirs(os.path.join(args.results_dir, args.drive, 'completions'), exist_ok=True) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) saver = tf.train.Saver() saver.restore(sess, args.checkpoint) car_ids = [ filename.split('.')[0] for filename in sorted(os.listdir(osp.join(args.base_dir, args.drive))) if '.ply' in filename and 'car' in filename ] total_time = 0 total_points = 0 for i, car_id in enumerate(car_ids): partial = read_pcd( os.path.join(args.base_dir, args.drive, '%s.ply' % car_id)) affine_params = np.loadtxt( os.path.join(args.base_dir, args.drive, '%s.txt' % car_id)) #img_car = Image.open(os.path.join(args.base_dir, args.drive, '%s.png' % car_id)) #img_car = cv2.imread(os.path.join(args.base_dir, args.drive, '%s.png' % car_id)) total_points += partial.shape[0] # Calculate center, rotation and scale center = affine_params[0:3] yaw = affine_params[3] rotation = np.array([[np.cos(yaw), -np.sin(yaw), 0], [np.sin(yaw), np.cos(yaw), 0], [0, 0, 1]]) scale = affine_params[4] partial = np.dot(partial - center, rotation) / scale partial = np.dot(partial, [[1, 0, 0], [0, 0, 1], [0, 1, 0]]) start = time.time() completion = sess.run(model.outputs, feed_dict={ inputs: [partial], npts: [partial.shape[0]] }) total_time += time.time() - start completion = completion[0] completion_w = np.dot(completion, [[1, 0, 0], [0, 0, 1], [0, 1, 0]]) scale_after = np.max(completion[:, 0]) - np.min(completion[:, 0]) #completion_w = np.dot(completion_w * scale/scale_after, rotation.T) + center #completion_w[:,2] -= np.min(completion_w[:,2]) + 1.73 with open( os.path.join( args.base_dir, args.drive, '%s.txt' % car_id.replace('car', 'completion_transform')), 'w') as f: f.write("{:f} {:f} {:f} {:f} {:f} {:f}".format(center[0], center[1], center[2],\ scale, scale_after, yaw)) pcd_path = os.path.join(args.base_dir, args.drive, '%s.ply' % car_id.replace('car', 'completion')) save_pcd(pcd_path, completion_w) #if i % args.plot_freq == 0: #plot_path = os.path.join(args.results_dir, args.drive, 'plots', '%s.png' % car_id) #plot_pcd_img(plot_path, [partial, completion], img_car, ['input', 'output'], # '%d input points' % partial.shape[0], [5, 0.5]) print('Average # input points:', total_points / len(car_ids)) print('Average time:', total_time / len(car_ids)) sess.close()