目錄
- 變換的類型
- 主要代碼說明
- 代碼展示
- 密度聚類
- 層次聚類
增加訓練數據, 則能夠提升算法的准確率, 因為這樣可以避免過擬合, 而避免了過擬合你就可以增大你的網絡結構了。 當訓練數據有限的時候, 可以通過一些變換來從已有的訓練數據集中生成一些新的數據, 來擴大訓練數據。 數據增強的方法有:
一、變換的類型
1.1、水平翻轉
1.2、隨機裁剪
如原始圖像大小為256*256, 隨機裁剪出一些圖像224*224的圖像。 如下圖, 紅色方框內為隨機裁剪出的224*224的圖片。 AlexNet 訓練時, 對左上、 右上、 左下、 右下、 中間做了5次裁剪, 然后翻轉, 得到10張裁剪的圖片。 防止大網絡過擬合(under ubstantial overfitting)
1.3、fancy PCA
在訓練集像素值的RGB顏色空間進行PCA, 得到RGB空間的3個主方向向量,3個特征值, p1,p2, p3, λ1, λ2, λ3. 對每幅圖像的每個像素加上如下的變化: 其中:αi是滿足均值為0,方差為0.1的隨機變量
1.4、樣本不均衡
樣本不均衡即有些類別圖像特別多, 有些特別少。 類別不平衡數據的處理: Label shuffle。
1.5、其他
平移變換;
旋轉/仿射變換;
高斯噪聲、 模糊處理
對顏色的數據增強: 圖像亮度、 飽和度、 對比度變化。
1.6、訓練和測試要協調
- 在訓練的時候, 我們通常都需要做數據增強, 在測試的時候, 我們通常很少去做數據增強。 這其中似乎有些不協調, 因為你訓練和測試之間有些不一致。 實驗發現, 訓練的最后幾個迭代, 移除數據增強, 和傳統一樣測試, 可以提升一點性能。
- 如果訓練的時候一直使用尺度和長寬比增強數據增強, 在測試的時候也同樣做這個變化, 隨機取32個裁剪圖片來測試, 也可以在最后的模型上提升一點性能。
- 就是多尺度的訓練, 多尺度的測試。
- 訓練過程的中間結果, 加入做測試, 可以一定程度上降低過擬合。
二、主要代碼說明
2.1 圖像數據轉換為像素點的數據 - tf.image.decode_png
image_tensor = tf.image.decode_png(contents=file_contents, channels=3)
返回對象: [height, width, num_channels], 如果是gif圖像返回[num_frames, height, width, num_channels]
- height: 圖片的高度的像素大小、width:水平寬度的像素大小 、num_channels: 圖像的通道數,也就是API中的channels的值
- num_frames: 因為gif的圖像是一個動態圖像,可以將每一個動的畫面看成一個靜態圖像,num_frames相當於在這個gif圖像中有多少個靜態圖像
- 參數channels:可選值:0 1 3 4,默認為0, 一般使用0 1 3,不建議使用4
0:使用圖像的默認通道,也就是圖像是幾通道的就使用幾通道
1:使用灰度級別的圖像數據作為返回值(只有一個通道:黑白)
3:使用RGB三通道讀取數據
4:使用RGBA四通道讀取數據(R:紅色,G:綠色,B:藍色,A:透明度)
2.2 改變圖像大小 - tf.image.resize_images
resize_image_tensor = tf.image.resize_images(images=image_tensor, size=(200, 200),
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
- API返回值和images格式一樣,唯一區別是height和width變化為給定的值
- images: 給定需要進行大小轉換的圖像對應的tensor對象,格式為:[height, width, num_channels]或者[batch, height, width, num_channels]
- ResizeMethod的三種參數:
BILINEAR = 0 線性插值,默認
NEAREST_NEIGHBOR = 1 最近鄰插值,失真最小
BICUBIC = 2 三次插值
AREA = 3 面積插值
2.3 圖片的剪切&填充
resize_image_with_crop_or_pad
圖片重置大小,通過圖片的剪切或者填充(從中間開始計算新圖片的大小)
crop_or_pad_image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor, 200, 200)
central_crop
中間等比例剪切
central_crop_image_tensor = tf.image.central_crop(image_tensor, central_fraction=0.2)
pad_to_bounding_box
填充數據(給定位置開始填充)offset_height=400, offset_width=490,表示偏移初始位置
pad_to_bounding_box_image_tensor = tf.image.pad_to_bounding_box(image_tensor, offset_height=400, offset_width=490,target_height=1000, target_width=1000)
crop_to_bounding_box
剪切數據(給定位置開始剪切)
crop_to_bounding_box_image_tensor = tf.image.crop_to_bounding_box(image_tensor, offset_height=10, offset_width=40,target_height=200, target_width=300)
2.4、旋轉
# 上下交換
tf.image.flip_up_down(image_tensor)
# 左右交換
tf.image.flip_left_right(image_tensor)
# 轉置
tf.image.transpose_image(image_tensor)
# 旋轉(90度、180度、270度....)# k*90度旋轉,逆時針旋轉
tf.image.rot90(image_tensor, k=4)
2.5 顏色空間的轉換(rgb、hsv、gray)
顏色空間的轉換必須講image的值轉換為float32類型,不能使用unit8類型
float32_image_tensor = tf.image.convert_image_dtype(image_tensor, dtype=tf.float32)
# rgb -> hsv(h: 圖像的色彩/色度,s:圖像的飽和度,v:圖像的亮度)
hsv_image_tensor = tf.image.rgb_to_hsv(float32_image_tensor)
# hsv -> rgb
rgb_image_tensor = tf.image.hsv_to_rgb(hsv_image_tensor)
# rgb -> gray
gray_image_tensor = tf.image.rgb_to_grayscale(rgb_image_tensor)
2.6 圖像的調整
tf.image.adjust_brightness
亮度調整
adjust_brightness_image_tensor = tf.image.adjust_brightness(image=image_tensor, delta=0.8)
- image: RGB圖像信息,設置為float類型和unit8類型的效果不一樣,一般建議設置為float類型
- delta: 取值范圍(-1,1)之間的float類型的值,表示對於亮度的減弱或者增強的系數值
- 底層執行:rgb -> hsv -> h,s,v*delta -> rgb
tf.image.adjust_hue
色調調整
adjust_hue_image_tensor = tf.image.adjust_hue(image_tensor, delta=-0.8)
參數和上面一樣
tf.image.adjust_saturation
飽和度調整
adjust_saturation_image_tensor = tf.image.adjust_saturation(image_tensor, saturation_factor=20)
參數和上面一樣
image.adjust_contrast
對比度調整,公式:(x-mean) * contrast_factor + mean
adjust_contrast_image_tensor = tf.image.adjust_contrast(image_tensor, contrast_factor=10)
tf.image.adjust_gamma
圖像的gamma校正
adjust_gamma_image_tensor = tf.image.adjust_gamma(float32_image_tensor, gamma=100)
images: 要求必須是float類型的數據
gamma:任意值,Oup = In * Gamma
tf.image.per_image_standardization
圖像的歸一化(x-mean)/adjusted_sttdev, adjusted_sttdev=max(stddev, 1.0/sqrt(image.NumElements()))
per_image_standardization_image_tensor = tf.image.per_image_standardization(image_tensor)
2.7、噪音數據的加入
noisy_image_tensor = image_tensor + tf.cast(5 * tf.random_normal(shape=[600, 510, 3], mean=0, stddev=0.1), tf.uint8)
三、代碼展示
1 # -- encoding:utf-8 -- 2 """ 3 圖像處理的Python庫:OpenCV、PIL、matplotlib、tensorflow等 4 """ 5 import numpy as np 6 import matplotlib.pyplot as plt 7 import tensorflow as tf 8 # 打印numpy的數組對象的時候,中間不省略 9 np.set_printoptions(threshold=np.inf) 10 def show_image_tensor(image_tensor): 11 # 要求:使用交互式會話 12 # 獲取圖像tensor對象對應的image對象,image對象時一個[h,w,c] 13 # print(image_tensor) 14 image = image_tensor.eval() 15 # print(image) 16 print("圖像大小為:{}".format(image.shape)) 17 if len(image.shape) == 3 and image.shape[2] == 1: 18 # 黑白圖像 19 plt.imshow(image[:, :, 0], cmap='Greys_r') 20 plt.show() 21 elif len(image.shape) == 3: 22 # 彩色圖像 23 plt.imshow(image) 24 plt.show() 25 # 1. 交互式會話啟動 26 sess = tf.InteractiveSession() 27 image_path = 'data/xiaoren.png' 28 # 一、圖像格式的轉換 29 # 讀取數據 30 file_contents = tf.read_file(image_path) 31 #圖像數據轉換為像素點的數據,返回對象: [height, width, num_channels], 如果是gif圖像返回[num_frames, height, width, num_channels] 32 image_tensor = tf.image.decode_png(contents=file_contents, channels=3) 33 show_image_tensor(image_tensor) 34 # 二、圖像大小重置 35 # images: 給定需要進行大小轉換的圖像對應的tensor對象,格式為:[height, width, num_channels]或者[batch, height, width, num_channels] 36 # API返回值和images格式一樣,唯一區別是height和width變化為給定的值 37 resize_image_tensor = tf.image.resize_images(images=image_tensor, size=(200, 200), 38 method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) 39 show_image_tensor(resize_image_tensor) 40 # 三、圖片的剪切&填充 41 # 3.1 圖片重置大小,通過圖片的剪切或者填充(從中間開始計算新圖片的大小) 42 crop_or_pad_image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor, 200, 200) 43 show_image_tensor(crop_or_pad_image_tensor) 44 # 3.2 中間等比例剪切 45 central_crop_image_tensor = tf.image.central_crop(image_tensor, central_fraction=0.2) 46 show_image_tensor(central_crop_image_tensor) 47 # 3.3 填充數據(給定位置開始填充) 48 pad_to_bounding_box_image_tensor = tf.image.pad_to_bounding_box(image_tensor, offset_height=400, offset_width=490, 49 target_height=1000, 50 target_width=1000) 51 show_image_tensor(pad_to_bounding_box_image_tensor) 52 # 3.4 剪切數據(給定位置開始剪切) 53 crop_to_bounding_box_image_tensor = tf.image.crop_to_bounding_box(image_tensor, offset_height=10, offset_width=40, 54 target_height=200, target_width=300) 55 show_image_tensor(crop_to_bounding_box_image_tensor) 56 #四、旋轉 57 # 上下交換 58 flip_up_down_image_tensor = tf.image.flip_up_down(image_tensor) 59 show_image_tensor(flip_up_down_image_tensor) 60 # 左右交換 61 flip_left_right_image_tensor = tf.image.flip_left_right(image_tensor) 62 show_image_tensor(flip_left_right_image_tensor) 63 # 轉置 64 transpose_image_tensor = tf.image.transpose_image(image_tensor) 65 show_image_tensor(transpose_image_tensor) 66 # 旋轉(90度、180度、270度....) 67 # k*90度旋轉,逆時針旋轉 68 k_rot90_image_tensor = tf.image.rot90(image_tensor, k=4) 69 show_image_tensor(k_rot90_image_tensor) 70 # 五、顏色空間的轉換(rgb、hsv、gray) 71 # 顏色空間的轉換必須講image的值轉換為float32類型,不能使用unit8類型 72 float32_image_tensor = tf.image.convert_image_dtype(image_tensor, dtype=tf.float32) 73 show_image_tensor(float32_image_tensor) 74 # rgb -> hsv(h: 圖像的色彩/色度,s:圖像的飽和度,v:圖像的亮度) 75 hsv_image_tensor = tf.image.rgb_to_hsv(float32_image_tensor) 76 show_image_tensor(hsv_image_tensor) 77 # hsv -> rgb 78 rgb_image_tensor = tf.image.hsv_to_rgb(hsv_image_tensor) 79 show_image_tensor(rgb_image_tensor) 80 # rgb -> gray 81 gray_image_tensor = tf.image.rgb_to_grayscale(rgb_image_tensor) 82 show_image_tensor(gray_image_tensor) 83 # 可以從顏色空間中提取圖像的輪廓信息(圖像的二值化) 84 a = gray_image_tensor 85 b = tf.less_equal(a, 0.9) 86 # 0是黑,1是白 87 # condition?true:false 88 # condition、x、y格式必須一模一樣,當condition中的值為true的之后,返回x對應位置的值,否則返回y對應位置的值 89 # 對於a中所有大於0.9的像素值,設置為0 90 c = tf.where(condition=b, x=a, y=a - a) 91 # 對於a中所有小於等於0.9的像素值,設置為1 92 d = tf.where(condition=b, x=c - c + 1, y=c) 93 show_image_tensor(d) 94 # 六、圖像的調整 95 # 亮度調整 96 # image: RGB圖像信息,設置為float類型和unit8類型的效果不一樣,一般建議設置為float類型 97 # delta: 取值范圍(-1,1)之間的float類型的值,表示對於亮度的減弱或者增強的系數值 98 # 底層執行:rgb -> hsv -> h,s,v*delta -> rgb 99 adjust_brightness_image_tensor = tf.image.adjust_brightness(image=image_tensor, delta=0.8) 100 show_image_tensor(adjust_brightness_image_tensor) 101 # 色調調整 102 # image: RGB圖像信息,設置為float類型和unit8類型的效果不一樣,一般建議設置為float類型 103 # delta: 取值范圍(-1,1)之間的float類型的值,表示對於色調的減弱或者增強的系數值 104 # 底層執行:rgb -> hsv -> h*delta,s,v -> rgb 105 adjust_hue_image_tensor = tf.image.adjust_hue(image_tensor, delta=-0.8) 106 show_image_tensor(adjust_hue_image_tensor) 107 # 飽和度調整 108 # image: RGB圖像信息,設置為float類型和unit8類型的效果不一樣,一般建議設置為float類型 109 # saturation_factor: 一個float類型的值,表示對於飽和度的減弱或者增強的系數值,飽和因子 110 # 底層執行:rgb -> hsv -> h,s*saturation_factor,v -> rgb 111 adjust_saturation_image_tensor = tf.image.adjust_saturation(image_tensor, saturation_factor=20) 112 show_image_tensor(adjust_saturation_image_tensor) 113 114 # 對比度調整,公式:(x-mean) * contrast_factor + mean 115 adjust_contrast_image_tensor = tf.image.adjust_contrast(image_tensor, contrast_factor=10) 116 show_image_tensor(adjust_contrast_image_tensor) 117 # 圖像的gamma校正 118 # images: 要求必須是float類型的數據 119 # gamma:任意值,Oup = In * Gamma 120 adjust_gamma_image_tensor = tf.image.adjust_gamma(float32_image_tensor, gamma=100) 121 show_image_tensor(adjust_gamma_image_tensor) 122 # 圖像的歸一化(x-mean)/adjusted_sttdev, adjusted_sttdev=max(stddev, 1.0/sqrt(image.NumElements())) 123 per_image_standardization_image_tensor = tf.image.per_image_standardization(image_tensor) 124 show_image_tensor(per_image_standardization_image_tensor) 125 # 七、噪音數據的加入 126 noisy_image_tensor = image_tensor + tf.cast(5 * tf.random_normal(shape=[600, 510, 3], mean=0, stddev=0.1), tf.uint8) 127 show_image_tensor(noisy_image_tensor)
結果:原本是單個圖片的,本人做了下匯總,如下所示: