-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsample_query_point.py
180 lines (156 loc) · 6.84 KB
/
sample_query_point.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
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 14 22:00:36 2020
@author: Administrator
"""
import numpy as np
from scipy.spatial import cKDTree
import os
import shutil
import tensorflow as tf
import os
import random
import argparse
import re
parser = argparse.ArgumentParser()
parser.add_argument('--out_dir', type=str, required=True)
parser.add_argument('--CUDA', type=int, default=0)
parser.add_argument('--dataset', type=str, default="shapenet")
parser.add_argument('--input_dir', type=str, required=True)
parser.add_argument('--class_idx', type=str, default="026911156")
a = parser.parse_args()
cuda_idx = str(a.CUDA)
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]=cuda_idx
def safe_norm(x, epsilon=1e-12, axis=None):
return tf.sqrt(tf.reduce_sum(x ** 2, axis=axis) + epsilon)
def near_point_idx(array1, array2, num_point1,num_point2, num_features = 3):
array1 = tf.reshape(array1,[-1,3])
array2 = tf.reshape(array2,[-1,3])
#num_point, num_features = array1.shape
expanded_array1 = tf.tile(array1, (num_point2, 1))
expanded_array2 = tf.reshape(
tf.tile(tf.expand_dims(array2, 1),
(1, num_point1, 1)),
(-1, num_features))
distances = safe_norm(expanded_array1-expanded_array2, axis=-1)
distances = tf.reshape(distances, (num_point2, num_point1))
dis_idx = tf.argmin(distances, axis=1)
return dis_idx
points_target = tf.placeholder(tf.float32, shape=[1,None,3])
input_points_3d = tf.placeholder(tf.float32, shape=[1,None,3])
points_target_num = tf.placeholder(tf.int32, shape=[1,1])
points_input_num = tf.placeholder(tf.int32, shape=[1,1])
near_idx = near_point_idx(points_target,input_points_3d,points_target_num[0,0],points_input_num[0,0])
point_target_near = tf.gather(points_target,axis=1,indices=near_idx)
OUTPUT_DIR = a.out_dir
INPUT_DIR = a.INPUT_DIR
if os.path.exists(OUTPUT_DIR):
shutil.rmtree(OUTPUT_DIR)
print ('test_res_dir: deleted and then created!')
os.makedirs(OUTPUT_DIR)
files = []
if(a.dataset=="shapenet"):
f = open('./data/shapenet_val.txt','r')
for index,line in enumerate(f):
if(line.strip().split('/')[0]==a.class_idx):
print(line)
files.append(line.strip().split('/')[1])
f.close()
if(a.dataset == "famous"):
f = open('./data/famous_testset.txt','r')
for index,line in enumerate(f):
#print(line)
files.append(line.strip('\n'))
f.close()
if(a.dataset == "ABC"):
f = open('./data/abc_testset.txt','r')
for index,line in enumerate(f):
#print(line)
files.append(line.strip('\n'))
f.close()
if(a.dataset == "other"):
fileAll = os.listdir(INPUT_DIR)
for file in fileAll:
if(re.findall(r'.*.xyz.npz', file, flags=0)):
print(file.strip().split('.')[0])
files.append(file.strip().split('.')[0])
print('shap num:',len(files))
mm = 0
POINT_NUM = 5000
POINT_NUM_GT = 20000
POINT_NUM_GT_bs = np.array(POINT_NUM_GT).reshape(1,1)
points_input_num_bs = np.array(POINT_NUM).reshape(1,1)
with tf.Session() as sess:
for file in files:
print(file)
#print(INPUT_DIR + file)
print(OUTPUT_DIR + file + '.npz')
# if(os.path.exists(OUTPUT_DIR + file + '.npz')):
# print('exit')
# continue
if(a.dataset=="shapenet"):
data = np.load(INPUT_DIR + file + '/pointcloud.npz')
#pointcloud = data['points'].reshape(-1,3)
pointcloud = data['points'].reshape(-1,3)
normal = data['normals'].reshape(-1,3)
else:
pointcloud = np.load(INPUT_DIR + file + '.xyz.npy').reshape(-1,3)
point_idx = np.random.choice(pointcloud.shape[0], POINT_NUM_GT, replace = False)
pointcloud = pointcloud[point_idx,:]
print(np.max(pointcloud[:,0]),np.max(pointcloud[:,1]),np.max(pointcloud[:,2]),np.min(pointcloud[:,0]),np.min(pointcloud[:,1]),np.min(pointcloud[:,2]))
pnts = pointcloud
ptree = cKDTree(pnts)
i = 0
sigmas = []
for p in np.array_split(pnts,100,axis=0):
d = ptree.query(p,51)
sigmas.append(d[0][:,-1])
i = i+1
sigmas = np.concatenate(sigmas)
sigmas_big = 0.05 * np.ones_like(sigmas)
print(sigmas)
sigmas = sigmas*2
sample = []
sample_near = []
for i in range(25):
tt = pnts + 1*np.expand_dims(sigmas,-1) * np.random.normal(0.0, 1.0, size=pnts.shape)
sample.append(tt)
tt = tt.reshape(-1,POINT_NUM,3)
sample_t = []
for j in range(tt.shape[0]):
point_target_near_c = sess.run([point_target_near],feed_dict={points_target_num:POINT_NUM_GT_bs,points_input_num:points_input_num_bs,
points_target:pnts.reshape(1,-1,3),input_points_3d:tt[j,:,:].reshape(1,-1,3)})
point_target_near_c = np.asarray(point_target_near_c)
#print('point_target_near_c_b:',point_target_near_c.shape)
point_target_near_c = point_target_near_c.reshape(-1,3)
#print('point_target_near_c_a:',point_target_near_c.shape)
sample_t.append(point_target_near_c)
sample_t = np.asarray(sample_t)
#print('sample_t:',sample_t.shape)
sample_t = sample_t.reshape(-1,3)
sample_near.append(sample_t)
for i in range(15):
tt = pnts + 0.5*np.expand_dims(sigmas,-1) * np.random.normal(0.0, 1.0, size=pnts.shape)
sample.append(tt)
tt = tt.reshape(-1,POINT_NUM,3)
sample_t = []
for j in range(tt.shape[0]):
point_target_near_c = sess.run([point_target_near],feed_dict={points_target_num:POINT_NUM_GT_bs,points_input_num:points_input_num_bs,
points_target:pnts.reshape(1,-1,3),input_points_3d:tt[j,:,:].reshape(1,-1,3)})
point_target_near_c = np.asarray(point_target_near_c)
#print('point_target_near_c_b:',point_target_near_c.shape)
point_target_near_c = point_target_near_c.reshape(-1,3)
#print('point_target_near_c_a:',point_target_near_c.shape)
sample_t.append(point_target_near_c)
sample_t = np.asarray(sample_t)
#print('sample_t:',sample_t.shape)
sample_t = sample_t.reshape(-1,3)
sample_near.append(sample_t)
sample = np.asarray(sample)
sample_near = np.asarray(sample_near)
#print('sample:',sample.shape,sample_near.shape)
if(a.dataset=="shapenet"):
np.savez(OUTPUT_DIR + file, sample = sample, point = pnts, normal = normal[point_idx,:],sample_near = sample_near)
else:
np.savez(OUTPUT_DIR + file, sample = sample, point = pnts, sample_near = sample_near)