數(shù)據(jù)增強(qiáng):當(dāng)訓(xùn)練數(shù)據(jù)非常有限時(shí),可以通過(guò)一些變換,從已有訓(xùn)練集去生成一些新的數(shù)據(jù),來(lái)人工擴(kuò)大訓(xùn)練集樣本個(gè)數(shù),從而獲得更充足的訓(xùn)練集,使模型訓(xùn)練效果更好。通常可采用:水平翻轉(zhuǎn)、改變對(duì)比度、隨機(jī)裁剪等方式。
1. 圖像編碼處理
一張RGB色彩模式的圖像可以看成一個(gè)三維矩陣,矩陣中每個(gè)數(shù)表示圖像上不同位置、不同顏色的亮度。但是圖像存儲(chǔ)不是直接記錄這些數(shù)字,而是記錄壓縮編碼后的結(jié)果。所以要將三維圖像還原成三維矩陣,還需要解碼。TensorFlow提供了圖像編碼和解碼的函數(shù)。
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
#讀取圖像的原始數(shù)據(jù)
image_raw_data = tf.gfile.FastGFile('dog.jpg','rb').read()
with tf.Session() as sess:
#對(duì)圖像進(jìn)行jpeg的格式解碼從而得到圖像對(duì)應(yīng)的三維矩陣
img_data = tf.image.decode_jpeg(image_raw_data)
#img_data = tf.image.decode_png(image_raw_data) #對(duì)png格式圖像解碼
#解碼后的結(jié)果是一個(gè)張量
print(img_data.eval())
#可視化
plt.imshow(img_data.eval())
plt.show()
2. 圖像縮放
tf.image.resize_images(images, new_height, new_width, method)
神經(jīng)網(wǎng)絡(luò)輸入的節(jié)點(diǎn)個(gè)數(shù)是固定的,所以在將圖像像素作為輸入提供給神經(jīng)網(wǎng)絡(luò)之前,要將圖像大小進(jìn)行統(tǒng)一。
- 雙線性插值法 ResizeMethod.BILINEAR(默認(rèn)設(shè)置),對(duì)應(yīng)method=0
- 最近鄰插值法 NEAREST_NEIGHBOR,對(duì)應(yīng)method=1
- 雙立方插值法 BICUBIC,對(duì)應(yīng)method=2
- 像素區(qū)域插值法 AREA,對(duì)應(yīng)method=3
#處理后圖像差別不大,以method=0為例
with tf.Session() as sess:
resized1 = tf.image.resize_images(img_data,[256,256],method=0)
resized1 = np.asarray(resized1.eval(),dtype="uint8")
plt.imshow(resized1)
plt.show()
3. 剪裁或填充后縮放
tf.image.resize_image_with_crop_or_pad(image, target_height, target_width)
如果目標(biāo)圖像尺寸小于原始圖像尺寸,則在中心位置剪裁,反之則用黑色像素進(jìn)行填充。
with tf.Session() as sess:
#對(duì)圖像進(jìn)行jpeg的格式解碼從而得到圖像對(duì)應(yīng)的三維矩陣
img_data = tf.image.decode_jpeg(image_raw_data)
croped = tf.image.resize_image_with_crop_or_pad(img_data,200,200)
plt.imshow(croped.eval())
plt.show()
with tf.Session() as sess:
#對(duì)圖像進(jìn)行jpeg的格式解碼從而得到圖像對(duì)應(yīng)的三維矩陣
img_data = tf.image.decode_jpeg(image_raw_data)
croped = tf.image.resize_image_with_crop_or_pad(img_data,800,800)
plt.imshow(croped.eval())
plt.show()
4. 隨機(jī)裁剪
tf.image.random_crop(image, size, seed=None, name=None)
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
random_croped = tf.random_crop(img_data,[200,200,3])
plt.imshow(random_croped.eval())
plt.show()
5. 水平翻轉(zhuǎn)
tf.image.flip_left_right(img_data)
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
plt.imshow(img_data.eval())
plt.axis('off')
plt.show()
flip_left_right = tf.image.flip_left_right(img_data)
plt.imshow(flip_left_right.eval())
plt.axis('off')
plt.show()
6. 上下翻轉(zhuǎn)
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
plt.imshow(img_data.eval())
plt.axis('off')
plt.show()
flip_up_down = tf.image.flip_up_down(img_data)
plt.imshow(flip_up_down.eval())
plt.axis('off')
plt.show()
7. 改變對(duì)比度
tf.image.random_contrast
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
plt.imshow(img_data.eval())
plt.show()
#將圖像對(duì)比度降低至原來(lái)的二分之一
contrast = tf.image.adjust_contrast(img_data,0.5)
#將圖像對(duì)比度提高至原來(lái)的5倍
#contrast = tf.image.adjust_contrast(img_data,5)
#在[lower,upper]范圍隨機(jī)調(diào)整圖像對(duì)比度
#contrast = tf.image.random_contrast(img_data,lower=0.2,upper=3)
plt.imshow(contrast.eval())
plt.show()
8. 白化處理
將圖像的像素值轉(zhuǎn)化成零均值和單位方差
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
plt.imshow(img_data.eval())
plt.show()
standardization = tf.image.per_image_standardization(img_data)
plt.imshow(np.asanyarray(standardization.eval(), dtype='uint8'))
plt.show()