如何在keras中使用ImageDataGenerator和flow_from_directory保存调整大小的图像
how to save resized images using ImageDataGenerator and flow_from_directory in keras
我正在使用以下代码调整存储在文件夹(两个 类)中的 RGB 图像的大小:
from keras.preprocessing.image import ImageDataGenerator
dataset=ImageDataGenerator()
dataset.flow_from_directory('/home/1',target_size=(50,50),save_to_dir='/home/resized',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)
我的数据树如下:
1/
1_1/
img1.jpg
img2.jpg
........
1_2/
IMG1.jpg
IMG2.jpg
........
resized/
1_1/ (here i want to save resized images of 1_1)
2_2/ (here i want to save resized images of 1_2)
在 运行 代码之后,我得到以下输出但不是图像:
Found 271 images belonging to 2 classes.
Out[12]: <keras.preprocessing.image.DirectoryIterator at 0x7f22a3569400>
如何保存图片?
flow_from_directory
方法为您提供 "iterator",如输出中所述。迭代器本身并没有真正做任何事情。它正在等待迭代,然后才会读取和生成实际数据。
Keras中拟合的迭代器是这样使用的:
generator = dataset.flow_from_directory('/home/1',target_size=(50,50),save_to_dir='/home/resized',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)
for inputs,outputs in generator:
#do things with each batch of inputs and ouptus
通常,您无需执行上述循环,只需将生成器传递给 fit_generator
方法即可。没有真正需要做一个 for 循环:
model.fit_generator(generator, ......)
Keras 只会在图像加载并通过遍历生成器扩充后保存图像。
这是一个非常简单的版本,可以将一张图像的增强图像保存到任何你想要的地方:
第一步,初始化图像数据生成器
在这里我们弄清楚我们要对原始图像进行哪些更改并生成增强图像
您可以在此处阅读有关差异效应的信息- https://keras.io/preprocessing/image/
datagen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1,
height_shift_range=0.1,shear_range=0.15,
zoom_range=0.1,channel_shift_range = 10, horizontal_flip=True)
第 2 步:这里我们选择原始图像进行增强
读入图片
image_path = 'C:/Users/Darshil/gitly/Deep-Learning/My
Projects/CNN_Keras/test_augment/caty.jpg'
image = np.expand_dims(ndimage.imread(image_path), 0)
第 3 步:选择要保存增强图像的位置
save_here = 'C:/Users/Darshil/gitly/Deep-Learning/My
Projects/CNN_Keras/test_augment'
第四步,我们拟合原图
datagen.fit(image)
第 5 步:迭代图像并使用 "save_to_dir" 参数保存
for x, val in zip(datagen.flow(image, #image we chose
save_to_dir=save_here, #this is where we figure out where to save
save_prefix='aug', # it will save the images as 'aug_0912' some number for every new augmented image
save_format='png'),range(10)) : # here we define a range because we want 10 augmented images otherwise it will keep looping forever I think
pass
它只是一个声明,你必须使用那个生成器,例如.next()
from keras.preprocessing.image import ImageDataGenerator
dataset=ImageDataGenerator()
image = dataset.flow_from_directory('/home/1',target_size=(50,50),save_to_dir='/home/resized',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)
image.next()
然后您将在 /home/resized
中看到图片
如果您想将图像保存在与标签同名的文件夹下,那么您可以遍历标签列表并在循环中调用增强代码。
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Augmentation + save augmented images under augmented folder
IMAGE_SIZE = 224
BATCH_SIZE = 500
LABELS = ['lbl_a','lbl_b','lbl_c']
for label in LABELS:
datagen_kwargs = dict(rescale=1./255)
dataflow_kwargs = dict(target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE, interpolation="bilinear")
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
rotation_range=40,
horizontal_flip=True,
width_shift_range=0.1, height_shift_range=0.1,
shear_range=0.1, zoom_range=0.1,
**datagen_kwargs)
train_generator = train_datagen.flow_from_directory(
'original_images', subset="training", shuffle=True, save_to_dir='aug_images/'+label, save_prefix='aug', classes=[label], **dataflow_kwargs)
# Following line triggers execution of train_generator
batch = next(train_generator)
既然生成器可以直接传递给模型,为什么还要这样做呢?如果您想使用 tflite-model-maker
,它不接受生成器并接受每个标签的文件夹下的标签数据:
from tflite_model_maker import ImageClassifierDataLoader
data = ImageClassifierDataLoader.from_folder('aug_images')
结果
aug_images
|
|__ lbl_a
| |
| |_____aug_img_a.png
|
|__ lbl_b
| |
| |_____aug_img_b.png
|
|__ lbl_c
| |
| |_____aug_img_c.png
注意:您需要确保文件夹已经存在。
datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
rotation_range =15,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip = True,
fill_mode = 'nearest',
brightness_range=[0.5, 1.5])
DATA_DIR = 'splited/train/'
save_here = 'aug dataset/train/normal2/'
cancer = os.listdir(DATA_DIR + 'cancer/')
for i, image_name in enumerate(cancer):
try:
if (image_name.split('.')[1] == 'png'):
image = np.expand_dims(cv2.imread(DATA_DIR +'classs 1/' + image_name), 0)
for x, val in zip(datagen.flow(image, #image we chose save_to_dir=save_here, #this is where we figure out where to save
save_prefix='aug', # it will save the images as 'aug_0912' some number for every new augmented image
save_format='png'),range(10)) : # here we define a range because we want 10 augmented images otherwise it will keep looping forever I think
pass
except Exception:
print("Could not read image {} with name {}".format(i, image_name))
您可以尝试这个简单的代码示例并根据需要进行修改:
(它根据您的数据生成增强图像,然后将它们保存到不同的文件夹中)
from keras.preprocessing.image import ImageDataGenerator
data_dir = 'data/train' #Due to the structure of ImageDataGenerator, you need to have another folder under train contains your data, for example: data/train/faces
save_dir = 'data/resized'
datagen = ImageDataGenerator(rescale=1./255)
resized = datagen.flow_from_directory(data_dir, target_size=(224, 224),
save_to_dir=save_dir,
color_mode="rgb", # Choose color mode
class_mode='categorical',
shuffle=True,
save_prefix='N',
save_format='jpg', # Formate
batch_size=1)
for in in range(len(resized)):
resized.next()
我正在使用以下代码调整存储在文件夹(两个 类)中的 RGB 图像的大小:
from keras.preprocessing.image import ImageDataGenerator
dataset=ImageDataGenerator()
dataset.flow_from_directory('/home/1',target_size=(50,50),save_to_dir='/home/resized',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)
我的数据树如下:
1/
1_1/
img1.jpg
img2.jpg
........
1_2/
IMG1.jpg
IMG2.jpg
........
resized/
1_1/ (here i want to save resized images of 1_1)
2_2/ (here i want to save resized images of 1_2)
在 运行 代码之后,我得到以下输出但不是图像:
Found 271 images belonging to 2 classes.
Out[12]: <keras.preprocessing.image.DirectoryIterator at 0x7f22a3569400>
如何保存图片?
flow_from_directory
方法为您提供 "iterator",如输出中所述。迭代器本身并没有真正做任何事情。它正在等待迭代,然后才会读取和生成实际数据。
Keras中拟合的迭代器是这样使用的:
generator = dataset.flow_from_directory('/home/1',target_size=(50,50),save_to_dir='/home/resized',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)
for inputs,outputs in generator:
#do things with each batch of inputs and ouptus
通常,您无需执行上述循环,只需将生成器传递给 fit_generator
方法即可。没有真正需要做一个 for 循环:
model.fit_generator(generator, ......)
Keras 只会在图像加载并通过遍历生成器扩充后保存图像。
这是一个非常简单的版本,可以将一张图像的增强图像保存到任何你想要的地方:
第一步,初始化图像数据生成器
在这里我们弄清楚我们要对原始图像进行哪些更改并生成增强图像
您可以在此处阅读有关差异效应的信息- https://keras.io/preprocessing/image/
datagen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1,
height_shift_range=0.1,shear_range=0.15,
zoom_range=0.1,channel_shift_range = 10, horizontal_flip=True)
第 2 步:这里我们选择原始图像进行增强
读入图片
image_path = 'C:/Users/Darshil/gitly/Deep-Learning/My
Projects/CNN_Keras/test_augment/caty.jpg'
image = np.expand_dims(ndimage.imread(image_path), 0)
第 3 步:选择要保存增强图像的位置
save_here = 'C:/Users/Darshil/gitly/Deep-Learning/My
Projects/CNN_Keras/test_augment'
第四步,我们拟合原图
datagen.fit(image)
第 5 步:迭代图像并使用 "save_to_dir" 参数保存
for x, val in zip(datagen.flow(image, #image we chose
save_to_dir=save_here, #this is where we figure out where to save
save_prefix='aug', # it will save the images as 'aug_0912' some number for every new augmented image
save_format='png'),range(10)) : # here we define a range because we want 10 augmented images otherwise it will keep looping forever I think
pass
它只是一个声明,你必须使用那个生成器,例如.next()
from keras.preprocessing.image import ImageDataGenerator
dataset=ImageDataGenerator()
image = dataset.flow_from_directory('/home/1',target_size=(50,50),save_to_dir='/home/resized',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)
image.next()
然后您将在 /home/resized
如果您想将图像保存在与标签同名的文件夹下,那么您可以遍历标签列表并在循环中调用增强代码。
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Augmentation + save augmented images under augmented folder
IMAGE_SIZE = 224
BATCH_SIZE = 500
LABELS = ['lbl_a','lbl_b','lbl_c']
for label in LABELS:
datagen_kwargs = dict(rescale=1./255)
dataflow_kwargs = dict(target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE, interpolation="bilinear")
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
rotation_range=40,
horizontal_flip=True,
width_shift_range=0.1, height_shift_range=0.1,
shear_range=0.1, zoom_range=0.1,
**datagen_kwargs)
train_generator = train_datagen.flow_from_directory(
'original_images', subset="training", shuffle=True, save_to_dir='aug_images/'+label, save_prefix='aug', classes=[label], **dataflow_kwargs)
# Following line triggers execution of train_generator
batch = next(train_generator)
既然生成器可以直接传递给模型,为什么还要这样做呢?如果您想使用 tflite-model-maker
,它不接受生成器并接受每个标签的文件夹下的标签数据:
from tflite_model_maker import ImageClassifierDataLoader
data = ImageClassifierDataLoader.from_folder('aug_images')
结果
aug_images
|
|__ lbl_a
| |
| |_____aug_img_a.png
|
|__ lbl_b
| |
| |_____aug_img_b.png
|
|__ lbl_c
| |
| |_____aug_img_c.png
注意:您需要确保文件夹已经存在。
datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
rotation_range =15,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip = True,
fill_mode = 'nearest',
brightness_range=[0.5, 1.5])
DATA_DIR = 'splited/train/'
save_here = 'aug dataset/train/normal2/'
cancer = os.listdir(DATA_DIR + 'cancer/')
for i, image_name in enumerate(cancer):
try:
if (image_name.split('.')[1] == 'png'):
image = np.expand_dims(cv2.imread(DATA_DIR +'classs 1/' + image_name), 0)
for x, val in zip(datagen.flow(image, #image we chose save_to_dir=save_here, #this is where we figure out where to save
save_prefix='aug', # it will save the images as 'aug_0912' some number for every new augmented image
save_format='png'),range(10)) : # here we define a range because we want 10 augmented images otherwise it will keep looping forever I think
pass
except Exception:
print("Could not read image {} with name {}".format(i, image_name))
您可以尝试这个简单的代码示例并根据需要进行修改:
(它根据您的数据生成增强图像,然后将它们保存到不同的文件夹中)
from keras.preprocessing.image import ImageDataGenerator
data_dir = 'data/train' #Due to the structure of ImageDataGenerator, you need to have another folder under train contains your data, for example: data/train/faces
save_dir = 'data/resized'
datagen = ImageDataGenerator(rescale=1./255)
resized = datagen.flow_from_directory(data_dir, target_size=(224, 224),
save_to_dir=save_dir,
color_mode="rgb", # Choose color mode
class_mode='categorical',
shuffle=True,
save_prefix='N',
save_format='jpg', # Formate
batch_size=1)
for in in range(len(resized)):
resized.next()