GhostRider
GhostRider

Reputation: 2170

Keras CONV training during data augmentation appears to be displaying the wrong batch size and number of training examples

I am learning how to implement data augmentation using Keras and the CIFAR-10 dataset. I am learning with the help of online tutorials and this book Deep learning with Keras.

The specific details of the code are here.

Here is my issue, and I am certain it relates to some misunderstanding on my part:

This is my CONV set up.

 IMG_CHANNELS = 3
 IMG_ROWS = 32
 IMG_COLS = 32
 BATCH_SIZE = 128
 NB_EPOCH = 50
 NB_CLASSES = 10
 VERBOSE = 1
 VALIDATION_SPLIT = 0.2
 OPTIM = RMSprop()

Load the dataset, convert to categorical, float and normalise:

(X_train, y_train), (X_test, y_test) = cifar10.load_data()
Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES) 
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

Create generator

datagen = ImageDataGenerator(
        featurewise_center=False,  # set input mean to 0 over the dataset
        samplewise_center=False,  # set each sample mean to 0
        featurewise_std_normalization=False,  # divide inputs by std of the dataset
        samplewise_std_normalization=False,  # divide each input by its std
        zca_whitening=False,  # apply ZCA whitening
        rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)
        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,  # randomly flip images
        vertical_flip=False)  # randomly flip images
 datagen.fit(X_train)

Train the model (I haven't listed the model)

model.fit_generator(datagen.flow(X_train, Y_train,
                   batch_size=BATCH_SIZE),
                   samples_per_epoch=X_train.shape[0],
                   nb_epoch=NB_EPOCH, 
                   verbose=VERBOSE)

My issue is that as I train the following is displayed:

 Epoch 1/40
 390/390 [==============================] - 199s - loss: 0.9751 - acc: 0.6588 

I cannot see why I am getting 390 examples. Samples_per_epoch is equal to X_train.shape[0] which is 50000 and batch size is 128 so I thought it should go up to 50000 in batches of 128.

Upvotes: 2

Views: 246

Answers (1)

Wilmar van Ommeren
Wilmar van Ommeren

Reputation: 7689

The progress-bar is not showing the number of samples but the number of steps or batches (When you use model.fit instead of model.fit_generator it will automatically display the samples). Each batch contains 128 samples and there are a total of 50,000 samples. 50,000/128 = 390.625. That's why you see 390 instead of 50,000.

Because you are using model.fit_generator it is not possible to show the total number of samples. Except if you set the batch_size to 1. The reason for this is that the generator is expected to loop over its data indefinitely until the steps_per_epochs or samples_per_epoch threshold is reached (*).

By the way, you are able to change this in model.fit with the callback ProgbarLogger, look here.

Upvotes: 3

Related Questions