深度學(xué)習(xí)煉丹-數(shù)據(jù)預(yù)處理和增強(qiáng)

前言

一般機(jī)器學(xué)習(xí)任務(wù)其工作流程可總結(jié)為如下 pipeline。

機(jī)器學(xué)習(xí)任務(wù)pipeline

在工業(yè)界,數(shù)據(jù)預(yù)處理步驟對(duì)模型精度的提高的發(fā)揮著重要作用。對(duì)于機(jī)器學(xué)習(xí)任務(wù)來(lái)說(shuō),廣泛的數(shù)據(jù)預(yù)處理一般有四個(gè)階段(視覺(jué)任務(wù)一般只需 Data Transformation): 數(shù)據(jù)清洗(Data Cleaning)、數(shù)據(jù)整合(Data Integration)、數(shù)據(jù)轉(zhuǎn)換(Data Transformation)和數(shù)據(jù)縮減(Data Reduction)。

steps_in_data_preprocessing

1,Data Cleaning 數(shù)據(jù)清理是數(shù)據(jù)預(yù)處理步驟的一部分,通過(guò)填充缺失值、平滑噪聲數(shù)據(jù)、解決不一致和刪除異常值來(lái)清理數(shù)據(jù)。
2,Data Integration 用于將存在于多個(gè)源中的數(shù)據(jù)合并到一個(gè)更大的數(shù)據(jù)存儲(chǔ)中,如數(shù)據(jù)倉(cāng)庫(kù)。例如,將來(lái)自多個(gè)醫(yī)療節(jié)點(diǎn)的圖像整合起來(lái),形成一個(gè)更大的數(shù)據(jù)庫(kù)。
3,在完成 Data Cleaning 后,我們需要通過(guò)使用下述數(shù)據(jù)轉(zhuǎn)換策略更改數(shù)據(jù)的值、結(jié)構(gòu)或格式。

  • Generalization: 使用概念層次結(jié)構(gòu)將低級(jí)或粒度數(shù)據(jù)轉(zhuǎn)換為高級(jí)信息。例如將城市等地址中的原始數(shù)據(jù)轉(zhuǎn)化為國(guó)家等更高層次的信息。
  • Normalization: 目的是將數(shù)字屬性按比例放大或縮小以適合指定范圍。Normalization 常見(jiàn)方法:
    • Min-max normalization
    • Z-Score normalization
    • Decimal scaling normalization

4,Data Reduction 數(shù)據(jù)倉(cāng)庫(kù)中數(shù)據(jù)集的大小可能太大而無(wú)法通過(guò)數(shù)據(jù)分析和數(shù)據(jù)挖掘算法進(jìn)行處理。一種可能的解決方案是獲得數(shù)據(jù)集的縮減表示,該數(shù)據(jù)集的體積要小得多,但會(huì)產(chǎn)生相同質(zhì)量的分析結(jié)果。常見(jiàn)的數(shù)據(jù)縮減策略如下:

  • Data cube aggregation
  • Dimensionality reduction: 降維技術(shù)用于執(zhí)行特征提取。數(shù)據(jù)集的維度是指數(shù)據(jù)的屬性或個(gè)體特征。該技術(shù)旨在減少我們?cè)跈C(jī)器學(xué)習(xí)算法中考慮的冗余特征的數(shù)量。降維可以使用主成分分析(PCA)等技術(shù)來(lái)完成。
  • Data compression: 通過(guò)使用編碼技術(shù),數(shù)據(jù)的大小可以顯著減小。
  • Discretization: 數(shù)據(jù)離散化用于將具有連續(xù)性的屬性劃分為具有區(qū)間的數(shù)據(jù)。這樣做是因?yàn)檫B續(xù)特征往往與目標(biāo)變量相關(guān)的可能性較小。例如,屬性年齡可以離散化為 18 歲以下、18-44 歲、44-60 歲、60 歲以上等區(qū)間。

