数据预处理——数据增强

数据增强(Data Augmentation)

我们常常会遇到数据不足的情况。比如,你遇到的一个任务,目前只有小几百的数据,然而,你知道目前现在流行的最先进的神经网络都是成千上万的图片数据。
为了获得更多的数据,我们只要对现有的数据集进行微小的改变。比如旋转(flips)、移位(translations)、旋转(rotations)等微小的改变。我们的网络会认为这是不同的图片。
一个卷积神经网络,如果能够对物体即使它放在不同的地方也能稳健的分类,就被称为具有不变性的属性。更具体的,CNN可以对移位(translation)、视角(viewpoint)、大小(size)、照明(illumination)(或者以上的组合)具有不变性。
这本质上是数据增强的前提。在现实场景中,我们可能会有一批在有限场景中拍摄的数据集。但是我们的目标应用可能存在于不同的条件,比如在不同的方向、位置、缩放比例、亮度等。

import matplotlib.pyplot as plt
import torchvision
from PIL import Image

path = ["imgs/1.jpg"]
for index, p in enumerate(path):
    plt.subplot(2,2,1+index)
    img_array = Image.open(p)
    print("原始图像大小为:",img_array.size)
    # 一定比例对图像进行缩放
    # new_img_array = torchvision.transforms.Resize((224,224))(img_array)
    # 随机剪裁
    # new_img_array = torchvision.transforms.RandomCrop((224, 224))(img_array)
    # 随即水平翻转
    # new_img_array  = torchvision.transforms.RandomHorizontalFlip()(img_array)
    # print("resize后的图像大小为:",new_img_array.size)
    # 随机角度翻转
    # new_img_array = torchvision.transforms.RandomRotation(45)(img_array)
    # 亮度、对比度
    # new_img_array = torchvision.transforms.ColorJitter(brightness=1)(img_array)
    # new_img_array = torchvision.transforms.ColorJitter(contrast=1)(img_array)
    # 彩色图转灰色图
    new_img_array = torchvision.transforms.Grayscale()(img_array)
    # transforms.Compose()串联多个transform操作
    trans_totensor = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
    img_tensor = trans_totensor(new_img_array)
    print(img_tensor.shape)
    plt.imshow(new_img_array)
    plt.show()

原始图像大小为: (486, 500)
torch.Size([1, 500, 486])

在这里插入图片描述

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值