-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
339 lines (276 loc) · 10.4 KB
/
utils.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
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
import os
import h5py
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow.contrib.slim as slim
from PIL import Image
from pyntcloud import PyntCloud
from sklearn.metrics import auc, roc_curve
matplotlib.use('TkAgg') #uncomment this line if you are using macos
def show_all_trainable_variables():
model_vars = tf.trainable_variables()
slim.model_analyzer.analyze_vars(model_vars, print_info=True)
def display_point(pts, color, color_label=None, title=None, fname=None, axis="on", marker_size=5):
pts = np.squeeze(pts)
if isinstance(color, np.ndarray):
color = np_color_to_hex_str(np.squeeze(color))
DPI = 300
PIX_h = 1000
MARKER_SIZE = marker_size
if color_label is None:
PIX_w = PIX_h
else:
PIX_w = PIX_h * 2
X = pts[:, 0]
Y = pts[:, 2]
Z = pts[:, 1]
max_range = np.array([X.max() - X.min(), Y.max() - Y.min(), Z.max() - Z.min()]).max() / 2.0
mid_x = (X.max() + X.min()) * 0.5
mid_y = (Y.max() + Y.min()) * 0.5
mid_z = (Z.max() + Z.min()) * 0.5
fig = plt.figure()
fig.set_size_inches(PIX_w / DPI, PIX_h / DPI)
if axis == "off":
plt.subplots_adjust(top=1.2, bottom=-0.2, right=1.5, left=-0.5, hspace=0, wspace=-0.7)
plt.margins(0, 0)
if color_label is None:
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X, - Y, Z, c=color, edgecolors="none", s=MARKER_SIZE, depthshade=True)
ax.set_xlim(mid_x - max_range, mid_x + max_range)
ax.set_ylim(mid_y - max_range, mid_y + max_range)
ax.set_zlim(mid_z - max_range, mid_z + max_range)
plt.axis(axis)
# ax.set_yticklabels([])
# ax.set_xticklabels([])
# ax.set_zticklabels([])
# ax.set_axis_off()
if title is not None:
ax.set_title(title, fontdict={'fontsize': 30})
ax.set_aspect("equal")
# ax.grid("off")
if fname:
plt.savefig(fname, transparent=True, dpi=DPI)
plt.close(fig)
else:
plt.show()
else:
ax = fig.add_subplot(121, projection='3d')
bx = fig.add_subplot(122, projection='3d')
ax.scatter(X, Y, Z, c=color, edgecolors="none", s=MARKER_SIZE, depthshade=True)
bx.scatter(X, Y, Z, c=color_label, edgecolors="none", s=MARKER_SIZE, depthshade=True)
ax.set_xlim(mid_x - max_range, mid_x + max_range)
ax.set_ylim(mid_y - max_range, mid_y + max_range)
ax.set_zlim(mid_z - max_range, mid_z + max_range)
bx.set_xlim(mid_x - max_range, mid_x + max_range)
bx.set_ylim(mid_y - max_range, mid_y + max_range)
bx.set_zlim(mid_z - max_range, mid_z + max_range)
bx.patch.set_alpha(0)
ax.set_aspect("equal")
ax.grid("off")
bx.set_aspect("equal")
bx.grid("off")
ax.axis('off')
bx.axis("off")
plt.axis('off')
if fname:
plt.savefig(fname, transparent=True, dpi=DPI)
plt.close(fig)
else:
plt.show()
def int16_to_hex_str(color):
hex_str = ""
color_map = {i: str(i) for i in range(10)}
color_map.update({10: "A", 11: "B", 12: "C", 13: "D", 14: "E", 15: "F", 16: "F"})
# print(color_map)
hex_str += color_map[color // 16]
hex_str += color_map[color % 16]
return hex_str
def horizontal_concatnate_pic(fout, *fnames):
images = [Image.open(i).convert('RGB') for i in fnames]
# images = map(Image.open, fnames)
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset, 0))
x_offset += im.size[0]
new_im.save(fout)
def vertical_concatnate_pic(fout, *fnames):
images = [Image.open(i).convert('RGB') for i in fnames]
# images = map(Image.open, fnames)
widths, heights = zip(*(i.size for i in images))
max_widths = max(widths)
width_ratio = [max_widths / width for width in widths]
new_height = [int(width_ratio[idx]) * height for idx, height in enumerate(heights)]
new_images = [i.resize((max_widths, new_height[idx])) for idx, i in enumerate(images)]
total_heights = sum(new_height)
new_im = Image.new('RGB', (max_widths, total_heights))
x_offset = 0
for im in new_images:
new_im.paste(im, (0, x_offset))
x_offset += im.size[1]
new_im.save(fout)
def rgb_to_hex_str(*rgb):
hex_str = "#"
for item in rgb:
hex_str += int16_to_hex_str(item)
return hex_str
def np_color_to_hex_str(color):
"""
:param color: an numpy array of shape (N, 3)
:return: a list of hex color strings
"""
hex_list = []
for rgb in color:
hex_list.append(rgb_to_hex_str(rgb[0], rgb[1], rgb[2]))
return hex_list
def load_h5(path, *kwd):
f = h5py.File(path)
list_ = []
for item in kwd:
list_.append(f[item][:])
print("{0} of shape {1} loaded!".format(item, f[item][:].shape))
if item == "ndata" or item == "data":
pass # print(np.mean(f[item][:], axis=1))
if item == "color":
print("color is of type {}".format(f[item][:].dtype))
return list_
def load_single_cat_h5(cat, num_pts, type, *kwd):
fpath = os.path.join("./data/category_h5py", cat, "PTS_{}".format(num_pts), "ply_data_{}.h5".format(type))
return load_h5(fpath, *kwd)
def printout(flog, data): # follow up
print(data)
flog.write(data + '\n')
def save_ply(data, color, fname):
color = color.astype(np.uint8)
df1 = pd.DataFrame(data, columns=["x", "y", "z"])
df2 = pd.DataFrame(color, columns=["red", "green", "blue"])
pc = PyntCloud(pd.concat([df1, df2], axis=1))
pc.to_file(fname)
def label2onehot(labels, m):
idx = np.eye(m)
onehot_labels = np.zeros(shape=(labels.shape[0], m))
for idx, i in enumerate(labels):
onehot_labels[idx] = idx[i]
return onehot_labels
def multiclass_AUC(y_true, y_pred):
"""
:param y_true: shape (num_instance,)
:param y_pred: shape (num_instance, num_class)
:return:
"""
num_classes = np.unique(y_true).shape[0]
print(num_classes)
tpr = dict()
fpr = dict()
roc_auc = dict()
num_instance = dict()
total_roc_auc = 0
for i in range(num_classes):
binary_label = np.where(y_true == i, 1, 0)
class_score = y_pred[:, i]
num_instance[i] = np.sum(y_true == i)
fpr[i], tpr[i], _ = roc_curve(binary_label, class_score)
roc_auc[i] = auc(fpr[i], tpr[i])
total_roc_auc += roc_auc[i]
return total_roc_auc / 16
# print(roc_auc, num_instance)
def softmax(logits):
"""
:param logits: of shape (num_instance, num_classes)
:return: prob of the same shape as that of logits, with each row summed up to 1
"""
assert logits.shape[-1] == 16
regulazation = np.max(logits, axis=-1) # (num_instance,)
logits -= regulazation[:, np.newaxis]
prob = np.exp(logits) / np.sum(np.exp(logits), axis=-1)[:, np.newaxis]
assert prob.shape == logits.shape
return prob
def construct_label_weight(label, weight):
"""
:param label: a numpy ndarray of shape (batch_size,) with each entry in [0, num_class)
:param weight: weight list of length num_class
:return: a numpy ndarray of the same shape as that of label
"""
return [weight[i] for i in label]
def jitter_point_cloud(batch_data, sigma=0.01, clip=0.05):
""" Randomly jitter points. jittering is per point.
Input:
BxNx3 array, original batch of point clouds
Return:
BxNx3 array, jittered batch of point clouds
"""
B, N, C = batch_data.shape
assert (clip > 0)
jittered_data = np.clip(sigma * np.random.randn(B, N, C), -1 * clip, clip)
jittered_data += batch_data
return jittered_data
def generate_sphere(num_pts, radius):
# http://electron9.phys.utk.edu/vectors/3dcoordinates.htm
r = np.random.rand(num_pts, 1) * radius
f = np.random.rand(num_pts, 1) * np.pi * 2
q = np.random.rand(num_pts, 1) * np.pi
x = r * np.sin(q) * np.cos(f)
y = r * np.sin(q) * np.sin(f)
z = r * np.cos(q)
return np.concatenate((x, y, z), axis=-1)
def generate_sphere_surface(num_pts, radius):
r = radius
f = np.random.rand(num_pts, 1) * np.pi * 2
q = np.random.rand(num_pts, 1) * np.pi
x = r * np.sin(q) * np.cos(f)
y = r * np.sin(q) * np.sin(f)
z = r * np.cos(q)
return np.concatenate((x, y, z), axis=-1)
def generate_cube(num_pts, length):
return (np.random.rand(num_pts, 3) - 0.5) * length
def generate_cube_surface(num_pts, length):
pass
def generate_ncolor(num_pts):
return ((np.ones((num_pts, 3)) * 127) - 127.5) / 127.5
def generate_plane(num_pts, length, type="xy"):
pts = (np.random.rand(num_pts, 2) - 0.5) * length
if type == "xy":
pts = np.insert(pts, 2, 0, axis=1)
elif type == "xz":
pts = np.insert(pts, 1, 0, axis=1)
elif type == "yz":
pts = np.insert(pts, 0, 0, axis=1)
return pts
def img_to_set(img_path, img_name):
img = Image.open(os.path.join(img_path, img_name)).convert('RGB')
w, h = img.size
# img = img.resize((int(w/10), int(h/10)), Image.LANCZOS)
# img.save(os.path.join(img_path, img_name.split(".")[0] + "_resized.jpg"), "jpeg")
print(img.size)
return np.reshape(np.array(img), [-1, 3])
def prepare_content_or_style(path, downsample_points=None):
if path.endswith("ply"):
content = PyntCloud.from_file(path).points.values
if downsample_points:
mask = np.random.choice(content.shape[0], downsample_points)
content = content[mask]
content_ndata = content[:, :3]
content_ncolor = (content[:, 3:6] - 127.5) / 127.5
return content_ndata, content_ncolor
elif path.endswith("npy"):
content = np.load(path)
if downsample_points:
mask = np.random.choice(content.shape[0], downsample_points)
content = content[mask]
content_ndata = content[:, :3]
content_ncolor = (content[:, 3:6] - 127.5) / 127.5
return content_ndata, content_ncolor
else:
img = Image.open(path).convert("RGB")
style_color = np.reshape(np.array(img), [-1, 3])
style_color = (style_color - 127.5) / 127.5
if downsample_points:
mask = np.random.choice(style_color.shape[0], downsample_points)
style_color = style_color[mask]
return style_color