一 单张图片+flow函数
###图片来源形式
"""
E:/tmp/0010.jpg
E:/tmp/0010.png
"""
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
img_x = load_img('E:/tmp/0010.jpg') # this is a PIL image
x = img_to_array(img_x) # this is a Numpy array with shape (267, 400, 3)
x1 = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 267, 400, 3)
img_y = load_img('E:/tmp/0010.png') # this is a PIL image
y = img_to_array(img_y) # this is a Numpy array with shape (267, 400, 3)
y1 = y.reshape((1,) + y.shape) # this is a Numpy array with shape (1, 267, 400, 3)
# the .flow() command below generates batches of randomly transformed images
# and saves the results to the `preview/` directory
seed = 1
i = 0
for batch in datagen.flow(x1, batch_size=1,seed=seed,
save_to_dir='E:/tmp/augment', save_format='jpg'):
i += 1
if i > 20:
break # otherwise the generator would loop indefinitely
i = 0
for batch in datagen.flow(y1, batch_size=1,seed=seed,
save_to_dir='E:/tmp/augment', save_format='png'):
i += 1
if i > 20:
break
二 多张图片+flow函数
###图片来源形式应该为
"""
E:/tmp/augment1/image/1.jpg # flow_from_directory(directory='E:/tmp/augment1',
/2.jpg
/3.jpg
...
/n.jpg
E:/tmp/augment3/mask/1.png # flow_from_directory(directory='E:/tmp/augment3',
/2.png
/3.png
...
/n.png
"""
### import module
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import numpy as np
import os
from skimage import io, transform # python的Image和skimage处理图片
### path
data_path = 'E:/tmp/augment1/image/'
label_path = 'E:/tmp/augment3/mask/'
### parameters
w = 224
h = 224
c = 3
### read the data
def read_img(path):
imgs = []
for filename in os.listdir(path):
# print(filename)
img = io.imread(path + filename)
x = transform.resize(img, (w, h, c)) # this is a Numpy array with shape (w, h, 3))
imgs.append(x) # this is a Numpy array with shape (images_number, w, h, 3)
return np.asarray(imgs, np.float32)
data = read_img(data_path)
label = read_img(label_path)
### ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
seed = 1
i = 0
for batch in datagen.flow(data, batch_size=1,seed=seed,
save_to_dir='E:/tmp/augment', save_format='jpg'):
i += 1
if i > 20:
break
i = 0
for batch in datagen.flow(label, batch_size=1,seed=seed,
save_to_dir='E:/tmp/augment', save_format='png'):
i += 1
if i > 20:
break
三 单张image和单张mask
### error
""" Found 0 images belonging to 0 classes.
current_index = (self.batch_index * self.batch_size) % self.n
ZeroDivisionError: integer division or modulo by zero
"""
### error_reason
"""
E:/tmp/augment2/0010.jpg
'E:/tmp/augment1'文件夹下直接是图片,不可以。
"""
### modify_error
"""
flow_from_directory(dire)
dire文件夹下必须有子文件夹才行,子文件夹下再放图片
E:/tmp/augment2/0010.jpg >>> E:/tmp/augment2/train/0010.jpg
"""
###error_modified_succeeded
"""Found 1 images belonging to 1 classes."""
###图片来源形式应该为
"""
E:/tmp/augment1/image/i.jpg # flow_from_directory(directory='E:/tmp/augment1',
E:/tmp/augment3/mask/i.png # flow_from_directory(directory='E:/tmp/augment3',
"""
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
seed= 1
train_images_generator = train_datagen.flow_from_directory(
'E:/tmp/augment1',
target_size=(224, 224),
batch_size=1,
color_mode="rgb",
class_mode=None,
save_to_dir='E:/tmp/augment2',
save_format='jpg',
seed=seed)
train_masks_generator = train_datagen.flow_from_directory(
'E:/tmp/augment3',
target_size=(224, 224),
batch_size=1,
color_mode="grayscale",
class_mode=None,
save_to_dir='E:/tmp/augment2',
save_format='png',
seed=seed)
i = 0
for batch in train_images_generator:
i += 1
if i > 20:
break # otherwise the generator would loop indefinitely
# result: 20 images, 与 batch_size=1选项无关。
i = 0
for batch in train_masks_generator:
i += 1
if i > 20:
break # otherwise the generator would loop indefinitely
四 多张images和多张masks
###图片来源形式应该为
"""
E:/tmp/augment1/image/1.jpg # flow_from_directory(directory='E:/tmp/augment1',
/2.jpg
/3.jpg
...
/n.jpg
E:/tmp/augment3/mask/1.png # flow_from_directory(directory='E:/tmp/augment3',
/2.png
/3.png
...
/n.png
"""
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest')
seed = 1
train_images_generator = train_datagen.flow_from_directory(
'E:/tmp/augment1',
target_size=(224, 224),
batch_size=1,
color_mode="rgb",
class_mode=None,
save_to_dir='E:/tmp/augment4',
save_format='jpg',
seed=seed,
shuffle=True)
train_masks_generator = train_datagen.flow_from_directory(
'E:/tmp/augment3',
target_size=(224, 224),
batch_size=1,
color_mode="grayscale",
class_mode=None,
save_to_dir='E:/tmp/augment5',
save_format='png',
seed=seed,
shuffle=True)
i = 0
for batch in train_images_generator:
i += 1
if i > 20:
break # otherwise the generator would loop indefinitely
if i == 1:
print(batch.shape) # this is a Numpy array with shape (1, 224, 224, 3)
i = 0
for batch in train_masks_generator:
i += 1
if i > 20:
break # otherwise the generator would loop indefinitely
if i == 1:
print(batch.shape) # this is a Numpy array with shape (1, 224, 224, 1)
### result: 20 images,
"""与 batch_size=1选项无关。
与'E:/tmp/augment3'的文件夹flower下的图片数无关。新生成的图片数量只与i有关,文件夹下数目为n,
1)i=10,n=5;则10/5=2,每张图片变形新生成两张;总生成10张图片
2)i=10,n=15;则10/15,随机10张图片变形新生成1张;总生成10张图片
"""
参考:https://blog.csdn.net/xijuezhu8128/article/details/79918456