【问题标题】:For semantic segmentation how to perform data augmentation in Pytorch?对于语义分割,如何在 Pytorch 中执行数据增强?
【发布时间】:2020-11-04 21:56:51
【问题描述】:

我正在使用 PyTorch 进行语义分割,但我遇到了一个问题,因为我使用的是图像,以及它们的标签。我想执行数据增强,例如 RandomHorizo​​ntalFlip 和 RandomCrop 等。

这是我的代码,请检查并告诉我,如何在提供的代码中嵌入以下操作。

import torchvision.transforms.functional as F

class ToTensor(object):
    def __call__(self, sample):
        image, label = sample['image'], sample['label']
        return {'image': F.to_tensor(image), 'label': F.to_tensor(label)}

my_transform = transforms.Compose([ ToTensor() ])

dataset = Mydataset(image_dir, label_dir, transform = my_transform)

# Print dataset output
dataset[1]

输出

{'image': tensor([[[0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          ...,
          [0.0902, 0.0902, 0.0902,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0745, 0.0745, 0.0745,  ..., 0.0824, 0.0824, 0.0824]],
 
         [[0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          ...,
          [0.0902, 0.0902, 0.0902,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0745, 0.0745, 0.0745,  ..., 0.0824, 0.0824, 0.0824]],
 
         [[0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          ...,
          [0.0902, 0.0902, 0.0902,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0745, 0.0745, 0.0745,  ..., 0.0824, 0.0824, 0.0824]]]),
 'label': tensor([[[0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          ...,
          [0.0902, 0.0902, 0.0902,  ..., 0.0824, 0.0824, 0.0824],
          [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
          [0.0745, 0.0745, 0.0745,  ..., 0.0824, 0.0824, 0.0824]]])}

【问题讨论】:

    标签: python pytorch transformation torchvision


    【解决方案1】:

    您应该使用 transforms.Compose() 方法来组合不同的转换。例如:

    my_transforms = transforms.Compose([  
        transforms.RandomHorizontalFlip(),
        transforms.RandomRotation(20),
        transforms.RandomRotation(10),
        transforms.RandomCrop((512, 512)),
        transforms.ColorJitter(brightness=0.2, saturation=0.2, contrast=0.2),
        transforms.ToTensor()
        ])
    dataset = Mydataset(image_dir, label_dir, transform = my_transforms)
    

    【讨论】:

    • 但我希望我的 ToTensor() 返回 {'image': F.to_tensor(image), 'label': F.to_tensor(label)}
    猜你喜欢
    • 2020-02-01
    • 2019-11-29
    • 2016-11-04
    • 2020-05-03
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多