|
| 1 | +""" |
| 2 | +Copyright 2021 Lance Galletti |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +from PIL import Image as im |
| 20 | +import matplotlib.pyplot as plt |
| 21 | +from tensorflow.keras import models |
| 22 | + |
| 23 | + |
| 24 | +class ConvGraph(): |
| 25 | + """ |
| 26 | + Class for creating and rendering visualization of Keras |
| 27 | + Sequential Model with Convolutional Layers |
| 28 | +
|
| 29 | + Attributes: |
| 30 | + model : tf.keras.Model |
| 31 | + a compiled keras sequential model |
| 32 | +
|
| 33 | + Methods: |
| 34 | + render : |
| 35 | + Shows all the convolution activations |
| 36 | +
|
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self, model): |
| 40 | + self.model = model |
| 41 | + |
| 42 | + |
| 43 | + def _snap_layer(self, display_grid, scale, filename, xticks=None, yticks=None): |
| 44 | + fig, ax = plt.subplots(figsize=(int(scale * display_grid.shape[1]), int(scale * display_grid.shape[0]))) |
| 45 | + ax.set_xticks(xticks) |
| 46 | + ax.set_yticks(yticks) |
| 47 | + ax.grid(True) |
| 48 | + ax.axis('off') |
| 49 | + ax.imshow(display_grid, aspect='auto') |
| 50 | + fig.savefig(filename + '.png', transparent=True, bbox_inches='tight', pad_inches=0) |
| 51 | + plt.close() |
| 52 | + return np.asarray(im.open(filename + '.png')) |
| 53 | + |
| 54 | + |
| 55 | + def animate(self, X=None, filename='conv_animation'): |
| 56 | + """ |
| 57 | + Render animation of a Convolutional layers based on a stream |
| 58 | + of input. |
| 59 | +
|
| 60 | + Parameters: |
| 61 | + X : ndarray |
| 62 | + input to a Keras model - ideally of the same class |
| 63 | + filename : str |
| 64 | + name of file to which visualization will be saved |
| 65 | +
|
| 66 | + Returns: |
| 67 | + None |
| 68 | + """ |
| 69 | + |
| 70 | + layer_outputs = [layer.output for layer in self.model.layers] |
| 71 | + # Creates a model that will return these outputs, given the model input |
| 72 | + activation_model = models.Model(inputs=self.model.input, outputs=layer_outputs) |
| 73 | + images_per_row = 8 |
| 74 | + |
| 75 | + for i in range(len(self.model.layers)): |
| 76 | + # Ignore non-conv2d layers |
| 77 | + layer_name = self.model.layers[i].name |
| 78 | + if not layer_name.startswith("conv2d"): |
| 79 | + continue |
| 80 | + |
| 81 | + images = [] |
| 82 | + heat = [] |
| 83 | + for j in range(len(X)): |
| 84 | + activations = activation_model.predict(X[j]) |
| 85 | + # Number of features in the feature map |
| 86 | + n_features = activations[i].shape[-1] |
| 87 | + # The feature map has shape (1, size, size, n_features). |
| 88 | + size = activations[i].shape[1] |
| 89 | + # Tiles the activation channels in this matrix |
| 90 | + n_cols = n_features // images_per_row |
| 91 | + display_grid = np.zeros((size * n_cols, images_per_row * size)) |
| 92 | + # Tiles each filter into a big horizontal grid |
| 93 | + for col in range(n_cols): |
| 94 | + for row in range(images_per_row): |
| 95 | + # Displays the grid |
| 96 | + display_grid[ |
| 97 | + col * size: (col + 1) * size, |
| 98 | + row * size: (row + 1) * size] = activations[i][0, :, :, col * images_per_row + row] |
| 99 | + |
| 100 | + snapped = self._snap_layer( |
| 101 | + display_grid, 1.0 / size, |
| 102 | + filename + "_" + layer_name, |
| 103 | + xticks=np.linspace(-1, display_grid.shape[1], images_per_row + 1), |
| 104 | + yticks=np.linspace(-1, display_grid.shape[0], n_cols + 1)) |
| 105 | + heat.append(snapped) |
| 106 | + images.append(im.fromarray(snapped)) |
| 107 | + |
| 108 | + images[0].save( |
| 109 | + filename + "_" + layer_name + '.gif', |
| 110 | + optimize=False, # important for transparent background |
| 111 | + save_all=True, |
| 112 | + append_images=images[1:], |
| 113 | + loop=0, |
| 114 | + duration=100, |
| 115 | + transparency=255, # prevent PIL from making background black |
| 116 | + disposal=2 |
| 117 | + ) |
| 118 | + |
| 119 | + heatmap = heat[0] |
| 120 | + for i in range(1, len(heat)): |
| 121 | + heatmap = np.where(heatmap < heat[i], heat[i], heatmap) |
| 122 | + fig, ax = plt.subplots() |
| 123 | + ax.axis('off') |
| 124 | + ax.imshow(heatmap) |
| 125 | + fig.savefig(filename + "_" + layer_name + '_heatmap.png', transparent=True, bbox_inches='tight', pad_inches=0) |
| 126 | + |
| 127 | + return |
| 128 | + |
| 129 | + |
| 130 | + def render(self, X=None, filename='conv_filters'): |
| 131 | + """ |
| 132 | + Render visualization of a Convolutional keras model |
| 133 | +
|
| 134 | + Parameters: |
| 135 | + X : ndarray |
| 136 | + input to a Keras model |
| 137 | + filename : str |
| 138 | + name of file to which visualization will be saved |
| 139 | +
|
| 140 | + Returns: |
| 141 | + None |
| 142 | + """ |
| 143 | + |
| 144 | + layer_outputs = [layer.output for layer in self.model.layers] |
| 145 | + # Creates a model that will return these outputs, given the model input |
| 146 | + activation_model = models.Model(inputs=self.model.input, outputs=layer_outputs) |
| 147 | + images_per_row = 8 |
| 148 | + |
| 149 | + for j in range(len(X)): |
| 150 | + activations = activation_model.predict(X[j]) |
| 151 | + |
| 152 | + for i in range(len(activations)): |
| 153 | + # Ignore non-conv2d layers |
| 154 | + layer_name = self.model.layers[i].name |
| 155 | + if not layer_name.startswith("conv2d"): |
| 156 | + continue |
| 157 | + |
| 158 | + # Number of features in the feature map |
| 159 | + n_features = activations[i].shape[-1] |
| 160 | + # The feature map has shape (1, size, size, n_features). |
| 161 | + size = activations[i].shape[1] |
| 162 | + # Tiles the activation channels in this matrix |
| 163 | + n_cols = n_features // images_per_row |
| 164 | + display_grid = np.zeros((size * n_cols, images_per_row * size)) |
| 165 | + # Tiles each filter into a big horizontal grid |
| 166 | + for col in range(n_cols): |
| 167 | + for row in range(images_per_row): |
| 168 | + # Displays the grid |
| 169 | + display_grid[ |
| 170 | + col * size: (col + 1) * size, |
| 171 | + row * size: (row + 1) * size] = activations[i][0, :, :, col * images_per_row + row] |
| 172 | + |
| 173 | + self._snap_layer( |
| 174 | + display_grid, 1. / size, |
| 175 | + filename + "_" + str(j) + "_" + layer_name, |
| 176 | + xticks=np.linspace(-1, display_grid.shape[1], images_per_row + 1), |
| 177 | + yticks=np.linspace(-1, display_grid.shape[0], n_cols + 1)) |
| 178 | + |
| 179 | + return |
0 commit comments