I want to classify dog breed using data augmentation and transfer learning using VGG16 as the cnn.
First I'm doing some data augmentation using ImageDataGenerator from keras
train_datagen = ImageDataGenerator(rotation_range = 30,
width_shift_range = 0.2,
height_shift_range = 0.2,
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = 'nearest')
train_generator = train_datagen.flow_from_directory('../data/train/',
target_size = (224, 224),
batch_size = batch_size,
class_mode = 'categorical')
The flow_from_directory
method returns a DirectoryIterator yielding tuples of (x, y) where x is a numpy array containing a batch of images with shape (batch_size, *target_size, channels) and y is a numpy array of corresponding labels. Since here the class_mode is caterogical, it's supposed to return 2D one-hot encoded labels for y.
Then I do transfer learning removing only the last layer replacing it with a dense layer with a softmax activation.
model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
for layer in model.layers:
layer.trainable = False
x = model.output
predictions = Dense(120, activation='softmax')(x)
new_model = Model(inputs=model.input, outputs=predictions)
Then I fit my data to the model :
new_model.fit_generator(train_generator,
steps_per_epoch = 6680 // batch_size,
epochs = 50,
validation_data = validation_generator,
validation_steps = 835 // batch_size,
verbose=2)
And I get the error : ValueError: Error when checking target: expected dense_3 to have 4 dimensions, but got array with shape (16, 120)
I have no idea where the problem comes from :(
Thanks for your help !