Mrli
别装作很努力,
因为结局不会陪你演戏。
Contacts:
QQ博客园

Keras使用——图像增强

2019/09/15 图像处理 深度学习
Word count: 1,580 | Reading time: 7min

Keras使用——图像增强

使用Keras进行深度学习的图像增强(Image Augmentation)

Keras是一个高层神经网络API,Keras由纯Python编写而成并基Tensorflow、Theano以及CNTK后端。
Keras为支持快速实验而生,能够把你的idea迅速转换为结果,如果你有如下需求,请选择Keras:

  • 简易和快速的原型设计(keras具有高度模块化,极简,和可扩充特性)
  • 支持CNN和RNN,或二者的结合
  • 无缝CPU和GPU切换

ImageDataGenerator

Keras提供了ImageDataGenerator类,定义关于图片准备和增强的配置。包括以下功能:样本级的标准化特征级的标准化ZCA白化.随机旋转、转换、剪切、翻转维度重排.将增强的图片保存在本地.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import os
Datagen = ImageDataGenerator(rotation_range=20,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip = True,
fill_mode='nearest')
for file_name in os.listdir('.'):
if file_name =='.config' or file_name == 'sample_data':
continue
img = load_img(file_name)
x_img = img_to_array(img)
x_img = x_img.reshape((1,)+ x_img.shape)
i = 0
for img_batch in Datagen.flow(x_img,
batch_size=32,
save_to_dir='.',
save_prefix='candy',
save_format='jpg'):
i +=1
if i > 10:
break

ImageDataGenerator参数详解及用法实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
keras.preprocessing.image.ImageDataGenerator(featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-6,
rotation_range=0.,
width_shift_range=0.,
height_shift_range=0.,
shear_range=0.,
zoom_range=0.,
channel_shift_range=0.,
fill_mode='nearest',
cval=0.,
horizontal_flip=False,
vertical_flip=False,
rescale=None,
preprocessing_function=None,
data_format=K.image_data_format())

▲用以生成一个batch的图像数据,支持实时数据提升。训练时该函数会无限生成数据,直到达到规定的epoch次数为止。

  • featurewise_center:布尔值,使输入数据集去中心化(均值为0), 按feature执行
  • samplewise_center:布尔值,使输入数据的每个样本均值为0
  • featurewise_std_normalization:布尔值,将输入除以数据集的标准差以完成标准化, 按feature执行
  • samplewise_std_normalization:布尔值,将输入的每个样本除以其自身的标准差
  • zca_whitening:布尔值,对输入数据施加ZCA白化
  • zca_epsilon: ZCA使用的eposilon,默认1e-6
  • rotation_range:整数,数据提升时图片随机转动的角度
  • width_shift_range:浮点数,图片宽度的某个比例,数据提升时图片水平偏移的幅度
  • height_shift_range:浮点数,图片高度的某个比例,数据提升时图片竖直偏移的幅度
  • shear_range:浮点数,剪切强度(逆时针方向的剪切变换角度)
  • zoom_range:浮点数或形如[lower,upper]的列表,随机缩放的幅度,若为浮点数,则相当于[lower,upper] = [1 - zoom_range, 1+zoom_range]
  • channel_shift_range:浮点数,随机通道偏移的幅度
  • fill_mode:;‘constant’,‘nearest’,‘reflect’或‘wrap’之一,当进行变换时超出边界的点将根据本参数给定的方法进行处理
  • cval:浮点数或整数,当fill_mode=constant时,指定要向超出边界的点填充的值
  • horizontal_flip:布尔值,进行随机水平翻转
  • vertical_flip:布尔值,进行随机竖直翻转
  • rescale: 重放缩因子,默认为None. 如果为None或0则不进行放缩,否则会将该数值乘到数据上(在应用其他变换之前)
  • preprocessing_function: 将被应用于每个输入的函数。该函数将在图片缩放和数据提升之后运行。该函数接受一个参数,为一张图片(秩为3的numpy array),并且输出一个具有相同shape的numpy array
  • data_format:字符串,“channel_first”或“channel_last”之一,代表图像的通道维的位置。该参数是Keras 1.x中的image_dim_ordering,“channel_last”对应原本的“tf”,“channel_first”对应原本的“th”。以128x128的RGB图像为例,“channel_first”应将数据组织为(3,128,128),而“channel_last”应将数据组织为(128,128,3)。该参数的默认值是~/.keras/keras.json中设置的值,若从未设置过,则为“channel_last”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import os

