0

I'm performing multiclass image classification using "xception" model and fine-tuning it. I don't have a large quantity of data so I am doing data augmentation thanks to flow_image_from_directory.

To make sure everything runs properly before applying it to my whole dataset with many augmentations, I have tried my code with no augmentation apart from a simple "rescale" and applied it to 20 pictures (5 for each of the 4 classes I have).

my_train_generator <- image_data_generator(rescale = 1/255, 
                                           validation_split = 1/5)

my_train_images <- flow_images_from_directory(directory = my_train_directory,
                                              generator = my_train_generator,
                                              target_size = c(width, height),
                                              color_mode = "rgb",
                                              batch_size = my_batch_size,
                                              class_mode = "categorical",
                                              shuffle = FALSE,
                                              save_to_dir = my_augm_train_directory,
                                              save_format = "jpeg",
                                              subset = 'training')

my_validation_images <- flow_images_from_directory(directory = my_train_directory,
                                                   generator = my_train_generator,
                                                   target_size = c(width, height),
                                                   color_mode = "rgb",
                                                   class_mode = "categorical",
                                                   batch_size = my_batch_size,
                                                   shuffle = FALSE,
                                                   save_to_dir = my_augm_val_directory,
                                                   save_format = "jpeg",
                                                   subset = 'validation')

However, thanks to the "save_to_dir" option, I see that the number of pictures generated throughout the fitting isn't as expected :

  • train & validation split is 1/5 --> train set is 16 images (4 per class) and validation set is 4 images (1 per class)
  • batch = 1 and epoch = 1

Based on that, I should have 16 images in the file generated from augmentation of the train set, and 4 images in the one generated from validation. I actually have respectively 36 and 5... Additionally, there are no transformation apart from "rescale" applied to the images in the file based on train set, but some of the 5 augmented validation images are flipped.

I am lost here, so many thanks in advance for your help :)

1 Answers1

0

Not sure what language you are using but for python the code below demonstrates how the ImageDataGenerator creates augmented images

my_train_batch_size=16 # each time next is called for train generator 16 image files and labels are returned
my_valid_batch_size=4 # each time next is called for valid generator 16 image files and labels are returned
width=100
height=100
my_train_directory=r'C:\Temp\temp' # directory has 4 classes with 5 images in each class
my_augm_train_directory=r'C:\Temp\tempaug\train' # where to store augmented train images
my_augm_val_directory=r'C:\Temp\tempaug\valid' # where to store augmented validation images
my_train_generator = ImageDataGenerator(rescale = 1/255, 
                                           validation_split = 1/5) # define the generator

my_train_images= my_train_generator. flow_from_directory(directory = my_train_directory,                                              
                                              target_size = (width, height),
                                              color_mode = "rgb",
                                              batch_size = my_train_batch_size,
                                              class_mode = "categorical",
                                              shuffle = False,
                                              save_to_dir = my_augm_train_directory,
                                              save_format = "jpeg",
                                              subset = 'training') # ddefine train images

my_validation_images = my_train_generator. flow_from_directory(directory = my_train_directory,                                                   
                                                   target_size = (width, height),
                                                   color_mode = "rgb",
                                                   class_mode = "categorical",
                                                   batch_size = my_valid_batch_size,
                                                   shuffle = False,
                                                   save_to_dir = my_augm_val_directory,
                                                   save_format = "jpeg",
                                                   subset = 'validation') # define validation images
images, labels=next(my_train_images) # call next of the train images generator - fetches a batch of 16 files
images, labels=next(my_validation_images) # call next of the validation images generator - fetches a batch of 4 files
train_aug_list=os.listdir(my_augm_train_directory) # list of files in train aug directory
valid_aug_list=os.listdir(my_augm_val_directory) # list of files in valid aug directory
print('files in my_augm_train_directory= ', len(train_aug_list), '  files in my_augm_val_directory= ', len(valid_aug_list))

the printed result is

files in my_augm_train_directory=  16   files in my_augm_val_directory=  4

Note you must create the directories to store the augmented images prior to running the code.

Gerry P
  • 7,662
  • 3
  • 10
  • 20
  • Thanks @Gerry for your answer :) I am writing in R but your code helps a lot as the two languages are quite similar. Indeed, I did create the directories to store prior to running the code, but using only one batch size for both train and val generation. I tried setting 2 different batch size and running it through xception (or my fine-tunned version) and it actually gives me twice as many images saved in the resp. files as set with the batch size. Also some of the val. images stored are flipped. Any idea why running it through a CNN might have different results vs. your solution ? – LaurianeHt Jun 05 '22 at 10:42