Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

python - What is Keras ImageDataGenerator logic?

I was wondering about how the datagenerator works, especially the image multiplying part during training.

I used the generator with .next() and saw the results. But I am a little bit confused if it multiplies images during training.

In the lines of code below is the implementation of the generator, but the steps_per_epoch is just the number of the batches of my images len(train_generator.classes) // batch_size. So if my database consists of 1024 images, I will get 16 batches with 64 images in each, as well as I will have 16 steps per epoch. So if I am doing steps just for my initial images, where do the multiplied images go? Or maybe I don't really understand the step_per_epoch part?

# Import of the data
data_generator = ImageDataGenerator(
        #rescale=1.0/255.0,
        rotation_range=10,
        width_shift_range=0.1,
        horizontal_flip=True,
        height_shift_range=0.1,
        validation_split=0.3
        )

train_generator = data_generator.flow_from_directory(
        train_path,
        target_size=(img_height, img_width),
        batch_size=batch_size,
        color_mode="grayscale",
        class_mode="categorical",
        subset="training"
        )

model.fit(train_generator,
                 steps_per_epoch=len(train_generator.classes) // batch_size,
                 epochs=epoch_n,
                 validation_steps=len(valid_generator.classes) // batch_size,
                 validation_data=valid_generator,
)
question from:https://stackoverflow.com/questions/65842406/what-is-keras-imagedatagenerator-logic

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is not exactly how ImageDataGenerator works, it does not "multiply" your data. If you don't pass any argument during the instantiation, it will just fetch your images from the folder and feed them, unspoiled to the network.

When you add : rotation_range, width_shift_range... you are instructing the data_generator to apply random modification to the incoming images within the specified range. These are random augmentation during the training, they are not pushed after your done with your initial data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...