• 【PyTorch】Transforms基本使用


    二、Transforms基本使用

    Transforms主要是对特定格式的图片进行一些变化。

    1、Transforms的结构及用法

    Compose:

    ToTensor:

    1.1 如何使用

    PIL Image to Tensor

    from torchvision import transforms
    from PIL import Image
    
    img_path = "../data/tensorboard_data/train/ants_image/0013035.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
    • 9
    • 10
    tensor([[[0.3137, 0.3137, 0.3137,  ..., 0.3176, 0.3098, 0.2980],
             [0.3176, 0.3176, 0.3176,  ..., 0.3176, 0.3098, 0.2980],
             [0.3216, 0.3216, 0.3216,  ..., 0.3137, 0.3098, 0.3020],
             ...,
             ...,
             [0.9294, 0.9294, 0.9255,  ..., 0.5529, 0.9216, 0.8941],
             [0.9294, 0.9294, 0.9255,  ..., 0.8863, 1.0000, 0.9137],
             [0.9294, 0.9294, 0.9255,  ..., 0.9490, 0.9804, 0.9137]]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    numpy.ndarry to Tensor

    from torchvision import transforms
    import cv2
    
    img_path = "../data/tensorboard_data/train/ants_image/0013035.jpg"
    img = cv2.imread(img_path)  # 
    
    tensor_trans = transforms.ToTensor()
    tensor_img = tensor_trans(img)
    
    print(tensor_img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    tensor([[[0.9137, 0.9137, 0.9137,  ..., 0.9176, 0.9098, 0.8980],
             [0.9176, 0.9176, 0.9176,  ..., 0.9176, 0.9098, 0.8980],
             [0.9216, 0.9216, 0.9216,  ..., 0.9137, 0.9098, 0.9020],
             ...,
             ...,
             [0.3412, 0.3412, 0.3373,  ..., 0.1725, 0.3725, 0.3529],
             [0.3412, 0.3412, 0.3373,  ..., 0.3294, 0.3529, 0.3294],
             [0.3412, 0.3412, 0.3373,  ..., 0.3098, 0.3059, 0.3294]]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    1.2 TensorBoard查看

    from torch.utils.tensorboard import SummaryWriter
    from torchvision import transforms
    from PIL import Image
    
    img_path = "../data/tensorboard_data/train/ants_image/0013035.jpg"
    
    img = Image.open(img_path)
    
    writer = SummaryWriter("logs")
    
    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
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2、常用的Transforms

    2.1 ToTensor

    from torch.utils.tensorboard import SummaryWriter
    from torchvision import transforms
    from PIL import Image
    
    writer = SummaryWriter("logs")
    img = Image.open("../images/pytorch.webp")
    
    trans_totensor = transforms.ToTensor()
    img_totensor = trans_totensor(img)
    
    writer.add_image("ToTensor", img_totensor)
    writer.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.2 Normalize

    1、计算公式:

    output[channel] = (input[channel] - mean[channel]) / std[channel]

    2、本实例中即:

    (input - 0.5)/0.5 = 2 * input - 1

    3、大致范围:

    input[0, 1] —> result[-1, 1]

    from torch.utils.tensorboard import SummaryWriter
    from torchvision import transforms
    from PIL import Image
    
    writer = SummaryWriter("logs")
    img = Image.open("../images/pytorch.webp")
    
    # 1.ToTensor
    trans_totensor = transforms.ToTensor()
    img_tensor = trans_totensor(img)
    writer.add_image("ToTensor", img_tensor)
    
    # 2.Normalize
    print(img_tensor[0][0][0])
    trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
    img_norm = trans_norm(img_tensor)
    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

    2.3 Resize

    from torch.utils.tensorboard import SummaryWriter
    from torchvision import transforms
    from PIL import Image
    
    writer = SummaryWriter("logs")
    img = Image.open("../images/pytorch.webp")
    
    # 1.ToTensor
    trans_totensor = transforms.ToTensor()
    img_tensor = trans_totensor(img)
    writer.add_image("ToTensor", img_tensor)
    
    # 2.Resize
    print(img.size)  # (889, 500)
    # img PIL -> resize -> img_resize PIL
    trans_resize = transforms.Resize((450, 450))
    # img_resize PIL -> totensor -> img_resize tensor
    img_resize = trans_resize(img)
    print(img_resize)  # 
    img_resize = trans_totensor(img_resize)
    writer.add_image("Resize", img_resize, 0)
    
    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

    2.4 Compose

    还可以直接使用Compose

    from torch.utils.tensorboard import SummaryWriter
    from torchvision import transforms
    from PIL import Image
    
    writer = SummaryWriter("logs")
    img = Image.open("../images/pytorch.webp")
    
    # 1.ToTensor
    trans_totensor = transforms.ToTensor()
    img_tensor = trans_totensor(img)
    writer.add_image("ToTensor", img_tensor)
    
    # 2.Compose - Resize - 2
    trans_resize_2 = transforms.Resize(100)
    # PIL -> PIL -> tensor
    trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
    img_resize_2 = trans_compose(img)
    writer.add_image("Compose - Resize", img_resize_2, 0)
    writer.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.5 RandomCrop

    from torch.utils.tensorboard import SummaryWriter
    from torchvision import transforms
    from PIL import Image
    
    writer = SummaryWriter("logs")
    img = Image.open("../images/pytorch.webp")
    
    # 1.ToTensor
    trans_totensor = transforms.ToTensor()
    img_tensor = trans_totensor(img)
    writer.add_image("ToTensor", img_tensor)
    
    # 2.RandomCrop
    trans_random = transforms.RandomCrop((250, 444))
    trans_compose_2 = transforms.Compose([trans_random, trans_totensor])
    for i in range(10):
        img_crop = trans_compose_2(img)
        writer.add_image("RandomCrop", 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

  • 相关阅读:
    R语言提取站点的nc文件时间序列数据
    根据服务器系统选择对应的MySQL版本
    [Ynoi2006]rsrams
    python-----字典练习
    微服务架构最佳实践:消息队列
    猿创征文|链路追踪-Skywalking入门
    商业化广告--体系学习-- 8 -- 业务实战篇 -- 计价与效果(下):如何制定合理的效果评估指标?
    SRP:单一职责原则
    vue3表单参数校验+正则表达式
    docker-compose 中 depends_on 的作用
  • 原文地址:https://blog.csdn.net/m0_70885101/article/details/127866082