• Pytorch中如何加载数据、Tensorboard、Transforms的使用


    一、Pytorch中如何加载数据
    Pytorch中涉及到如何读取数据,主要是两个类一个类是Dataset、Dataloader
    Dataset 提供一种方式获取数据,及其对应的label。主要包含以下两个功能:
    如何获取每一个数据以及label
    告诉我们总共有多少的数据

    Dataloader,可以对数据进行打包,为后面的网络提供不同的数据形式。

    二、Tensorboard的使用,用来观察训练结果

    from torch.utils.tensorboard import SummaryWriter
    
    writer = SummaryWriter("log")
    
    # writer.add_image()
    
    for i in range(100):
        writer.add_scalar("y=x", i, i)
    
    writer.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在Terminal中先切换到conda activate pytorch
    使用命令 tensorboard --logdir=logs
    在这里插入图片描述

    TensorBoard的使用
    1、使用add_image()方法

    from torch.utils.tensorboard import SummaryWriter
    import numpy as np
    from PIL import Image
    # 利用openCV中的numpy库可以获得numpy型的图片
    
    writer = SummaryWriter("log")
    img_path = "../dataset/bees/26589803_5ba7000313.jpg"
    img_PIL = Image.open(img_path)    # 打开图片
    img_array = np.array(img_PIL)     # 图片转换
    print(type(img_array))   # 打印图片类型
    print(img_array.shape)  # 打印图片格式
    
    writer.add_images("test", img_array, 2, dataformats='HWC')  # 根据img_array.shape来指定,如果不指定dataformats就会报错
    # y = 2x
    for i in range(100):
        writer.add_scalar("y=2x", 2*i, i)
    
    writer.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述
    三、Transforms的使用
    transform表示对图片进行一些变换
    python的用法 -> tensor数据类型
    通过transform.ToTensor去解决两个问题:
    transforms该如何使用(Python)
    为什么我们需要Tensor的数据类型?
    在这里插入图片描述

    from torchvision import transforms
    from PIL import Image
    
    img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
    img = Image.open(img_path)
    tensor_trans = transforms.ToTensor()
    tensor_img = tensor_trans(img)
    print(tensor_img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    运行后的结果

    D:\tools\anaconda\envs\pytorch\python.exe D:/code/captcha_ocr-main/learn/transforms.py
    tensor([[[0.5725, 0.5725, 0.5725,  ..., 0.5686, 0.5725, 0.5765],
             [0.5725, 0.5725, 0.5725,  ..., 0.5686, 0.5725, 0.5765],
             [0.5686, 0.5686, 0.5725,  ..., 0.5686, 0.5725, 0.5765],
             ...,
             [0.5490, 0.5647, 0.5725,  ..., 0.6314, 0.6235, 0.6118],
             [0.5608, 0.5765, 0.5843,  ..., 0.5961, 0.5843, 0.5765],
             [0.5725, 0.5843, 0.5922,  ..., 0.5647, 0.5529, 0.5490]],
    
            [[0.4471, 0.4471, 0.4471,  ..., 0.4235, 0.4275, 0.4314],
             [0.4471, 0.4471, 0.4471,  ..., 0.4235, 0.4275, 0.4314],
             [0.4431, 0.4431, 0.4471,  ..., 0.4235, 0.4275, 0.4314],
             ...,
             [0.4000, 0.4157, 0.4235,  ..., 0.4706, 0.4627, 0.4510],
             [0.4118, 0.4275, 0.4353,  ..., 0.4431, 0.4314, 0.4235],
             [0.4235, 0.4353, 0.4431,  ..., 0.4118, 0.4000, 0.3961]],
    
            [[0.2471, 0.2471, 0.2471,  ..., 0.2588, 0.2627, 0.2667],
             [0.2471, 0.2471, 0.2471,  ..., 0.2588, 0.2627, 0.2667],
             [0.2431, 0.2431, 0.2471,  ..., 0.2588, 0.2627, 0.2667],
             ...,
             [0.2157, 0.2314, 0.2392,  ..., 0.2510, 0.2431, 0.2314],
             [0.2275, 0.2431, 0.2510,  ..., 0.2196, 0.2078, 0.2000],
             [0.2392, 0.2510, 0.2588,  ..., 0.1961, 0.1843, 0.1804]]])
    
    Process finished with exit code 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    加载tensor类型的图片:

    from torchvision import transforms
    from PIL import Image
    from torch.utils.tensorboard import SummaryWriter
    img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
    img = Image.open(img_path)
    writer = SummaryWriter("log")
    tensor_trans = transforms.ToTensor()
    tensor_img = tensor_trans(img)
    writer.add_image("Tensor_img", tensor_img)
    writer.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    四、常见的transforms类的使用

    1. ToTensor类
      将PIL图片转换成tensor图片。
    from torchvision import transforms
    from PIL import Image
    from torch.utils.tensorboard import SummaryWriter
    # Tensor的使用
    writer = SummaryWriter("log")
    img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
    img = Image.open(img_path)
    print(img)
    tensor_trans = transforms.ToTensor()    # 创建ToTensor()对象
    tensor_img = tensor_trans(img)    # 传入图片参数,将PIL图片转换成tensor图片
    writer.add_image("Tensor_img", tensor_img)
    writer.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述
    2. Normalize类

    对tensor类型的图片进行归一化处理。
    Normalize的使用:归一化处理
    公式:output[channel] = (input[channel] - mean[channel]) / std[channel]

    from torchvision import transforms
    from PIL import Image
    from torch.utils.tensorboard import SummaryWriter
    
    # Tensor的使用
    writer = SummaryWriter("log")
    img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
    img = Image.open(img_path)
    print(img)
    
    
    tensor_trans = transforms.ToTensor()
    tensor_img = tensor_trans(img)
    
    writer.add_image("Tensor_img", tensor_img)
    
    # Normalize的使用
    print(tensor_img[0][0][0])    # 归一化处理之前的数据
    trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
    img_norm = trans_norm(tensor_img)
    print(img_norm[0][0][0])     # 归一化处理后的结果
    writer.add_image("Normalize", img_norm)
    
    writer.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述
    在这里插入图片描述
    3. Resize类:
    重置图片大小。

    from torchvision import transforms
    from PIL import Image
    from torch.utils.tensorboard import SummaryWriter
    
    # Tensor的使用
    writer = SummaryWriter("log")
    img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
    img = Image.open(img_path)
    print(img)
    
    
    tensor_trans = transforms.ToTensor()
    tensor_img = tensor_trans(img)
    
    writer.add_image("Tensor_img", tensor_img)
    
    print(tensor_img[0][0][0])    # 归一化处理之前的数据
    trans_norm = transforms.Normalize([1, 3, 5], [3, 2, 1])
    img_norm = trans_norm(tensor_img)
    print(img_norm[0][0][0])     # 归一化处理后的结果
    writer.add_image("Normalize", img_norm)
    
    writer.close()
    
    # Resize的使用:重置图片大小
    print(img.size)    # (500, 464) 
    trans_resize = transforms.Resize((512, 512))
    
    img_resize = trans_resize(img)
    print(img_resize)   #  
     
    img_resize = tensor_trans(img_resize)
    writer.add_image("Resize", img_resize, 0)
    print("" + img_resize)
    writer.close()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    在这里插入图片描述
    在这里插入图片描述
    4. Compose的使用
    等比例缩放。
    Compose的使用:整体缩放,不改变高宽比例
    Compose()中的参数需要的是一个列表,列表中的数据需要的是transforms类型。
    即 Compose([transforms参数1, transforms参数2, …])

    from torchvision import transforms
    from PIL import Image
    from torch.utils.tensorboard import SummaryWriter
    
    # Tensor的使用
    writer = SummaryWriter("log")
    img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
    img = Image.open(img_path)
    print(img)
    
    
    tensor_trans = transforms.ToTensor()
    tensor_img = tensor_trans(img)
    
    writer.add_image("Tensor_img", tensor_img)
    
    print(tensor_img[0][0][0])    # 归一化处理之前的数据
    trans_norm = transforms.Normalize([1, 3, 5], [3, 2, 1])
    img_norm = trans_norm(tensor_img)
    # print(img_norm[0][0][0])     # 归一化处理后的结果
    writer.add_image("Normalize", img_norm)
    
    
    # Resize的使用:重置图片大小
    print(img.size)    # (500, 464)
    trans_resize = transforms.Resize((512, 512))
    
    img_resize = trans_resize(img)
    print(img_resize)   # 
    
    # Compose的使用
    img_resize = tensor_trans(img_resize)
    writer.add_image("Resize", img_resize, 0)
    # print(img_resize)
    trans_resize_2 = transforms.Resize(1024)
    trans_compose = transforms.Compose([trans_resize_2, tensor_trans])
    img_resize_2 = trans_compose(img)
    writer.add_image("Resize", img_resize_2, 1)
    
    writer.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    5.RandomCrop类的使用

    随机裁剪。

    from torchvision import transforms
    from PIL import Image
    from torch.utils.tensorboard import SummaryWriter
    
    # Tensor的使用
    writer = SummaryWriter("log")
    img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
    img = Image.open(img_path)
    print(img)
    
    
    tensor_trans = transforms.ToTensor()
    tensor_img = tensor_trans(img)
    
    writer.add_image("Tensor_img", tensor_img)
    
    print(tensor_img[0][0][0])    # 归一化处理之前的数据
    trans_norm = transforms.Normalize([1, 3, 5], [3, 2, 1])
    img_norm = trans_norm(tensor_img)
    # print(img_norm[0][0][0])     # 归一化处理后的结果
    writer.add_image("Normalize", img_norm)
    
    
    # Resize的使用:重置图片大小
    print(img.size)    # (500, 464)
    trans_resize = transforms.Resize((512, 512))
    
    img_resize = trans_resize(img)
    print(img_resize)   # 
    
    
    #  Compose的使用:
    img_resize = tensor_trans(img_resize)
    writer.add_image("Resize", img_resize, 0)
    # print(img_resize)
    trans_resize_2 = transforms.Resize(1024)
    trans_compose = transforms.Compose([trans_resize_2, tensor_trans])
    img_resize_2 = trans_compose(img)
    writer.add_image("Resize", img_resize_2, 1)
    
    
    # RendomCrop类的使用:随机裁剪
    # trans_random = transforms.RandomCrop(512)
    trans_random = transforms.RandomCrop(1000, 500)
    trans_compose_2 = transforms.Compose([trans_random, tensor_trans])
    for i in range(10):
        img_crop = trans_compose_2(img)
        # writer.add_image("RancomCrop", img_crop, i)
        writer.add_image("RancomCropHW", img_crop, i)
    
    writer.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    在这里插入图片描述

  • 相关阅读:
    浏览器打开JupyterLab后所有快捷键与窗口按键均失效怎么办?
    【情态动词练习题】Can / Could you
    自己动手写PBR
    vue computed计算属性
    不要再稀里糊涂的使用反射了,四万字带你搞定Java反射(JDK8)
    大部分PHP程序员,都搞不懂如何安全代码部署【二】(nginx篇)
    【python量化】将Transformer模型用于股票价格预测
    基于位置的前馈神经网络
    制造企业如何通过PLM系统实现BOM管理的飞跃
    『Linux小程序』进度条
  • 原文地址:https://blog.csdn.net/qq_30353203/article/details/131905959