河北網(wǎng)站開發(fā)費(fèi)用百度電話人工服務(wù)
keras.preprocessing.image
?Keras 庫中的一個(gè)模塊,用于處理和增強(qiáng)圖像數(shù)據(jù),它提供了一些實(shí)用的函數(shù),如圖像的加載、預(yù)處理、增強(qiáng)等。
常用函數(shù)?
1、load_img
用于加載圖像文件,并返回一個(gè) NumPy 數(shù)組表示該圖像
示例
from keras.preprocessing.image import load_img,load_img,array_to_img
import numpy as np#從指定路徑加載圖像,并將其調(diào)整為指定的大小(默認(rèn)為 (224, 224))
img = image.load_img('test.jpg', target_size=(224, 224))
2、img_to_array
將圖像轉(zhuǎn)換為 NumPy 數(shù)組。
示例
from keras.preprocessing.image import load_img,load_img,array_to_img
import numpy as np# 加載圖像并調(diào)整大小
img = image.load_img('example.jpg', target_size=(224, 224))# 將 PIL 圖像轉(zhuǎn)換為 numpy 數(shù)組
x = image.img_to_array(img)
?3、array_to_img
將 numpy 數(shù)組或 PIL 圖像轉(zhuǎn)換為 PIL 圖像。
示例
from keras.preprocessing.image import load_img,load_img,array_to_img# 加載圖像并調(diào)整大小
img = load_img('example.jpg', target_size=(224, 224))# 將 PIL 圖像轉(zhuǎn)換為 numpy 數(shù)組
x = img_to_array(img)# 將 numpy 數(shù)組轉(zhuǎn)換為 PIL 圖像
y = array_to_img(x)
4、ImageDataGenerator?
用于數(shù)據(jù)增強(qiáng)的類,可以對(duì)圖像進(jìn)行旋轉(zhuǎn)、縮放、平移、翻轉(zhuǎn)等操作。
示例1
# 導(dǎo)入Keras的ImageDataGenerator模塊,用于數(shù)據(jù)增強(qiáng)
from keras.preprocessing.image import ImageDataGenerator# 創(chuàng)建一個(gè)ImageDataGenerator對(duì)象,設(shè)置各種數(shù)據(jù)增強(qiáng)參數(shù)
datagen = ImageDataGenerator(rotation_range=40, # 隨機(jī)旋轉(zhuǎn)的角度范圍width_shift_range=0.2, # 水平平移的范圍height_shift_range=0.2, # 垂直平移的范圍shear_range=0.2, # 剪切強(qiáng)度zoom_range=0.2, # 隨機(jī)縮放的范圍horizontal_flip=True, # 是否進(jìn)行水平翻轉(zhuǎn)fill_mode='nearest' # 填充新創(chuàng)建像素的方法
)#使用flow_from_directory方法從指定的目錄中讀取圖像數(shù)據(jù)
#并將其傳遞給ImageDataGenerator對(duì)象進(jìn)行處理
train_generator = datagen.flow_from_directory('path/train/data', #訓(xùn)練數(shù)據(jù)的目錄target_size=(150, 150), #將所有圖像調(diào)整為150x150大小batch_size=32, #每個(gè)批次包含32個(gè)圖像class_mode='binary' #對(duì)于多分類問題使用 'categorical',對(duì)于二元分類問題使用 'binary',對(duì)于無標(biāo)簽問題使用 None
)#使用生成器對(duì)象來訓(xùn)練模型
model.fit_generator(train_generator,steps_per_epoch=2000, # 每個(gè)epoch需要遍歷的批次數(shù)epochs=50 # 訓(xùn)練的總輪數(shù)
)
注:
flow_from_directory
方法中的target_size
參數(shù)需要與模型輸入層的大小相匹
示例2?
from keras.preprocessing.image import ImageDataGenerator,load_img, img_to_array# 創(chuàng)建一個(gè)ImageDataGenerator實(shí)例
datagen = ImageDataGenerator(rotation_range=40, # 隨機(jī)旋轉(zhuǎn)的角度范圍width_shift_range=0.2, # 水平平移的范圍height_shift_range=0.2, # 垂直平移的范圍shear_range=0.2, # 剪切強(qiáng)度zoom_range=0.2, # 隨機(jī)縮放的范圍horizontal_flip=True, # 是否進(jìn)行水平翻轉(zhuǎn)fill_mode='nearest' # 填充新創(chuàng)建像素的方法
)# 加載圖像數(shù)據(jù)
img = load_img('input.jpg')
x = img_to_array(img) # 將其轉(zhuǎn)換為NumPy數(shù)組的形狀(3, 150, 150)
x = x.reshape((1,) + x.shape) # 將其重新整形為(1, 3, 150, 150)# 使用ImageDataGenerator生成增強(qiáng)后的圖像
i = 0
for batch in datagen.flow(x, batch_size=1, save_to_dir='preview', save_prefix='img', save_format='jpeg'):i += 1if i > 20:break #增強(qiáng)圖像數(shù)量20張
注:
save_to_dir:
指定保存生成的圖像的目錄。如果該目錄不存在,Keras將自動(dòng)創(chuàng)建它。save_prefix:
指定保存的圖像的前綴。每個(gè)生成的圖像都將以其前綴命名。save_format:
指定保存的圖像的格式。默認(rèn)情況下,Keras將保存為JPEG格式的圖像