有任何问题欢迎在下面留言
本篇文章的代码运行界面均在Jupyter Notebook中进行
本篇文章配套的代码资源已经上传
对于图像数据,将其进行翻转、放缩、平移、旋转操作就可以得到一组新的数据:
import matplotlib.pyplot as plt
from PIL import Image
%matplotlib inline
from keras.preprocessing import image
import keras.backend as K
import os
import glob
import numpy as np
def print_result(path):
name_list = glob.glob(path)
fig = plt.figure(figsize=(12,16))
for i in range(3):
img = Image.open(name_list[i])
sub_img = fig.add_subplot(131+i)
sub_img.imshow(img)
img_path = './img/superman/*'
in_path = './img/'
out_path = './output/'
name_list = glob.glob(img_path)
print(name_list)
print_result(img_path)
打印结果:
[‘./img/superman\00000008.jpg’,
‘./img/superman\00000009.jpg’,
‘./img/superman\00000010.jpg’]
datagen = image.ImageDataGenerator()
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'resize',
save_prefix='gen', target_size=(224, 224))
for i in range(3):
gen_data.next()
print_result(out_path+'resize/*')
datagen = image.ImageDataGenerator(rotation_range=45)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'rotation_range',save_prefix='gen', target_size=(224, 224))
for i in range(3):
gen_data.next()
print_result(out_path+'rotation_range/*')
打印结果:
Found 3 images belonging to 1 classes.
Found 3 images belonging to 1 classes.
datagen = image.ImageDataGenerator(width_shift_range=0.3,height_shift_range=0.3)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'shift',save_prefix='gen', target_size=(224, 224))
for i in range(3):
gen_data.next()
print_result(out_path+'shift/*')
与3中不同的是,这段代码是进行平移变换进行数据增强,指定了平移变换的参数,width_shift_range=0.3,height_shift_range=0.3,这两个参数分别表示会在水平方向和垂直方向±30%的范围内随机移动
打印结果:
Found 3 images belonging to 1 classes.
Found 3 images belonging to 1 classes.
datagen = image.ImageDataGenerator(width_shift_range=-0.3,height_shift_range=0.3)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'shift2',save_prefix='gen', target_size=(224, 224))
for i in range(3):
gen_data.next()
print_result(out_path+'shift2/*')
由于是随机的,这两段代码完全一样,但是结果却不同
打印结果:
Found 3 images belonging to 1 classes.
Found 3 images belonging to 1 classes.
datagen = image.ImageDataGenerator(zoom_range=0.5)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'zoom',save_prefix='gen', target_size=(224, 224))
for i in range(3):
gen_data.next()
print_result(out_path+'zoom/*')
这段代码与3中不同的就是,这里指定缩放参数来进行缩放数据增强
打印结果:
Found 3 images belonging to 1 classes.
Found 3 images belonging to 1 classes.
datagen = image.ImageDataGenerator(channel_shift_range=15)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'channel',save_prefix='gen', target_size=(224, 224))
for i in range(3):
gen_data.next()
print_result(out_path+'channel/*')
这段代码与3中不同的就是,这里指定通道偏移参数来进行通道偏移数据增强
打印结果:
Found 3 images belonging to 1 classes.
Found 3 images belonging to 1 classes.
datagen = image.ImageDataGenerator(horizontal_flip=True)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'horizontal',save_prefix='gen', target_size=(224, 224))
for i in range(3):
gen_data.next()
print_result(out_path+'horizontal/*')
这段代码与3中不同的就是,这里指定水平翻转参数来进行水平翻转数据增强
datagen = image.ImageDataGenerator(rescale= 1/255)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'rescale',save_prefix='gen', target_size=(224, 224))
for i in range(3):
gen_data.next()
print_result(out_path+'rescale/*')
这段代码与3中不同的就是,这里指定rescale重新缩放参数来进行rescale重新缩放数据增强
通常用于归一化图像数据。将图像像素值从 [0, 255] 缩放到 [0, 1] 范围,有助于模型的训练
datagen = image.ImageDataGenerator(fill_mode='wrap', zoom_range=[4, 4])
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'fill_mode',save_prefix='gen', target_size=(224, 224))
for i in range(3):
gen_data.next()
print_result(out_path+'fill_mode/*')
fill_mode='wrap'
:当应用几何变换后,图像中可能会出现一些新的空白区域。fill_mode
定义了如何填充这些空白区域。在这种情况下,使用 'wrap'
模式,意味着空白区域将用图像边缘的像素“包裹”填充。zoom_range=[4, 4]
:这设置了图像缩放的范围。在这里,它被设置为在 4 倍范围内进行随机缩放。由于最小和最大缩放因子相同,这将导致所有图像都被放大 4 倍用原图像填充,任何超出原始图像边界的区域将被图像的对边界像素填充
datagen = image.ImageDataGenerator(fill_mode='nearest', zoom_range=[4, 4])
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'nearest',save_prefix='gen', target_size=(224, 224))
for i in range(3):
gen_data.next()
print_result(out_path+'nearest/*')