博客專欄

EEPW首頁(yè) > 博客 > 各種 AI 數(shù)據(jù)增強(qiáng)方法,都在這兒了

各種 AI 數(shù)據(jù)增強(qiáng)方法,都在這兒了

發(fā)布人:AI科技大本營(yíng) 時(shí)間:2021-03-21 來(lái)源:工程師 發(fā)布文章

以下文章來(lái)源于算法進(jìn)階 ,作者算法進(jìn)階

數(shù)據(jù)、算法、算力是人工智能發(fā)展的三要素。數(shù)據(jù)決定了Ai模型學(xué)習(xí)的上限,數(shù)據(jù)規(guī)模越大、質(zhì)量越高,模型就能夠擁有更好的泛化能力。

然而在實(shí)際工程中,經(jīng)常有數(shù)據(jù)量太少(相對(duì)模型而言)、樣本不均衡、很難覆蓋全部的場(chǎng)景等問(wèn)題,解決這類問(wèn)題的一個(gè)有效途徑是通過(guò)數(shù)據(jù)增強(qiáng)(Data Augmentation),使模型學(xué)習(xí)獲得較好的泛化性能。

1.png

01 數(shù)據(jù)增強(qiáng)介紹

數(shù)據(jù)增強(qiáng)(Data Augmentation)是在不實(shí)質(zhì)性的增加數(shù)據(jù)的情況下,從原始數(shù)據(jù)加工出更多的表示,提高原數(shù)據(jù)的數(shù)量及質(zhì)量,以接近于更多數(shù)據(jù)量產(chǎn)生的價(jià)值。其原理是,通過(guò)對(duì)原始數(shù)據(jù)融入先驗(yàn)知識(shí),加工出更多數(shù)據(jù)的表示,有助于模型判別數(shù)據(jù)中統(tǒng)計(jì)噪聲,加強(qiáng)本體特征的學(xué)習(xí),減少模型過(guò)擬合,提升泛化能力。

如經(jīng)典的機(jī)器學(xué)習(xí)例子--哈士奇誤分類為狼:通過(guò)可解釋性方法,可發(fā)現(xiàn)錯(cuò)誤分類是由于圖像上的雪造成的。通常狗對(duì)比狼的圖像里面雪地背景比較少,分類器學(xué)會(huì)使用雪作為一個(gè)特征來(lái)將圖像分類為狼還是狗,而忽略了動(dòng)物本體的特征。此時(shí),可以通過(guò)數(shù)據(jù)增強(qiáng)的方法,增加變換后的數(shù)據(jù)(如背景換色、加入噪聲等方式)來(lái)訓(xùn)練模型,幫助模型學(xué)習(xí)到本體的特征,提高泛化能力。

2.png

需要關(guān)注的是,數(shù)據(jù)增強(qiáng)樣本也有可能是引入片面噪聲,導(dǎo)致過(guò)擬合。此時(shí)需要考慮的是調(diào)整數(shù)據(jù)增強(qiáng)方法,或者通過(guò)算法(可借鑒Pu-Learning思路)選擇增強(qiáng)數(shù)據(jù)的最佳子集,以提高模型的泛化能力。

常用數(shù)據(jù)增強(qiáng)方法可分為:基于樣本變換的數(shù)據(jù)增強(qiáng)及基于深度學(xué)習(xí)的數(shù)據(jù)增強(qiáng)。

02 基于樣本變換的數(shù)據(jù)增強(qiáng)

樣本變換數(shù)據(jù)增強(qiáng)即采用預(yù)設(shè)的數(shù)據(jù)變換規(guī)則進(jìn)行已有數(shù)據(jù)的擴(kuò)增,包含單樣本數(shù)據(jù)增強(qiáng)和多樣本數(shù)據(jù)增強(qiáng)。

2.1 單樣本增強(qiáng)

單(圖像)樣本增強(qiáng)主要有幾何操作、顏色變換、隨機(jī)擦除、添加噪聲等方法,可參見(jiàn)imgaug開(kāi)源庫(kù)。

3.png

2.2 多樣本數(shù)據(jù)增強(qiáng)方法

