当前位置: 首页 > news >正文

模型中间部分的卷积可视化

整体代码如下:

    def forward(self, x):x = self.conv1(x)x1 = xout_img3 = x1.squeeze()print(out_img3.shape)print("经过第一个卷积之后的输出:",x.shape)
import yaml
from omegaconf import OmegaConf
from pathlib import Path
from PIL import Image
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
# 加载图像
img_path = "E:/fasterrcnn/input_images/cat_dog.png"
image = Image.open(img_path)
transform = transforms.ToTensor()
tensor_image = transform(image)
transform_size = transforms.Resize((512,512))
out = transform_size(tensor_image)
tensor_image_batch = out.unsqueeze(0)yaml_file_path = 'F:/code/DEKR-main/experiments/coco/w32/w32_4x_reg03_bs10_512_adam_lr1e-3_coco_x140.yaml'
yaml_file = Path(yaml_file_path)
if yaml_file.exists():with yaml_file.open('r') as file:cfg_dict = yaml.safe_load(file)cfg = OmegaConf.create(cfg_dict)out_img3 = PoseHigherResolutionNet(cfg)input = torch.randn(1, 3, 512, 512)out_img3 = out_img3.forward(tensor_image_batch)# 选择要可视化的通道索引selected_channels = [0, 15, 30, 45, 63]  # 例如,选择第0、15、30、45和63个通道# 创建一个包含多个子图的图形fig, axes = plt.subplots(1, len(selected_channels), figsize=(15, 5))# 遍历选定的通道并绘制它们for i, ax in enumerate(axes):channel_image = out_img3[selected_channels[i]]  # 提取选定通道的图像print(channel_image.shape)out_img1 = transforms.ToPILImage()(channel_image)ax.imshow(out_img1, cmap='viridis')  # 使用 viridis 颜色图显示图像ax.set_title(f'Channel {selected_channels[i]}')  # 设置子图的标题ax.axis('off')  # 关闭坐标轴# 显示图形plt.tight_layout()  # 调整子图参数, 使之填充整个图像区域plt.show()# print(pose.forward(input))else:print(f"Error: The YAML file {yaml_file_path} does not exist.")

首先加载图像

img_path = "E:/fasterrcnn/input_images/cat_dog.png"
image = Image.open(img_path)

然后将图像转换为tensor,也就是数组的形式

transform = transforms.ToTensor()
tensor_image = transform(image)

之后resize大小为(3,512,512)的形式

transform_size = transforms.Resize((512,512))
out = transform_size(tensor_image)

到了这里还不能作为模型的输入,模型的输入是四维的(batch_size,input_channel,width,height)

所以要加一个维度

tensor_image_batch = out.unsqueeze(0)

调用模型

out_img3 = PoseHigherResolutionNet(cfg)input = torch.randn(1, 3, 512, 512)out_img3 = out_img3.forward(tensor_image_batch)

我们取前向传播的第一个卷积输出,输出的大小为(1,64,112,112),将输出x定义为x1,并用squeeze将其batch_size去掉变成(64,112,112)

    def forward(self, x):x = self.conv1(x)x1 = xout_img3 = x1.squeeze()print(out_img3.shape)

 选择可视化的对应通道

# 选择要可视化的通道索引selected_channels = [0, 15, 30, 45, 63]  # 例如,选择第0、15、30、45和63个通道

创建一个包含多个通道子图的图形

# 创建一个包含多个子图的图形fig, axes = plt.subplots(1, len(selected_channels), figsize=(15, 5))

之后遍历通道并显示出来

# 遍历选定的通道并绘制它们for i, ax in enumerate(axes):channel_image = out_img3[selected_channels[i]]  # 提取选定通道的图像print(channel_image.shape)out_img1 = transforms.ToPILImage()(channel_image)ax.imshow(out_img1, cmap='viridis')  # 使用 viridis 颜色图显示图像ax.set_title(f'Channel {selected_channels[i]}')  # 设置子图的标题ax.axis('off')  # 关闭坐标轴

显示

# 显示图形plt.tight_layout()  # 调整子图参数, 使之填充整个图像区域plt.show()

让其封装到一些函数,方便调用

