1 數(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í)到本體的特征,提高泛化能力。

需要關(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)。

2  基于樣本變換的數(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ù)。

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

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

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è)新的樣本。

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

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)度。

# 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

3  基于深度學(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方法等。

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模型
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:?ZX(輸入噪聲z, 輸出生成的圖像數(shù)據(jù)x), 判別網(wǎng)絡(luò)判別輸入是來(lái)自真實(shí)數(shù)據(jù)還是生成網(wǎng)絡(luò)生成的數(shù)據(jù)。

# 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)格。

神經(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)(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)確。

本文章轉(zhuǎn)載微信公眾號(hào)@算法進(jìn)階

上一篇:

一文概覽量子機(jī)器學(xué)習(xí)!

下一篇:

一文通俗講透樹(shù)模型
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門(mén)場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)