-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdesignapps.py
726 lines (644 loc) · 30 KB
/
designapps.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
"""
## LICENSE: GPL 3.0
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Script for shrink-wrapping 3D point clouds based on an initial polygonal
mesh
Shrink-wrapping algorithm based on L. P. Kobbelt, J. Vorsatz, U. Labsik,
and H. P. Seidel, “A Shrink Wrapping Approach to rRmeshing Polygonal
Surfaces” Computer Graphics Forum, vol. 18, no. 3, pp. 119–130, 1999.
Pre-requisites:
- Python 3.6.10
- numpy 1.19.1
- Trimesh 3.8.10
- Scipy 1.3.1
- Ubuntu 18.04
Copyright (c)
Honda Research Institute Europe GmbH
Authors: Thiago Rios, Sneha Saha
Contact: [email protected]
"""
# ------------------------------------------------------------------------------
# Libraries
# ------------------------------------------------------------------------------
# Basic tools
import numpy as np
import os
import ast
#from pandas._config import config
# Visualization tools
import pyvista as pv
import matplotlib
matplotlib.use('TkAgg')
matplotlib.rcParams['mathtext.fontset'] = 'stix'
matplotlib.rcParams['font.family'] = 'STIXGeneral'
# Custom colormap
cdict = {'red': [[0.0, 0/256, 0/256],
[0.50, 149/256, 149/256],
[1.0, 200/256, 200/256]],
'green': [[0.0, 51/256, 51/256],
[0.50, 157/256, 157/256],
[1.0, 16/256, 16/256]],
'blue': [[0.0, 76/256, 76/256],
[0.50, 168/256, 168/256],
[1.0, 46/256, 46/256]]}
cm_custm = matplotlib.colors.LinearSegmentedColormap('cstmmap', cdict, N=256)
import matplotlib.pyplot as plt
import seaborn as sns
palette = ["#00334c", "#959da8", "#c8102e"]
# Data science/machine learning tools
import tensorflow as tf
# GDL4DesignApps
# from gdl4designapps.preprocess_methods import CAE2PC
from gdl4designapps.preprocess_methods import CAE2PC
# ------------------------------------------------------------------------------
# VISUALIZATION TOOLS
# ------------------------------------------------------------------------------
# (Geometric) Data visualization
class Vis3D:
# Plot 3D point clouds
def pcplot(Splot, figname, pointsize=20, colorpoints="#c8102e",
cam_az=0, cam_el=0, wsize=(3440, 1440)):
''' Function for visualizing 3D point clouds.
Input:
- Splot: Array with the Cartesian coordinates of the 3D point cloud.
Type: <array, (N,3)>
- figname: Name of the file to save the figure. Type: String
- pointsize (standard value: 20): Size of the spheres that represent
the point clouds. Type <int>
- colorpoints (standard value: "#c8102e"): Color of the spheres.
- cam_az (standard value: 0): Camera azimuthal position.
Type: <float (1)>
- cam_el (standard value: 0): Camera elevation. Type: <float (1)>
- wsize (standard value (3440, 1440)): Size of the window in pixels
to plot the image. Type: <tuple (int, int)>
Output:
- None. The image is directly saved in the specified path.
'''
pltv = pv.Plotter(off_screen=True, window_size=wsize)
pltv.set_background("white")
pltv.add_mesh(Splot, point_size=pointsize, style="points",
render_points_as_spheres=True,
lighting=True, color=colorpoints)
light = pv.Light(position=(1, 0, 0), light_type='scene light')
pltv.add_light(light)
pltv.view_isometric()
pltv.camera.azimuth= cam_az
pltv.camera.elevation= cam_el
pltv.show(screenshot=figname)
pltv.close()
return()
# Plot color maps onto 3D point clouds
def pccmap(Splot, cscalar, figname, pointsize=20, vlim=[-1,1],
cam_az=0, cam_el=0, wsize=(3440, 1440)):
''' Function for visualizing 3D point clouds with color maps.
Input:
- Splot: Array with the Cartesian coordinates of the 3D point cloud.
Type: <array, (N,3)>
- cscalar: Array with the scalar values associated with each point.
Type: <array, (N)>
- figname: Name of the file to save the figure. Type: String
- pointsize (standard value: 20): Size of the spheres that represent
the point clouds. Type <int>
- vlim (standard value: [-1,1]): Minimum and maximum values of the
color scale. Type: <list, [min, max]>
- colorpoints (standard value: "#c8102e"): Color of the spheres.
- cam_az (standard value: 0): Camera azimuthal position.
Type: <float (1)>
- cam_el (standard value: 0): Camera elevation. Type: <float (1)>
- wsize (standard value (3440, 1440)): Size of the window in pixels
to plot the image. Type: <tuple (int, int)>
Output:
- None. The image is directly saved in the specified path.
'''
pltv = pv.Plotter(off_screen=True, window_size=wsize)
pv.set_plot_theme("document")
pltv.set_background("white")
light = pv.Light(position=(1, 0, 0), light_type='scene light')
pltv.add_light(light)
pltv.add_mesh(Splot, point_size=pointsize, style="points",
render_points_as_spheres=True,
lighting=True, scalars=cscalar.flatten(), cmap=cm_custm,
show_scalar_bar=True, clim=vlim)
pltv.add_scalar_bar(title_font_size=48, label_font_size=40,
shadow=True, n_labels=4,
color='black', italic=True, fmt="%.1E",
font_family="times")
pltv.view_yz()
pltv.camera.azimuth= cam_az
pltv.camera.elevation= cam_el
pltv.show(screenshot=figname)
pltv.close()
return()
# Comparison between point clouds
def pccomp(Sref, Scomp, figname, pointsize=20, cscalar=None, vlim=[0,1],
cam_az=0, cam_el=0, wsize=(3440, 1440)):
''' Function for comparing two point clouds.
Input:
- Sref: Array with the Cartesian coordinates of the reference
3D point cloud.
Type: <array, (N,3)>
- Scomp: Array with the Cartesian coordinates of the target 3D point cloud.
Type: <array, (N,3)>
- cscalar: Array with the scalar values associated with each point.
Type: <array, (N)>
- figname: Name of the file to save the figure. Type: String
- pointsize (standard value: 20): Size of the spheres that represent
the point clouds. Type <int>
- vlim (standard value: [-1,1]): Minimum and maximum values of the
color scale. Type: <list, [min, max]>
- colorpoints (standard value: "#c8102e"): Color of the spheres.
- cam_az (standard value: 0): Camera azimuthal position.
Type: <float (1)>
- cam_el (standard value: 0): Camera elevation. Type: <float (1)>
- wsize (standard value (3440, 1440)): Size of the window in pixels
to plot the image. Type: <tuple (int, int)>
Output:
- None. The image is directly saved in the specified path.
'''
pltv = pv.Plotter(off_screen=True,
window_size=wsize)
pv.set_plot_theme("document")
pltv.set_background("white")
light = pv.Light(position=(1, 0, 0), light_type='scene light')
pltv.add_light(light)
pltv.add_mesh(Sref, point_size=pointsize, style="points",
render_points_as_spheres=True,
lighting=True, color="#00334c")
Sref_yspan = np.max(Sref[:,1]) - np.min(Sref[:,1])
if not type(cscalar)==type(None):
pltv.add_mesh(Scomp+[0,2*Sref_yspan,0],
point_size=pointsize, style="points",
render_points_as_spheres=True,
lighting=True, scalars=cscalar.flatten(),
cmap=cm_custm,
show_scalar_bar=True, clim=vlim)
pltv.add_scalar_bar(title_font_size=48, label_font_size=40,
shadow=True, n_labels=4,
color='black', italic=True, fmt="%.1E",
font_family="times")
else:
pltv.add_mesh(Scomp+[0,2*Sref_yspan,0],
point_size=pointsize, style="points",
render_points_as_spheres=True,
lighting=True, color="#c8102e")
pltv.view_yz()
pltv.camera.azimuth= cam_az
pltv.camera.elevation= cam_el
pltv.show(screenshot=figname)
pltv.close()
return()
# ------------------------------------------------------------------------------
# DESIGN APPLICATIONS
# ------------------------------------------------------------------------------
# Free-form deformation algorithm
# Standard free-form deformation (FFD) algorithm
class FFD:
# FFD lattice
def FFD_lattice(S,limits,L,M,N):
'''Function to generate the representation of the control lattice and
normalized point cloud representation
Input:
- S: 3D point cloud representation. Type <array, (N,3)>
- limits: array with the bounding values of the x, y and z
coordinates -> [[x_min,x_max],[y_min,y_max],[z_min,z_max]].
Type: <array, (3,2)>
- L: Number of control planes in the s direction. Type: <int>
- M: Number of control planes in the t direction. Type: <int>
- N: Number of control planes in the u direction. Type: <int>
Output:
- Sn: 3D point cloud normalized to [0,1], [0,1], [0,1]. Type
<array, (N,3)>
- V: Coordinates of the control points. Type <array, (Nv,3)>
'''
# Axes seed
x_seed = np.linspace(np.min(limits[0,:]), np.max(limits[0,:]), L)
y_seed = np.linspace(np.min(limits[1,:]), np.max(limits[1,:]), M)
z_seed = np.linspace(np.min(limits[2,:]), np.max(limits[2,:]), N)
# Generate lattice
V = np.zeros((L*M*N, 3))
cntrV = 0
for i in range(L):
for j in range(M):
for k in range(N):
V[cntrV, :] = np.array([x_seed[i], y_seed[j], z_seed[k]])
cntrV+=1
# Embedd point cloud
Sn = np.zeros(S.shape)
for i in range(3):
Sn[:,i] = (S[:,i]-np.min(V[:,i]))/(np.max(V[:,i])-np.min(V[:,i]))
return(Sn, V)
# Free-form deformation: Bernstein coefficients
def BernsteinPoly(L,M,N,Sn):
'''Function to generate the matrix with the coefficients
of the Bernstein tri-variate polynomial
Input:
- L: Number of control planes in the s direction. Type: <int>
- M: Number of control planes in the t direction. Type: <int>
- N: Number of control planes in the u direction. Type: <int>
- Sn: Normalized coordinates of the 3D point cloud
representation. Type: <array, (N,3)>
Output:
- B_marix: matrix with the coefficients of the tri-variate Bernstein
polynomial. Type <array, (N, Nv)>
'''
# Normalized point cloud
s = Sn[:,0]
t = Sn[:,1]
u = Sn[:,2]
# Bernstein matrix
shpBern = np.max([L, M, N])
Mbernstein = np.matrix(np.zeros([shpBern, shpBern]))
Mbernstein[0,0] = 1
for i in range(1, Mbernstein.shape[0]):
for j in range(Mbernstein.shape[1]):
if j == 1: Mbernstein[i,j] = 1
Mbernstein[i,j] = Mbernstein[i-1, j-1] + Mbernstein[i-1, j]
# Polynomial coefficients
B_matrix = np.zeros((s.shape[0], L*M*N))
cnt = 0
for i in range (L):
s_term = Mbernstein[L-1,i]*(1-s)**(L-1-i)*s**i
for j in range(M):
t_term = Mbernstein[M-1,j]*(1-t)**(M-1-j)*t**j
for k in range(N):
B_matrix[:, cnt] = s_term*t_term*(Mbernstein[N-1,k]*(1-u)**(N-1-k)*u**k)
cnt += 1
return(B_matrix)
# FFD operator
def FFD_op(V, B):
''' Deformation of a 3D shape using FFD
Input:
- V: Matrix of control points. Type <array, (Nv,3)>
- B: Coefficients of the tri-variate Bernstein polynomial.
Type <array, (N, Nv)>
Output:
- S_out: Deformed 3D point cloud. Type <array, (N,3)>
'''
S_out = np.matmul(B,V)
return(S_out)
# Algorithms for generating latent representations, 3D point clouds and
# visualizing network features
class DesignApps:
# Function to import the network graph
def import_net_graph(config_path, GPUid):
''' Function to load graph nodes and start a tensorflow session
that can be used for GDL applications with the trained architectures.
Input:
- config_path: Path to the dictionary (.py) with the settings for
training the autoencoder. Type: <string>
- GPUid (default=-1): ID of the GPU that will be used. If no GPU is
avaliable, the value '-1' allows to train the model on CPU.
Output:
- sess: Tensorflow session that should be used
- S_in: Tensorflow placeholder for the input point clouds
- Z: Tensor of the latent representation. Type: <tensor, (-1, Lz, 1)>
- S_out: Tensor with the output point clouds.
Type: <tensor, (-1,pc_size,3)>
- feat_layer: Tensor with the set of feature values (activations)
of the processed point clouds obtained at the last convolutional
layer. Type: <tensor, (-1, pc_size, n_features)>
- pc_size: Size of the output point cloud. Type int
- dpout: Tensorflow placeholder for the dropout ratio utilized in the PC-VAE. Type <tensor ()>
- gamma_n: Tensorflow placeholder for enabling or disabling the
Gaussian noise in the latent layer. Type <tensor ()>.
- latt_def: Tensor of deformed control points that were utilized to
generate the output shape. Type <tensor (-1,n_control_points,3)>
- flags: List with arrays that indicate the architecture that is
utilized. Type <list, [Boolean (VAE), Boolean (Point2FFD)]>
'''
## Allocate GPU
os.putenv('CUDA_VISIBLE_DEVICES','{}'.format(GPUid))
ast.literal_eval
## Read configuration dictionary
if os.path.exists(config_path):
os.system("cp {} configdict.py".format(config_path))
from configdict import confignet as config
else:
print("ERROR! Configuration file not found!")
print("File: {}".format(config_path))
return()
## Network output directory
if type(config["out_data"]) == type(None):
out_dir = "."
else:
out_dir = str(config["out_data"])
# Network directory
net_dir = "{}/{}".format(out_dir, str(config["net_id"]))
## Load the architecture
flag_vae = False
flag_p2ffd = False
dpout = None
gamma_n = None
latt_def = None
# In case the network is PC-AE
try:
# - Import Graph at latest state (after training)
TFmetaFile = str.format("{}/pcae.meta", net_dir)
TFDirectory = str.format("{}/", net_dir)
# import graph data
new_saver = tf.train.import_meta_graph(TFmetaFile,
clear_devices=True)
except:
try:
# In case the network is PC-VAE
# - Import Graph at latest state (after training)
TFmetaFile = str.format("{}/pcvae.meta", net_dir)
TFDirectory = str.format("{}/", net_dir)
# import graph data
new_saver = tf.train.import_meta_graph(TFmetaFile,
clear_devices=True)
flag_vae = True
except:
# In case the network is Point2FFD
# - Import Graph at latest state (after training)
TFmetaFile = str.format("{}/p2ffd.meta", net_dir)
TFDirectory = str.format("{}/", net_dir)
# import graph data
new_saver = tf.train.import_meta_graph(TFmetaFile,
clear_devices=True)
dpout=None
flag_p2ffd = True
sess=tf.Session()
new_saver.restore(sess, tf.train.latest_checkpoint(TFDirectory))
graph = tf.get_default_graph()
# Import network layers
# Input
S_in = graph.get_tensor_by_name("S_in:0")
# - Latent representation
Z = graph.get_tensor_by_name("Z:0")
S_out = graph.get_tensor_by_name("S_out:0")
pc_size = S_out.shape[1]
# Droput (PC-VAE)
if flag_vae: dpout = graph.get_tensor_by_name("do_rate:0")
if flag_p2ffd:
gamma_n = graph.get_tensor_by_name("gamma_n:0")
latt_def = graph.get_tensor_by_name("Vd:0")
layer_index = len(list(config["encoder_layers"]))
feat_layer = graph.get_tensor_by_name(
str.format("enclayer_{}:0", layer_index))
flags = [flag_vae, flag_p2ffd]
return(sess, S_in, Z, S_out, feat_layer, pc_size, dpout,
gamma_n, latt_def, flags)
# Calculate the latent representations of 3D point clouds
def pointcloud_to_Z(config_path, sess, Sin, Z, pc_batch, flags,
dpout=None, gamma_n=None, GPUid=-1):
'''Function to compress 3D point clouds to latent representations
using a trained architecture.
Input:
- config_path: Path to the dictionary (.py) with the settings for
training the autoencoder. Type: <string>
- sess: Tensorflow session that should be used
- Sin: Tensorflow placeholder for the input point clouds
- Z: Tensor of the latent representation. Type: <tensor, (-1, Lz, 1)>
- pc_batch: Batch with 3D point clouds for calculating the latent
representations. Type: <array (-1,N,3)>
- flags: List with arrays that indicate the architecture that is
utilized. Type <list, [Boolean (VAE), Boolean (Point2FFD)]>
- GPUid (default=-1): ID of the GPU that will be used. If no GPU is
avaliable, the value '-1' allows to train the model on CPU.
Output:
- z_batch: Array with the corresponding batch of latent
representations calculated for the input pc_batch.
Type: <array (-1,Lz,1)>
'''
## Allocate GPU
os.putenv('CUDA_VISIBLE_DEVICES','{}'.format(GPUid))
## Read configuration dictionary
if os.path.exists(config_path):
os.system("cp {} configdict.py".format(config_path))
from configdict import confignet as config
else:
print("ERROR! Configuration file not found!")
print("File: {}".format(config_path))
return()
## Network output directory
if type(config["out_data"]) == type(None):
out_dir = "."
else:
out_dir = str(config["out_data"])
# Network directory
net_dir = "{}/{}".format(out_dir, str(config["net_id"]))
## Load and normalize the data set
# check input shape
if len(pc_batch.shape) == 2:
pc_batch = np.reshape(pc_batch, (1,-1,3))
# Load normalization limits
normlim = np.load("{}/norm_inp_limits.npy".format(net_dir))
data_set = CAE2PC.data_set_norm(pc_batch,
np.array([0.1, 0.9]),
inp_lim=normlim)[0]
# Number of shapes
nshapes = data_set.shape[0]
# Architecture flags
flag_vae, flag_p2ffd = flags
## Calculate reconstruction losses and latent representations
print("\n")
# Array of latent representations
z_batch = np.zeros((data_set.shape[0], Z.shape[1], 1))
for i in range(nshapes):
print('Shape {} of {}'.format(i+1, nshapes), end="\r")
xin = np.reshape(data_set[i,:,:], (1, -1, 3))
# Calculate the latent representation and point cloud
# reconstruction
if flag_vae:
z_batch[i,:,:] = sess.run(Z,\
feed_dict={Sin: xin, dpout: 1.0})[0,:,:]
else:
if flag_p2ffd:
z_batch[i,:,:] = sess.run(Z, feed_dict={
Sin: xin, gamma_n:0})[0,:,:]
else:
z_batch[i,:,:] = sess.run(Z, feed_dict={Sin: xin})[0,:,:]
return(z_batch)
# Generate 3D point clouds from latent representations
def Z_to_pointcloud(config_path, sess, S_out, Z, z_batch, flags,
dpout=None, gamma_n=None, latt_def=None, GPUid=-1):
'''Function to generate 3D point clouds from a batch of latent
representations.
Input:
- config_path: Path to the dictionary (.py) with the settings for
training the autoencoder. Type: <string>
- sess: Tensorflow session that should be used
- S_out: Tensor with the output point clouds.
Type: <tensor, (-1,pc_size,3)>
- Z: Tensor of the latent representation. Type: <tensor, (-1, Lz, 1)>
- z_batch: Array with the corresponding batch of latent
representations calculated for the input pc_batch.
Type: <array (-1,Lz,1)>
- flags: List with arrays that indicate the architecture that is
utilized. Type <list, [Boolean (VAE), Boolean (Point2FFD)]>
- GPUid (default=-1): ID of the GPU that will be used. If no GPU is
avaliable, the value '-1' allows to train the model on CPU.
Output:
- pc_batch: Batch with 3D point clouds for calculating the latent
representations. Type: <array (-1,N,3)>
- V_batch: Batch of deformed control points that were utilized to
generate the output shape. Type <array (-1,n_control_points,3)>
'''
## Allocate GPU
os.putenv('CUDA_VISIBLE_DEVICES','{}'.format(GPUid))
## Read configuration dictionary
if os.path.exists(config_path):
os.system("cp {} configdict.py".format(config_path))
from configdict import confignet as config
else:
print("ERROR! Configuration file not found!")
print("File: {}".format(config_path))
return()
## Output directory
if type(config["out_data"]) == type(None):
out_dir = "."
else:
out_dir = str(config["out_data"])
## Network directory
net_dir = "{}/{}".format(out_dir, str(config["net_id"]))
## Load and normalize the data set
# check input shape
if len(z_batch.shape) == 2:
if z_batch.shape[0] == 1:
pc_batch = np.reshape(z_batch, (1,-1,1))
else:
pc_batch = np.reshape(z_batch, (z_batch.shape[0],-1,1))
# Number of shapes
nshapes = z_batch.shape[0]
# Point cloud size
pc_size = S_out.shape[1]
# Architecture flags
flag_vae, flag_p2ffd = flags
## Calculate reconstruction losses and latent representations
print("\n")
# Batch of point clouds
pc_batch = np.zeros((z_batch.shape[0], pc_size, 3))
# Batch of control points (Point2FFD)
if flag_p2ffd:
V_batch = np.zeros((z_batch.shape[0], latt_def.shape[1], 3))
else:
V_batch = np.zeros((z_batch.shape[0], 1, 3))
for i in range(nshapes):
print('Shape {} of {}'.format(i+1, nshapes), end="\r")
zin = np.reshape(z_batch[i,:,:], (1, -1, 1))
if flag_vae:
pc_batch[i,:,:] = sess.run(S_out,\
feed_dict={Z: zin, dpout: 1.0})[0,:,:]
else:
if flag_p2ffd:
pc, latt= sess.run([S_out, latt_def],\
feed_dict={Z: zin, gamma_n: 0.0})
pc_batch[i,:,:] = pc[0,:,:]
V_batch[i,:,:] = latt[0,:,:]
else:
pc_batch[i,:,:] = sess.run(S_out, feed_dict={
Z: zin})[0,:,:]
# Load normalization limits
normlim = np.load("{}/norm_inp_limits.npy".format(net_dir))
pc_batch = CAE2PC.data_set_norm(pc_batch,
inp_lim=np.array([0.1, 0.9]),
out_lim=normlim)[0]
V_batch = CAE2PC.data_set_norm(V_batch,
inp_lim=np.array([0.1, 0.9]),
out_lim=normlim)[0]
# Output shapes
return(pc_batch, V_batch)
# Feature Visualization
def featvis(config_path, sess, S_in, feat_layer, pc_batch, flags,
dpout=None, gamma_n=None, GPUid=-1, plot=True):
''' Function for visualizing the features learned by the PC-AE in the
last convolutional layer (prior to max-pooling).
Input:
- config_path: Path to the dictionary (.py) with the settings for
training the autoencoder. Type: <string>
- sess: Tensorflow session that should be used
- S_in: Tensorflow placeholder for the input point clouds
- feat_layer: Tensor of the latent representation.
Type: <tensor, (-1, Lz, 1)>
- pc_batch: Batch with 3D point clouds for calculating the latent
representations. Type: <array (-1,N,3)>
- flags: List with arrays that indicate the architecture that is
utilized. Type <list, [Boolean (VAE), Boolean (Point2FFD)]>
- GPUid (default=-1): ID of the GPU that will be used. If no GPU is
avaliable, the value '-1' allows to train the model on CPU.
- plot (default=True): Option that enables plot and store the
visualizations as .png images. Type: Boolean
Output:
- feat_set: Set of feature values (activations) corresponding to the
processed point clouds
- The visualization of the features is stored as .png images in the
point cloud directory
'''
## Allocate GPU
os.putenv('CUDA_VISIBLE_DEVICES','{}'.format(GPUid))
ast.literal_eval
## Read configuration dictionary
if os.path.exists(config_path):
os.system("cp {} configdict.py".format(config_path))
from configdict import confignet as config
else:
print("ERROR! Configuration file not found!")
print("File: {}".format(config_path))
return()
## Network output directory
if type(config["out_data"]) == type(None):
out_dir = "."
else:
out_dir = str(config["out_data"])
# Network directory
net_dir = "{}/{}".format(out_dir, str(config["net_id"]))
## Create directory to save the files
output_test = str.format("{}/network_verification", net_dir)
if not os.path.exists(output_test):
os.system(str.format("mkdir {}", output_test))
## Load and normalize the data set
# check input shape
if len(pc_batch.shape) == 2:
pc_batch = np.reshape(pc_batch, (1,-1,3))
# Load normalization limits
normlim = np.load("{}/norm_inp_limits.npy".format(net_dir))
data_set = CAE2PC.data_set_norm(pc_batch,
np.array([0.1, 0.9]),
inp_lim=normlim)[0]
# Number of shapes
nshapes = data_set.shape[0]
# Architecture flags
flag_vae, flag_p2ffd = flags
## Create directory to save the files
output_test = str.format("{}/network_verification", net_dir)
if not os.path.exists(output_test):
os.system(str.format("mkdir {}", output_test))
## Calculate reconstruction losses and latent representations
print("\n")
# Array of latent representations
f_batch = np.zeros((data_set.shape[0], feat_layer.shape[1],
feat_layer.shape[2]))
for i in range(nshapes):
print('Shape {} of {}'.format(i+1, nshapes), end="\r")
xin = np.reshape(data_set[i,:,:], (1, -1, 3))
# Calculate the latent representation and point cloud
# reconstruction
if flag_vae:
f_batch[i,:,:] = sess.run(feat_layer,\
feed_dict={S_in: xin, dpout: 1.0})[0,:,:]
else:
if flag_p2ffd:
f_batch[i,:,:] = sess.run(feat_layer,
feed_dict={S_in: xin,
gamma_n:0})[0,:,:]
else:
f_batch[i,:,:] = sess.run(feat_layer,
feed_dict={S_in: xin})[0,:,:]
# Plot designs
if plot:
for j in range(f_batch.shape[2]):
fname = "{}/Shape_{:03}_Feat_{:03}".format(output_test, i,j)
Vis3D.pccmap(xin[0,:,:], f_batch[i,:,j], fname,
pointsize=20, vlim=[-1,1],
cam_az=0, cam_el=0, wsize=(3440, 1440))
return(f_batch)