MATLAB: How to randomize the order of training images of a CNN

cnnconvolutional neural networkMATLABtrainnetwork

Dear masters of Matlab,
I am training a Convolutional Neural Network (CNN). and I cannot change the order of the images in the training set. Let's see if I can explain the problem.
  • 1. About the training set
The training set is given so that the folders are the classes to be used:
Train_imds = imageDatastore(fullfile(TrainFolder, categories), ...
'LabelSource', 'foldernames');
It gives the next structure:
I have 56341 images to train 10 classes.
The images are order by each class. All the images of class "A" are in first position, secondly the images of class "B" all together, etc. I would like to randomize these order.
  • 2. Effect of this order in the training
When I run trainNetwork, each iteration takes part of these images until every image has "passed through" the net. Some iterations will be training only one class before the next class begins. I would like to randomize this order so that becomes more probable that every class trains each iteration.
  • 3. Failed attempts
If I change Folders{} , the Labels{} change to empty.
If I make a copy so that I can change the structure, both structures change.
Any help will be welcomed. Thanks a lot

Best Answer

  • if isShuffleable(Train_imds)
    newds = shuffle(Train_imds);
    disp('Shuffling successful.')
    disp('Datastore is not shuffleable.')
    A second option is to enable shuffling in the training options.
    solverName = 'sgdm'; % choose your solver
    options = trainingOptions(solverName,'Shuffle','every-epoch');