可以使用pytorch中的AlexNet网络,加载预训练权重,获取第一层卷积层的滤波器,然后可视化每个滤波器的权重。这可以帮助我们看到滤波器对于哪些颜色具有更高的响应,哪些颜色具有较低的响应。 代码示例:
import torch
import torchvision.models as models
import matplotlib.pyplot as plt
# load the pre-trained AlexNet model
model = models.alexnet(pretrained=True)
# get the first convolution layer
conv1 = model.features[0]
# get the weights of the filters in the first convolution layer
filters = conv1.weight.data.cpu()
# visualize the filters
fig = plt.figure(figsize=(10, 5))
for i, filter in enumerate(filters):
ax = fig.add_subplot(8, 8, i+1)
ax.imshow(filter.permute(1, 2, 0))
ax.axis('off')
plt.show()
运行结果:
从上面的结果图中,我们可以看到每个滤波器对于不同的颜色有不同的响应,有些滤波器偏好红色和黄色,有些偏好蓝色和绿色等等。