语义分割 利用pytorch的transform数据增强方法同时对image和mask做数据增强

  今天阅读其他大佬的代码,发现了一种可以同时对image和mask做数据增强的方法,记录一下。

from torchvision.transforms import functional as F
from torchvision import transforms as tfs
from PIL import Image
import matplotlib.pyplot as plt
def rand_crop(image, label, height=300, width=300):
    '''
    data is PIL.Image object
    label is PIL.Image object
    '''
    crop_params = tfs.RandomCrop.get_params(image, (height, width))
    image = F.crop(image, *crop_params)
    label = F.crop(label, *crop_params)

    return image, label

img = Image.open("2007_000068.jpg")
lab = Image.open("2007_000068.png")

image, label = rand_crop(img, lab)

plt.figure()
plt.subplot(1, 2, 1)
plt.title("origin")
plt.imshow(image)

plt.subplot(1, 2, 2)
plt.title("gray")
plt.imshow(label, cmap="gray")
plt.show()

没有增强的原图

没有做增强的原图

增强之后的图片

增强过后的图片

总结:
  同时对label和mask做随机裁剪,需要先获取裁剪信息,然后再同时对其做裁剪,其他增强方法或许也能参考此方法。

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一些常用的数据增强方法PyTorch 代码示例: 1. 随机裁剪: ```python import torchvision.transforms as transforms transform = transforms.Compose([ transforms.RandomCrop(size=224), ]) ``` 2. 随机旋转: ```python import torchvision.transforms as transforms transform = transforms.Compose([ transforms.RandomRotation(degrees=30), ]) ``` 3. 随机缩放: ```python import torchvision.transforms as transforms transform = transforms.Compose([ transforms.RandomResizedCrop(size=224), ]) ``` 4. 随机翻转: ```python import torchvision.transforms as transforms transform = transforms.Compose([ transforms.RandomHorizontalFlip(), ]) ``` 5. 随机亮度、对比度、饱和度调整: ```python import torchvision.transforms as transforms transform = transforms.Compose([ transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5), ]) ``` 6. 随机噪声: ```python import torchvision.transforms as transforms transform = transforms.Compose([ transforms.RandomApply([transforms.GaussianBlur(kernel_size=3)], p=0.5), ]) ``` 7. 随机变形: ```python import torchvision.transforms as transforms transform = transforms.Compose([ transforms.RandomApply([transforms.RandomAffine(degrees=30)], p=0.5), ]) ``` 8. 随机颜色变换: ```python import torchvision.transforms as transforms transform = transforms.Compose([ transforms.ColorJitter(hue=0.5), ]) ``` 9. Mixup: ```python import random import torch import numpy as np def mixup_data(x, y, alpha=1.0): if alpha > 0: lam = np.random.beta(alpha, alpha) else: lam = 1 batch_size = x.size()[0] index = torch.randperm(batch_size) mixed_x = lam * x + (1 - lam) * x[index, :] y_a, y_b = y, y[index] return mixed_x, y_a, y_b, lam ``` 10. Cutout: ```python import random import numpy as np import torch def cutout(image, n_holes=1, length=16): h, w = image.shape[1], image.shape[2] mask = np.ones((h, w), np.float32) for n in range(n_holes): y = np.random.randint(h) x = np.random.randint(w) y1 = np.clip(y - length // 2, 0, h) y2 = np.clip(y + length // 2, 0, h) x1 = np.clip(x - length // 2, 0, w) x2 = np.clip(x + length // 2, 0, w) mask[y1:y2, x1:x2] = 0. mask = torch.from_numpy(mask) mask = mask.expand_as(image) image = image * mask return image ``` 这些代码示例可以在 PyTorch 中方便地进行数据增强,提高模型的泛化能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值