tensorboard学习日记:add_image的使用

from torch.utils.tensorboard import SummaryWriter
#利用numpy.array(),对PIL图片进行转换
import numpy as ny
from PIL import Image

#如果想要查看这个类,ctrl+鼠标点击
#
#这里使用SummaryWriter创建一个tensorboard文件
writer = SummaryWriter("logs")#这个咱们只需要给他一个文件的名称 即folder location
image_path = "D:\PyCharm Community Edition 2021.3.1\\tensorboard\data\\train\\ants_image\\0013035.jpg"
img_PIL = Image.open(image_path)#创建PIL的图片类
# print(type(img_PIL))在控制台查询类型  
image_array = ny.array(img_PIL)  #转成也就是add_image  img_tensor所需要的参数类型

# 开始向add_image添加参数
writer.add_image("test",image_array,1)
# """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  添加图片,本次采取的是numpy.array的类型
#             global_step (int): Global step value to record
#
#

在经过以上的将PIL的图片转成numpy.array之后我们便满足了add_image函数的参数类型要求,但此时运行仍然不行,原因出在add_image使用numpy.array类型时需要添加说明:

 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``.

意思便是img_tensor的形状有要求,而默认格式是(3,H,W)即通道(channel)为3,H为高度,W为宽度

我们在控制台显示其形状,并不是默认格式,所以需要添加额外说明

        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()

此时说明文档也给了我们例子,因此我们在写的时候需要在后面添加  dataformats='HWC'

from torch.utils.tensorboard import SummaryWriter
#利用numpy.array(),对PIL图片进行转换
import numpy as ny
from PIL import Image

#如果想要查看这个类,ctrl+鼠标点击
#
#这里使用SummaryWriter创建一个tensorboard文件
writer = SummaryWriter("logs")#这个咱们只需要给他一个文件的名称 即folder location
image_path = "D:\PyCharm Community Edition 2021.3.1\\tensorboard\data\\train\\ants_image\\0013035.jpg"
img_PIL = Image.open(image_path)#创建PIL的图片类
# print(type(img_PIL))在控制台查询类型  
image_array = ny.array(img_PIL)  #转成也就是add_image  img_tensor所需要的参数类型

# 开始向add_image添加参数
writer.add_image("test",image_array,1,dataformats='HWC')
# """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  添加图片,本次采取的是numpy.array的类型
#             global_step (int): Global step value to record
#
#



for i in range(100):

    writer.add_scalar("y=x",i,i)
# #add。scalar作用为将值存入到tensorboard中
# """Add scalar data to summary.简单来说就是添加数据
#
# Args:
#     tag (string): Data identifier数据标识符   简单来说就是咱给其结果图取一个名字
#     scalar_value (float or string/blobname): Value to save  传入的数值(也就是结果图的y轴值)
#     global_step (int): Global step value to record  也就是结果图的x轴记录训练的步数
#例如writer.add("train_loss",loss,(epoch*epoch_size+iteration))  分别表示给其存入到tensorboard取一个名称/存入的值变量/以及tensorboard的横坐标

writer.close()

#range的使用
# for i in  range(1, 4):
#     print(i)
# #设置停顿三秒
# time.sleep(3)
# #代表1到10,间隔2,不包含10
# for i in range(1, 10, 2):
#     print(i)
# time.sleep(3)
# #代表0到5,不包含5
# for i in range(5):
#     print(i)

运行代码之后打开tensorboard

tensorboard学习日记:add_image的使用_第1张图片

 添加第二张图片,

tensorboard学习日记:add_image的使用_第2张图片

我们改变图片的地址和步数step=2,再次运行代码

tensorboard学习日记:add_image的使用_第3张图片

你可能感兴趣的:(啥也不会的pytorch,python,深度学习,pytorch)