Pytorch学习笔记-05 可视化工具 TensorBoard

Pytorch学习笔记-05 可视化工具 TensorBoard

文章目录

  • Pytorch学习笔记-05 可视化工具 TensorBoard
    • SummaryWriter
      • add_scalar
      • add_scalars
      • add_histogram
      • add_image
      • torchvision.utils.make_grid
      • e.g.卷积核的可视化
      • add_graph
    • 补充:torchinfo
    • Hook 函数与 CAM 算法

SummaryWriter

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

  • log_dir event file 输出文件夹
  • comment :不指定 log_dir 时,文件夹后缀
  • filename_suffix event file

add_scalar

功能:记录标量

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

add_scalars

  • main_tag :该图的标签
  • tag_scalar_dict key 是变量的 tag value 是变量的值
or 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', {"xsinx": x * np.sin(x),
                                             "xcosx": x * np.cos(x),
                                             "arctanx": np.arctan(x)}, x)

add_histogram

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

  • tag :图像的标签名,图的唯一标识
  • values :要统计的参数
  • global_step y 轴
  • bins :取直方图的 bins
from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter()
for i in range(10):
    x = np.random.random(1000)
    writer.add_histogram('distribution centers', x + i, i)
writer.close()

add_image

功能:记录图像

  • tag :图像的标签名,图的唯一标识
  • img_tensor :图像数据,注意尺度
  • global_step x 轴
  • dataformats :数据形式 CHW HWC HW
from torch.utils.tensorboard import SummaryWriter
import numpy as np

img_batch = np.zeros((16, 3, 100, 100))
for i in range(16):
    img_batch[i, 0] = np.arange(0, 10000).reshape(100, 100) / 10000 / 16 * i
    img_batch[i, 1] = (1 - np.arange(0, 10000).reshape(100, 100) / 10000) / 16 * i

writer = SummaryWriter()
writer.add_images('my_image_batch', img_batch, 0)
writer.close()

torchvision.utils.make_grid

功能:制作网格图像

  • tensor :图像数据 BCH*W 形式
  • nrow :行数(列数自动计算
  • padding :图像间距(像素单位
  • normalize :是否将像素值标准化
  • range :标准化范围
  • scale_each :是否单张图维度标准化
  • pad_value padding 的像素值
    img_grid = vutils.make_grid(data_batch, nrow=4, normalize=True, scale_each=True)

e.g.卷积核的可视化

# ----------------------------------- kernel visualization -----------------------------------
# flag = 0
flag = 1
if flag:
    writer = SummaryWriter(comment='test_your_comment', filename_suffix="_test_your_filename_suffix")

    alexnet = models.alexnet(pretrained=True)

    kernel_num = -1
    vis_max = 1
    # 避免pytorch1.7下的一个小bug,增加 torch.no_grad
    with torch.no_grad():
        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()


# ----------------------------------- feature map visualization -----------------------------------
# flag = 0
flag = 1
if flag:
    with torch.no_grad():
        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()
    

add_graph

功能:可视化模型计算图

  • model :模型,必须是 nn.Module
  • input_to_model :输出给模型的数据
  • verbose :是否打印计算图结构信息

补充:torchinfo

实用工具

torchinfo · PyPI

Hook 函数与 CAM 算法

Hook函数机制:不改变主体,实现额外功能,像一个挂件,挂钩, hook

  • torch.Tensor.register_hook (
  • torch.nn.Module.register_forward_hook
  • torch.nn.Module.register_forward_pre_hook
  • torch.nn.Module.register_backward_hook

CAM:类激活图 class activation map

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P8I94B9m-1637330994925)(https://i.loli.net/2021/11/19/1CYGuhKwde6QsXi.png#pic_center)]

Grad-CAM:CAM 改进版,利用梯度作为特征图权重

详解:PyTorch的hook及其在Grad-CAM中的应用 - 知乎 (zhihu.com)
ackward_hook

CAM:类激活图 class activation map

Pytorch学习笔记-05 可视化工具 TensorBoard_第1张图片

Grad-CAM:CAM 改进版,利用梯度作为特征图权重

详解:PyTorch的hook及其在Grad-CAM中的应用 - 知乎 (zhihu.com)

你可能感兴趣的:(Pytorch学习,pytorch,深度学习,神经网络)