当前位置:网站首页>Pytorch uses hook to get feature map
Pytorch uses hook to get feature map
2022-07-28 05:37:00 【A little knowledge, a hundred Xiaosheng】
For more detailed suggestions, please refer to Use hook Get feature map visualization
Define the following two hook function , The feature map can be obtained in a few steps
fmap_block = dict() # loading feature map
grad_block = dict() # Loading gradient
def backward_hook(module, grad_in, grad_out):
grad_block['grad_in'] = grad_in
grad_block['grad_out'] = grad_out
def farward_hook(module, inp, outp):
fmap_block['input'] = inp
fmap_block['output'] = outp
import torch
from torch import nn
# Define the network
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(3,6,3,1,1)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(2,2)
self.conv2 = nn.Conv2d(6,9,3,1,1)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(2,2)
self.fc1 = nn.Linear(8*8*9, 120)
self.relu3 = nn.ReLU()
self.fc2 = nn.Linear(120,10)
def forward(self, x):
out = self.pool1(self.relu1(self.conv1(x)))
out = self.pool2(self.relu2(self.conv2(out)))
out = out.view(out.shape[0], -1)
out = self.relu3(self.fc1(out))
out = self.fc2(out)
return out
# Instantiate the network
net = LeNet()
# register hook
net.conv2.register_forward_hook(farward_hook)
Define input
input_img = torch.randn(1,3,32,32).requires_grad_()
fmap_block = dict() # loading feature map
outs = net(input_img)
print(len(fmap_block))
print(fmap_block["output"].shape)
边栏推荐
- 子父线程交互
- 科研论文写作方法:在方法部分添加分析和讨论说明自己的贡献和不同
- How to compare long and integer and why to report errors
- Framework step by step easy-to-use process
- Export excel, generate multiple sheet pages, and name them
- Mybats foreach multi select query, index loop, and cancel the and/or tag
- 【idea插件神器】教你如何使用IDEA一键set实体类中所有属性
- C language: some self realization of string functions
- oracle查看锁表语句、解锁方法
- 2021csdn blog star selection, mutual investment
猜你喜欢

Thinking on multi system architecture design

Video twins: the starting point of informatization upgrading of smart Parks

多线程进阶:volatile的作用以及实现原理

Framework step by step easy-to-use process
Printf function of input and output function in C language

BigDecimal 进行四舍五入 四舍六入和保留两位小数

架构设计思考之一(SSO设计)

IDEA配置 service(Run Dashboard) 服务,多模块同时启动

冶金物理化学复习 --- 液 - 液相反应动力学

框架一步一步方便使用的流程
随机推荐
冶金物理化学复习 --- 气-液相反应动力学
冶金物理化学复习 --- 液 - 液相反应动力学
ssm项目快速搭建项目配置文件
repackag failed: Unable to find main class
【单例模式】懒汉模式的线程安全问题
Redis' bloom filter
接口幂等性问题
Lamda gets the current number of cycles, atomicinteger
SSM project quick build project configuration file
2022 summer practice (PowerDesigner tutorial learning record) (first week)
Advanced multi threading: the underlying principle of synchronized, the process of lock optimization and lock upgrade
openjudge:大小写字母互换
Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid
How about ink cloud?
24小时内的时间段无交叉
How to compare long and integer and why to report errors
restFul接口使用个人总结
repackag failed: Unable to find main class
深度学习医学图像模型复现
Eccv2022 | 29 papers of Tencent Youtu were selected, including face security, image segmentation, target detection and other research directions