forked from ntnu-arl/ORACLE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
555 lines (487 loc) · 24.8 KB
/
utilities.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
import itertools
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
from scipy.ndimage.filters import gaussian_filter
import matplotlib.cm as cm
import sensor_msgs.msg as sensor_msgs
import std_msgs.msg as std_msgs
import rospy
def get_files_ending_with(folder_or_folders, ext):
if isinstance(folder_or_folders, str):
folder = folder_or_folders
assert os.path.exists(folder)
fnames = []
for fname in os.listdir(folder):
if fname.endswith(ext):
fnames.append(os.path.join(folder, fname))
return sorted(fnames)
else:
assert hasattr(folder_or_folders, '__iter__')
print('folder_or_folders:', folder_or_folders)
return list(itertools.chain(*[get_files_ending_with(folder, ext) for folder in folder_or_folders]))
def GiB(val):
return val * 1 << 30
def plot_trained_model(custom_predictor_model):
"""di = depth_image_cnn(DI_SHAPE)
activations = get_activations(di,current_di, layer_names="obs_im/conv0")
display_activations(activations, cmap="gray", save=False, )
for layer in di.layers:
if 'obs_im/conv0' in layer.name:
weights, bias= layer.get_weights()
print(layer.name, weights.shape)
#normalize filter values between 0 and 1 for visualization
f_min, f_max = weights.min(), weights.max()
filters = (weights - f_min) / (f_max - f_min)
print(filters.shape)
filter_cnt=1
filters = np.reshape(filters, [filters.shape[3], filters.shape[0], filters.shape[1]])
#plotting all the filters
fig1, ax1 = plt.subplots()
for i in range(filters.shape[0]):
plt.subplot(6, 6, i+1)
im = plt.imshow((filters[i]))
fig1.subplots_adjust(right=0.8)
cbar_ax = fig1.add_axes([0.85, 0.15, 0.05, 0.7])
fig1.colorbar(im, cax=cbar_ax)
fig1 = plt.gcf()
fig1.suptitle("Not trained", fontsize=20)
#plt.show()
"""
for layer in custom_predictor_model.layers[0].layers:
if 'conv2d' == layer.name: # first conv layer name
weights, bias= layer.get_weights()
print(layer.name, weights.shape)
#normalize filter values between 0 and 1 for visualization
f_min, f_max = weights.min(), weights.max()
filters = (weights - f_min) / (f_max - f_min)
filter_cnt=1
print(filters[0])
filters = np.reshape(filters, [filters.shape[3], filters.shape[0], filters.shape[1]])
#plotting all the filters
fig2, ax2 = plt.subplots()
for i in range(filters.shape[0]):
plt.subplot(6, 6, i+1)
im = plt.imshow((filters[i]))
fig2.subplots_adjust(right=0.8)
cbar_ax = fig2.add_axes([0.85, 0.15, 0.05, 0.7])
fig2.colorbar(im, cax=cbar_ax)
fig2 = plt.gcf()
fig2.suptitle("First layer weights - Trained", fontsize=20)
plt.show()
def make_gradcam_heatmap(state, current_di, action_seq, model, last_conv_layer_name, pred_index=None):
# First, we create a model that maps the input image to the activations
# of the last conv layer as well as the output predictions
grad_model = tf.keras.models.Model(
[model.inputs], [model.get_layer(last_conv_layer_name).output, model.output]
)
# Then, we compute the gradient of the top predicted class for our input image
# with respect to the activations of the last conv layer
with tf.GradientTape() as tape:
last_conv_layer_output, preds = grad_model([state, current_di, action_seq])
if pred_index is None:
pred_index = tf.argmax(preds[0,:,0])
class_channel = preds
#print("class channel: ", class_channel)
print(type(class_channel))
grads = tape.gradient(class_channel, last_conv_layer_output)
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
last_conv_layer_output = last_conv_layer_output[0]
heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis]
heatmap = tf.squeeze(heatmap)
# For visualization purpose, we will also normalize the heatmap between 0 & 1
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
return heatmap.numpy()
def make_gradcam_heatmap_no_state(current_di, action_seq, model, last_conv_layer_name, pred_index=None):
# First, we create a model that maps the input image to the activations
# of the last conv layer as well as the output predictions
grad_model = tf.keras.models.Model(
[model.inputs], [model.get_layer(last_conv_layer_name).output, model.output]
)
# Then, we compute the gradient of the top predicted class for our input image
# with respect to the activations of the last conv layer
with tf.GradientTape() as tape:
last_conv_layer_output, preds = grad_model([current_di, action_seq])
if pred_index is None:
pred_index = tf.argmax(preds[0,:,0])
class_channel = preds
#print("class channel: ", class_channel)
print(type(class_channel))
grads = tape.gradient(class_channel, last_conv_layer_output)
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
last_conv_layer_output = last_conv_layer_output[0]
heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis]
heatmap = tf.squeeze(heatmap)
# For visualization purpose, we will also normalize the heatmap between 0 & 1
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
return heatmap.numpy()
def save_and_display_gradcam(img, heatmap, cam_path="cam.jpg", alpha=0.4):
# Load the original image
#img = keras.preprocessing.image.load_img(img_path)
#img = keras.preprocessing.image.img_to_array(img)
img = np.reshape(img, DI_SHAPE) # [270,480,1]
# Rescale heatmap to a range 0-255
heatmap = np.uint8(255 * heatmap)
# Use jet colormap to colorize heatmap
jet = cm.get_cmap("jet")
# Use RGB values of the colormap
jet_colors = jet(np.arange(256))[:, :3]
jet_heatmap = jet_colors[heatmap]
# Create an image with RGB colorized heatmap
jet_heatmap = keras.preprocessing.image.array_to_img(jet_heatmap)
jet_heatmap = jet_heatmap.resize((img.shape[1], img.shape[0]))
jet_heatmap = keras.preprocessing.image.img_to_array(jet_heatmap)
# Superimpose the heatmap on original image
superimposed_img = jet_heatmap * alpha + img
superimposed_img = keras.preprocessing.image.array_to_img(superimposed_img)
# Save the superimposed image
superimposed_img.save(cam_path)
input_img = keras.preprocessing.image.array_to_img(img)
input_img.save("input.jpg")
# Display Grad CAM
# display(Image(cam_path))
def save_and_display_gradcam_info(img, heatmap, cam_path="cam.jpg", alpha=0.4):
# Load the original image
#img = keras.preprocessing.image.load_img(img_path)
#img = keras.preprocessing.image.img_to_array(img)
img = np.uint8(255 * img[0]) # remove the batch channel
# Rescale heatmap to a range 0-255
heatmap = np.uint8(255 * heatmap)
# Use jet colormap to colorize heatmap
jet = cm.get_cmap("jet")
# Use RGB values of the colormap
jet_colors = jet(np.arange(256))[:, :3]
jet_heatmap = jet_colors[heatmap]
# Create an image with RGB colorized heatmap
jet_heatmap = keras.preprocessing.image.array_to_img(jet_heatmap)
jet_heatmap = jet_heatmap.resize((img.shape[1], img.shape[0]))
jet_heatmap = keras.preprocessing.image.img_to_array(jet_heatmap)
# Superimpose the heatmap on original image
superimposed_img = jet_heatmap * alpha + 0.5 * img[..., [1]]
superimposed_img = np.vstack((superimposed_img, np.tile(img[..., [1]], (1, 1, 3)), np.tile(img[..., [0]], (1, 1, 3))))
superimposed_img = keras.preprocessing.image.array_to_img(superimposed_img)
# Save the superimposed image
superimposed_img.save(cam_path)
# input_img = keras.preprocessing.image.array_to_img(img)
# input_img.save("input.jpg")
# Display Grad CAM
# display(Image(cam_path))
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
BLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def diamond(n):
a = np.arange(n)
b = np.minimum(a,a[::-1])
bool_table = (b[:,None]+b)>=(n-1)//2
diamond = np.zeros([n,n])
for i in range(len(bool_table)):
for j in range(len(bool_table[i])):
if bool_table[i][j]:
diamond[i][j] = 1
return np.asarray(diamond, dtype=np.uint8)
class FilterKernel:
FULL_KERNEL_3 = np.ones((3, 3), np.uint8)
FULL_KERNEL_5 = np.ones((5, 5), np.uint8)
FULL_KERNEL_7 = np.ones((7, 7), np.uint8)
FULL_KERNEL_9 = np.ones((9, 9), np.uint8)
FULL_KERNEL_15 = np.ones((15, 15), np.uint8)
FULL_KERNEL_21 = np.ones((21, 21), np.uint8)
FULL_KERNEL_31 = np.ones((31, 31), np.uint8)
FULL_KERNEL_41 = np.ones((41, 41), np.uint8)
# 3x3 cross kernel
CROSS_KERNEL_3 = np.asarray(
[
[0, 1, 0],
[1, 1, 1],
[0, 1, 0],
], dtype=np.uint8)
# 5x5 cross kernel
CROSS_KERNEL_5 = np.asarray(
[
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
], dtype=np.uint8)
# 5x5 diamond kernel
DIAMOND_KERNEL_5 = np.array(
[
[0, 0, 1, 0, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0],
], dtype=np.uint8)
# 7x7 cross kernel
CROSS_KERNEL_7 = np.asarray(
[
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
], dtype=np.uint8)
# 7x7 diamond kernel
DIAMOND_KERNEL_7 = np.asarray(
[
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
], dtype=np.uint8)
DIAMOND_KERNEL_21 = np.asarray(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
], dtype = np.uint8)
class HSVFilter:
# arena bright
# HSVLOW_YELLOW = np.array([30, 100, 110])
# HSVHIGH_YELLOW = np.array([45, 255, 255])
# elektro_hall night
HSVLOW_YELLOW = np.array([30, 100, 85])
HSVHIGH_YELLOW = np.array([45, 255, 255])
def binary_blobs(shape, blob_size_fraction, volume_fraction=0.5, seed=None, pixel=None):
"""
Generate synthetic binary image with several rounded blob-like objects.
Modified from https://github.com/scikit-image/scikit-image/blob/v0.19.2/skimage/data/_binary_blobs.py#L6-L63
Parameters
----------
length : tuple of int
Linear size of output image.
blob_size_fraction : tuple of float
Typical linear size of blob, should be smaller than 1.
volume_fraction : float, default 0.5
Fraction of image pixels covered by the blobs (where the output is 1).
Should be in [0, 1].
seed : {None, int, `numpy.random.Generator`}, optional
If `seed` is None the `numpy.random.Generator` singleton is used.
If `seed` is an int, a new ``Generator`` instance is used,
seeded with `seed`.
If `seed` is already a ``Generator`` instance then that instance is
used.
Returns
-------
blobs : ndarray of bools
Output binary image
Examples
--------
"""
assert len(shape) == len(blob_size_fraction)
n_dim = len(shape)
assert n_dim > 0
rs = np.random.default_rng(seed)
# shape = tuple([length] * n_dim)
mask = np.zeros(shape)
n_pts = max(np.rint(1. / np.prod(blob_size_fraction)).astype(int), 1)
# print('n_pts:', n_pts)
shape = np.array(shape)
# print('shape:', shape)
points = (shape.reshape((np.size(shape),1)) * rs.random((n_dim, n_pts))).astype(int)
# print('points:', np.shape(points))
if pixel != None:
pixel = np.array(pixel).reshape((np.size(pixel),1))
# print('pixel:', np.shape(pixel))
points = np.append(points, pixel, axis=1)
# print('points:', np.shape(points))
# points = pixel
mask[tuple(indices for indices in points)] = 1
mask = gaussian_filter(mask, sigma=0.25 * shape * blob_size_fraction)
threshold = np.percentile(mask, 100 * (1 - volume_fraction))
mask = np.logical_not(mask < threshold)
return mask
def deterministic_binary_blobs(pixel, shape, blob_size_fraction, volume_fraction=0.5):
"""
Generate synthetic binary image with several rounded blob-like objects.
Modified from https://github.com/scikit-image/scikit-image/blob/v0.19.2/skimage/data/_binary_blobs.py#L6-L63
Parameters
----------
length : tuple of int
Linear size of output image.
blob_size_fraction : tuple of float
Typical linear size of blob, should be smaller than 1.
volume_fraction : float, default 0.5
Fraction of image pixels covered by the blobs (where the output is 1).
Should be in [0, 1].
seed : {None, int, `numpy.random.Generator`}, optional
If `seed` is None the `numpy.random.Generator` singleton is used.
If `seed` is an int, a new ``Generator`` instance is used,
seeded with `seed`.
If `seed` is already a ``Generator`` instance then that instance is
used.
Returns
-------
blobs : ndarray of bools
Output binary image
Examples
--------
"""
assert len(shape) == len(blob_size_fraction)
n_dim = len(shape)
assert n_dim > 0
# shape = tuple([length] * n_dim)
mask = np.zeros(shape)
# print('n_pts:', n_pts)
shape = np.array(shape)
# print('shape:', shape)
# print('points:', np.shape(points))
# if pixel != None:
# pixel = np.array(pixel).reshape((np.size(pixel),1))
# # print('pixel:', np.shape(pixel))
# points = np.append(points, pixel, axis=1)
# # print('points:', np.shape(points))
# # points = pixel
# mask[pixel] = 1
mask[tuple(indices for indices in pixel)] = 1
mask = gaussian_filter(mask, sigma=0.25 * shape * blob_size_fraction)
threshold = np.percentile(mask, 100 * (1 - volume_fraction))
mask = np.logical_not(mask < threshold)
return mask
# https://gist.github.com/pgorczak/5c717baa44479fa064eb8d33ea4587e0
def create_point_cloud(points, parent_frame, time_stamp = None):
""" Creates a point cloud message.
Args:
points: Nx4 array of xyz positions (m) and intensity
parent_frame: frame in which the point cloud is defined
Returns:
sensor_msgs/PointCloud2 message
"""
ros_dtype = sensor_msgs.PointField.FLOAT32
dtype = np.float32
itemsize = np.dtype(dtype).itemsize
data = points.astype(dtype).tobytes()
fields = [sensor_msgs.PointField(
name=n, offset=i*itemsize, datatype=ros_dtype, count=1)
for i, n in enumerate(['x', 'y', 'z', 'intensity'])]
if time_stamp == None:
header = std_msgs.Header(frame_id=parent_frame, stamp=rospy.Time.now())
else:
header = std_msgs.Header(frame_id=parent_frame, stamp=time_stamp)
return sensor_msgs.PointCloud2(
header=header,
height=1,
width=points.shape[0],
is_dense=False,
is_bigendian=False,
fields=fields,
point_step=(itemsize * 4),
row_step=(itemsize * 4 * points.shape[0]),
data=data
)
def create_point_cloud_xyz(points, parent_frame, time_stamp = None):
""" Creates a point cloud message.
Args:
points: Nx3 array of xyz positions (m)
parent_frame: frame in which the point cloud is defined
Returns:
sensor_msgs/PointCloud2 message
"""
ros_dtype = sensor_msgs.PointField.FLOAT32
dtype = np.float32
itemsize = np.dtype(dtype).itemsize
data = points.astype(dtype).tobytes()
fields = [sensor_msgs.PointField(
name=n, offset=i*itemsize, datatype=ros_dtype, count=1)
for i, n in enumerate(['x', 'y', 'z'])]
if time_stamp == None:
header = std_msgs.Header(frame_id=parent_frame, stamp=rospy.Time.now())
else:
header = std_msgs.Header(frame_id=parent_frame, stamp=time_stamp)
return sensor_msgs.PointCloud2(
header=header,
height=1,
width=points.shape[0],
is_dense=False,
is_bigendian=False,
fields=fields,
point_step=(itemsize * 3),
row_step=(itemsize * 3 * points.shape[0]),
data=data
)
# https://on-demand.gputechconf.com/gtc-cn/2019/pdf/CN9577/presentation.pdf
def get_batch_norm_params(weights, layer_name):
g0 = weights[layer_name + '/gamma'].reshape(-1)
m0 = weights[layer_name + '/mean'].reshape(-1)
v0 = weights[layer_name + '/var'].reshape(-1)
scale0 = g0 / np.sqrt(v0 + 1e-3)
shift0 = -m0 / np.sqrt(v0 + 1e-3) * g0 + weights[layer_name + '/beta'].reshape(-1)
power0 = np.ones(len(g0), dtype=np.float32)
return shift0, scale0, power0