输出的结果是两张特征图(可以实现跑一次看多层),代码如下。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import requests
import cv2
import torch
from torch import nn
import torch.nn.functional as F
from torchvision import models
from torchvision import transforms
from PIL import Image
from model_M7 import mobilenet_M7_large
import json
#导入预先训练好的网络
model = mobilenet_M7_large(num_classes=4)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model_weight_path = "M7/best_M7.pth" #修改自己的权重文件
model.load_state_dict(torch.load(model_weight_path))
# model = torch.load(model_weight_path, map_location=device)
model.eval()
print(model) #可以注释掉
#读取一张图片,对其可视化
im = Image.open("test_image/0.jpg")
imarray = np.asarray(im) / 255.0
# plt.figure()
# plt.imshow(imarray)
# plt.show()
#数据预处理
data_transforms = transforms.Compose([
transforms.Resize((224,224)),
transforms.ToTensor(),
transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])
])
input_im = data_transforms(im).unsqueeze(0)
print("input_im.shape:",input_im.shape)
#定义辅助函数,获取指定层名称的特征,定义钩子hook
activation = {}
def get_activation(name):
def hook(model,input,output):
activation[name] = output.detach()
return hook
#获取中间卷积后的图像特征
model.eval()
#获取网络中第6层,经过第一次注意力后的特征映射,第一张特征图。
model.features[6].register_forward_hook(get_activation("SqueezeExcitation"))
_ = model(input_im)
SqueezeExcitation = activation["SqueezeExcitation"]
print("获取特征的尺寸为:",SqueezeExcitation.shape)
plt.figure(figsize=(10, 4))
for ii in range(SqueezeExcitation.shape[1]):
# 可视化每张手写体
plt.subplot(4, 10, ii + 1)
plt.imshow(SqueezeExcitation.data.numpy()[0, ii, :, :], cmap="gray")
plt.axis("off")
plt.subplots_adjust(wspace=0.1, hspace=0.1)
plt.savefig("SqueezeExcitation6 map")
#获取更深层次的卷积后的图像特征
model.eval()
#获取网络中第14层,经过第二次注意力的特征映射,第二张特征图。
model.features[14].register_forward_hook(get_activation("SqueezeExcitation"))
_ = model(input_im)
SqueezeExcitation = activation["SqueezeExcitation"]
print("获取特征的尺寸为:",SqueezeExcitation.shape)
plt.figure(figsize=(10, 4))
for ii in range(40):
# 可视化每张手写体
plt.subplot(4, 10, ii + 1)
plt.imshow(SqueezeExcitation.data.numpy()[0, ii, :, :], cmap="gray")
plt.axis("off")
plt.subplots_adjust(wspace=0.1, hspace=0.1)
plt.savefig("SqueezeExcitation14 map")
plt.show()
第6层特征:
第14层特征:



