|
| 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 | +import matplotlib.pyplot as plt |
| 20 | +from tensorflow.keras import models |
| 21 | + |
| 22 | + |
| 23 | +class ConvGraph(): |
| 24 | + """ |
| 25 | + Class for creating and rendering visualization of Keras |
| 26 | + Sequential Model with Convolutional Layers |
| 27 | +
|
| 28 | + Attributes: |
| 29 | + model : tf.keras.Model |
| 30 | + a compiled keras sequential model |
| 31 | +
|
| 32 | + Methods: |
| 33 | + render : |
| 34 | + Shows all the convolution activations |
| 35 | +
|
| 36 | + """ |
| 37 | + |
| 38 | + def __init__(self, model): |
| 39 | + self.model = model |
| 40 | + |
| 41 | + |
| 42 | + def _snap_layer(self, display_grid, scale, filename): |
| 43 | + fig, ax = plt.subplots(figsize=(int(scale * display_grid.shape[1]), int(scale * display_grid.shape[0]))) |
| 44 | + ax.grid(False) |
| 45 | + ax.imshow(display_grid, aspect='auto') |
| 46 | + fig.savefig(filename + '.png', transparent=True) |
| 47 | + plt.close() |
| 48 | + return |
| 49 | + |
| 50 | + |
| 51 | + def render(self, X=None, filename='conv_filters'): |
| 52 | + """ |
| 53 | + Render visualization of a Convolutional keras model |
| 54 | +
|
| 55 | + Parameters: |
| 56 | + X : ndarray |
| 57 | + input to a Keras model |
| 58 | + filename : str |
| 59 | + name of file to which visualization will be saved |
| 60 | +
|
| 61 | + Returns: |
| 62 | + None |
| 63 | + """ |
| 64 | + |
| 65 | + layer_outputs = [layer.output for layer in self.model.layers] |
| 66 | + # Creates a model that will return these outputs, given the model input |
| 67 | + activation_model = models.Model(inputs=self.model.input, outputs=layer_outputs) |
| 68 | + images_per_row = 8 |
| 69 | + |
| 70 | + for j in range(len(X)): |
| 71 | + activations = activation_model.predict(X[j]) |
| 72 | + |
| 73 | + for i in range(len(activations)): |
| 74 | + # Ignore non-conv2d layers |
| 75 | + layer_name = self.model.layers[i].name |
| 76 | + if not layer_name.startswith("conv2d"): |
| 77 | + continue |
| 78 | + |
| 79 | + # Number of features in the feature map |
| 80 | + n_features = activations[i].shape[-1] |
| 81 | + # The feature map has shape (1, size, size, n_features). |
| 82 | + size = activations[i].shape[1] |
| 83 | + # Tiles the activation channels in this matrix |
| 84 | + n_cols = n_features // images_per_row |
| 85 | + display_grid = np.zeros((size * n_cols, images_per_row * size)) |
| 86 | + # Tiles each filter into a big horizontal grid |
| 87 | + for col in range(n_cols): |
| 88 | + for row in range(images_per_row): |
| 89 | + # Displays the grid |
| 90 | + display_grid[ |
| 91 | + col * size: (col + 1) * size, |
| 92 | + row * size: (row + 1) * size] = activations[i][0, :, :, col * images_per_row + row] |
| 93 | + |
| 94 | + self._snap_layer(display_grid, 1. / size, filename + "_" + str(j) + "_" + layer_name) |
| 95 | + |
| 96 | + return |
0 commit comments