— 全文閱讀5分鐘 —
在本文中,你將學習到以下內容:
通過數據增強增加樣本量
調整圖片大小便于網絡訓練
前言
圖像識別的準備工作就是要對我們拿到手的樣本圖片進行預處理,具體就是數據增強和調整圖片大小,這些準備工作都是為訓練網絡做準備。圖片預處理一定要合理有效,符合機器學習的要求。
數據增強(data augmentation)
當我們拿到一套圖片數據準備進行機器學習的時候,樣本量往往不夠多,因此需要對現有的圖片進行數據增強。一方面是為了增加樣本量,另一方面能夠提高模型的泛化能力。
假設我們有一組商標圖片,如下:
商標圖片
當我們進行100類的機器學習時,顯然這一類的樣本量不夠多,在這里我們通過keras庫進行數據增強。以商標圖片中的第一張圖片為例:
from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
pic_path = r'./3ac79f3df8dcd100755525327e8b4710b8122fdc.jpg'
augmentation_path = r'./data_augmentation'
1
2
3
4
fromkeras.preprocessing.imageimportImageDataGenerator,img_to_array,load_img
pic_path=r'./3ac79f3df8dcd100755525327e8b4710b8122fdc.jpg'
augmentation_path=r'./data_augmentation'
首先導入keras庫,建立圖片路徑和數據增強保存路徑,接下來定義ImageDataGenerator,告訴他通過哪些操作產生新的圖片。
data_gen = ImageDataGenerator(
rotation_range=30,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.2,
fill_mode='nearest')
1
2
3
4
5
6
data_gen=ImageDataGenerator(
rotation_range=30,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.2,
fill_mode='nearest')
在這里根據當前的圖片需求,選擇了旋轉、平移、縮放、邊緣填充的操作,其他操作詳見。有些操作的設置要符合實際情況,比如旋轉操作,不能把圖片完全倒立了,這樣的數據增強反而不利機器學習。
img = load_img(pic_path)
x = img_to_array(img)
x = x.reshape((1,) + x.shape)
n = 1
for batch in data_gen.flow(x, batch_size=1, save_to_dir=augmentation_path, save_prefix='train', save_format='jpeg'):
n += 1
if n > 6: # 6表示生成6張新的圖片
break
1
2
3
4
5
6
7
8
img=load_img(pic_path)
x=img_to_array(img)
x=x.reshape((1,)+x.shape)
n=1
forbatchindata_gen.flow(x,batch_size=1,save_to_dir=augmentation_path,save_prefix='train',save_format='jpeg'):
n+=1
ifn>6:# 6表示生成6張新的圖片
break
加載圖片的地址,轉變成array格式給ImageDataGenerator,save_prefix表示新圖片的名字前綴,save_format表示新圖片保存的格式。需要注意的是,在這里根據我們定義的操作,從這些操作中隨機選擇幾種生成6張圖片。
最終在data_augmentation文件夾中生成6張新的商標圖片:
新的商標圖片
在實際操作中,應該多去嘗試數據增強的各種操作。好的樣本擴充能夠增加模型的泛化能力,提高準確率。數據增強完整代碼如下:
from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
pic_path = r'./3ac79f3df8dcd100755525327e8b4710b8122fdc.jpg'
augmentation_path = r'./data_augmentation'
data_gen = ImageDataGenerator(
rotation_range=30,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.2,
fill_mode='nearest')
img = load_img(pic_path)
x = img_to_array(img)
x = x.reshape((1,) + x.shape)
n = 1
for batch in data_gen.flow(x, batch_size=1, save_to_dir=augmentation_path, save_prefix='train', save_format='jpeg'):
n += 1
if n > 6:
break
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fromkeras.preprocessing.imageimportImageDataGenerator,img_to_array,load_img
pic_path=r'./3ac79f3df8dcd100755525327e8b4710b8122fdc.jpg'
augmentation_path=r'./data_augmentation'
data_gen=ImageDataGenerator(
rotation_range=30,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.2,
fill_mode='nearest')
img=load_img(pic_path)
x=img_to_array(img)
x=x.reshape((1,)+x.shape)
n=1
forbatchindata_gen.flow(x,batch_size=1,save_to_dir=augmentation_path,save_prefix='train',save_format='jpeg'):
n+=1
ifn>6:
break
圖片大小調整(resize)
統一調整圖片的大小,便于后面進行機器學習。我們以調整data_augmentation文件夾生成的新圖片為例:
from PIL import Image
import os
img_path = r'./data_augmentation'
resize_path = r'./resize_image'
for i in os.listdir(img_path):
im = Image.open(os.path.join(img_path,i))
out = im.resize((224, 224))
if not os.path.exists(resize_path):
os.makedirs(resize_path)
out.save(os.path.join(resize_path, i))
1
2
3
4
5
6
7
8
9
10
11
12
fromPILimportImage
importos
img_path=r'./data_augmentation'
resize_path=r'./resize_image'
foriinos.listdir(img_path):
im=Image.open(os.path.join(img_path,i))
out=im.resize((224,224))
ifnotos.path.exists(resize_path):
os.makedirs(resize_path)
out.save(os.path.join(resize_path,i))
使用PIL庫改變圖片大小,使用os庫讀取文件路徑,將resize后的圖片放到resize_image文件夾中。resize后的大小為224*224(這個大小是為了后面ResNet使用)。resize后的圖片效果如下:
resize后的圖片
當你完成這一步的時候,圖像識別的準備工作就完成一半了,剩下的就是將這些圖片制成tfrecord格式,方便訓練網絡讀取。