MATLAB Answers

How to randomize the order of training images of a CNN?

1 view (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 18 Sep 2020
Edited: Mohammad Sami on 18 Sep 2020
if isShuffleable(Train_imds)
newds = shuffle(Train_imds);
disp('Shuffling successful.')
else
disp('Datastore is not shuffleable.')
end
A second option is to enable shuffling in the training options.
solverName = 'sgdm'; % choose your solver
options = trainingOptions(solverName,'Shuffle','every-epoch');

  1 Comment

Jaime Almonacid-Caballer
Jaime Almonacid-Caballer on 18 Sep 2020
Thanks a lot,
It was in front of me and I did not see it.
Kind regards,

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!