当前位置:网站首页>Pytorch visualization
Pytorch visualization
2022-07-23 09:16:00 【Orange @ orange】
( One ) Network structure visualization
Use torchinfo Open Source Toolkit for structured output , Just use torchinfo.summary() Just go . However, it should be noted that the visual network structure needs a forward propagation to obtain the information of a specific layer .
import torchvision
from torchinfo import summary
resnet18=torchvision.models.resnet18()
summary(resnet18,(batchize=64,channels=3,high=224,weight=225))# Carry out a forward communication to obtain information ( Two ) Convolution CNN visualization
1.CNN Convolution kernel Visualization
The purpose of visual convolution kernel is to see what features the model extracts .
conv1 = dict(model.features.named_children())['conv1'] # By the end of 1 Layer convolution as an example , Visualize the corresponding parameters
kernel_set = conv1.weight.detach()
num = len(conv1.weight.detach())
print(kernel_set.shape)
for i in range(0,num):
i_kernel = kernel_set[i]
plt.figure(figsize=(20, 17))
if (len(i_kernel)) > 1:
for idx, filer in enumerate(i_kernel):
plt.subplot(9, 9, idx+1)
plt.axis('off')
plt.imshow(filer[ :, :].detach(),cmap='bwr')2.CNN Visual feature map
Characteristics of figure : The data obtained by each convolution layer of the input original image . Visual feature map is to see what the features extracted by the model look like .
This usually uses PyTorch Medium hook Interface , It is equivalent to that the characteristic graph in the forward propagation of data will be hook capture , You can view it separately after forward propagation . commonly hook In addition to using what you define , You can also use PyTorch Self contained nn.Module.register_forward_hook() Methods such as .
class Hook(object):
def __init__(self):
self.module_name = []
self.features_in_hook = []
self.features_out_hook = []
def __call__(self,module, fea_in, fea_out): #module To know which layer of data is obtained
print("hooker working", self)
self.module_name.append(module.__class__)
self.features_in_hook.append(fea_in)
self.features_out_hook.append(fea_out)
return None
def plot_feature(model, idx, inputs):
hh = Hook()
model.features[i].register_forward_hook(hh) # utilize PyTorch Properties of the layer of the built-in model
#register_forward_hook(), Add hooks .
# forward_model(model,False)
model.eval()
_ = model(inputs)
# Printed information
print(hh.module_name)
print((hh.features_in_hook[0][0].shape))
print((hh.features_out_hook[0].shape))
out1 = hh.features_out_hook[0]
total_ft = out1.shape[1]
first_item = out1[0].cpu().clone()
plt.figure(figsize=(20, 17))
for ftidx in range(total_ft):
if ftidx > 99:
break
ft = first_item[ftidx]
plt.subplot(10, 10, ftidx+1)
plt.axis('off')
#plt.imshow(ft[ :, :].detach(),cmap='gray')
plt.imshow(ft[ :, :].detach())( 3、 ... and ) Use TensorBoard visualization
# install tensorboardX
from tensorboardX import SummaryWriter
# If you use PyTorch Self contained tensorboard, Then the following quotation
from torch.utils.tensorboard import SummaryWriter
# start-up , Command line input
tensorboard --logdir=/path/to/logs/ --port=xxxx
# in order to tensorboard Can run continuously in the background , You can also use nohup Order or tmux Tools to run tensorboard
# call :
writer = SummaryWriter('./runs')# Instantiation SummaryWritter As a variable writer, And designate writer The output directory of is... Under the current directory "runs" Catalog
writer.add_graph(model, input_to_model = torch.rand(1, 3, 224, 224))# Given an input data , The structure of the model is obtained after forward propagation , Re pass TensorBoard Visualizing
writer.close()
# Continuous variable visualization
writer = SummaryWriter('./tb_logs')
for i in range(500): # Put a picture
x = i
writer.add_image("x", x, i) # It's recorded in the log x In the step i Value
writer.close()
# The actual use
writer = SummaryWriter('./logs')
writer.add_image(tudui,input)
writer.close()
# Parameter distribution visualization :
for (key,val) in zip(resnet18.state_dict().keys(),resnet18.state_dict().values()):
print(key,val)
writer = SummaryWriter('./tb_logs/')
for step, (key,val) in enumerate(zip(resnet18.state_dict().keys(),resnet18.state_dict().values()):
writer.add_histogram("w", val, step)
writer.flush()
writer.close()边栏推荐
- SQL Server database design -- select statement 2
- rust allow dead_code
- 【华为联机对战服务】客户端退出重连或中途进入游戏,新玩家如何补帧?
- [cann training camp] learning notes - Comparison between diffusion and Gan, dalle2 and Party
- LiveQing直播点播流媒体OBS推流直播如何获得接口校验token视频校验streamToken及配置token有效期
- 涅槃重生!字节大牛力荐大型分布式手册,凤凰架构让你浴火成神
- -Bash: wget: command not found
- 求解最大公约数和最小公倍数
- Internet Download Manager简直就是下载器中的大杀器
- 驱动单片机硬件调试器的一些开源库总结(包含stlink调试器)
猜你喜欢

Development of ordering system in epidemic isolation area

UGUI源码解析——StencilMaterial

服务器托管、服务器租用、云主机各自的优势

详解Vector

【管理篇 / 升级】* 02. 查看升级路径 * FortiGate 防火墙

基于SSM的博客系统【带后台管理】

C语言实战之猜数游戏

TCP连接原理

求解最大公约数和最小公倍数
![[Huawei online battle service] how can new players make up frames when the client quits reconnection or enters the game halfway?](/img/b6/f00ac1b9a244832300f2dd898120f9.png)
[Huawei online battle service] how can new players make up frames when the client quits reconnection or enters the game halfway?
随机推荐
VS2022中出现const char* 无法赋值 char*
实行自动化网络性能监控的优势
一个月学透阿里整理的分布式架构笔记
Metauniverse is not an existence that regards traffic as the ultimate pursuit like the Internet
详解Vector
Ascension begins with change
【C语言】文件操作
BCG 使用之NOTIFYICONDATA托盘
基于时频图的音频处理-matlab
Internet Download Manager简直就是下载器中的大杀器
Go-Excelize API源码阅读(五)—— Close()、NewSheet()
transformer汇总
基于共轭梯度法的对抗攻击
程序员必须掌握的600个英语单词
2000. reverse word prefix
LiveQing直播点播流媒体OBS推流直播如何获得接口校验token视频校验streamToken及配置token有效期
Huawei applications have called the checkappupdate interface. Why is there no prompt for version update in the application
Props and context in setup
IDM downloader free high-quality win download tool without restrictions
程序员不会 jvm?骨灰级工程师:全等着被淘汰吧!这是必会技能!