Pytorch-TensorBoard

简介

TensorBoard:Tensorflow中强大的可视化工具

Pytorch-TensorBoard_第1张图片
TensorBoard测试代码

import numpy as np
from torch.utils.tensorboard import SummaryWriter

writer=SummaryWriter(comment='test_tensorboard')

for x in range(100):
	writer.add_scalar('y=2x',x*2,x)
	writer.add_scalar('y=pow(2,x)',2**x,x)
	
	writer.add_scalars('data/scalar_group',{"sins":x*np.sin(x),
											"xcox":x*np.cos(x),
											"arctanx":np.arctan(x)},x)
writer.close()

如何读取

在目标文件内命令行内,输入tensorboard --logdir=./runs其中runs是event_file所在的文件夹

此后在终端内,会出现一个网址一般为:http://localhost:6006/在这里插入图片描述

Pytorch-TensorBoard_第2张图片

注意事项

安装时会提示报错,ModuleNotFoundError:No module named ‘past’

通过pip install future解决

SummaryWriter

功能:提供创建event file的高级接口
主要属性:

  • log_fir:event file输出文件夹如果不设置,会在当前文件夹下的runs文件夹
  • comment:不指定log_dir时,文件夹后缀,如果log_dir为空会生效,给runs文件下的文件夹后缀
  • filename_suffix:evebnt file 文件名后缀,给event file文件设置的后缀

方法

1.add_scalar()

功能:记录标量

  • tag:图像的标签名,图的唯一标识
  • scalar_value:要记录的标量
  • global_step:x轴

缺陷:只能记录一条曲线

2.add_scalars()

  • main_tag:该图的标签
  • tag_scalar_dict:key是变量的tag,value是变量的值
max_epoch = 100                                                               
                                                                              
writer = SummaryWriter(comment='test_comment', filename_suffix="test_suffix") 
                                                                              
for x in range(max_epoch):                                                    
    writer.add_scalar('y=2x', x * 2, x)                                       
    writer.add_scalar('y=pow_2_x', 2 ** x, x)                                 
                                                                              
    writer.add_scalars('data/scalar_group', {"xsinx": x * np.sin(x),          
                                             "xcosx": x * np.cos(x)}, x)      
                                                                              
writer.close()                                                                

3.add_histogram()

功能:统计直方图与多分位数折线图

  • tag:图像的标签名,图的唯一标识
  • values:要统计的参数
  • global_step:y轴
  • bins:取直方图bins
writer = SummaryWriter(comment='test_comment', filename_suffix="test_suffix") 
                                                                              
for x in range(2):                                                            
                                                                              
    np.random.seed(x)                                                         
                                                                              
    data_union = np.arange(100)                                               
    data_normal = np.random.normal(size=1000)                                 
                                                                              
    writer.add_histogram('distribution union', data_union, x)                 
    writer.add_histogram('distribution normal', data_normal, x)               
                                                                              
    plt.subplot(121).hist(data_union, label="union")                          
    plt.subplot(122).hist(data_normal, label="normal")                        
    plt.legend()                                                              
    plt.show()                                                                
                                                                              
writer.close()                                                                

4.add_image()

功能:记录图像

  • tag:图像的标签名,图的唯一标识
  • img_tensor:图像数据,注意尺度,如果所有像素点都是(0,1)之间就默认乘以255,如果有像素点大于1就默认所有像素点都是0~255的尺度
  • global_step:x轴
  • dataformats:数据形式,CHW,HWC,HW
    Pytorch-TensorBoard_第3张图片
writer = SummaryWriter(comment='test_your_comment', filename_suffix="_test_your_filename_suffix") 
                                                                                                  
# img 1     random                                                                                
fake_img = torch.randn(3, 512, 512)                                                               
writer.add_image("fake_img", fake_img, 1)                                                         
time.sleep(1)                                                                                     
                                                                                                  
# img 2     ones                                                                                  
fake_img = torch.ones(3, 512, 512)                                                                
time.sleep(1)                                                                                     
writer.add_image("fake_img", fake_img, 2)                                                         
                                                                                                  
# img 3     1.1                                                                                   
fake_img = torch.ones(3, 512, 512) * 1.1                                                          
time.sleep(1)                                                                                     
writer.add_image("fake_img", fake_img, 3)                                                         
                                                                                                  
# img 4     HW                                                                                    
fake_img = torch.rand(512, 512)                                                                   
writer.add_image("fake_img", fake_img, 4, dataformats="HW")                                       
                                                                                                  
# img 5     HWC                                                                                   
fake_img = torch.rand(512, 512, 3)                                                                
writer.add_image("fake_img", fake_img, 5, dataformats="HWC")                                      
                                                                                                  
writer.close()                                                                                    

torchvision.utils.make_grid

功能:制作网格图像
Pytorch-TensorBoard_第4张图片
可视化MNIST数据集

transform = Compose([transforms.ToTensor()])                                    
datasets=mnist.MNIST(root='./data',train=True,transform=transform,download=True)
datalode=DataLoader(datasets,batch_size=16,num_workers=2)                       
                                                                                
