MMROTATE 1.X特征图可视化(绘制Heat Map)

本文参考MMYOLO官方的特征图可视化教程,对MMROTATE相关算法进行特征图可视化

1. 新建featmap_vis_demo.py文件

在mmrotate项目文件夹下新建 featmap_vis_demo.py

# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import os
from typing import Sequenceimport mmcv
from mmdet.apis import inference_detector, init_detector
from mmengine import Config, DictAction
from mmengine.registry import init_default_scope
from mmengine.utils import ProgressBarfrom mmrotate.registry import VISUALIZERS
from mmrotate.utils.misc import auto_arrange_images, get_file_listdef parse_args():parser = argparse.ArgumentParser(description='Visualize feature map')parser.add_argument('img', help='Image path, include image file, dir and URL.')parser.add_argument('config', help='Config file')parser.add_argument('checkpoint', help='Checkpoint file')parser.add_argument('--out-dir', default='./output', help='Path to output file')parser.add_argument('--target-layers',default=['backbone'],nargs='+',type=str,help='The target layers to get feature map, if not set, the tool will ''specify the backbone')parser.add_argument('--preview-model',default=False,action='store_true',help='To preview all the model layers')parser.add_argument('--device', default='cuda:0', help='Device used for inference')parser.add_argument('--score-thr', type=float, default=0.3, help='Bbox score threshold')parser.add_argument('--show', action='store_true', help='Show the featmap results')parser.add_argument('--channel-reduction',default='select_max',help='Reduce multiple channels to a single channel')parser.add_argument('--topk',type=int,default=4,help='Select topk channel to show by the sum of each channel')parser.add_argument('--arrangement',nargs='+',type=int,default=[2, 2],help='The arrangement of featmap when channel_reduction is ''not None and topk > 0')parser.add_argument('--cfg-options',nargs='+',action=DictAction,help='override some settings in the used config, the key-value pair ''in xxx=yyy format will be merged into config file. If the value to ''be overwritten is a list, it should be like key="[a,b]" or key=a,b ''It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" ''Note that the quotation marks are necessary and that no white space ''is allowed.')args = parser.parse_args()return argsclass ActivationsWrapper:def __init__(self, model, target_layers):self.model = modelself.activations = []self.handles = []self.image = Nonefor target_layer in target_layers:self.handles.append(target_layer.register_forward_hook(self.save_activation))def save_activation(self, module, input, output):self.activations.append(output)def __call__(self, img_path):self.activations = []results = inference_detector(self.model, img_path)return results, self.activationsdef release(self):for handle in self.handles:handle.remove()def main():args = parse_args()cfg = Config.fromfile(args.config)if args.cfg_options is not None:cfg.merge_from_dict(args.cfg_options)init_default_scope(cfg.get('default_scope', 'mmyolo'))channel_reduction = args.channel_reductionif channel_reduction == 'None':channel_reduction = Noneassert len(args.arrangement) == 2model = init_detector(args.config, args.checkpoint, device=args.device)if not os.path.exists(args.out_dir) and not args.show:os.mkdir(args.out_dir)if args.preview_model:print(model)print('\n This flag is only show model, if you want to continue, ''please remove `--preview-model` to get the feature map.')returntarget_layers = []for target_layer in args.target_layers:try:target_layers.append(eval(f'model.{target_layer}'))except Exception as e:print(model)raise RuntimeError('layer does not exist', e)activations_wrapper = ActivationsWrapper(model, target_layers)# init visualizervisualizer = VISUALIZERS.build(model.cfg.visualizer)visualizer.dataset_meta = model.dataset_meta# get file listimage_list, source_type = get_file_list(args.img)progress_bar = ProgressBar(len(image_list))for image_path in image_list:result, featmaps = activations_wrapper(image_path)if not isinstance(featmaps, Sequence):featmaps = [featmaps]flatten_featmaps = []for featmap in featmaps:if isinstance(featmap, Sequence):flatten_featmaps.extend(featmap)else:flatten_featmaps.append(featmap)img = mmcv.imread(image_path)img = mmcv.imconvert(img, 'bgr', 'rgb')if source_type['is_dir']:filename = os.path.relpath(image_path, args.img).replace('/', '_')else:filename = os.path.basename(image_path)out_file = None if args.show else os.path.join(args.out_dir, filename)# show the resultsshown_imgs = []visualizer.add_datasample('result',img,data_sample=result,draw_gt=False,show=False,wait_time=0,out_file=None,pred_score_thr=args.score_thr)drawn_img = visualizer.get_image()for featmap in flatten_featmaps:shown_img = visualizer.draw_featmap(featmap[0],drawn_img,channel_reduction=channel_reduction,topk=args.topk,arrangement=args.arrangement)shown_imgs.append(shown_img)shown_imgs = auto_arrange_images(shown_imgs)progress_bar.update()if out_file:mmcv.imwrite(shown_imgs[..., ::-1], out_file)print(f'{out_file} has been saved')if args.show:visualizer.show(shown_imgs)if not args.show:print(f'All done!'f'\nResults have been saved at {os.path.abspath(args.out_dir)}')# Please refer to the usage tutorial:
# https://github.com/open-mmlab/mmyolo/blob/main/docs/zh_cn/user_guides/visualization.md # noqa
if __name__ == '__main__':main()

