标签:map img 力图 feature heatmap mmdetection import model 绘制
#coding: utf-8 import cv2 import matplotlib.pyplot as plt import mmcv import numpy as np import os import torch import torch.nn as nn import warnings from mmcv.ops import RoIAlign, RoIPool from mmcv.parallel import collate, scatter from mmcv.runner import load_checkpoint from mmdet.apis import inference_detector, init_detector from mmdet.core import get_classes from mmdet.datasets.pipelines import Compose from mmdet.models import build_detector from mmdet.models.dense_heads import * def featuremap_2_heatmap(feature_map): assert isinstance(feature_map, torch.Tensor) feature_map = feature_map.detach() heatmap = feature_map[:,0,:,:]*0 for c in range(feature_map.shape[1]): heatmap+=feature_map[:,c,:,:] heatmap = heatmap.cpu().numpy() heatmap = np.mean(heatmap, axis=0) heatmap = np.maximum(heatmap, 0) heatmap /= np.max(heatmap) return heatmap def draw_feature_map(model, img_path, save_dir): ''' :param model: 加载了参数的模型 :param img_path: 测试图像的文件路径 :param save_dir: 保存生成图像的文件夹 :return: ''' img = mmcv.imread(img_path) modeltype = str(type(model)).split('.')[-1].split('\'')[0] model.eval() model.draw_heatmap = True featuremaps = inference_detector(model, img) i=0 for featuremap in featuremaps: heatmap = featuremap_2_heatmap(featuremap) heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0])) # 将热力图的大小调整为与原始图像相同 heatmap = np.uint8(255 * heatmap) # 将热力图转换为RGB格式 heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) # 将热力图应用于原始图像 superimposed_img = heatmap * 0.4 + img # 这里的0.4是热力图强度因子 cv2.imwrite(os.path.join(save_dir,'featuremap_'+str(i)+'.png'), superimposed_img) # 将图像保存到硬盘 i=i+1 from argparse import ArgumentParser def main(): parser = ArgumentParser() parser.add_argument('img', help='Image file') parser.add_argument('save_dir', help='Dir to save heatmap') parser.add_argument('config', help='Config file') parser.add_argument('checkpoint', help='Checkpoint file') parser.add_argument('--device', default='cuda:0', help='Device used for inference') args = parser.parse_args() # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) draw_feature_map(model,args.img,args.save_dir) if __name__ == '__main__': main()
用例:
标签:map,img,力图,feature,heatmap,mmdetection,import,model,绘制 来源: https://www.cnblogs.com/aimhabo/p/13770395.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。