关于人工智能:12个常用的图像数据增强技术总结

5次阅读

共计 2490 个字符,预计需要花费 7 分钟才能阅读完成。

机器学习或深度学习模型的训练的指标是成为“通用”模型。这就须要模型没有适度拟合训练数据集,或者换句话说,咱们的模型对看不见的数据有很好的理解。数据加强也是防止适度拟合的泛滥办法之一。

扩大用于训练模型的数据量的过程称为数据加强。通过训练具备多种数据类型的模型,咱们能够取得更“泛化”的模型。“多种数据类型”是什么意思呢?本片文章只探讨“图像”数据加强技术,只具体地介绍各种图片数据加强策略。咱们还将应用 PyTorch 入手实际并实现图像数据或计算机视觉中次要应用的数据加强技术。

因为介绍的是数据加强技术。所以只应用一张图片就能够了,咱们先看看可视话的代码

 import PIL.Image as Image
 import torch
 from torchvision import transforms
 import matplotlib.pyplot as plt
 import numpy as np
 import warnings
 
 def imshow(img_path, transform):
     """
     Function to show data augmentation
     Param img_path: path of the image
     Param transform: data augmentation technique to apply
     """
     img = Image.open(img_path)
     fig, ax = plt.subplots(1, 2, figsize=(15, 4))
     ax[0].set_title(f'Original image {img.size}')
     ax[0].imshow(img)
     img = transform(img)
     ax[1].set_title(f'Transformed image {img.size}')
     ax[1].imshow(img)

Resize/Rescale

此函数用于将图像的高度和宽度调整为咱们想要的特定大小。上面的代码演示了咱们想要将图像从其原始大小调整为 224 x 224。

 path = './kitten.jpeg'
 transform = transforms.Resize((224, 224))
 imshow(path, transform)

Cropping

该技术将要抉择的图像的一部分利用于新图像。例如,应用 CenterCrop 来返回一个核心裁剪的图像。

 transform = transforms.CenterCrop((224, 224))
 imshow(path, transform)

RandomResizedCrop

这种办法同时联合了裁剪和调整大小。

 transform = transforms.RandomResizedCrop((100, 300))
 imshow(path, transform)

Flipping

程度或垂直翻转图像,上面代码将尝试利用程度翻转到咱们的图像。

 transform = transforms.RandomHorizontalFlip()
 imshow(path, transform)

Padding

填充包含在图像的所有边缘上按指定的数量填充。咱们将每条边填充 50 像素。

 transform = transforms.Pad((50,50,50,50))
 imshow(path, transform)

Rotation

对图像随机施加旋转角度。咱们将这个角设为 15 度。

 transform = transforms.RandomRotation(15)
 imshow(path, transform)

Random Affine

这种技术是一种放弃核心不变的变换。这种技术有一些参数:

  • degrees:旋转角度
  • translate:程度和垂直转换
  • scale:缩放参数
  • share:图片裁剪参数
  • fillcolor:图像内部填充的色彩
 transform = transforms.RandomAffine(1, translate=(0.5, 0.5), scale=(1, 1), shear=(1,1), fillcolor=(256,256,256))
 imshow(path, transform)

Gaussian Blur

图像将应用高斯含糊进行含糊解决。

 transform = transforms.GaussianBlur(7, 3)
 imshow(path, transform)

Grayscale

将彩色图像转换为灰度。

 transform = transforms.Grayscale(num_output_channels=3)
 imshow(path, transform)

色彩加强,也称为色彩抖动,是通过扭转图像的像素值来批改图像的色彩属性的过程。上面的办法都是色彩相干的操作。

Brightness

扭转图像的亮度当与原始图像比照时,生成的图像变暗或变亮。

 transform = transforms.ColorJitter(brightness=2)
 imshow(path, transform)

Contrast

图像最暗和最亮局部之间的区别水平被称为对比度。图像的对比度也能够作为加强进行调整。

 transform = transforms.ColorJitter(contrast=2)
 imshow(path, transform)

Saturation

图片中色彩的拆散被定义为饱和度。

 transform = transforms.ColorJitter(saturation=20)
 imshow(path, transform)

Hue

色调被定义为图片中色彩的深浅。

 transform = transforms.ColorJitter(hue=2)
 imshow(path, transform)

总结

图像自身的变动将有助于模型对未见数据的泛化,从而不会对数据进行过拟合。以上整顿的都是咱们常见的数据加强技术,torchvision 中还蕴含了很多办法,能够在他的文档中找到:https://pytorch.org/vision/st…

https://avoid.overfit.cn/post/44424d7841b34cdc83a9180de677b7f6

作者:Prabowo Yoga Wicaksana

正文完
 0