-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcnn_utils.py
123 lines (104 loc) · 5.03 KB
/
cnn_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
import numpy as np
import matplotlib.pyplot as plt
params = {'legend.fontsize': 'x-large',
'figure.figsize': (15, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
plt.rcParams.update(params)
def make_prediction(model=None,img_vector=[],
label_dict={},top_N=3,
model_input_shape=None):
if model:
# get model input shape
if not model_input_shape:
model_input_shape = (1,)+model.get_input_shape_at(0)[1:]
# get prediction
prediction = model.predict(img_vector.reshape(model_input_shape))[0]
# get top N with confidence
labels_predicted = [label_dict[idx] for idx in np.argsort(prediction)[::-1][:top_N]]
confidence_predicted = np.sort(prediction)[::-1][:top_N]
return labels_predicted, confidence_predicted
def plot_predictions(model,dataset,
dataset_labels,label_dict,
batch_size,grid_height,grid_width):
if model:
f, ax = plt.subplots(grid_width, grid_height)
f.set_size_inches(12, 12)
random_batch_indx = np.random.permutation(np.arange(0,len(dataset)))[:batch_size]
img_idx = 0
for i in range(0, grid_width):
for j in range(0, grid_height):
actual_label = label_dict.get(dataset_labels[random_batch_indx[img_idx]].argmax())
preds,confs_ = make_prediction(model,
img_vector=dataset[random_batch_indx[img_idx]],
label_dict=label_dict,
top_N=1)
ax[i][j].axis('off')
ax[i][j].set_title('Actual:'+actual_label[:10]+\
'\nPredicted:'+preds[0] + \
'(' +str(round(confs_[0],2)) + ')')
ax[i][j].imshow(dataset[random_batch_indx[img_idx]])
img_idx += 1
plt.subplots_adjust(left=0, bottom=0, right=1,
top=1, wspace=0.4, hspace=0.55)
# source: https://github.com/keras-team/keras/issues/431#issuecomment-317397154
def get_activations(model, model_inputs,
print_shape_only=True, layer_name=None):
import keras.backend as K
print('----- activations -----')
activations = []
inp = model.input
model_multi_inputs_cond = True
if not isinstance(inp, list):
# only one input! let's wrap it in a list.
inp = [inp]
model_multi_inputs_cond = False
# all layer outputs
outputs = [layer.output for layer in model.layers if
layer.name == layer_name or layer_name is None]
# evaluation functions
funcs = [K.function(inp + [K.learning_phase()], [out]) for out in outputs]
if model_multi_inputs_cond:
list_inputs = []
list_inputs.extend(model_inputs)
list_inputs.append(1.)
else:
list_inputs = [model_inputs, 1.]
# Learning phase. 1 = Test mode (no dropout or batch normalization)
# layer_outputs = [func([model_inputs, 1.])[0] for func in funcs]
layer_outputs = [func(list_inputs)[0] for func in funcs]
for layer_activations in layer_outputs:
activations.append(layer_activations)
if print_shape_only:
print(layer_activations.shape)
else:
print(layer_activations)
return activations
# source :https://github.com/philipperemy/keras-visualize-activations/blob/master/read_activations.py
def display_activations(activation_maps):
batch_size = activation_maps[0].shape[0]
assert batch_size == 1, 'One image at a time to visualize.'
for i, activation_map in enumerate(activation_maps):
print('Displaying activation map {}'.format(i))
shape = activation_map.shape
if len(shape) == 4:
activations = np.hstack(np.transpose(activation_map[0], (2, 0, 1)))
elif len(shape) == 2:
# try to make it square as much as possible. we can skip some activations.
activations = activation_map[0]
num_activations = len(activations)
# too hard to display it on the screen.
if num_activations > 1024:
square_param = int(np.floor(np.sqrt(num_activations)))
activations = activations[0: square_param * square_param]
activations = np.reshape(activations, (square_param, square_param))
else:
activations = np.expand_dims(activations, axis=0)
else:
raise Exception('len(shape) = 3 has not been implemented.')
#plt.imshow(activations, interpolation='None', cmap='binary')
fig, ax = plt.subplots(figsize=(18, 12))
ax.imshow(activations, interpolation='None', cmap='binary')
plt.show()