對(duì)于計(jì)算機(jī)視覺(jué)任務(wù)來(lái)說(shuō),在訓(xùn)練 CNN 模型之前,對(duì)于輸入數(shù)據(jù)特征做歸一化(normalization)預(yù)處理(data preprocessing)操作是最常見(jiàn)的步驟。

一,Normalization 概述

這里沒(méi)有翻譯成中文,是因?yàn)槟壳爸形姆g有些歧義,根據(jù)我查閱的博客資料,翻譯為“歸一化”比較多,僅供可參考。

1.1,Normalization 定義

Normalization 操作被用于對(duì)數(shù)據(jù)屬性進(jìn)行縮放,使其落在較小的范圍之內(nèi)(即變化到某個(gè)固定區(qū)間中),比如 [-1,1] 和 [0, 1],簡(jiǎn)單理解就是特征縮放過(guò)程。很多機(jī)器學(xué)習(xí)算法都受益于 Normalization 操作,比如:

  • 通常對(duì)分類(lèi)算法有用。
  • 在梯度下降等機(jī)器學(xué)習(xí)算法的核心中使用的優(yōu)化算法很有用。
  • 對(duì)于加權(quán)輸入的算法(如回歸和神經(jīng)網(wǎng)絡(luò))以及使用距離度量的算法(如 K 最近鄰)也很有用。

1.2,什么情況需要 Normalization

當(dāng)我們處理的數(shù)據(jù)具有不同尺度(范圍)different scale)時(shí),通常就需要進(jìn)行 normalization 操作了,它可能會(huì)導(dǎo)致一個(gè)重要屬性(在較低尺度上)的有效性被稀釋?zhuān)驗(yàn)槠渌麑傩跃哂懈蠓秶ǔ叨龋┑闹?,?jiǎn)單點(diǎn)理解就是范圍(scale)大點(diǎn)屬性在模型當(dāng)中更具優(yōu)先級(jí),具體示例如下圖所示。

different scale

總而言之,就是當(dāng)數(shù)據(jù)存在多個(gè)屬性但其值具有不同尺度(scale)時(shí),這可能會(huì)導(dǎo)致我們?cè)谧鰯?shù)據(jù)挖掘操作時(shí)數(shù)據(jù)模型表現(xiàn)不佳,因此執(zhí)行 normalization 操作將所有屬性置于相同的尺寸內(nèi)是很有必要的。

1.3,Data Normalization 方法

1,z-Score Normalization

zero-mean Normalization,有時(shí)也稱(chēng)為 standardization,將數(shù)據(jù)特征縮放成均值為 0,方差為 1 的分布,對(duì)應(yīng)公式:

{x}' = \frac{x-mean(x)}{\sigma}

其中 mean(x)(有些地方用 \mu =\frac{1}{N}\sum_{i=1}^{N} x_i) 表示變量 x 的均值,\sigma = \sqrt{\frac{1}{N} \sum_{i=1}^{N}(x_i - \mu)^2} 表示變量的標(biāo)準(zhǔn)差,{x}' 是數(shù)據(jù)縮放后的新值。

2,Min-Max Normalization

執(zhí)行線(xiàn)性操作,將數(shù)據(jù)范圍縮放到 [0,1] 區(qū)間內(nèi),對(duì)應(yīng)公式:

{x}' = \frac{x - min(x)}{max(x) - min(x)}

其中 max(x) 是變量最大值,min(x) 是變量最小值。

1.4,示例代碼

1,以下是使用 Python 和 Numpy 庫(kù)實(shí)現(xiàn) Min-Max Normalization 的示例代碼:

# 導(dǎo)入必要的庫(kù)
import numpy as np
# 定義數(shù)據(jù)集
X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 計(jì)算數(shù)據(jù)集的最小值和最大值
Xmin = X.min()
Xmax = X.max()
# 計(jì)算最小-最大規(guī)范化
X_norm = (X - Xmin) / (Xmax - Xmin)
# 打印結(jié)果
print(X_norm)

程序輸出結(jié)果如下,可以看出原始數(shù)組數(shù)據(jù)都被縮放到 [0, 1] 范圍內(nèi)了。

