Feature map visualization helps us see what a neural network learns inside. It shows how the network looks at parts of an image.
0
0
Feature map visualization in TensorFlow
Introduction
You want to understand what parts of an image a convolutional neural network focuses on.
You want to check if your model is learning useful features during training.
You want to explain your model's decisions to others by showing visual patterns.
You want to debug your model by seeing if feature maps look noisy or empty.
Syntax
TensorFlow
feature_maps = model.layers[layer_index].output
activation_model = tf.keras.models.Model(inputs=model.input, outputs=feature_maps)
activations = activation_model.predict(input_image)Replace layer_index with the index of the convolutional layer you want to visualize.
input_image must be preprocessed and shaped correctly for the model.
Examples
This gets the feature maps from the second layer of the model.
TensorFlow
layer_index = 1 activation_model = tf.keras.models.Model(inputs=model.input, outputs=model.layers[layer_index].output) activations = activation_model.predict(input_image)
This gets feature maps by layer name instead of index.
TensorFlow
layer_name = 'conv2d' activation_model = tf.keras.models.Model(inputs=model.input, outputs=model.get_layer(layer_name).output) activations = activation_model.predict(input_image)
Sample Model
This code builds a small CNN, creates a random image, extracts feature maps from the first convolutional layer, and shows them as images.
TensorFlow
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # Load a simple CNN model model = tf.keras.Sequential([ tf.keras.layers.InputLayer(input_shape=(28, 28, 1)), tf.keras.layers.Conv2D(8, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D((2,2)), tf.keras.layers.Conv2D(16, (3,3), activation='relu') ]) # Create a random input image (like a grayscale 28x28 image) input_image = np.random.rand(1, 28, 28, 1).astype(np.float32) # Choose the first Conv2D layer to visualize layer_index = 0 activation_model = tf.keras.models.Model(inputs=model.input, outputs=model.layers[layer_index].output) # Get feature maps activations = activation_model.predict(input_image) # Plot the feature maps num_features = activations.shape[-1] fig, axes = plt.subplots(1, num_features, figsize=(num_features*2, 2)) for i in range(num_features): ax = axes[i] ax.imshow(activations[0, :, :, i], cmap='viridis') ax.axis('off') plt.show() print(f"Feature maps shape: {activations.shape}")
OutputSuccess
Important Notes
Feature maps show how each filter responds to the input image.
Visualizing feature maps helps you understand what the model 'sees' at each layer.
Use matplotlib or similar to display feature maps as images.
Summary
Feature map visualization shows internal patterns learned by convolutional layers.
It helps understand and debug CNN models by visualizing filter outputs.
Use a model that outputs intermediate layer results and plot them as images.