天天看點

12個常用的圖像資料增強技術總結

作者:deephub

機器學習或深度學習模型的訓練的目标是成為“通用”模型。這就需要模型沒有過度拟合訓練資料集,或者換句話說,我們的模型對看不見的資料有很好的了解。資料增強也是避免過度拟合的衆多方法之一。

擴充用于訓練模型的資料量的過程稱為資料增強。通過訓練具有多種資料類型的模型,我們可以獲得更“泛化”的模型。 “多種資料類型”是什麼意思呢?本片文章隻讨論“圖像”資料增強技術,隻詳細地介紹各種圖檔資料增強政策。我們還将使用 PyTorch 動手實踐并實作圖像資料或計算機視覺中主要使用的資料增強技術。

12個常用的圖像資料增強技術總結

因為介紹的是資料增強技術。是以隻使用一張圖檔就可以了,我們先看看可視話的代碼

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)

12個常用的圖像資料增強技術總結

Cropping

該技術将要選擇的圖像的一部分應用于新圖像。 例如,使用 CenterCrop 來傳回一個中心裁剪的圖像。

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

imshow(path, transform)

12個常用的圖像資料增強技術總結

RandomResizedCrop

這種方法同時結合了裁剪和調整大小。

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

imshow(path, transform)

12個常用的圖像資料增強技術總結

Flipping

水準或垂直翻轉圖像,下面代碼将嘗試應用水準翻轉到我們的圖像。

transform = transforms.RandomHorizontalFlip()

imshow(path, transform)

12個常用的圖像資料增強技術總結

Padding

填充包括在圖像的所有邊緣上按指定的數量填充。我們将每條邊填充50像素。

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

imshow(path, transform)

12個常用的圖像資料增強技術總結

Rotation

對圖像随機施加旋轉角度。我們将這個角設為15度。

transform = transforms.RandomRotation(15)

imshow(path, transform)

12個常用的圖像資料增強技術總結

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)

12個常用的圖像資料增強技術總結

Gaussian Blur

圖像将使用高斯模糊進行模糊處理。

transform = transforms.GaussianBlur(7, 3)

imshow(path, transform)

12個常用的圖像資料增強技術總結

Grayscale

将彩色圖像轉換為灰階。

transform = transforms.Grayscale(num_output_channels=3)

imshow(path, transform)

12個常用的圖像資料增強技術總結

顔色增強,也稱為顔色抖動,是通過改變圖像的像素值來修改圖像的顔色屬性的過程。下面的方法都是顔色相關的操作。

Brightness

改變圖像的亮度當與原始圖像對比時,生成的圖像變暗或變亮。

transform = transforms.ColorJitter(brightness=2)

imshow(path, transform)

12個常用的圖像資料增強技術總結

Contrast

圖像最暗和最亮部分之間的差別程度被稱為對比度。圖像的對比度也可以作為增強進行調整。

transform = transforms.ColorJitter(contrast=2)

imshow(path, transform)

12個常用的圖像資料增強技術總結

Saturation

圖檔中顔色的分離被定義為飽和度。

transform = transforms.ColorJitter(saturation=20)

imshow(path, transform)

12個常用的圖像資料增強技術總結

Hue

色調被定義為圖檔中顔色的深淺。

transform = transforms.ColorJitter(hue=2)

imshow(path, transform)

12個常用的圖像資料增強技術總結

總結

圖像本身的變化将有助于模型對未見資料的泛化,進而不會對資料進行過拟合。以上整理的都是我們常見的資料增強技術,torchvision中還包含了很多方法,可以在他的文檔中找到:https://pytorch.org/vision/stable/transforms.html

作者:Prabowo Yoga Wicaksana

繼續閱讀