程序輸出結(jié)果

二,normalize images

2.1,圖像 normalization 定義

當(dāng)我們使用卷積神經(jīng)網(wǎng)絡(luò)解決計(jì)算機(jī)視覺(jué)任務(wù)時(shí),一般需要對(duì)輸入圖像數(shù)據(jù)做 normalization 來(lái)完成預(yù)處理工作,常見(jiàn)的圖像 normalization 方法有兩種: min-max normalizationzero-mean normalization。

1,以單張圖像的 zero-mean Normalization 為例,它使得圖像的均值和標(biāo)準(zhǔn)差分別變?yōu)?0.01.0。因?yàn)槭嵌嗑S數(shù)據(jù),與純表格數(shù)據(jù)不同,它首先需要從每個(gè)輸入通道中減去通道平均值,然后將結(jié)果除以通道標(biāo)準(zhǔn)差。因此可定義兩種 normalization 形式如下所示:

# min-max Normalization
output[channel] = (input[channel] - min[channel]) / (max[channel] - min[channel])
# zero-mean Normalization
output[channel] = (input[channel] - mean[channel]) / std[channel]

2.2,圖像 normalization 的好處

圖像 normalization 有助于使數(shù)據(jù)處于一定范圍內(nèi)并減少偏度skewness),從而有助于模型更快更好地學(xué)習(xí)。歸一化還可以解決梯度遞減和爆炸的問(wèn)題。

2.3,PyTorch 實(shí)踐圖像 normalization

Pytorch 框架中,圖像變換(image transformation)是指將圖像像素的原始值改變?yōu)樾轮档倪^(guò)程。其中常見(jiàn)的 transformation 操作是使用 torchvision.transforms.ToTensor() 方法將圖像變換為 Pytorch 張量(tensor),它實(shí)現(xiàn)了將像素范圍為 [0, 255] 的 PIL 圖像轉(zhuǎn)換為形狀為(C,H,W)且范圍為 [0.0, 1.0] 的 Pytorch FloatTensor。另外,torchvision.transforms.normalize() 方法實(shí)現(xiàn)了逐 channel 的對(duì)圖像進(jìn)行標(biāo)準(zhǔn)化(均值變?yōu)?0,標(biāo)準(zhǔn)差變?yōu)?1)??偨Y(jié)如下:

  • min-max Normalization: 對(duì)應(yīng) torchvision.transforms.ToTensor() 方法
  • zero-mean Normalization: 對(duì)應(yīng) torchvision.transforms.Normalize() 方法,利用用均值和標(biāo)準(zhǔn)差對(duì)張量圖像進(jìn)行 zero-mean Normalization。

ToTensor() 函數(shù)的語(yǔ)法如下:

"""
Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
Converts a PIL Image or numpy.ndarray (H x W x C) in the range
[0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].

Args:
    pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
Returns:
    Tensor: Converted image.
"""

Normalize() 函數(shù)的語(yǔ)法如下:

Syntax: torchvision.transforms.Normalize()

Parameter:
    * mean: Sequence of means for each channel.
    * std: Sequence of standard deviations for each channel.
    * inplace: Bool to make this operation in-place.
Returns: Normalized Tensor image.

在 PyTorch 中對(duì)圖像執(zhí)行 zero-mean Normalization 的步驟如下:

  1. 加載原圖像;
  2. 使用 ToTensor() 函數(shù)將圖像轉(zhuǎn)換為 Tensors;
  3. 計(jì)算 Tensors 的均值和方差;
  4. 使用 Normalize() 函數(shù)執(zhí)行 zero-mean Normalization 操作。

下面給出利用 PyTorch 實(shí)踐 Normalization 操作的詳細(xì)代碼和輸出圖。

# import necessary libraries
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import torchvision.transforms as transforms
import matplotlib.pyplot as plt