2. 修改或替换mmrotate的misc.py文件

mmrotate-1.x/mmrotate/utils/misc.py , 删除里面的内容,填入以下内容:
在这里插入图片描述

# Copyright (c) OpenMMLab. All rights reserved.
from typing import Unionfrom mmengine.config import Config, ConfigDict# Copyright (c) OpenMMLab. All rights reserved.
import os
import urllibimport numpy as np
import torch
from mmengine.utils import scandirIMG_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif','.tiff', '.webp')def get_test_pipeline_cfg(cfg: Union[str, ConfigDict]) -> ConfigDict:"""Get the test dataset pipeline from entire config.Args:cfg (str or :obj:`ConfigDict`): the entire config. Can be a configfile or a ``ConfigDict``.Returns::obj:`ConfigDict`: the config of test dataset."""if isinstance(cfg, str):cfg = Config.fromfile(cfg)def _get_test_pipeline_cfg(dataset_cfg):if 'pipeline' in dataset_cfg:return dataset_cfg.pipeline# handle dataset wrapperelif 'dataset' in dataset_cfg:return _get_test_pipeline_cfg(dataset_cfg.dataset)# handle dataset wrappers like ConcatDatasetelif 'datasets' in dataset_cfg:return _get_test_pipeline_cfg(dataset_cfg.datasets[0])raise RuntimeError('Cannot find `pipeline` in `test_dataloader`')return _get_test_pipeline_cfg(cfg.test_dataloader.dataset)def auto_arrange_images(image_list: list, image_column: int = 2) -> np.ndarray:"""Auto arrange image to image_column x N row.Args:image_list (list): cv2 image list.image_column (int): Arrange to N column. Default: 2.Return:(np.ndarray): image_column x N row merge image"""img_count = len(image_list)if img_count <= image_column:# no need to arrangeimage_show = np.concatenate(image_list, axis=1)else:# arrange image according to image_columnimage_row = round(img_count / image_column)fill_img_list = [np.ones(image_list[0].shape, dtype=np.uint8) * 255] * (image_row * image_column - img_count)image_list.extend(fill_img_list)merge_imgs_col = []for i in range(image_row):start_col = image_column * iend_col = image_column * (i + 1)merge_col = np.hstack(image_list[start_col:end_col])merge_imgs_col.append(merge_col)# merge to one imageimage_show = np.vstack(merge_imgs_col)return image_showdef get_file_list(source_root: str) -> [list, dict]:"""Get file list.Args:source_root (str): image or video source pathReturn:source_file_path_list (list): A list for all source file.source_type (dict): Source type: file or url or dir."""is_dir = os.path.isdir(source_root)is_url = source_root.startswith(('http:/', 'https:/'))is_file = os.path.splitext(source_root)[-1].lower() in IMG_EXTENSIONSsource_file_path_list = []if is_dir:# when input source is dirfor file in scandir(source_root, IMG_EXTENSIONS, recursive=True,case_sensitive=False):source_file_path_list.append(os.path.join(source_root, file))elif is_url:# when input source is urlfilename = os.path.basename(urllib.parse.unquote(source_root).split('?')[0])file_save_path = os.path.join(os.getcwd(), filename)print(f'Downloading source file to {file_save_path}')torch.hub.download_url_to_file(source_root, file_save_path)source_file_path_list = [file_save_path]elif is_file:# when input source is single imagesource_file_path_list = [source_root]else:print('Cannot find image file.')source_type = dict(is_dir=is_dir, is_url=is_url, is_file=is_file)return source_file_path_list, source_type

3. 输出热力图

首先要配置好并且切换到mmrotate虚拟环境,然后运行以下命令:

