This article is a note of my Keras study which there is an only small amount of data and I need to improve the accuracy of my training result. Since Data Augmentation becomes quite bit popular for many of the ML researchers, it is vitally important to understand what is it and how to implement it in practice.
#############
Updating...
#############
- How exactly the number of training examples after
ImageDataGenerator
has been implemented.
https://stackoverflow.com/questions/51748514/does-imagedatagenerator-add-more-images-to-my-dataset
You need to set the steps_per_epoch argument of fit method to n_samples / batch_size, where n_samples is the total number of training data you have (i.e. 1000 in your case). This way in each epoch, each training sample is augmented only one time and therefore 1000 transformed images will be generated in each epoch.
Let me make this more clearer.
-
steps_per_epoch
[Source]../keras/engine/training.py/Model/
def fit_generator(self, generator,
steps_per_epoch=None,
epochs=1,
verbose=1,
callbacks=None,
validation_data=None,
validation_steps=None,
class_weight=None,
max_queue_size=10,
workers=1,
use_multiprocessing=False,
shuffle=True,
initial_epoch=0):
steps_per_epoch: Integer.
Total number of steps (batches of samples)
to yield fromgenerator
before declaring one epoch
finished and starting the next epoch. It should typically
be equal to the number of samples of your dataset
divided by the batch size.
Optional forSequence
: if unspecified, will use
thelen(generator)
as a number of steps.
In other words, if the argument steps_per_epoch
equals the number of iteration in each epoch, then the total augmented images will be exactly the same as the input samples, i.e. the augmented image will be generated exactly once(or we shall double the number of our input data)
To make this more clearer please check my other 简书 post
Appendix of steps_per_epoch
- batch_size determines the number of samples in each mini batch. Its maximum is the number of all samples, which makes gradient descent accurate, the loss will decrease towards the minimum if the learning rate is small enough, but iterations are slower. Its minimum is 1, resulting in stochastic gradient descent: Fast but the direction of the gradient step is based only on one example, the loss may jump around. batch_size allows to adjust between the two extremes: accurate gradient direction and fast iteration. Also, the maximum value for batch_size may be limited if your model + data set does not fit into the available (GPU) memory.
- steps_per_epoch the number of batch iterations before a training epoch is considered finished. If you have a training set of fixed size you can ignore it but it may be useful if you have a huge data set or if you are generating random data augmentations on the fly, i.e. if your training set has a (generated) infinite size. If you have the time to go through your whole training data set I recommend to skip this parameter.
- validation_steps similar to steps_per_epoch but on the validation data set instead on the training data. If you have the time to go through your whole validation data set I recommend to skip this parameter.
网友评论