Datagen = ImageDataGenerator(
rotation_range=5,#图片随机翻转的角度
width_shift_range=[0,0.15],
height_shift_range=[0,0.2],
zoom_range=[0.8,1.5], #随机放大
zca_whitening=True,
brightness_range=[0.1, 1.5] ,
fill_mode='nearest'
)


IMG_PATH = './images'
OUT_PATH = './add'

# for i in os.listdir(OUT_PATH):
# if i.startswith('N'):
# os.remove(os.path.join(OUT_PATH,i) )

for file_name in os.listdir(IMG_PATH):
img = load_img( os.path.join(IMG_PATH,file_name) )
x_img = img_to_array(img)
x_img = x_img.reshape( (1,)+ x_img.shape )
i = 0
if( not os.path.exists( os.path.join( OUT_PATH,file_name ) ) ):
os.mkdir( os.path.join( OUT_PATH,file_name ))
for img_batch in Datagen.flow(x_img,
batch_size=2, #batch_size一次增强的图片个数
save_to_dir= os.path.join(OUT_PATH,file_name),
save_prefix='N',
save_format='jpg'):
i +=1
if i == 80:
break
print('done')

参考资料:

参数说明

keras中文手册

https://www.cnblogs.com/hutao722/p/10075150.html

https://www.cnblogs.com/hutao722/p/10075150.html

flow_from_directory实现从文件夹中提取图片和进行简单归一化处理

1
2
3
4
5
6
7
8
flow_from_directory(self, directory,
target_size=(256, 256), color_mode='rgb',
classes=None, class_mode='categorical',
batch_size=32, shuffle=True, seed=None,
save_to_dir=None,
save_prefix='',
save_format='jpeg',
follow_links=False)

最值得注意的是directory这个参数:

directory: path to the target directory. It should contain one subdirectory per class. Any PNG, JPG, BMP, PPM or TIF images inside each of the subdirectories directory tree will be included in the generator.

这是官方文档的定义,它的目录格式一定要注意是包含一个子目录下的所有图片这种格式,driectoty路径只要写到标签路径上面的那个路径即可。

  • target_size:可是实现对图片的尺寸转换,是预处理中比较常用的方法
  • save_to_dir: 可以设置保存处理后图片的路径。
  • save_prefix: 可以对处理后图片设置前缀。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import matplotlib.pyplot as  plt
import glob
from PIL import Image
from keras.preprocessing import image

path = 'train/'
gen_path = 'result/'

def print_result(path):
name_list = glob.glob(path)
fig = plt.figure()
for i in range(9):
img = Image.open(name_list[i])
# add_subplot(331) 参数一:子图总行数,参数二:子图总列数,参数三:子图位置
sub_img = fig.add_subplot(331 + i)
sub_img.imshow(img)
plt.show()
return fig

# 打印图片列表
name_list = glob.glob(path + '*/*')
print(name_list)
# ['train\\00a366d4b4a9bbb6c8a63126697b7656.jpg', 'train\\00f34ac0a16ef43e6fd1de49a26081ce.jpg', 'train\\0a5f744c5077ad8f8d580081ba599ff5.jpg', 'train\\0a70f64352edfef4c82c22015f0e3a20.jpg', 'train\\0a783538d5f3aaf017b435ddf14cc5c2.jpg', 'train\\0a896d2b3af617df543787b571e439d8.jpg', 'train\\0abdda879bb143b19e3c480279541915.jpg', 'train\\0ac12f840df2b15d46622e244501a88c.jpg', 'train\\0b6c5bc46b7a0e29cddfa45b0b786d09.jpg']

# 打印图片
fig = print_result(path + '*/*')

# 保存图片
fig.savefig(gen_path + '/original_0.png', dpi=200, papertype='a5')

# 原图
datagen = image.ImageDataGenerator()
gen_data = datagen.flow_from_directory(path, batch_size=1, shuffle=False, save_to_dir=gen_path ,
save_prefix='dog_gen', target_size=(224, 224))
for i in range(9):
gen_data.next()

fig = print_result(gen_path + '/*')
fig.savefig(gen_path + '/original_1.png', dpi=200, papertype='a5')

Author: Mrli

Link: https://nymrli.top/2019/05/27/Keras使用——图像增强/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
Google Colab使用
NextPost >
Python机器学习及实践——从零开始通往Kaggle竞赛之路
CATALOG
  1. 1. Keras使用——图像增强
    1. 1.1. 使用Keras进行深度学习的图像增强(Image Augmentation)
      1. 1.1.1. ImageDataGenerator
        1. 1.1.1.1. ImageDataGenerator参数详解及用法实例
      2. 1.1.2. flow_from_directory实现从文件夹中提取图片和进行简单归一化处理