def figure_trans():# 加载图像并转换为数组的形式,调整大小img_path = "E:/fasterrcnn/input_images/cat_dog.png"image = Image.open(img_path)transform = transforms.ToTensor()tensor_image = transform(image)transform_size = transforms.Resize((512, 512))out = transform_size(tensor_image)tensor_image_batch = out.unsqueeze(0)return tensor_image_batchdef yml_plot():yaml_file_path = 'F:/code/DEKR-main/experiments/coco/w32/w32_4x_reg03_bs10_512_adam_lr1e-3_coco_x140.yaml'yaml_file = Path(yaml_file_path)tensor_image_batch = figure_trans()if yaml_file.exists():with yaml_file.open('r') as file:cfg_dict = yaml.safe_load(file)cfg = OmegaConf.create(cfg_dict)out_img3 = PoseHigherResolutionNet(cfg)# input = torch.randn(1, 3, 512, 512)out_img3 = out_img3.forward(tensor_image_batch)# 选择要可视化的通道索引selected_channels = [0, 15, 30, 45, 63]  # 例如,选择第0、15、30、45和63个通道# 创建一个包含多个子图的图形fig, axes = plt.subplots(1, len(selected_channels), figsize=(15, 5))# 遍历选定的通道并绘制它们for i, ax in enumerate(axes):channel_image = out_img3[selected_channels[i]]  # 提取选定通道的图像print(channel_image.shape)out_img1 = transforms.ToPILImage()(channel_image)ax.imshow(out_img1, cmap='viridis')  # 使用 viridis 颜色图显示图像ax.set_title(f'Channel {selected_channels[i]}')  # 设置子图的标题ax.axis('off')  # 关闭坐标轴# 显示图形plt.tight_layout()  # 调整子图参数, 使之填充整个图像区域plt.show()# print(pose.forward(input))else:print(f"Error: The YAML file {yaml_file_path} does not exist.")plt_figure = yml_plot()

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 轴承知识大全,详细介绍(附3D图纸免费下载)
  • 中秋节如何利用Python发送彩信
  • 国内外大模型汇总(包括科大星火、文心一言、通义千问、智普清言、华为大模型)
  • WPS中JS宏使用说明(持续优化...)
  • LSPosed 模块开发入门和踩的坑
  • MacBook air pro验机流程
  • STM32(一)简介
  • 【总结】CSS(SCSS) 不常用属性
  • 【高级编程】实用类详解(下)万字整理Java时间日期类 JDK8新日期
  • c++ linux——进程共享内存
  • 《JavaEE进阶》----3.<SpringBoot项目创建细节大全+打jar包运行>
  • 若依框架登录鉴权详解(动态路由)
  • 鸿蒙轻内核M核源码分析系列四 中断Hwi
  • Spring Boot 整合 Sentinel 实现流量控制
  • LabVIEW与Python联合图像处理
  • ----------
  • 2017届校招提前批面试回顾
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • JavaScript服务器推送技术之 WebSocket
  • js如何打印object对象
  • Making An Indicator With Pure CSS
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • MYSQL 的 IF 函数
  • Spring Boot快速入门(一):Hello Spring Boot
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • windows-nginx-https-本地配置
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 如何使用 JavaScript 解析 URL
  • 如何使用 OAuth 2.0 将 LinkedIn 集成入 iOS 应用
  • 山寨一个 Promise
  • 突破自己的技术思维
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • (27)4.8 习题课
  • (Java)【深基9.例1】选举学生会
  • (独孤九剑)--文件系统
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (一)80c52学习之旅-起始篇
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • (转)scrum常见工具列表
  • (转)原始图像数据和PDF中的图像数据
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .Family_物联网
  • .htaccess配置重写url引擎
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径
  • .net FrameWork简介,数组,枚举
  • .NET 中选择合适的文件打开模式(CreateNew, Create, Open, OpenOrCreate, Truncate, Append)
  • /etc/sudoer文件配置简析
  • @ConfigurationProperties注解对数据的自动封装
  • @kafkalistener消费不到消息_消息队列对战之RabbitMq 大战 kafka
  • [《百万宝贝》观后]To be or not to be?
  • [Algorithm][动态规划][01背包问题][目标和][最后一块石头的重量Ⅱ]详细讲解