Berk Karadas
Berk Karadas

Reputation: 37

CNN Transfer Learning Takes Too Much Time

I'm trying to train my model with transfer learning with Vgg16 via Google Colab(using GPU) but it takes too time and validation and test accuracy is low. Additional informations; Train data is 16057 , test data is 4000, validation data is 2000 with different sized rgb images. Classes facial mood expressions (Happy,Sad,Energetic,Neutral) .Any suggestion ??

#source root directory and distination root directory
train_src = "/content/drive/MyDrive/Affectnet/train_class/"
val_src = "/content/drive/MyDrive/Affectnet/val_class/"
test_src="/content/drive/MyDrive/Affectnet/test_classs/"
train_datagen = tensorflow.keras.preprocessing.image.ImageDataGenerator(
      rescale=1./255, 
      rotation_range=40, 
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest'
      )
train_generator = train_datagen.flow_from_directory(
        train_src,
        target_size=(224,224),
        batch_size=32,
        shuffle=True
        )
validation_datagen = tensorflow.keras.preprocessing.image.ImageDataGenerator(
        rescale=1./255
        )

validation_generator = validation_datagen.flow_from_directory(
        val_src,
        target_size=(224, 224),
        batch_size=32,
        )
conv_base = tensorflow.keras.applications.VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(224, 224, 3)
                  )

for layer in conv_base.layers:
    layer.trainable=False
# An empyty model is created.
model = tensorflow.keras.models.Sequential()

# VGG16 is added as convolutional layer.
model.add(conv_base)

# Layers are converted from matrices to a vector.
model.add(tensorflow.keras.layers.Flatten())

# Our neural layer is added.
model.add(tensorflow.keras.layers.Dense(4, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer=tensorflow.keras.optimizers.Adam(lr=1e-5),
              metrics=['acc'])
history = model.fit_generator(
      train_generator,
      epochs=50,
      steps_per_epoch=100,
      validation_data=validation_generator,
      validation_steps=5)

Training Part

EDIT= I set the workers = 8 and model started training faster but i took .69 test accuracy after 30 epoch . Any Suggestion ?

Upvotes: 1

Views: 507

Answers (1)

ahmet hamza emra
ahmet hamza emra

Reputation: 630

Issue most likely related to "ImageDataGenerator", try using workers=8 in your fit_generator.

Upvotes: 1

Related Questions