-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize_laplacian.py
451 lines (368 loc) · 15.8 KB
/
visualize_laplacian.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
# ---------------------------------------------------------------------------------------
# Find image that maximizes activations of a specified neuron/layer
#
# Ref = http://nbviewer.jupyter.org/github/tensorflow/tensorflow/blob/master/tensorflow/
# examples/tutorials/deepdream/deepdream.ipynb
# ---------------------------------------------------------------------------------------
import pickle
import matplotlib.pyplot as plt
import numpy as np
from functools import partial
import tensorflow as tf
import model.hed as model_hed
import model.doc as model_doc
def get_layer_callback(l_name, model_graph):
"""
Helper function for getting layer output tensor
:param l_name:
:param model_graph:
:return:
"""
return model_graph.get_tensor_by_name("{}:0".format(l_name))
def vis_normalize(a, s=0.1):
"""
Normalize a image for display
:param s:
:param a:
:return:
"""
return s * (a - a.mean()) / (max(a.std(), 1e-4)) + 0.5
def display_image(a, axis=None):
if axis is None:
_, axis = plt.subplots()
a = np.uint8(np.clip(a, 0, 1) * 255.0)
axis.imshow(a)
def simple_gradient_ascent(tgt_cb, in_cb, in_img, n_iter=20, step=1.0):
t_score = tf.reduce_mean(tgt_cb) # Reduce the mean activation of the output
t_grad = tf.gradients(t_score, in_cb)[0] # wrt to the input variable
img = in_img.copy()
with tf.Session() as s:
s.run(tf.global_variables_initializer())
for i in range(n_iter):
g, score = s.run([t_grad, t_score], {in_cb: img})
# normalizing the gradient, so the same step size should work for different layers and networks
# for different layers and networks
g /= g.std() + 1e-8
img += g * step
print("{}: Score {}".format(i, score))
# Display the image
img = np.squeeze(img, axis=0)
return vis_normalize(img)
def get_number_of_channels(tgt_l, model_graph):
"""
Get the number of channels in a specified layer
:param tgt_l:
:param model_graph:
:return:
"""
return int(model_graph.get_tensor_by_name(tgt_l + ':0').get_shape()[-1])
def visualize_max_neuron_activation(t_layer_name, t_chan, t_loc, g, n_iter=100):
"""
:param t_layer_name:
:param t_chan:
:param t_loc:
:param g: graph
:param n_iter:
:return:
"""
t_layer_cb = get_layer_callback(t_layer_name, g)
print("Target Layer {}. shape {}".format(t_layer_cb.name, t_layer_cb.get_shape()))
# Start with a gray image with noise
start_img = np.random.uniform(size=(256, 256, 3)) + 100.
start_img = np.expand_dims(start_img, 0)
final_img = simple_gradient_ascent(
t_layer_cb[:, t_loc, t_loc, t_chan],
input_cb,
start_img,
n_iter=n_iter,
)
display_image(vis_normalize(final_img))
plt.title("Layer name {}. Channel Index {}. Neuron at index ({},{})".format(
t_layer_name, t_chan, t_loc, t_loc))
def tffunc(*argtypes):
""" Helper that transforms TF-graph generating function into a regular one.
See "resize" function below.
"""
placeholders = list(map(tf.placeholder, argtypes))
def wrap(f):
out = f(*placeholders)
def wrapper(*args, **kw):
return out.eval(dict(zip(placeholders, args)), session=kw.get('session'))
return wrapper
return wrap
k = np.float32([1, 4, 6, 4, 1])
k = np.outer(k, k)
k5x5 = k[:, :, None, None]/k.sum() * np.eye(3, dtype=np.float32)
def lap_split(img):
""" Split the image into lo and hi frequency components """
with tf.name_scope('split'):
lo = tf.nn.conv2d(img, k5x5, [1, 2, 2, 1], 'SAME')
lo2 = tf.nn.conv2d_transpose(lo, k5x5*4, tf.shape(img), [1, 2, 2, 1])
hi = img-lo2
return lo, hi
def lap_split_n(img, n):
"""Build Laplacian pyramid with n splits"""
levels = []
print("inside lap_split_n function ")
for i in range(n):
img, hi = lap_split(img)
levels.append(hi)
levels.append(img)
return levels[::-1]
def lap_merge(levels):
""" Merge Laplacian pyramid """
img = levels[0]
for hi in levels[1:]:
with tf.name_scope('merge'):
img = tf.nn.conv2d_transpose(img, k5x5*4, tf.shape(hi), [1, 2, 2, 1]) + hi
return img
def normalize_std(img, eps=1e-10):
""" Normalize image by making its standard deviation = 1.0 """
with tf.name_scope('normalize'):
std = tf.sqrt(tf.reduce_mean(tf.square(img)))
return img/tf.maximum(std, eps)
def lap_normalize(img, scale_n=4):
"""Perform the Laplacian pyramid normalization. """
# img = tf.expand_dims(img, 0)
# print("Inside lap_normalize Function, img shape {}".format(tf.shape(img)))
tlevels = lap_split_n(img, scale_n)
tlevels = list(map(normalize_std, tlevels))
out = lap_merge(tlevels)
return out[0, :, :, :]
def lap_pyramid_gradient_ascent(tgt_cb, in_cb, in_img, n_iter=20, step=1.0, lap_n=4):
t_score = tf.reduce_mean(tgt_cb) # Reduce the mean activation of the output
t_grad = tf.gradients(t_score, in_cb)[0] # wrt to the input variable
lap_norm_func = tffunc(np.float32)(partial(lap_normalize, scale_n=lap_n))
img = in_img.copy()
with tf.Session() as s:
s.run(tf.global_variables_initializer())
for i in range(n_iter):
g, score = s.run([t_grad, t_score], {in_cb: img})
g = lap_norm_func(g)
# normalizing the gradient, so the same step size should work for different layers and networks
# for different layers and networks
g /= g.std() + 1e-8
img += g * step
print("{}: Score {}".format(i, score))
# Display the image
img = np.squeeze(img, axis=0)
return vis_normalize(img)
def visualize_max_neuron_activation_lap_pyramid(t_layer_name, t_chan, t_loc, graph_1, n_iter=100):
t_layer_cb = get_layer_callback(t_layer_name, graph_1)
print("Target Layer {}. shape {}".format(t_layer_cb.name, t_layer_cb.get_shape()))
# Start with a gray image with noise
start_img = np.random.uniform(size=(256, 256, 3)) + 100.
start_img = np.expand_dims(start_img, 0)
final_img = lap_pyramid_gradient_ascent(
t_layer_cb[:, t_loc, t_loc, t_chan],
input_cb,
start_img,
n_iter=n_iter,
)
with open('visualize-lap-{}-{}-{}.pkl'.format(t_layer_name, t_chan, t_loc), 'wb') as f:
pickle.dump(final_img, f)
display_image(vis_normalize(final_img))
plt.title("Layer name {}. Channel Index {}. Neuron at index ({},{})".format(
t_layer_name, t_chan, t_loc, t_loc))
if __name__ == '__main__':
plt.ion()
np.random.seed(7)
# # ***********************************************************************************
# # HED Model
# # ***********************************************************************************
# print("Analyzing HED Network")
#
# # Load the Model
# input_cb, output_cb = model_hed.KitModel(weight_file='./model/hed.npy')
# graph = tf.get_default_graph()
#
# with tf.Session() as sess:
# tensorboard_logs_dir = './logs'
# train_writer = tf.summary.FileWriter(tensorboard_logs_dir)
# train_writer.add_graph(sess.graph)
#
# # To view:
# # [1] tensorboard --logdir=./logs
# # [2] In a browser window, open http://localhost:6006/
#
# # List Layer names of interest
# with tf.Session() as sess:
#
# layers = [op.name for op in graph.get_operations() if ((op.type == 'Conv2D') or (op.type == 'Relu'))]
#
# feature_nums = [int(graph.get_tensor_by_name(name + ':0').get_shape()[-1]) for name in layers]
#
# print("Total Number of layers {}".format(len(layers)))
# for l_idx, layer in enumerate(layers):
# print("{} has {} kernels".format(layer, feature_nums[l_idx]))
#
# # Visualize max activations of target cells
# # Good Border cells
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 441
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Border Cell - Laplacian Method")
#
# # Good Contrast Cells
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 88
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 130
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 154
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 170
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 314
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 464
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 488
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
# ***********************************************************************************
# DOC Model
# ***********************************************************************************
print("Analyzing DOC Network")
# Load the Model
input_cb, output_cb = model_doc.KitModel(weight_file='./model/doc.npy')
graph = tf.get_default_graph()
with tf.Session() as sess:
tensorboard_logs_dir = './logs'
train_writer = tf.summary.FileWriter(tensorboard_logs_dir)
train_writer.add_graph(sess.graph)
# To view:
# [1] tensorboard --logdir=./logs
# [2] In a browser window, open http://localhost:6006/
# # List Layer names of interest
# with tf.Session() as sess:
#
# layers = [op.name for op in graph.get_operations() if ((op.type == 'Conv2D') or (op.type == 'Relu'))]
#
# feature_nums = [int(graph.get_tensor_by_name(name + ':0').get_shape()[-1]) for name in layers]
#
# print("Total Number of layers {}".format(len(layers)))
# for l_idx, layer in enumerate(layers):
# print("{} has {} kernels".format(layer, feature_nums[l_idx]))
# # Visualize Target Cells
# # Good Border cells
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 121
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Border Cell - Laplacian Method")
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 204
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Border Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 254
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Border Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 326
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Border Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 476
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Border Cell - Laplacian Method")
# Good Contrast Cells
tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
tgt_channel = 81
center_neuron_idx = 8 # for size 256, 256
visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 94
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 199
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 205
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 226
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 328
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# tgt_layer = 'convolution_12' # the convolution operation right before relu5_3
# tgt_channel = 491
# center_neuron_idx = 8 # for size 256, 256
#
# visualize_max_neuron_activation_lap_pyramid(tgt_layer, tgt_channel, center_neuron_idx, graph)
# plt.suptitle("Good Contrast Cell - Laplacian Method")
#
# # -----------------------------------------------------------------------------------
# # End
# # -----------------------------------------------------------------------------------
# input("Press any Key to Exit")