《電子技術(shù)應(yīng)用》
您所在的位置:首頁 > 人工智能 > 業(yè)界動態(tài) > 基于PyTorch的圖像數(shù)據(jù)增強技術(shù)

基于PyTorch的圖像數(shù)據(jù)增強技術(shù)

2022-11-29
來源:機械視覺沙龍
關(guān)鍵詞: PyTorch 圖像數(shù)據(jù)

  機器學習或深度學習模型的訓練的目標是成為“通用”模型。這就需要模型沒有過度擬合訓練數(shù)據(jù)集,或者換句話說,我們的模型對看不見的數(shù)據(jù)有很好的了解。數(shù)據(jù)增強也是避免過度擬合的眾多方法之一。

  擴展用于訓練模型的數(shù)據(jù)量的過程稱為數(shù)據(jù)增強。通過訓練具有多種數(shù)據(jù)類型的模型,我們可以獲得更“泛化”的模型?!岸喾N數(shù)據(jù)類型”是什么意思呢?本片文章只討論“圖像”數(shù)據(jù)增強技術(shù),只詳細地介紹各種圖片數(shù)據(jù)增強策略。我們還將使用 PyTorch 動手實踐并實現(xiàn)圖像數(shù)據(jù)或計算機視覺中主要使用的數(shù)據(jù)增強技術(shù)。

66.JPG

  因為介紹的是數(shù)據(jù)增強技術(shù)。所以只使用一張圖片就可以了,我們先看看可視話的代碼

  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

  此函數(shù)用于將圖像的高度和寬度調(diào)整為我們想要的特定大小。下面的代碼演示了我們想要將圖像從其原始大小調(diào)整為 224 x 224。

  path = './kitten.jpeg'

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

  imshow(path, transform)

 67.JPG

  Cropping

  該技術(shù)將要選擇的圖像的一部分應(yīng)用于新圖像。例如,使用 CenterCrop 來返回一個中心裁剪的圖像。

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

  imshow(path, transform)

  68.JPG

  RandomResizedCrop

  這種方法同時結(jié)合了裁剪和調(diào)整大小。

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

  imshow(path, transform)

 69.JPG

  Flipping

  水平或垂直翻轉(zhuǎn)圖像,下面代碼將嘗試應(yīng)用水平翻轉(zhuǎn)到我們的圖像。

  transform = transforms.RandomHorizontalFlip()

  imshow(path, transform)

70.JPG

  Padding

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

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

  imshow(path, transform)

71.JPG

  Rotation

  對圖像隨機施加旋轉(zhuǎn)角度。我們將這個角設(shè)為15度。

  transform = transforms.RandomRotation(15)

  imshow(path, transform)

 72.JPG

  Random Affine

  這種技術(shù)是一種保持中心不變的變換。這種技術(shù)有一些參數(shù):

  degrees:旋轉(zhuǎn)角度

  translate:水平和垂直轉(zhuǎn)換

  scale:縮放參數(shù)

  share:圖片裁剪參數(shù)

  fillcolor:圖像外部填充的顏色

  transform = transforms.RandomAffine(1, translate=(0.5, 0.5), scale=(1, 1), shear=(1,1), fillcolor=(256,256,256))

  imshow(path, transform)

 73.JPG

  Gaussian Blur

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

  transform = transforms.GaussianBlur(7, 3)

  imshow(path, transform)

 74.JPG

  Grayscale

  將彩色圖像轉(zhuǎn)換為灰度。

  transform = transforms.Grayscale(num\_output\_channels=3)

  imshow(path, transform)

75.JPG

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

  Brightness

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

  transform = transforms.ColorJitter(brightness=2)

  imshow(path, transform)

76.JPG

  Contrast

  圖像最暗和最亮部分之間的區(qū)別程度被稱為對比度。圖像的對比度也可以作為增強進行調(diào)整。

  transform = transforms.ColorJitter(contrast=2)

  imshow(path, transform)

77.JPG

  Saturation

  圖片中顏色的分離被定義為飽和度。

  transform = transforms.ColorJitter(saturation=20)

  imshow(path, transform)

 78.JPG

  Hue

  色調(diào)被定義為圖片中顏色的深淺。

  transform = transforms.ColorJitter(hue=2)

  imshow(path, transform)

  79.JPG

  總結(jié)

  圖像本身的變化將有助于模型對未見數(shù)據(jù)的泛化,從而不會對數(shù)據(jù)進行過擬合。



更多信息可以來這里獲取==>>電子技術(shù)應(yīng)用-AET<< 

mmexport1621241704608.jpg

本站內(nèi)容除特別聲明的原創(chuàng)文章之外,轉(zhuǎn)載內(nèi)容只為傳遞更多信息,并不代表本網(wǎng)站贊同其觀點。轉(zhuǎn)載的所有的文章、圖片、音/視頻文件等資料的版權(quán)歸版權(quán)所有權(quán)人所有。本站采用的非本站原創(chuàng)文章及圖片等內(nèi)容無法一一聯(lián)系確認版權(quán)者。如涉及作品內(nèi)容、版權(quán)和其它問題,請及時通過電子郵件或電話通知我們,以便迅速采取適當措施,避免給雙方造成不必要的經(jīng)濟損失。聯(lián)系電話:010-82306118;郵箱:aet@chinaaet.com。