Augmenting on the fly: running out of data and validation accuracy=0.5

493 Views Asked by At

My validation accuracy is stuck at 50% while my training accuracy manages to converge to 100%. The pitfall is that i have very few data: 46 images in train set and 12 in validation set. Therefore, I am augmenting my data while training but i am running out of data too early. and as i saw from previous answers that i should specify steps_per_epoch. however, using steps_per_epoch=46/batch_size is not returning that much of iteration (maximum of 10 if i specify a very low batch size).

I assume data augmentation is not being applied? How can i be sure my data is indeed being augmented? Below is my data augmentation code:

gen=ImageDataGenerator(rotation_range=180,
    horizontal_flip=True,
    vertical_flip=True,
    )

train_batches=gen.flow(
    x=x_train,
    y=Y_train,
    batch_size=5,
    subset=None,
    shuffle=True
    
)


val_batches=gen.flow(
    x=x_val,
    y=Y_val,
    batch_size=3,
    subset=None,
    shuffle=True
)


history= model.fit(
          train_batches,
           batch_size=32,
        #  steps_per_epoch=len(x_train)/batch_size,  
          epochs=50, 
          verbose=2,
          validation_data=val_batches,
          validation_steps=len(x_val)/batch_size)

I will really appreciate your help!

1

There are 1 best solutions below

0
Olga Chernytska On

I think the mistake is not in your code. You have a very small dataset, you are using only 2 augmentations, and (I assume) you initialize your model with random weights. Your model expectedly overfits.

Here are a couple of ideas that may help you:

  1. Add more argumentations. Vertical and horizontal flips - are just not enough (with your small dataset). Think about crops, rotations, color changes etc. BTW here is a good tutorial on image augmentation where you'll find more ideas on what types of data augmentation you can use for your task: https://notrocketscience.blog/complete-guide-to-data-augmentation-for-computer-vision/

  2. Transfer learning - is a must-do for small datasets. If you are using popular/default architecture, PyTorch and Tensorflow allow you to load model weights trained on ImageNet, for instance. If your architecture is custom - download some open-source dataset (better similar to your task) and pretrain model with this data.

  3. Appropriate validation. Consider n-fold cross-validation, because a fixed train and test set is not a good idea for the small datasets. Your validation accuracy may be low by chance (for instance, all "hard" images are in the test set), but not because the model is bad.

Let me know if it helps!