Loading

深度学习可视化工具--tensorboard的使用

tensorboard的使用

官方文档

# writer.add_scalar()  # 添加标量
"""
        Args:
            tag (string): Data identifier  # 图表的Title
            scalar_value (float or string/blobname): Value to save  # 对应的y轴
            global_step (int): Global step value to record  # 对应的x轴
            walltime (float): Optional override default walltime (time.time())
              with seconds after epoch of event
"""

# demo
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter('runs')  # 实例化一个对象
for i in range(100):
    writer.add_scalar("y=x^2", i**2, i)  # 分别对应title y轴 x轴
writer.close()
# writer.add_image()  # 添加图片
"""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 %

        """
# demo
from torch.utils.tensorboard import SummaryWriter
import cv2
write = SummaryWriter("logs")
img_array1 = cv2.imread('./dog.jpeg')  # 读取图片,类型为ndarray
write.add_image("img", img_array1, 1, dataformats='HWC')  # title, 数据: ndarray/tensor step 数据的类型: 默认:'CHW'
write.close()

开启面板

# 方法1: tensorboard --logdir=runs
# 方法2: tensorboard --logdir runs
# 方法3: tensorboard --logdir=runs --port=6007  如果端口冲突使用不冲突的端口
posted @ 2022-01-05 14:20  zranguai  阅读(173)  评论(0编辑  收藏  举报