I wrote this model:
And I fit it to some data:
I can read in the model from the Network Monitoring tools:
For an image, I want to visualise every channel in every intermediate activation for the model.
I can read in the image:
To visualise the activations, I wrote:
The output prints:
And then an error that says:
Followed by some images.
Could someone explain what that error is saying and/or how to fix it? I'm not understanding how I get an error, but then it prints some intermediate activations images, so then I'm not sure I've done this properly?
Code Select
def create_network():
model = Sequential()
model.add(Input(shape=(150,150,3)))
model.add(Conv2D(32, kernel_size=3,strides=(1, 1),activation='relu',kernel_initializer="glorot_uniform", padding='valid', dilation_rate=1))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(64, kernel_size=3, strides=(1, 1), activation='relu',kernel_initializer="glorot_uniform", padding='valid', dilation_rate=1))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(128, kernel_size=3, strides=(1, 1), activation='relu',kernel_initializer="glorot_uniform", padding='valid', dilation_rate=1))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(128, kernel_size=3, strides=(1, 1), activation='relu',kernel_initializer="glorot_uniform",padding='valid', dilation_rate=1))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer = 'adam',
loss = 'binary_crossentropy',
metrics = ['accuracy'])
return model
create_network()
And I fit it to some data:
Code Select
def fit_model(train_generator=train_generator, validation_generator=validation_generator,network=create_network()):
checkpoint_path = "/content/drive/model_checkpoint.h5"
checkpoint_dir = os.path.dirname(checkpoint_path)
callbacks_list = [
callbacks.EarlyStopping(
monitor = 'accuracy',
patience = 2,
),
callbacks.ModelCheckpoint(
filepath=checkpoint_path,
monitor = 'val_loss',
#save_weights_only=True,
save_best_only=True,
),
]
model = network
history = model.fit(train_generator,
epochs=1000,
validation_data=validation_generator,
callbacks = callbacks_list,
verbose=1
)
score = model.evaluate(validation_generator)
return history,model
history,model = fit_model(train_generator,validation_generator)
I can read in the model from the Network Monitoring tools:
Code Select
model = load_model('/content/drive/model_checkpoint.h5')
For an image, I want to visualise every channel in every intermediate activation for the model.
I can read in the image:
Code Select
from keras.preprocessing import image
file_list = ['/content/drive/image.JPEG']
file_list = file_list[0]
test_image = image.load_img(file_list,target_size=(150,150))
images = image.img_to_array(test_image)
images /= 255.0
images = np.expand_dims(images, axis=0)
To visualise the activations, I wrote:
Code Select
layer_outputs = [layer.output for layer in model.layers[:]]
activation_model = tf.keras.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(images)
for i in range(len(layer_outputs)):
first_layer_activation = activations[i]
print(first_layer_activation.shape)
plt.matshow(first_layer_activation[0, :, :, 1], cmap='viridis')
The output prints:
Code Select
(1, 148, 148, 32)
(1, 74, 74, 32)
(1, 74, 74, 32)
(1, 72, 72, 64)
(1, 36, 36, 64)
(1, 36, 36, 64)
(1, 34, 34, 128)
(1, 17, 17, 128)
(1, 17, 17, 128)
(1, 15, 15, 128)
(1, 7, 7, 128)
(1, 7, 7, 128)
(1, 6272)
And then an error that says:
Code Select
2 first_layer_activation = activations[i]
3 print(first_layer_activation.shape)
----> 4 plt.matshow(first_layer_activation[0, :, :, 1], cmap='viridis')
IndexError: too many indices for array: array is 2-dimensional, but 4 were indexed
Followed by some images.
Could someone explain what that error is saying and/or how to fix it? I'm not understanding how I get an error, but then it prints some intermediate activations images, so then I'm not sure I've done this properly?