简体   繁体   English

如何使用 keras 和 tensorflow 的 ImageDataGenerator 执行数据增强

[英]How to perform data augmentation using keras and tensorflow's ImageDataGenerator

I'm having a hard time understanding how to implement data augmentation with tensorflow.我很难理解如何使用 tensorflow 实现数据增强。 I have a dataset (images), that is divided into two subsets;我有一个数据集(图像),分为两个子集; training and testing.培训和测试。 After I have called the ImageDataGenerator functions with various parameters, do I need to save the images (like using the flow() ) or will Tensorflow augment my data while the model is training?在我使用各种参数调用ImageDataGenerator函数后,我是否需要保存图像(如使用flow() )或者 Tensorflow 是否会在 model 训练时增加我的数据?

Here is my implemented code:这是我实现的代码:

# necessary imports

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    brightness_range=(0.3, 1.0),
    horizontal_flip=True,
    vertical_flip=True,
    fill_mode='nearest',
    validation_split=0.2
)

training_directory = '/tmp/dataset/training'
testing_directory = '/tmp/dataset/testing'

training_set = train_datagen.flow_from_directory(
    training_directory,
    target_size=(150, 150),
    batch_size=32,
    class_mode='binary',
    subset='training'
)

test_set = train_datagen.flow_from_directory(
    testing_directory,
    target_size=(150, 150),
    batch_size=32,
    class_mode='binary',
    subset='validation'
)

# creating a sequential model
...
# fitting and data plotting

The model summary: model总结:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d (Conv2D)              (None, 148, 148, 32)      896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 74, 74, 32)        0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 72, 72, 64)        18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 36, 36, 64)        0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 34, 34, 128)       73856
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 17, 17, 128)       0
_________________________________________________________________
dropout (Dropout)            (None, 17, 17, 128)       0
_________________________________________________________________
flatten (Flatten)            (None, 36992)             0
_________________________________________________________________
dense (Dense)                (None, 512)               18940416
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 513
=================================================================
Total params: 19,034,177
Trainable params: 19,034,177
Non-trainable params: 0
_________________________________________________________________

You don't have to save new data.您不必保存新数据。

When calling flow method, data are augmented on the fly and are served as input to the model.调用流方法时,数据会即时扩充并作为 model 的输入。

So, data are being generated in real time and feed immediately into your model.因此,数据正在实时生成并立即输入您的 model。

You don't need to save the data.您无需保存数据。 The augmented data (train/test) is fed directly into the model for training or evaluation steps using the train and test data generators.增强数据(训练/测试)直接输入 model 以使用训练和测试数据生成器进行训练或评估步骤。

Here is your code updated with all the steps using the created data generators train_generator and test_generator .这是使用创建的数据生成器train_generatortest_generator更新了所有步骤的代码。

 datagenerator = ImageDataGenerator(
    rescale=1. / 255,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    brightness_range=(0.3, 1.0),
    horizontal_flip=True,
    vertical_flip=True,
    fill_mode='nearest',
    validation_split=0.2
)
 
training_directory = '/tmp/dataset/training'
testing_directory = '/tmp/dataset/testing'

train_generator = datagenerator.flow_from_directory(
    training_directory,
    target_size=(150, 150),
    batch_size=32,
    class_mode='binary',
    subset='training'
)

test_generator = datagenerator.flow_from_directory(
    testing_directory,
    target_size=(150, 150),
    batch_size=32,
    class_mode='binary',
    subset='validation'
)

# Build and compile the model
....
# Get the number of steps per epoch for each of the data generators
train_steps_per_epoch = train_generator.n // train_generator.batch_size
test_steps_per_epoch = test_generator.n // test_generator.batch_size

# Fit the model
model.fit_generator(train_generator, steps_per_epoch=train_steps_per_epoch, epochs=your_nepochs)

# Evaluate the model
model.evaluate_generator(test_generator, steps=test_steps_per_epoch)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 keras-python 中使用 ImageDataGenerator 进行数据增强 - Data Augmentation using ImageDataGenerator in keras-python 使用 ImageDataGenerator 进行数据增强 - Data augmentation using ImageDataGenerator Keras ImageDataGenerator:如何使用图像路径进行数据增强 - Keras ImageDataGenerator : how to use data augmentation with images paths 使用 TensorFlow-Keras API 进行数据增强 - Data Augmentation using TensorFlow-Keras API 如何使用Keras执行离线图像增强? - How to perform offline image augmentation using Keras? 使用 ImageDataGenerator 和 flow_from_directory 时,Keras 中的数据增强是否应用于验证集 - Is data augmentation in Keras applied to the validation set when using ImageDataGenerator and flow_from_directory 如何在 keras.preprocessing.image.ImageDataGenerator 前后显示数据增强的结果 - how to show results of data augmentation before and after keras.preprocessing.image.ImageDataGenerator 使用 ImageDataGenerator 对 Keras 中的视频(4D 张量)进行数据增强 - Data augmentation with ImageDataGenerator for videos (4D tensors) in Keras 执行增强(使用ImageDataGenerator)并将增强图像保存为原始名称 - Perform Augmentation (using ImageDataGenerator) and save augmented image as original name 如何在keras或tensorflow中使用ImageDataGenerator获得相同的增强图像? - How to obtain same augmented images using ImageDataGenerator in keras or tensorflow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM