深度学习最依赖的就是数据量了,同样的一张照片轻松一变数据量就翻倍了
所有可能的变化,比如说平移或者按角度的旋转,放大缩小综合的随机执行这些变换
导入基础python包
import matplotlib.pyplot as plt
from PIL import Image
%matplotlib inline
from keras.preprocessing import image #函数在Image模块下,图像变化现成的框架工具包,平移,放缩,旋转
# keras预处理模块下有一个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 = '/Users/mac/Desktop/chest_xray/img/x/*'
in_path = '/Users/mac/Desktop/chest_xray/img/'
out_path = '/Users/mac/Desktop/chest_xray/output/'
name_list = glob.glob(img_path)
name_list
['/Users/mac/Desktop/chest_xray/img/x/person117_bacteria_553.jpeg',
'/Users/mac/Desktop/chest_xray/img/x/person117_bacteria_556.jpeg',
'/Users/mac/Desktop/chest_xray/img/x/person117_bacteria_557.jpeg']
print_result(img_path)
指定target_size后的图像都变为相同大小好送入神经网络
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))
#save_prefix = 'gen'加前缀方便自己识别
#把三张图像都执行一次操作
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/*')
平移操作
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/*')
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/*')
缩放
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/*')
channel_shift 对颜色通道值的一个变换
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/*')
翻转
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/*')
可能我们看图像的话是没有什么太大差别的,但是我们把这些图像转换为向量就可以看见不同变化的差别。
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))
gen_data.next()
Found 3 images belonging to 1 classes.
(array([[[[ 44., 44., 44.],
[ 9., 9., 9.],
[ 87., 87., 87.],
...,
[ 13., 13., 13.],
[ 12., 12., 12.],
[ 14., 14., 14.]],
[[ 39., 39., 39.],
[ 47., 47., 47.],
[202., 202., 202.],
...,
[ 13., 13., 13.],
[ 12., 12., 12.],
[ 14., 14., 14.]],
[[ 20., 20., 20.],
[184., 184., 184.],
[193., 193., 193.],
...,
[ 13., 13., 13.],
[ 12., 12., 12.],
[ 14., 14., 14.]],
...,
[[ 15., 15., 15.],
[ 15., 15., 15.],
[ 15., 15., 15.],
...,
[ 19., 19., 19.],
[ 19., 19., 19.],
[ 19., 19., 19.]],
[[ 15., 15., 15.],
[ 15., 15., 15.],
[ 15., 15., 15.],
...,
[ 19., 19., 19.],
[ 19., 19., 19.],
[ 19., 19., 19.]],
[[ 15., 15., 15.],
[ 15., 15., 15.],
[ 15., 15., 15.],
...,
[ 19., 19., 19.],
[ 19., 19., 19.],
[ 19., 19., 19.]]]], dtype=float32),
array([[1.]], dtype=float32))
rescale(归一化)
datagen = image.ImageDataGenerator(rescale = 1/255) #都除以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/*')
gen_data = datagen.flow_from_directory(in_path,batch_size=1, #每篇论文必须要去做的,把像素压缩到0-1之间
shuffle=False,save_to_dir=out_path+'rescale',
save_prefix='gen',target_size=(224,224))
gen_data.next()
(array([[[[0.05490196, 0.05490196, 0.05490196],
[0.04705883, 0.04705883, 0.04705883],
[0.0509804 , 0.0509804 , 0.0509804 ],
...,
[0.34117648, 0.34117648, 0.34117648],
[0.03529412, 0.03529412, 0.03529412],
[0.17254902, 0.17254902, 0.17254902]],
[[0.05490196, 0.05490196, 0.05490196],
[0.04705883, 0.04705883, 0.04705883],
[0.0509804 , 0.0509804 , 0.0509804 ],
...,
[0.79215693, 0.79215693, 0.79215693],
[0.18431373, 0.18431373, 0.18431373],
[0.15294118, 0.15294118, 0.15294118]],
[[0.05490196, 0.05490196, 0.05490196],
[0.04705883, 0.04705883, 0.04705883],
[0.0509804 , 0.0509804 , 0.0509804 ],
...,
[0.7568628 , 0.7568628 , 0.7568628 ],
[0.72156864, 0.72156864, 0.72156864],
[0.07843138, 0.07843138, 0.07843138]],
...,
[[0.07450981, 0.07450981, 0.07450981],
[0.07450981, 0.07450981, 0.07450981],
[0.07450981, 0.07450981, 0.07450981],
...,
[0.05882353, 0.05882353, 0.05882353],
[0.05882353, 0.05882353, 0.05882353],
[0.05882353, 0.05882353, 0.05882353]],
[[0.07450981, 0.07450981, 0.07450981],
[0.07450981, 0.07450981, 0.07450981],
[0.07450981, 0.07450981, 0.07450981],
...,
[0.05882353, 0.05882353, 0.05882353],
[0.05882353, 0.05882353, 0.05882353],
[0.05882353, 0.05882353, 0.05882353]],
[[0.07450981, 0.07450981, 0.07450981],
[0.07450981, 0.07450981, 0.07450981],
[0.07450981, 0.07450981, 0.07450981],
...,
[0.05882353, 0.05882353, 0.05882353],
[0.05882353, 0.05882353, 0.05882353],
[0.05882353, 0.05882353, 0.05882353]]]], dtype=float32),
array([[1.]], dtype=float32))
填充方法:
'constant':kkkkkkkk|abcd|kkkkkkkk(cval=k)
'nearest':aaaaaaaa|abcd|dddddddd
'reflect':abcddcba|abcd|dcbaabcd
'wrap':abcdabcd|abcd|abcdabcd