这里是引用

python featmap_vis_demo.py <path to your photo> \<path to your config file> \<path to your weight file> \--target-layers <想要输出特征图的位置 backbone or neck ...> \--channel-reduction select_max \--out-dir '<path to your output dir>'

具体的例子为:

python featmap_vis_demo.py demo/heatMap.png \configs/rotated_rtmdet_tiny-9x-hrsc.py \weights/155647.pth \--target-layers neck \--channel-reduction select_max \--out-dir 'output'

然后就可以在output文件夹中看到输出的热力图:
在这里插入图片描述

参考MMYOLO教程:

玩转 MMYOLO 之工具篇(一):特征图可视化

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/148167.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

“吉林一号”宽幅02B系列卫星

离轴四反光学成像系统 1.光学系统参数&#xff1a; 焦距&#xff1a;77.5mm&#xff1b; F/#&#xff1a;7.4&#xff1b; 视场&#xff1a;≥56゜&#xff1b; 光谱范围&#xff1a;400nm&#xff5e;1000nm。 2.说明&#xff1a; 光学系统采用离轴全反射式结构&#xff0c;整…

CIM平台应用的一些思考

今天中午看了一些书&#xff0c;讲致幻剂在疾病治疗的一些作用。致幻剂包括毒蘑菇等这类东西&#xff0c;有精神疾病或其他心里疾病的患者在吸食或注射&#xff08;专业医生的指导下&#xff09;会忘记或减轻身体的痛苦&#xff0c;忘记死亡&#xff0c;相信永生&#xff0c;治…

VMWare虚拟机安装CentOS-7-x86_64-DVD-1611操作系统

VMWare虚拟机安装CentOS7操作系统 1、虚拟机准备打开VMware单机创建新的虚拟机典型与自定义安装选择虚拟机硬件兼容性选择安装程序光盘映射文件(iso)选择客户机操作系统命名虚拟机处理器配置内存配置网络连接类型I/O类型选择磁盘类型选择磁盘指定磁盘的最大磁盘大小磁盘名称我们…

高中数学:立体几何-平面的定义与公理

文章目录 一、平面定义及画法1、定义2、表示方法 二、公理1、公理12、公理23、公理3 一、平面定义及画法 1、定义 平面是向四周无限延展的。 2、表示方法 我们常用矩形的直观图&#xff0c;即平行四边形表示平面&#xff0e; 我们常用希腊字母α&#xff0c;β&#xff0c…

八. 实战:CUDA-BEVFusion部署分析-coordTrans Precomputation

目录 前言0. 简述1. 案例运行2. coordTrans3. Precomputation总结下载链接参考 前言 自动驾驶之心推出的 《CUDA与TensorRT部署实战课程》&#xff0c;链接。记录下个人学习笔记&#xff0c;仅供自己参考 本次课程我们来学习下课程第八章—实战&#xff1a;CUDA-BEVFusion部署分…

『正版软件』XYplorer 专业的 Windows 文件管理工具软件

在数字化时代&#xff0c;我们每天都在与各种文件打交道。无论是工作文档、个人照片还是多媒体资料&#xff0c;管理这些文件的效率直接关系到我们的工作效率和生活体验。今天&#xff0c;我要向大家推荐一款功能强大、操作简便的文件管理软件 —— XYplorer。 XYplorer&#x…

【小bug】使用 RestTemplate 工具从 JSON 数据反序列化为 Java 对象时报类型转换异常

起因&#xff1a;今天编写一个请求时需要通过RestTemplate调用外部接口&#xff0c;获取一些信息&#xff0c;但是在获取了外部接口响应内容后&#xff0c;使用强制转换发现报了类型转换异常。之前也遇到过&#xff0c;但是没记录下来&#xff0c;今天又查了一遍……干脆记录一…

【hot100-java】【搜索旋转排序数组】

R8-二分查找篇 等等&#xff0c;不是&#xff1f;O(logn)&#xff1f;那岂不是一次遍历数组都超时了hh. 二分查找分类 class Solution {public int search(int[] nums, int target) {if (numsnull||nums.length0){return -1;}int start0;int endnums.length-1;int mid;while …

以太坊客户端Geth的介绍与搭建

一、以太坊客户端 1.介绍 以太坊客户端是指用于连接、交互和参与以太坊区块链网络的软件。以太坊客户端允许用户执行各种操作&#xff0c;如发送交易、挖矿、部署智能合约、同步区块链数据等。 2.功能 区块链同步&#xff1a;客户端会下载并验证以太坊区块链的所有区块&…

