-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangle_vis.py
457 lines (363 loc) · 13.8 KB
/
angle_vis.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
""" # noqa
___ __________________ ___________
/ _/__ ____ / __/ ___/ _/ __/ |/ / ___/ __/
/ _/ _ \/ __/ _\ \/ /___/ // _// / /__/ _/ # noqa
/_/ \___/_/ /___/\___/___/___/_/|_/\___/___/ # noqa
Author : Benjamin Blundell - [email protected]
angle_vis.py - Visualise the angles from our random
rotation generation.
"""
from pandas.core.frame import DataFrame
from pyquaternion import Quaternion
from net.renderer import Splat
import torch.nn.functional as F
from util.plyobj import load_obj
from util.loadsave import load_checkpoint, load_model
import torch
from random import random
import numpy as np
import argparse
import math
import pickle
import os
from tqdm import tqdm
import scipy.stats
from util.math import VecRotTen, VecRot, TransTen, PointsTen, qdist, vec_to_quat, angles_to_axis
from util.image import NormaliseBasic, NormaliseNull
SCALE = 40
TITLE = "Visualising rotations."
def basic_viz(rot_pairs):
"""
Given input and output rotations plot in a way
that is easy to visualise.
Parameters
----------
rot_pairs : List of Tuple of VecRot
List of input/output rotation pairs
Returns
-------
self
"""
data_matrix = np.zeros([SCALE, SCALE, SCALE], dtype=np.uint8)
count_matrix = np.zeros([SCALE, SCALE, SCALE], dtype=np.uint8)
for pair in rot_pairs:
rot = pair[0]
q = Quaternion(axis=rot.get_normalised(),
radians=rot.get_length())
rot_f = VecRot(q.axis[0] * q.radians,
q.axis[1] * q.radians,
q.axis[2] * q.radians)
x = int(rot_f.x * 5 + SCALE / 2)
y = int(rot_f.y * 5 + SCALE / 2)
z = int(rot_f.z * 5 + SCALE / 2)
# Now get the error at this spot
q0 = vec_to_quat(rot)
q1 = vec_to_quat(pair[1])
dd = qdist(q0, q1)
data_matrix[x, y, z] += dd
count_matrix[x, y, x] += 1
count_matrix = np.where(count_matrix < 1.0, 1, count_matrix)
data_matrix = data_matrix / count_matrix
rot_max = np.max(data_matrix)
from vedo import Volume, show
vol = Volume(data_matrix, c='RdBu_r', alpha=0.1, mode=1)
vol.addScalarBar3D()
#lego = vol.legosurface(vmin=1, vmax=rot_max)
#lego.shrink()
#lego.addScalarBar3D()
show(vol, TITLE, axes=1).close()
def sigma_effect(args, points, prev_args, device):
"""
What effect does sigma have on the loss, particularly
with different rotations.
Do rotations that differ a lot give a bigger error or no?
Parameters
----------
args : namespace
The program command line arguments
points: Points
The points the model came up with
prev_args : dictionary
The arguments used by the network when it was run.
device :
The torch device we are running on.
Returns
-------
None
"""
import pprint
import pandas as pd
from pandas import DataFrame
import seaborn as sns
import matplotlib.pyplot as plt
dim_size = args.dim_size # how many angles to compare to each other
sigmas = [10, 8.1, 6.56, 5.31, 4.3, 3.65, 2.95, 2.39, 1.94, 1.57]
sigmas = [10, 4.3, 2.0]
# Which normalisation are we using?
normaliser = NormaliseNull()
if prev_args.normalise_basic:
normaliser = NormaliseBasic()
mask = []
for _ in range(len(points)):
mask.append(1.0)
mask = torch.tensor(mask, device=device)
base_points = PointsTen(device=device)
base_points.from_points(load_obj(args.obj))
mask_base = []
for _ in range(len(base_points)):
mask_base.append(1.0)
mask_base = torch.tensor(mask_base, device=device)
xt = torch.tensor([0.0], dtype=torch.float32)
yt = torch.tensor([0.0], dtype=torch.float32)
t = TransTen(xt, yt)
losses_basic = np.zeros((len(sigmas), dim_size, dim_size, 2), dtype=float)
rotations = []
for x in range(dim_size):
rotations.append(VecRot(0, 0, 0).random().to_ten(device=device))
for s in range(len(sigmas)):
for x in range(dim_size):
rx = rotations[x]
for y in range(dim_size):
ry = rotations[y]
q0 = vec_to_quat(rx)
q1 = vec_to_quat(ry)
rdist = qdist(q0, q1)
losses_basic[s][x][y][0] = rdist
splat = Splat(device=device)
for sidx in tqdm(range(len(sigmas))):
current_sigma = sigmas[sidx]
for xidx in range(dim_size-1):
r0 = rotations[xidx]
base_image = splat.render(base_points, r0, t, mask_base, sigma=current_sigma)
base_image = base_image.reshape(1, 1, 128, 128)
base_image = normaliser.normalise(base_image)
for yidx in range(xidx+1, dim_size):
r1 = rotations[yidx]
second_image = splat.render(base_points, r1, t, mask_base, sigma=current_sigma)
second_image = second_image.reshape(1, 1, 128, 128)
second_image = normaliser.normalise(second_image)
second_image = second_image.squeeze()
base_image = base_image.squeeze()
loss_base = F.l1_loss(base_image, second_image, reduction="sum")
losses_basic[sidx][xidx][yidx][1] = loss_base.item()
losses_basic[sidx][yidx][xidx][1] = loss_base.item()
# pp = pprint.PrettyPrinter(indent=4, width=dim_size * 10)
for i in range(len(sigmas)):
tf = losses_basic[i]
tf = tf.reshape(-1, 2)
tf = tf = np.delete(tf, np.where(tf == [0, 0]), axis=0)
tf = np.unique(tf, axis=0)
df0 = DataFrame(tf, columns=("dist", "loss"))
sns.regplot(x="dist", y="loss", data=df0, label="Sigma " + str(sigmas[i]))
labels = ["Sigma " + str(i) for i in sigmas]
plt.legend(labels=labels)
plt.show()
def sigma_effect_model(args, model, points, prev_args, device):
"""
What effect does sigma have on the loss, particularly
with different rotations.
Do rotations that differ a lot give a bigger error or no?
Parameters
----------
args : namespace
The program command line arguments
model : Net
Our model we are going to test
points: Points
The points the model came up with
prev_args : dictionary
The arguments used by the network when it was run.
device :
The torch device we are running on.
Returns
-------
None
"""
import pprint
import pandas as pd
from pandas import DataFrame
import seaborn as sns
import matplotlib.pyplot as plt
dim_size = args.dim_size # how many angles to compare to each other
sigmas = [10, 8.1, 6.56, 5.31, 4.3, 3.65, 2.95, 2.39, 1.94, 1.57]
# Which normalisation are we using?
normaliser = NormaliseNull()
if prev_args.normalise_basic:
normaliser = NormaliseBasic()
mask = []
for _ in range(len(points)):
mask.append(1.0)
mask = torch.tensor(mask, device=device)
base_points = PointsTen(device=device)
base_points.from_points(load_obj(args.obj))
mask_base = []
for _ in range(len(base_points)):
mask_base.append(1.0)
mask_base = torch.tensor(mask_base, device=device)
xt = torch.tensor([0.0], dtype=torch.float32)
yt = torch.tensor([0.0], dtype=torch.float32)
t = TransTen(xt, yt)
# Build our cube of results
# Each entry has the two angles and the error
losses_basic = np.zeros((len(sigmas), dim_size, 2), dtype=float)
rotations = []
for x in range(dim_size):
rotations.append(VecRot(0, 0, 0).random().to_ten(device=device))
splat = Splat(device=device)
for sidx in tqdm(range(len(sigmas))):
current_sigma = sigmas[sidx]
for xidx in range(dim_size-1):
r0 = rotations[xidx]
base_image = splat.render(base_points, r0, t, mask_base, sigma=current_sigma)
base_image = base_image.reshape(1, 1, 128, 128)
base_image = normaliser.normalise(base_image)
model_image = model.forward(base_image, points)
model_image = normaliser.normalise(model_image.reshape(1, 1, 128, 128))
loss_model = F.l1_loss(model_image, base_image, reduction="sum")
model_image = torch.squeeze(model_image.cpu()[0])
model_rots = model.get_render_params()
r1 = VecRot(model_rots[0][0], model_rots[0][1], model_rots[0][2])
# Rotation 0, Rotation 1, Rotation network, qdist, loss, loss network
q0 = vec_to_quat(r0)
q1 = vec_to_quat(r1)
rdist = qdist(q0, q1)
losses_basic[sidx][xidx][0] = rdist
losses_basic[sidx][xidx][1] = loss_model.item()
# pp = pprint.PrettyPrinter(indent=4, width=dim_size * 10)
for i in range(len(sigmas)):
tf = losses_basic[i]
tf = tf = np.delete(tf, np.where(tf == [0, 0]), axis=0)
tf = np.unique(tf, axis=0)
df0 = DataFrame(tf, columns=("dist", "loss"))
print(df0)
sns.regplot(x="dist", y="loss", data=df0, label="Sigma " + str(sigmas[i]))
labels = ["Sigma " + str(i) for i in sigmas]
plt.legend(labels=labels)
plt.savefig("sigma_effect_model.jpg")
plt.show()
def angle_check(args, model, points, prev_args, device):
"""
Given our model and some input angles, run through the
network and see what corresponding angles we get.
Parameters
----------
args : namespace
The program command line arguments
model : Net
Our neural network model
points: Points
The points the model came up with
prev_args : dictionary
The arguments used by the network when it was run.
device :
The torch device we are running on.
Returns
-------
List
a List of tuples of VecRot pairs
"""
xt = 0.0
yt = 0.0
xt = torch.tensor([xt], dtype=torch.float32, device=device)
yt = torch.tensor([yt], dtype=torch.float32, device=device)
trans = TransTen(xt, yt)
normaliser = NormaliseNull()
if prev_args.normalise_basic:
normaliser = NormaliseBasic()
# Load some base points from an obj
loaded_points = load_obj(objpath=args.obj)
mask = []
for _ in loaded_points:
mask.append(1.0)
mask = torch.tensor(mask, device=device)
scaled_points = PointsTen(device=device).from_points(loaded_points)
model.set_sigma(args.sigma)
rots_in_out = []
for i in range(args.num_rots):
rot = VecRot(0, 0, 0)
rot.random()
rot = rot.to_ten(device=device)
splat = Splat(device=device)
target = splat.render(scaled_points, rot, trans, mask, sigma=args.sigma)
target = target.reshape(1, 128, 128)
target = target.repeat(prev_args.batch_size, 1, 1, 1)
target = target.to(device)
target = normaliser.normalise(target)
output = model.forward(target, points)
output = normaliser.normalise(output.reshape(prev_args.batch_size, 1, 128, 128))
loss = F.l1_loss(output, target, reduction="sum")
prots = model.get_render_params().squeeze()
print("Loss:", loss.item())
rots_in_out.append((rot, VecRot(float(prots[0][0]), float(prots[0][1]), float(prots[0][2]))))
del target
del output
return rots_in_out
def load(args, device):
""" Begin our training routine on the selected device."""
# Continue training or start anew
# Declare the variables we absolutely need
model = None
points = None
model = load_model(args.savedir + "/model.tar", device)
if os.path.isfile(args.savedir + "/" + args.savename):
(model, points, _, _, _, _, prev_args) = load_checkpoint(
model, args.savedir, args.savename, device
)
model.to(device)
print("Loaded model", model)
else:
print("Error - need to pass in a model")
return
model.eval()
with torch.no_grad():
# results = angle_check(args, model, points, prev_args, device)
sigma_effect(args, points, prev_args, device)
#sigma_effect_model(args, model, points, prev_args, device)
#basic_viz(results)
if __name__ == "__main__":
# Training settings
parser = argparse.ArgumentParser(description="PyTorch Shaper Eval")
parser.add_argument(
"--no-cuda", action="store_true", default=False, help="disables CUDA training"
)
parser.add_argument(
"--savedir", default="./save", help="The name for checkpoint save directory."
)
parser.add_argument(
"--obj", default="teapot.obj", help="Path to the groundruth obj file"
)
parser.add_argument(
"--sigma", default=1.25, type=float, help="The sigma value for this testing"
)
parser.add_argument(
"--num-rots", default=360, type=int, help="The number of rots to try (default 360)."
)
parser.add_argument(
"--dim-size", default=20, type=int, help="How many angles in loss check (default 20)."
)
parser.add_argument(
"--seed", type=int, default=1, metavar="S", help="random seed (default: 1)"
)
parser.add_argument(
"--rots",
metavar="R",
type=float,
nargs=3,
help="Rotation around X, Y, Z axis in degrees.",
)
parser.add_argument(
"--trans", metavar="R", type=float, nargs=2, help="Translation on X, Y plane"
)
parser.add_argument(
"--savename",
default="checkpoint.pth.tar",
help="The name for checkpoint save file.",
)
# Initial setup of PyTorch
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {"num_workers": 1, "pin_memory": True} if use_cuda else {}
load(args, device)
print("Finished Angle Vis")