def show_images(imgs, num_rows, num_cols, titles=None, scale=8.5):
    """Plot a list of images.

    Defined in :numref:`sec_utils`"""
    figsize = (num_cols * scale, num_rows * scale)
    _, axes = plt.subplots(num_rows, num_cols, figsize=figsize)
    axes = axes.flatten()
    for i, (ax, img) in enumerate(zip(axes, imgs)):
        try:
            img = np.array(img)
        except:
            pass
        ax.imshow(img)
        ax.axes.get_xaxis().set_visible(False)
        ax.axes.get_yaxis().set_visible(False)
        if titles:
            ax.set_title(titles[i])
    return axes

def normalize_image(image_path):
    img = Image.open(img_path) # load the image
    # 1, use ToTensor function
    transform = transforms.Compose([
        transforms.ToTensor()
    ])
    img_tensor = transform(img) # transform the pIL image to tensor
    # 2, calculate mean and std by tensor's attributes
    mean, std = img_tensor.mean([1,2]), img_tensor.std([1,2])
    # 3, use Normalize function
    transform_norm = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean, std)
    ])
    img_normalized = transform_norm(img) # get normalized image

    img_np = np.array(img) # convert PIL image to numpy array
    # print array‘s shape mean and std
    print(img_np.shape) # (height, width, channel), (768, 1024, 3)
    print("mean and std before normalize:")
    print("Mean of the image:", mean)
    print("Std of the image:", std)
    return img_np, img_tensor, img_normalized

def convert_tensor_np(tensor):
    img_arr = np.array(tensor)
    img_tr = img_arr.transpose(1, 2, 0)
    return img_tr

if __name__ == '__main__': 
    img_path = 'Koalainputimage.jpeg'
    img_np, img_tensor, img_normalized = normalize_image(img_path)
    # transpose tensor to numpy array and shape of (3,,) to shape of (,,3)
    img_normalized1 = convert_tensor_np(img_tensor)
    img_normalized2 = convert_tensor_np(img_normalized)
    show_images([img_np, img_normalized1, img_normalized2], 1, 3, titles=["orignal","min-max normalization", "zero-mean normalization"])

1,程序輸出和兩種 normalization 操作效果可視化對(duì)比圖如下所示:

normalization效果圖

2,原圖和兩種 normalization 操作后的圖像像素值分布可視化對(duì)比圖如下所示:

normalization_dist_visual

像素值分布可視化用的代碼如下。

# plot the pixel values
plt.hist(img_np.ravel(), bins=50, density=True)
plt.xlabel("pixel values")
plt.ylabel("relative frequency")
plt.title("distribution of pixels")

三,數(shù)據(jù)增強(qiáng)(擴(kuò)增)

數(shù)據(jù)增強(qiáng)的目的是為了擴(kuò)充數(shù)據(jù)和提升模型的泛化能力。有效的數(shù)據(jù)擴(kuò)充不僅能擴(kuò)充訓(xùn)練樣本數(shù)量,還能增加訓(xùn)練樣本的多樣性,一方面可避免過(guò)擬合,另一方面又會(huì)帶來(lái)模型性能的提升。

在機(jī)器學(xué)習(xí)管道(pipeline)框架中,我們需要在送入模型之前,進(jìn)行數(shù)據(jù)增強(qiáng),一般有兩種處理方式:

  • 線(xiàn)下增強(qiáng)(offline augmentation): 適用于較小的數(shù)據(jù)集(smaller dataset)。
  • 線(xiàn)上增強(qiáng)(online augmentation): 適用于較大的數(shù)據(jù)集(larger datasets)。

數(shù)據(jù)擴(kuò)增幾種常用方法有: 圖像水平/豎直翻轉(zhuǎn)、隨機(jī)摳取、尺度變換和旋轉(zhuǎn)。其中尺度變換(scaling)、旋轉(zhuǎn)(rotating)等方法用來(lái)增加卷積卷積神經(jīng)網(wǎng)絡(luò)對(duì)物體尺度和方向上的魯棒性。

