-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.py
54 lines (51 loc) · 2.28 KB
/
display.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
# displays model data to graphs
# code from https://towardsdatascience.com/visualizing-intermediate-activation-in-convolutional-neural-networks-with-keras-260b36d60d0
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras import models
from tensorflow.keras.callbacks import CSVLogger
from matplotlib import pyplot as plt
def plotAccuracyLoss(history):
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, color='b', label='Training acc')
plt.plot(epochs, val_acc, color='r', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, color='b', label='Training loss')
plt.plot(epochs, val_loss, color='r', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
def displayActivations(model, image):
layer_outputs = [layer.output for layer in model.layers[3:15]]
layer_names = [layer.name for layer in model.layers[3:15]]
activation_model = models.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(image)
images_per_row = 8 # number of images per row on the graphs
for layer_name, layer_activation in zip(layer_names, activations): # for each layer and name
# (31, size, size, n_features)
n_features = layer_activation.shape[-1]
size = layer_activation.shape[1]
no_cols = n_features // images_per_row
display_grid = np.zeros((size * no_cols, images_per_row * size))
for col in range(no_cols):
for row in range(images_per_row):
channel_image = layer_activation[0, :, :, col * images_per_row + row]
channel_image -= channel_image.mean() # Post-processes the feature to make it visually palatable
channel_image /= channel_image.std()
channel_image *= 64
channel_image += 128
channel_image = np.clip(channel_image, 0, 255).astype('uint8')
display_grid[col * size : (col + 1) * size, row * size : (row + 1) * size] = channel_image
scale = 1. / size
plt.figure(figsize=(scale * display_grid.shape[1], scale * display_grid.shape[0]))
plt.title(layer_name)
plt.grid(False)
plt.imshow(display_grid, aspect='auto', cmap='viridis')
plt.show()