import cv2
import numpy as np
from PIL import Image
# 加载示例图像
image_path = 'path/to/your/image.jpg'
image = Image.open(image_path).convert('RGB')
# 预处理图像,以符合模型的输入要求
preprocess = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_data = preprocess(image).unsqueeze(0) # 添加 batch 维度
def get_cam(feature_map, spatial_att):
# 获取权重
weights = spatial_att.view(spatial_att.size(0), -1).unsqueeze(-1).unsqueeze(-1)
# 将权重乘以特征图
cam = (weights * feature_map).sum(dim=1, keepdim=True)
# 使用ReLU函数保留正值部分
cam = F.relu(cam)
# 将CAM调整到0到1之间
cam = cam - cam.min()
cam = cam / cam.max()
# 将CAM调整到图像原始尺寸
cam = F.interpolate(cam, (224, 224), mode='bilinear', align_corners=False)
# 转换为numpy数组
cam = cam.detach().cpu().numpy()[0, 0]
return cam
# 获取模型输出和对应的中间特征图
output, feature_maps = model(input_data)
# 获取CAM
cam = get_cam(feature_maps[0], feature_maps[1])
# 将CAM叠加在原始图像上
original_image = input_data.detach().cpu().numpy()[0].transpose((1, 2, 0))
cam_heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET)
cam_on_image = np.float32(cam_heatmap) / 255 + original_image
cam_on_image = cam_on_image / np.max(cam_on_image)
# 显示CAM叠加的图像
plt.imshow(cam_on_image)
plt.axis('off')
plt.show()
标签:cam,image,打印,feature,transforms,CAM,numpy,注意力
From: https://www.cnblogs.com/ZarkY/p/17920265.html