在此基礎(chǔ)上,對(duì)原圖或已變換的圖像(或圖像塊)進(jìn)行色彩抖動(dòng)(color jittering)也是一種常用的數(shù)據(jù)擴(kuò)充手段,即改變圖像顏色的四個(gè)方面:亮度、對(duì)比度、飽和度和色調(diào)。色彩抖動(dòng)是在 RGB 顏色空間對(duì)原有 RGB 色彩分布進(jìn)行輕微的擾動(dòng),也可在 HSV 顏色空間嘗試隨機(jī)改變圖像原有 的飽和度和明度(即,改變 SV 通道的值)或?qū)ι{(diào)進(jìn)行微調(diào)(小范圍改變 該通道的值)。

HSV 表達(dá)彩色圖像的方式由三個(gè)部分組成:
Hue(色調(diào)、色相)
Saturation(飽和度、色彩純凈度)
Value(明度)

3.1,opencv 圖像處理

圖像的幾何變換

OpenCV 提供的幾何變換函數(shù)如下所示:

1,拓展縮放: 拓展縮放,改變圖像的尺寸大小

cv2.resize(): 。常用的參數(shù)有設(shè)定圖像尺寸、縮放因子和插值方法。

2,平移: 將對(duì)象換一個(gè)位置。

cv2.warpAffine(): 函數(shù)第一個(gè)參數(shù)是原圖像,第二個(gè)參數(shù)是移動(dòng)矩陣,第三個(gè)參數(shù)是輸出圖像大小 (width,height)。舉例,如果要沿 (x,y) 方向移動(dòng),移動(dòng)的距離是 (tx ,ty),則以下面的方式構(gòu)建移動(dòng)矩陣:
\begin{bmatrix} 1 & 0 & t_x\\ 0 & 1 & t_y \end{bmatrix}

3,旋轉(zhuǎn): 對(duì)一個(gè)圖像旋轉(zhuǎn)角度 \theta。

先使用 cv2.getRotationMatrix2D 函數(shù)構(gòu)建旋轉(zhuǎn)矩陣 M,再使用 cv2.warpAffine() 函數(shù)將對(duì)象移動(dòng)位置。

getRotationMatrix2D 函數(shù)第一個(gè)參數(shù)為旋轉(zhuǎn)中心,第二個(gè)為旋轉(zhuǎn)角度,第三個(gè)為旋轉(zhuǎn)后的縮放因子

4,放射變換(也叫平面變換/徑向變換): 在仿射變換中,原圖中所有的平行線(xiàn)在結(jié)果圖像中依舊平行。

為了找到變換矩陣,我們需要從輸入圖像中得到三個(gè)點(diǎn),以及它們?cè)谳敵鰣D像中的對(duì)應(yīng)位置。然后使用 cv2. getAffineTransform 先構(gòu)建一個(gè) 2x3 變換矩陣,最后該矩陣將傳遞給 cv2.warpAffine 函數(shù)。

5,透視變換(也叫空間變換): 轉(zhuǎn)換之后,直線(xiàn)仍是直線(xiàn)。

原理: 透視變換(Perspective Transformation)是指利用透視中心、像點(diǎn)、目標(biāo)點(diǎn)三點(diǎn)共線(xiàn)的條件,按透視旋轉(zhuǎn)定律使承影面(透視面)繞跡線(xiàn)(透視軸)旋轉(zhuǎn)某一角度,破壞原有的投影光線(xiàn)束,仍能保持承影面上投影幾何圖形不變的變換。-來(lái)源百度百科。

對(duì)于透視變換,需要先構(gòu)建一個(gè) 3x3 變換矩陣。要找到此變換矩陣,需要在輸入圖像上找 4 個(gè)點(diǎn),以及它們?cè)谳敵鰣D像中的對(duì)應(yīng)位置。在這 4 個(gè)點(diǎn)中,其中任意 3 個(gè)不共線(xiàn)。然后可以通過(guò)函數(shù) cv2.getPerspectiveTransform 找到變換矩陣,將 cv2.warpPerspective 應(yīng)用于此 3x3 變換矩陣。

圖像幾何變換的實(shí)例代碼如下:

import cv2
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np

# Some Geometric Transformation of Images
class GeometricTransAug(object):
    def __init__(self, image_path):
        img = Image.open(image_path) # load the image
        self.img_np = np.array(img) # convert PIL image to numpy array
        self.rows, self.cols, self.ch = self.img_np.shape
        self.geometry_trans_aug_visual(self.img_np)
        
    def resize_aug(self, img_np):
        # 直接設(shè)置了縮放因子, 縮放原大小的2倍
        res = cv2.resize(img_np, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
        return res
    
    def warpAffine_aug(self, img_np):
        # 先構(gòu)建轉(zhuǎn)換矩陣, 將圖像像素點(diǎn)整體進(jìn)行(100,50)位移:
        M = np.float32([[1,0,100],[0,1,50]]) 
        res = cv2.warpAffine(img_np, M,(self.cols, self.rows))
        return res
    
    def rotation_aug(self, img_np):
        rows, cols, ch = img_np.shape
        # 先構(gòu)建轉(zhuǎn)換矩陣,圖像相對(duì)于中心旋轉(zhuǎn)90度而不進(jìn)行任何縮放。
        M = cv2.getRotationMatrix2D((self.cols/2, self.rows/2), 90, 1) 
        res = cv2.warpAffine(img_np, M, (self.cols, self.rows))
        return res
    
    def radial_trans_aug(self, img_np):
        # 仿射變換需要從原圖像中找到三個(gè)點(diǎn)以及他們?cè)谳敵鰣D像中的位置
        pts1 = np.float32([[50,50],[200,50],[50,200]])
        pts2 = np.float32([[10,100],[200,50],[100,250]])
        # 通過(guò) getAffineTransform 創(chuàng)建一個(gè) 2x3 的轉(zhuǎn)換矩陣
        M = cv2.getAffineTransform(pts1,pts2)

        res = cv2.warpAffine(img_np, M, dsize = (self.cols, self.rows))
        return res
    
    def perspective_trans_aug(self, img_np):
        # 透視變換需要一個(gè) 3x3 變換矩陣
        pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
        pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])

        M = cv2.getPerspectiveTransform(pts1,pts2)
        # dsize: size of the output image.
        res = cv2.warpPerspective(img_np, M, dsize = (300,300))
        
        return res
    
    def geometry_trans_aug_visual(self, img_np):
        res1 = self.resize_aug(img_np)
        res2 = self.warpAffine_aug(img_np)
        res3 = self.rotation_aug(img_np)
        res4 = self.radial_trans_aug(img_np)
        res5 = self.perspective_trans_aug(img_np)
        imgs = [res1, res2, res3, res4, res5]
        aug_titles = ["resize_aug", "warpAffine_aug", "rotation_aug", "radial_trans_aug", "perspective_trans_aug"]
        # show_images 函數(shù)前文已經(jīng)給出,這里不再?gòu)?fù)制過(guò)來(lái)
        axes = show_images(imgs, 2, 3, titles=aug_titles, scale=8.5)
            
if __name__ == '__main__': 
    img_path = 'Koalainputimage.jpeg'
    geometry_trans_aug = GeometricTransAug(img_path)
    img_np2 = geometry_trans_aug.img_np
    print(img_np2.shape)

程序運(yùn)行后輸出的幾何變換增強(qiáng)效果如下所示:

opencv幾何變換增強(qiáng)

3.2,pytorch 圖像增強(qiáng)

pytorch 框架中,transforms 類(lèi)提供了 22 個(gè)數(shù)據(jù)增強(qiáng)方法,對(duì)應(yīng)代碼在 transforms.py 文件中,它們既可以對(duì) PIL Image 也能對(duì) torch.*Tensor 數(shù)據(jù)類(lèi)型進(jìn)行增強(qiáng)。

api 的詳細(xì)介紹可以參考官網(wǎng)文檔-Transforming and augmenting images。本章只對(duì) transforms 的 22 個(gè)方法進(jìn)行簡(jiǎn)要介紹和總結(jié)。

總的來(lái)說(shuō) transforms.py 中的各個(gè)預(yù)處理方法可以歸納為四大類(lèi):

