Feature map visualization helps us see what a neural network learns inside. It shows how the network looks at parts of an image.
Feature map visualization in 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.
layer_index = 1 activation_model = tf.keras.models.Model(inputs=model.input, outputs=model.layers[layer_index].output) activations = activation_model.predict(input_image)
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)
This code builds a small CNN, creates a random image, extracts feature maps from the first convolutional layer, and shows them as images.
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}")
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.
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.
Practice
Solution
Step 1: Understand feature maps in CNNs
Feature maps are outputs of convolutional layers showing detected features like edges or textures.Step 2: Purpose of visualization
Visualizing feature maps helps us see what the CNN focuses on in the image, improving model interpretability.Final Answer:
To understand what patterns or features the CNN layers detect in the input image -> Option BQuick Check:
Feature map visualization = Understand CNN features [OK]
- Thinking visualization speeds up training
- Confusing feature maps with input resizing
- Believing visualization changes activation functions
Solution
Step 1: Identify correct Model creation syntax
To get intermediate outputs, create a new Model with original inputs and desired layer outputs.Step 2: Check each option
feature_map_model = tf.keras.Model(inputs=model.input, outputs=model.get_layer('conv1').output) correctly uses model.input and conv1 output. Others misuse inputs/outputs or use Sequential incorrectly.Final Answer:
feature_map_model = tf.keras.Model(inputs=model.input, outputs=model.get_layer('conv1').output) -> Option CQuick Check:
New model inputs = original inputs, outputs = layer output [OK]
- Using model.output as input
- Passing layer input as output
- Using Sequential instead of Model for intermediate outputs
feature_maps if the input image shape is (1, 64, 64, 3) and the 'conv1' layer outputs 32 filters of size 62x62?feature_map_model = tf.keras.Model(inputs=model.input, outputs=model.get_layer('conv1').output)
feature_maps = feature_map_model.predict(input_image)Solution
Step 1: Understand input and output shapes
Input batch size is 1, image size 64x64 with 3 channels. conv1 outputs 32 filters of size 62x62.Step 2: Predict output shape
Output shape includes batch size, height, width, and number of filters: (1, 62, 62, 32).Final Answer:
(1, 62, 62, 32) -> Option DQuick Check:
Output shape = (batch, height, width, filters) [OK]
- Ignoring batch dimension
- Confusing input shape with output shape
- Mixing filter count with spatial dimensions
feature_map_model = tf.keras.Model(inputs=model.input, outputs=model.get_layer('conv1').output)
feature_maps = feature_map_model.predict(input_image)
plt.imshow(feature_maps)What is the likely cause of the error?
Solution
Step 1: Analyze the error cause
feature_maps is a 4D array (batch, height, width, filters), but plt.imshow expects 2D or 3D image data.Step 2: Correct visualization approach
You must select one feature map (e.g., feature_maps[0, :, :, 0]) before plotting with plt.imshow.Final Answer:
Trying to plot a 4D tensor directly with plt.imshow -> Option AQuick Check:
Plot single 2D feature map slice, not full 4D tensor [OK]
- Plotting entire feature_maps array directly
- Ignoring batch dimension when plotting
- Assuming get_layer error without checking data shape
Solution
Step 1: Understand multi-output model creation
To get outputs from multiple layers, pass a list of their outputs to the Model constructor.Step 2: Evaluate options
feature_map_model = tf.keras.Model(inputs=model.input, outputs=[model.get_layer(name).output for name in ['conv1', 'conv2', 'conv3']]) correctly creates a model with inputs as original inputs and outputs as a list of layer outputs. Others misuse Sequential or inputs/outputs.Final Answer:
feature_map_model = tf.keras.Model(inputs=model.input, outputs=[model.get_layer(name).output for name in ['conv1', 'conv2', 'conv3']]) -> Option AQuick Check:
Use list of outputs for multiple feature maps [OK]
- Using Sequential instead of Model for intermediate outputs
- Passing model.output as inputs
- Using layer inputs as outputs
Ready to prove it?
Pass the test and this pattern moves unknown