多樣本增強(qiáng)是通過(guò)先驗(yàn)知識(shí)組合及轉(zhuǎn)換多個(gè)樣本,主要有Smote、SamplePairing、Mixup等方法在特征空間內(nèi)構(gòu)造已知樣本的鄰域值。

Smote

Smote(Synthetic Minority Over-sampling Technique)方法較常用于樣本均衡學(xué)習(xí),核心思想是從訓(xùn)練集隨機(jī)同類的兩近鄰樣本合成一個(gè)新的樣本,其方法可以分為三步:

1、 對(duì)于各樣本X_i,計(jì)算與同類樣本的歐式距離,確定其同類的K個(gè)(如圖3個(gè))近鄰樣本;

2、從該樣本k近鄰中隨機(jī)選擇一個(gè)樣本如近鄰X_ik,生成新的樣本:

Xsmote_ik =  Xi  +  rand(0,1) ? ∣X_i ? X_ik∣

3、重復(fù)2步驟迭代N次,可以合成N個(gè)新的樣本。

4.png

# SMOTE
from imblearn.over_sampling import SMOTE
print("Before OverSampling, counts of label\n{}".format(y_train.value_counts()))
smote = SMOTE()
x_train_res, y_train_res = smote.fit_resample(x_train, y_train)
print("After OverSampling, counts of label\n{}".format(y_train_res.value_counts()))

SamplePairing

SamplePairing算法的核心思想是從訓(xùn)練集隨機(jī)抽取的兩幅圖像疊加合成一個(gè)新的樣本(像素取平均值),使用第一幅圖像的label作為合成圖像的正確label。

5.png

Mixup

Mixup算法的核心思想是按一定的比例隨機(jī)混合兩個(gè)訓(xùn)練樣本及其標(biāo)簽,這種混合方式不僅能夠增加樣本的多樣性,且能夠使決策邊界更加平滑,增強(qiáng)了難例樣本的識(shí)別,模型的魯棒性得到提升。其方法可以分為兩步:

1、從原始訓(xùn)練數(shù)據(jù)中隨機(jī)選取的兩個(gè)樣本(xi, yi) and (xj, yj)。其中y(原始label)用one-hot 編碼。

2、對(duì)兩個(gè)樣本按比例組合,形成新的樣本和帶權(quán)重的標(biāo)簽

x? = λxi + (1 ? λ)xj
y? = λyi + (1 ? λ)yj

最終的loss為各標(biāo)簽上分別計(jì)算cross-entropy loss,加權(quán)求和。其中 λ ∈ [0, 1], λ是mixup的超參數(shù),控制兩個(gè)樣本插值的強(qiáng)度。

6.png

# Mixup
def mixup_batch(x, y, step, batch_size, alpha=0.2):
    """
    get batch data
    :param x: training data
    :param y: one-hot label
    :param step: step
    :param batch_size: batch size
    :param alpha: hyper-parameter α, default as 0.2
    :return:  x y 
    """
    candidates_data, candidates_label = x, y
    offset = (step * batch_size) % (candidates_data.shape[0] - batch_size)
    # get batch data
    train_features_batch = candidates_data[offset:(offset + batch_size)]
    train_labels_batch = candidates_label[offset:(offset + batch_size)]
    if alpha == 0:
        return train_features_batch, train_labels_batch
    if alpha > 0:
        weight = np.random.beta(alpha, alpha, batch_size)
        x_weight = weight.reshape(batch_size, 1)
        y_weight = weight.reshape(batch_size, 1)
        index = np.random.permutation(batch_size)
        x1, x2 = train_features_batch, train_features_batch[index]
        x = x1 * x_weight + x2 * (1 - x_weight)
        y1, y2 = train_labels_batch, train_labels_batch[index]
        y = y1 * y_weight + y2 * (1 - y_weight)
        return x, y

03 基于深度學(xué)習(xí)的數(shù)據(jù)增強(qiáng)

3.1 特征空間的數(shù)據(jù)增強(qiáng)

不同于傳統(tǒng)在輸入空間變換的數(shù)據(jù)增強(qiáng)方法,神經(jīng)網(wǎng)絡(luò)可將輸入樣本映射為網(wǎng)絡(luò)層的低維向量(表征學(xué)習(xí)),從而直接在學(xué)習(xí)的特征空間進(jìn)行組合變換等進(jìn)行數(shù)據(jù)增強(qiáng),如MoEx方法等。

