Harry Moreno
Harry Moreno

Reputation: 11673

Data Augmentation hurts accuracy Keras

I'm trying to adapt Deep Learning with Python section 5.3 Feature extraction with Data Augmentation to a 3-class problem with resnet50 (imagenet weights).

Full code at https://github.com/morenoh149/plantdisease

from keras import models
from keras import layers
from keras.applications.resnet50 import ResNet50
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator

input_shape = (224, 224, 3)
target_size = (224, 224)
batch_size = 20

conv_base = ResNet50(weights='imagenet', input_shape=input_shape, include_top=False)

model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(3, activation='softmax'))

conv_base.trainable = False

train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
    'input/train',
    target_size=target_size,
    batch_size=batch_size,
    class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
    'input/validation',
    target_size=target_size,
    batch_size=batch_size,
    class_mode='categorical')
model.compile(loss='categorical_crossentropy',
             optimizer=optimizers.RMSprop(lr=2e-5),
             metrics=['acc'])
history = model.fit_generator(
    train_generator,
    steps_per_epoch=96,
    epochs=30,
    verbose=2,
    validation_data=validation_generator,
    validation_steps=48)

Questions:

UPDATE: This may be an issue with keras itself

Upvotes: 0

Views: 1920

Answers (1)

sdcbr
sdcbr

Reputation: 7129

To answer your first question: steps_per_epochis the number of batches the training generator should yield before considering an epoch finished. If you have 600 training images with batch size 20, this would be 30 steps per epoch et cetera. validation_steps applies the same logic to the validation data generator, be it at the end of each epoch.

In general, steps_per_epoch is the size of your dataset divided by the batch size.

Upvotes: 0

Related Questions