windows 出现身份验证错误,要求的函数不受支持

现象环境&#xff1a; win10 mstsc内网远程server2016&#xff0c;出现错误代码&#xff1a; 远程桌面连接出现身份验证错误。要求的函数不受支持。这可能是由于CredSSP加密数据库修正 出现身份验证错误 原因&#xff1a; 系统更新&#xff0c;微软系统补丁的更新将 Cred…

【Godot4.3】2D程序生成植物概论

概述 Godot的2D程序化植物生成是我一直想要探讨的一个内容&#xff0c;但是一直没有真正开动&#xff0c;在刚过去的中秋节假期期间&#xff0c;在老家无聊&#xff0c;在一个素描本上构思了一系列想法。本篇就基于这些自己的想法介绍一下程序化植物生成的基本思路。不一定对&…

c++ 找到给定点集的简单闭合路径(Find Simple Closed Path for a given set of points)

给定一组点&#xff0c;将这些点连接起来而不相交 例子&#xff1a; 输入&#xff1a;points[] {(0, 3), (1, 1), (2, 2), (4, 4), (0, 0), (1, 2), (3, 1}, {3, 3}}; 输出&#xff1a;按以下顺序连接点将 不造成任何交叉 {(0, 0), (3, …

如何在Markdown写文章上传到wordpress保证图片不丢失

如何在Markdown写文章上传到wordpress保证图片不丢失 写文日期,2023-11-16 引文 众所周知markdown是一款nb的笔记软件&#xff0c;本篇文章讲解如何在markdown编写文件后上传至wordpress论坛。并且保证图片不丢失&#xff08;将图片上传至云端而非本地方法&#xff09; 一&…

offsetX、offsetY...

文章目录 offsetX & offsetYclientX & clientYpageX & pageYscreenX & screenYinnerHeight & innerWidthoffsetHeight & offsetWidthoffsetTop & offsetLeftscrollHeight & scrollWidthscrollTop & scrollLeft:与scrollHeight和scrollWidt…

二分查找算法(3) _x的平方根

个人主页&#xff1a;C忠实粉丝 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 C忠实粉丝 原创 二分查找算法(3) _x的平方根 收录于专栏【经典算法练习】 本专栏旨在分享学习算法的一点学习笔记&#xff0c;欢迎大家在评论区交流讨论&#x1f48c; 目录 温馨…

《深度学习》—— 卷积神经网络(CNN)的简单介绍和工作原理

文章目录 一、卷积神经网络的简单介绍二、工作原理(还未写完)1.输入层2.卷积层3.池化层4.全连接层5.输出层 一、卷积神经网络的简单介绍 基本概念 定义&#xff1a;卷积神经网络是一种深度学习模型&#xff0c;通常用于图像、视频、语音等信号数据的分类和识别任务。其核心思想…

MIPI协议

MIPI协议 MIPI介绍协议层显示接口&#xff08;Display&#xff09;DBI&#xff08;Display Bus Interface&#xff09;并行传输串行传输 DPI&#xff08;Display Pixel Interface&#xff0c;也称RGB接口&#xff09;DSI&#xff08;Display Serial Interface&#xff09;Appli…

关于使用低版本EPLAN打开高版本项目中的一些常见问题!

目录 1 前言 2 操作方法 2.1 经典版本 2.2 新版本 3 遇到的问题 4 结语 1 前言 在用EPLAN设计图纸时&#xff0c;经常要遇到使用低版本EPLAN打开高版本EPLAN的项目。 EPLAN软件根据操作界面的不同可以分为两种: 1&#xff0c;EPLAN 2.x版本&#xff0c;比如经典的2.9和2…

【Python报错已解决】TypeError: ‘<‘ not supported between instances of ‘str‘ and ‘int‘

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《C干货基地》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 专栏介绍 在软件开发和日常使用中&#xff0c;BUG是不可避免的。本专栏致力于为广大开发者和技术爱好者提供一个关于BUG解决的经…

从规范到实现解读Windows平台如何播放RTSP流

RTSP播放器应用场景 RTSP播放器在视频监控、远程视频会议、网络电视、实时流媒体传输、协同操控相关的智能设备、教育培训以及企业内部通讯与协作等多个领域都有着广泛的应用场景。 1. 视频监控 RTSP直播播放器在视频监控系统中扮演着重要角色。通过RTSP协议&#xff0c;播放…