7.png

3.2 基于生成模型的數(shù)據(jù)增強(qiáng)

生成模型如變分自編碼網(wǎng)絡(luò)(Variational Auto-Encoding network, VAE)和生成對(duì)抗網(wǎng)絡(luò)(Generative Adversarial Network, GAN),其生成樣本的方法也可以用于數(shù)據(jù)增強(qiáng)。這種基于網(wǎng)絡(luò)合成的方法相比于傳統(tǒng)的數(shù)據(jù)增強(qiáng)技術(shù)雖然過(guò)程更加復(fù)雜, 但是生成的樣本更加多樣。

變分自編碼器VAE

變分自編碼器(Variational Autoencoder,VAE)其基本思路是:將真實(shí)樣本通過(guò)編碼器網(wǎng)絡(luò)變換成一個(gè)理想的數(shù)據(jù)分布,然后把數(shù)據(jù)分布再傳遞給****網(wǎng)絡(luò),構(gòu)造出生成樣本,模型訓(xùn)練學(xué)習(xí)的過(guò)程是使生成樣本與真實(shí)樣本足夠接近。

8.png

# VAE模型
class VAE(keras.Model):
    ...
    def train_step(self, data):
        with tf.GradientTape() as tape:
            z_mean, z_log_var, z = self.encoder(data)
            reconstruction = self.decoder(z)
            reconstruction_loss = tf.reduce_mean(
                tf.reduce_sum(
                    keras.losses.binary_crossentropy(data, reconstruction), axis=(1, 2)
                )
            )
            kl_loss = -0.5 * (1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var))
            kl_loss = tf.reduce_mean(tf.reduce_sum(kl_loss, axis=1))
            total_loss = reconstruction_loss + kl_loss
        grads = tape.gradient(total_loss, self.trainable_weights)
        self.optimizer.apply_gradients(zip(grads, self.trainable_weights))
        self.total_loss_tracker.update_state(total_loss)
        self.reconstruction_loss_tracker.update_state(reconstruction_loss)
        self.kl_loss_tracker.update_state(kl_loss)
        return {
            "loss": self.total_loss_tracker.result(),
            "reconstruction_loss": self.reconstruction_loss_tracker.result(),
            "kl_loss": self.kl_loss_tracker.result(),
        }

生成對(duì)抗網(wǎng)絡(luò)GAN

生成對(duì)抗網(wǎng)絡(luò)-GAN(Generative Adversarial Network) 由生成網(wǎng)絡(luò)(Generator, G)和判別網(wǎng)絡(luò)(Discriminator, D)兩部分組成, 生成網(wǎng)絡(luò)構(gòu)成一個(gè)映射函數(shù)G: Z→X(輸入噪聲z, 輸出生成的圖像數(shù)據(jù)x), 判別網(wǎng)絡(luò)判別輸入是來(lái)自真實(shí)數(shù)據(jù)還是生成網(wǎng)絡(luò)生成的數(shù)據(jù)。

9.png

# DCGAN模型
class GAN(keras.Model):
    ...
    def train_step(self, real_images):
        batch_size = tf.shape(real_images)[0]
        random_latent_vectors = tf.random.normal(shape=(batch_size, self.latent_dim))
        # G: Z→X(輸入噪聲z, 輸出生成的圖像數(shù)據(jù)x)
        generated_images = self.generator(random_latent_vectors)
        # 合并生成及真實(shí)的樣本并賦判定的標(biāo)簽
        combined_images = tf.concat([generated_images, real_images], axis=0)
        labels = tf.concat(
            [tf.ones((batch_size, 1)), tf.zeros((batch_size, 1))], axis=0
        )
        # 標(biāo)簽加入隨機(jī)噪聲
        labels += 0.05 * tf.random.uniform(tf.shape(labels))
        # 訓(xùn)練判定網(wǎng)絡(luò)
        with tf.GradientTape() as tape:
            predictions = self.discriminator(combined_images)
            d_loss = self.loss_fn(labels, predictions)
        grads = tape.gradient(d_loss, self.discriminator.trainable_weights)
        self.d_optimizer.apply_gradients(
            zip(grads, self.discriminator.trainable_weights)
        )
        random_latent_vectors = tf.random.normal(shape=(batch_size, self.latent_dim))
        # 賦生成網(wǎng)絡(luò)樣本的標(biāo)簽(都賦為真實(shí)樣本)
        misleading_labels = tf.zeros((batch_size, 1))
        # 訓(xùn)練生成網(wǎng)絡(luò)
        with tf.GradientTape() as tape:
            predictions = self.discriminator(self.generator(random_latent_vectors))
            g_loss = self.loss_fn(misleading_labels, predictions)
        grads = tape.gradient(g_loss, self.generator.trainable_weights)
        self.g_optimizer.apply_gradients(zip(grads, self.generator.trainable_weights))
        # 更新?lián)p失
        self.d_loss_metric.update_state(d_loss)
        self.g_loss_metric.update_state(g_loss)
        return {
            "d_loss": self.d_loss_metric.result(),
            "g_loss": self.g_loss_metric.result(),
        }

