import matplotlib.pyplot as plt
import torch
from PIL import Image
import numpy as np
import sys
sys.path.append("..")
from torchvision import transforms,models
from torch import nn
# 对于给定的一个网络层的输出x,x为numpy格式的array,维度为[0, channels, width, height]
# %matplotlib inline
1.需要的包
def draw_features(width, height, channels,x,savename):
'''
x: 输入的array,某一层的网络层输出
savename: 特征可视化的保存路径
width, height: 分别表示可视化子图的个数,二者乘积等于channels
'''
fig = plt.figure(figsize=(32,32))
fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95, wspace=0.05, hspace=0.05)
for i in range(channels):
plt.subplot(height,width, i + 1)
plt.axis('off')
img = x[0, i, :, :]
pmin = np.min(img)
pmax = np.max(img)
img = (img - pmin) / (pmax - pmin + 0.000001)