images,labels=next(iter(datalode))                                              
imagesTogather=tutiles.make_grid(images,nrow=4)                                 
imagesTogather=imagesTogather.transpose(0,2)                                    
import matplotlib.pyplot as plt                                                 
plt.imshow(imagesTogather)                                                      
plt.show()                                                                      

Pytorch-TensorBoard_第5张图片

可视化卷积核

import torch.nn as nn                              
from PIL import Image                              
import torchvision.transforms as transforms        
from torch.utils.tensorboard import SummaryWriter  
import torchvision.utils as vutils                 
from tools.common_tools import set_seed            
import torchvision.models as models                
                                                   
set_seed(1)  # 设置随机种子                              

 writer = SummaryWriter(comment='test_your_comment', filename_suffix="_test_your_filename_suffix")                
                                                                                                                  
 alexnet = models.alexnet(pretrained=True)                                                                        
                                                                                                                  
 kernel_num = -1                                                                                                  
 vis_max = 1                                                                                                      
                                                                                                                  
 for sub_module in alexnet.modules():                                                                             
     if isinstance(sub_module, nn.Conv2d):                                                                        
         kernel_num += 1                                                                                          
         if kernel_num > vis_max:                                                                                 
             break                                                                                                
         kernels = sub_module.weight                                                                              
         c_out, c_int, k_w, k_h = tuple(kernels.shape)                                                            
                                                                                                                  
         for o_idx in range(c_out):                                                                               
             kernel_idx = kernels[o_idx, :, :, :].unsqueeze(1)   # make_grid需要 BCHW,这里拓展C维度                       
             kernel_grid = vutils.make_grid(kernel_idx, normalize=True, scale_each=True, nrow=c_int)              
             writer.add_image('{}_Convlayer_split_in_channel'.format(kernel_num), kernel_grid, global_step=o_idx) 
                                                                                                                  
         kernel_all = kernels.view(-1, 3, k_h, k_w)  # 3, h, w                                                    
         kernel_grid = vutils.make_grid(kernel_all, normalize=True, scale_each=True, nrow=8)  # c, h, w           
         writer.add_image('{}_all'.format(kernel_num), kernel_grid, global_step=322)                              
                                                                                                                  
         print("{}_convlayer shape:{}".format(kernel_num, tuple(kernels.shape)))                                  
                                                                                                                  
 writer.close()                                                                                                   

特征图可视化

writer = SummaryWriter(comment='test_your_comment', filename_suffix="_test_your_filename_suffix")      
                                                                                                       
# 数据                                                                                                   
path_img = "./lena.png"     # your path to image                                                       
normMean = [0.49139968, 0.48215827, 0.44653124]                                                        
normStd = [0.24703233, 0.24348505, 0.26158768]                                                         
                                                                                                       
norm_transform = transforms.Normalize(normMean, normStd)                                               
img_transforms = transforms.Compose([                                                                  
    transforms.Resize((224, 224)),                                                                     
    transforms.ToTensor(),                                                                             
    norm_transform                                                                                     
])                                                                                                     
                                                                                                       
img_pil = Image.open(path_img).convert('RGB')                                                          
if img_transforms is not None:                                                                         
    img_tensor = img_transforms(img_pil)                                                               
img_tensor.unsqueeze_(0)    # chw --> bchw                                                             
                                                                                                       
# 模型                                                                                                   
alexnet = models.alexnet(pretrained=True)                                                              
                                                                                                       
# forward                                                                                              
convlayer1 = alexnet.features[0]                                                                       
fmap_1 = convlayer1(img_tensor)                                                                        
                                                                                                       
# 预处理                                                                                                  
fmap_1.transpose_(0, 1)  # bchw=(1, 64, 55, 55) --> (64, 1, 55, 55)                                    
fmap_1_grid = vutils.make_grid(fmap_1, normalize=True, scale_each=True, nrow=8)                        
                                                                                                       
writer.add_image('feature map in conv1', fmap_1_grid, global_step=322)                                 
writer.close()                                                                                         
                                                                                                                                                                                                     

5.add_graph()

Pytorch1.2版本存在bug需要求换到1.3版本
Pytorch-TensorBoard_第6张图片
在这里插入图片描述

writer = SummaryWriter(comment='test_your_comment', filename_suffix="_test_your_filename_suffix")      
                                                                                                       
# 模型                                                                                                   
fake_img = torch.randn(1, 3, 32, 32)                                                                   
                                                                                                       
lenet = LeNet(classes=2)                                                                               
                                                                                                       
writer.add_graph(lenet, fake_img)                                                                      
                                                                                                       
writer.close()                                                                                         
                                                                                                       
                                                     
                                                                                                       

torchsummary

Pytorch-TensorBoard_第7张图片

此工具包属于第三方工具包,需要pip install安装一下
pip install torchsummary

from torchvision.models import alexnet                
Alexnet=alexnet()                                     
from torchsummary import summary                      
                                                      
print(summary(Alexnet, (3, 256,256), device="cpu"))   

Pytorch-TensorBoard_第8张图片

你可能感兴趣的:(Pytorch学习笔记)