ricky
ricky

Reputation: 11

how to fix overfitting or where is my fault in my code

I'm using a pre-trained model Vgg16 to do 100 classification problem. The dataset is tiny-imagenet, each class has 500 images, and I random choose 100 class from tiny-imagenet for my training(400) and validation(100) data. So I change input_shape of vgg16 for 32*32 size. The results always look like overfitting. Training acc is high, but val_acc always stuck at almost 40%. I used dropout, regularization L2, data augmentation ... , but val_acc is also stuck at almost 40%. How could I do for overfitting or correct my code. Thanks

img_width, img_height = 32, 32

epochs = 50

learning_rate = 1e-4

steps_per_epoch = 2500

train_path='./training_set_100A/'

valid_path='./testing_set_100A/'

test_path='./testing_set_100A/'

class_num = 100


train_batches = ImageDataGenerator(rescale=1. / 255
                ,rotation_range=20, zoom_range=0.15,
                width_shift_range=0.2, height_shift_range=0.2,     
shear_range=0.15,
                horizontal_flip=True, fill_mode="nearest"
                ).flow_from_directory(
                train_path, target_size=(img_width,img_height),     
batch_size=32, shuffle=True)

valid_batches = ImageDataGenerator(rescale=1. / 255).flow_from_directory(
                valid_path, target_size=(img_width,img_height),     
batch_size=10, shuffle=False)

test_batches = ImageDataGenerator(rescale=1. / 255).flow_from_directory(
test_path, target_size=    
(img_width,img_height),batch_size=10,shuffle=False)


seqmodel = Sequential()

VGG16Model = VGG16(weights='imagenet', include_top=False)

input = Input(shape=(img_width, img_height, 3), name='image_intput')

output_vgg16_conv = VGG16Model(input)

x = Flatten()(output_vgg16_conv)

x = Dense(4096, activation='relu')(x)

x = Dropout(0.5)(x)

x = Dense(4096, activation='relu')(x)

x = Dropout(0.5)(x)

x = Dense(class_num, activation='softmax')(x)

funcmodel = Model([input], [x])

funcmodel.summary()    

funcmodel.compile(optimizer=SGD(lr=learning_rate, momentum=0.9), 
loss='categorical_crossentropy', metrics=['accuracy'])

train_history = funcmodel.fit_generator(train_batches, 
steps_per_epoch=steps_per_epoch, validation_data=valid_batches, 
validation_steps=1000, epochs=epochs, verbose=1)

`

Upvotes: 1

Views: 334

Answers (2)

youngseok jeon
youngseok jeon

Reputation: 116

1) 50epoch is too much. Try running smaller epoch?

2) Check your validation accuracy for every epoch?

3) VGG is too deep for your small(32 * 32) image data. Try building your own network with lesser number of parameters. or Try Lenet?

Upvotes: 0

david.abekasis
david.abekasis

Reputation: 166

It seems you followed examples of implementing this from other sites, but you're training samples are very small to train the 2 new Dense layers of 4096 size each. you have to either lower the size of you layers or add a lot more samples 20,000 instead of 500.

Upvotes: 1

Related Questions