0
$\begingroup$

I am using this code in Python:

import matplotlib.pyplot as plt
import tensorflow as tf
from tf_keras_vis.activation_maximization import ActivationMaximization

def get_conv(in_model):
    target_layer = in_model.get_layer(name="block5_conv3")
    new_model = tf.keras.Model(inputs=in_model.inputs, outputs=target_layer.output)
    new_model.layers[-1].activation = tf.keras.activations.linear
    return new_model

def get_score(output):
    outputs = [o[0] for o in output]
    return outputs

model = tf.keras.applications.vgg16.VGG16()
vis = ActivationMaximization(model, get_conv)
seed = tf.random.uniform((1, *model.input.shape[1:]), 0, 255)
maps = vis(get_score, seed)
plt.imshow(maps[0])
plt.show()

And this is what I get:

enter image description here

I was wondering why there is a huge flat image region in the bottom half of the image.

$\endgroup$

1 Answer 1

1
$\begingroup$

I'm answering this myself as it may be helpful to others.

The error is in this line:

outputs = [o[0] for o in output]

I had copied and pasted this from dense-layer visualization code, where it is correct, selecting class 0 for visualization. Here, it is not wrong, but what it does is select the top row of that conv layer across all filters (and not the first filter of that layer). Hence the many small-scale features near the top row, more large-scale features near the middle, and nothing more near the bottom. I believe this may nicely visualize the perceptive field of the network.

For the record,

outputs = [o[:, :, 0] for o in output]

yields expected output.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.