[파이썬] fastai 가중치 및 활성화 시각화

Understanding and visualizing the weights and activations of neural networks can provide valuable insights into how a model is learning and making predictions. In this blog post, we’ll explore how to visualize the weights and activations of a fastai model in Python.

Prerequisites

Before getting started, make sure you have the following dependencies installed:

Loading a Pretrained Model

To visualize the weights and activations, we first need to load a pretrained model. Fastai provides easy-to-use methods for loading popular pretrained models such as ResNet and VGG. For this example, let’s load a pretrained ResNet model.

from fastai.vision.all import *

# Load the pretrained ResNet model
model = cnn_learner(dls, resnet34, pretrained=True, metrics=accuracy)

Visualizing Weights

Weights are the learnable parameters of a neural network that determine how inputs are transformed in each layer. Visualizing the weights can provide insights into what the model is learning. We can visualize the weights of a particular layer using the show method.

# Visualize the weights of the first convolutional layer
model.model[0][0].show()

This will display a grid of images corresponding to the learned filters of the first convolutional layer.

Visualizing Activations

Activations represent the output of each layer in a neural network. Visualizing the activations can help understand which features are being detected by different layers. We can visualize the activations of a particular layer using the hook_output method provided by fastai.

# Define a hook to extract activations from a specific layer
layer_idx = 3
hook_output = model.model[layer_idx].register_forward_hook(lambda module, input, output: output)

# Pass a sample input through the model to extract activations
sample_input = torch.randn(1, 3, 224, 224)  # Replace with your own input dimensions
_ = model.model.eval()(sample_input)

# Visualize the extracted activations
activations = hook_output.stored[0]
plt.imshow(activations[0][0].detach().cpu(), cmap='viridis')

This will display a heatmap of activations for the first sample image in the input.

Conclusion

Visualizing the weights and activations of a fastai model can provide valuable insights into its learning process. By understanding what features are being learned and how they are being transformed, we can gain a better understanding of our model’s performance and make informed decisions for further improvement.

Remember to experiment with different layers and analyze multiple samples to get a comprehensive understanding of the model’s behavior. Happy visualizing!