Pytorch学习(2)

Pytorch学习(2),第1张

关于tensorboard的使用

tensorboard是一个可视化的工具,能够对数据集和训练结果进行可视化

需要通torch.utils.tensorboard中的SummaryWriter

import torch
import numpy as np
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
import os

writer = SummaryWriter('logs')

能够在目标目录下生成一个logs文件夹来存放可视化数据文件


image_path = r'hymenoptera_data/train/ants'
image_item_path = os.listdir(r'hymenoptera_data/train/ants')
for i in range(10):
    print(os.path.join(image_path,image_item_path[i]))
    img_PIL = Image.open(os.path.join(image_path,image_item_path[i]))
    img_array = np.array(img_PIL)
    writer.add_image(f'{i}',img_array,i,dataformats='HWC')
writer.close()

将数据集图片的路径添加进去,将所需要展示的图片提取出来即img_PIL

add_image方法:

    def add_image(self, tag, img_tensor, global_step=None, walltime=None, dataformats='CHW'):
        """Add image data to summary.

        Note that this requires the ``pillow`` package.

        Args:
            tag (string): Data identifier
            img_tensor (torch.Tensor, numpy.array, or string/blobname): Image data
            global_step (int): Global step value to record
            walltime (float): Optional override default walltime (time.time())
              seconds after epoch of event
        Shape:
            img_tensor: Default is :math:`(3, H, W)`. You can use ``torchvision.utils.make_grid()`` to
            convert a batch of tensor into 3xHxW format or call ``add_images`` and let us do the job.
            Tensor with :math:`(1, H, W)`, :math:`(H, W)`, :math:`(H, W, 3)` is also suitable as long as
            corresponding ``dataformats`` argument is passed, e.g. ``CHW``, ``HWC``, ``HW``.

        Examples::

            from torch.utils.tensorboard import SummaryWriter
            import numpy as np
            img = np.zeros((3, 100, 100))
            img[0] = np.arange(0, 10000).reshape(100, 100) / 10000
            img[1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000

            img_HWC = np.zeros((100, 100, 3))
            img_HWC[:, :, 0] = np.arange(0, 10000).reshape(100, 100) / 10000
            img_HWC[:, :, 1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000

            writer = SummaryWriter()
            writer.add_image('my_image', img, 0)

            # If you have non-default dimension setting, set the dataformats argument.
            writer.add_image('my_image_HWC', img_HWC, 0, dataformats='HWC')
            writer.close()

        Expected result:

        .. image:: _static/img/tensorboard/add_image.png
           :scale: 50 %

        """

但是需要注意的是writer中的add_image方法只能够收取

img_tensor (torch.Tensor, numpy.array, or string/blobname): Image data

以上几种类型

所以需要将PIL类型转化为numpy类型,通过np.array

还需要注意的是图片的数据排布:

img_tensor: Default is :math:`(3, H, W)`. You can use ``torchvision.utils.make_grid()`` to
            convert a batch of tensor into 3xHxW format or call ``add_images`` and let us do the job.
            Tensor with :math:`(1, H, W)`, :math:`(H, W)`, :math:`(H, W, 3)` is also suitable as long as
            corresponding ``dataformats`` argument is passed, e.g. ``CHW``, ``HWC``, ``HW``.

需要通过

dataformats='HWC'

这个参数来调整,因为转换为numpy类型后,图片的数据排布是 高 宽 通道数,因此此参数设置为HWC


最后在终端中使用命令

tensorboard --logdir=logs
 #这里的logs是先前创建的文件夹名字

来开启端口即可查看

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/916503.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存