'Google Colab Session Crash After Using All Available Ram - Better method for viewing every channel in every intermediate activation in my CNN model?
For a 150 X 150 X 3 image, I want to visualise the intermediate activation for every channel in my image in my CNN model. I have this code:
import glob
import matplotlib
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import imageio as im
from keras import models
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
images = []
for img_path in glob.glob('pic_00002314*.JPEG'):
image1 = mpimg.imread(img_path)
open_file = image1 / 255
resize = cv2.resize(open_file,(150,150))
print(resize.shape)
images.append(resize)
plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
plt.subplot(len(images) / columns + 1, columns, i + 1)
plt.imshow(image)
layer_outputs = [layer.output for layer in model.layers[:]]
activation_model = tf.keras.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(tf.random.normal((1, 150, 150, 3)))
#activations = activation_model.predict(np.expand_dims(images[0]), axis=0) #commented out as a test because I was getting an error that axis 0 here didn't make sense
layer_names = []
for layer in model.layers[:12]:
layer_names.append(layer.name)
images_per_row = 16
for layer_name, layer_activation in zip(layer_names, activations):
n_features = layer_activation.shape[-1]
size = layer_activation.shape[1]
n_cols = n_features // images_per_row
display_grid = np.zeros((size * n_cols, images_per_row * size))
for col in range(n_cols):
for row in range(images_per_row):
channel_image = layer_activation[0,:, :,col * images_per_row + row]
channel_image -= channel_image.mean() 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')
But when I try to run it each time, it quickly crashes saying that it's using all available RAM on google colab. Would people expect this for this code and image size? Does anyone know of alternative code that achieves the same effect/ideas for how this code could be optimized with lower RAM requirements?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