3.3 基于神經(jīng)風(fēng)格遷移的數(shù)據(jù)增強(qiáng)

神經(jīng)風(fēng)格遷移(Neural Style Transfer)可以在保留原始內(nèi)容的同時(shí),將一個(gè)圖像的樣式轉(zhuǎn)移到另一個(gè)圖像上。除了實(shí)現(xiàn)類似色彩空間照明轉(zhuǎn)換,還可以生成不同的紋理和藝術(shù)風(fēng)格。

10.png

神經(jīng)風(fēng)格遷移是通過(guò)優(yōu)化三類的損失來(lái)實(shí)現(xiàn)的:

style_loss:使生成的圖像接近樣式參考圖像的局部紋理;

content_loss:使生成的圖像的內(nèi)容表示接近于基本圖像的表示;

total_variation_loss:是一個(gè)正則化損失,它使生成的圖像保持局部一致。

# 樣式損失
def style_loss(style, combination):
    S = gram_matrix(style)
    C = gram_matrix(combination)
    channels = 3
    size = img_nrows * img_ncols
    return tf.reduce_sum(tf.square(S - C)) / (4.0 * (channels ** 2) * (size ** 2))
# 內(nèi)容損失
def content_loss(base, combination):
    return tf.reduce_sum(tf.square(combination - base))
# 正則損失
def total_variation_loss(x):
    a = tf.square(
        x[:, : img_nrows - 1, : img_ncols - 1, :] - x[:, 1:, : img_ncols - 1, :]
    )
    b = tf.square(
        x[:, : img_nrows - 1, : img_ncols - 1, :] - x[:, : img_nrows - 1, 1:, :]
    )
    return tf.reduce_sum(tf.pow(a + b, 1.25))

3.4 基于元學(xué)習(xí)的數(shù)據(jù)增強(qiáng)

深度學(xué)習(xí)研究中的元學(xué)習(xí)(Meta learning)通常是指使用神經(jīng)網(wǎng)絡(luò)優(yōu)化神經(jīng)網(wǎng)絡(luò),元學(xué)習(xí)的數(shù)據(jù)增強(qiáng)有神經(jīng)增強(qiáng)(Neural augmentation)等方法。

神經(jīng)增強(qiáng)

神經(jīng)增強(qiáng)(Neural augmentation)是通過(guò)神經(jīng)網(wǎng)絡(luò)組的學(xué)習(xí)以獲得較優(yōu)的數(shù)據(jù)增強(qiáng)并改善分類效果的一種方法。其方法步驟如下:

1、獲取與target圖像同一類別的一對(duì)隨機(jī)圖像,前置的增強(qiáng)網(wǎng)絡(luò)通過(guò)CNN將它們映射為合成圖像,合成圖像與target圖像對(duì)比計(jì)算損失;

2、將合成圖像與target圖像神經(jīng)風(fēng)格轉(zhuǎn)換后輸入到分類網(wǎng)絡(luò)中,并輸出該圖像分類損失;

3、將增強(qiáng)與分類的loss加權(quán)平均后,反向傳播以更新分類網(wǎng)絡(luò)及增強(qiáng)網(wǎng)絡(luò)權(quán)重。使得其輸出圖像的同類內(nèi)差距減小且分類準(zhǔn)確。

11.png

*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。



關(guān)鍵詞:

相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