1,裁剪-Crop

  • 中心裁剪: transforms.CenterCrop
  • 隨機(jī)裁剪: transforms.RandomCrop
  • 隨機(jī)長(zhǎng)寬比裁剪: transforms.RandomResizedCrop
  • 上下左右中心裁剪: transforms.FiveCrop
  • 上下左右中心裁剪后翻轉(zhuǎn): transforms.TenCrop

2,翻轉(zhuǎn)和變換-Flip and Rotations

  • 依概率 p 水平翻轉(zhuǎn):transforms.RandomHorizontalFlip(p=0.5)
  • 依概率 p 垂直翻轉(zhuǎn):transforms.RandomVerticalFlip(p=0.5)
  • 隨機(jī)旋轉(zhuǎn):transforms.RandomRotation

3,圖像變換

  • resize: transforms.Resize
  • min-max Normalization: 對(duì)應(yīng) torchvision.transforms.ToTensor() 方法
  • zero-mean Normalization: 對(duì)應(yīng) torchvision.transforms.Normalize() 方法
  • 填充: transforms.Pad
  • 修改亮度、對(duì)比度和飽和度:transforms.ColorJitter
  • 轉(zhuǎn)灰度圖: transforms.Grayscale
  • 線(xiàn)性變換: transforms.LinearTransformation()
  • 仿射變換: transforms.RandomAffine
  • 依概率 p 轉(zhuǎn)為灰度圖: transforms.RandomGrayscale
  • 將數(shù)據(jù)轉(zhuǎn)換為 PILImage: transforms.ToPILImage
  • transforms.Lambda: Apply a user-defined lambda as a transform.

4,對(duì) transforms 操作,使數(shù)據(jù)增強(qiáng)更靈活

  • transforms.RandomChoice(transforms): 從給定的一系列 transforms 中選一個(gè)進(jìn)行操作
  • transforms.RandomApply(transforms, p=0.5): 給一個(gè) transform 加上概率,依概率進(jìn)行操作
  • transforms.RandomOrder: 將 transforms 中的操作隨機(jī)打亂

這里 resize 圖像增強(qiáng)方法為例,可視化其調(diào)整輸入圖像大小的效果。

# 為了節(jié)省空間,這里不再列出導(dǎo)入相應(yīng)庫(kù)的代碼和show_images函數(shù)
img_PIL = Image.open('astronaut.jpeg')
print(img_PIL.size)
# if you change the seed, make sure that the randomly-applied transforms
# properly show that the image can be both transformed and *not* transformed!
torch.manual_seed(0)
# size 參數(shù): desired output size.
resized_imgs = [transforms.Resize(size=size)(orig_img) for size in (30, 50, 100, orig_img.size)]
show_images(resized_imgs, 1, 4)

程序運(yùn)行后的輸出圖如下。

Resize

3.3,imgaug 圖像增強(qiáng)

imgaug 是一個(gè)用于機(jī)器學(xué)習(xí)實(shí)驗(yàn)中圖像增強(qiáng)的庫(kù)。 它支持廣泛的增強(qiáng)技術(shù),允許輕松組合這些技術(shù)并以隨機(jī)順序或在多個(gè) CPU 內(nèi)核上執(zhí)行它們,具有簡(jiǎn)單而強(qiáng)大的隨機(jī)接口,不僅可以增強(qiáng)圖像,還可以增強(qiáng)關(guān)鍵點(diǎn)/地標(biāo)、邊界框、 熱圖和分割圖。

單個(gè)輸入圖像的示例增強(qiáng)如下所示。

imgaug_example

imgaug 的圖像增強(qiáng)方法如下所示。

  • Basics
  • Keypoints
  • Bounding Boxes
  • Heatmaps
  • Segmentation Maps and Masks
  • Stochastic Parameters: 隨機(jī)參數(shù)
  • Blending/Overlaying images: 混合/疊加圖像
  • Augmenters: 增強(qiáng)器概述

各個(gè)方法的使用請(qǐng)參考 imaug 官網(wǎng)。

參考資料

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容