How to train a network that has multi-classes image classification

12 views (last 30 days)
Mohamed Elbeialy
Mohamed Elbeialy on 3 Apr 2020
Edited: Mohammad Sami on 8 Apr 2020
I have formulated a code to train an image datastore netwrok that has 5 number of classes, however the valdiation, testing, and training have done on one class only, evernthough there are 5 classes on the network. How to force network train, test and validate the 5 classes and give an equal result between classes
Note: all classes have same number of images
imds = imageDatastore('D:\dataimage','IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsValidation,imdsTest] = splitEachLabel(imds,0.8,0.1,0.1);
augimdsTrain = augmentedImageDatastore([227 227],imdsTrain); % resize images
augimdsValidation = augmentedImageDatastore([227 227],imdsValidation); % resize image
augimdsTest = augmentedImageDatastore([227 227],imdsTest);
numClasses = numel(categories(imdsTrain.Labels));
T = countEachLabel(imds); % count number of images in folder
y=ceil((T.Count*10/100)); % to count 10% of images integer number
sum (y); % for sum folders number (note) if other sum varible use, it will give error. Use 'clear sum'
z=ceil((y)+(T.Count* 40/100)); % 50% of whole images
m=ceil((z)+(T.Count*10/100)); % 60% % ceil: round the fraction number to nearest high integer
w=ceil((m)+(T.Count* 40/100)); % 90 %
t1=countEachLabel(trainset1); % # for training part
testset1=subset(imds,((z):(z+m-1))); % floor: round the fraction number to nearest small inger
Mohammad Sami
Mohammad Sami on 8 Apr 2020
If you want to split two separate training sets of 40% each just modify the splitEachLabel call as follows
[imdsTrain1,imdsTrain2,imdsValidation,imdsTest] = splitEachLabel(imds,0.4,0.4,0.1,0.1);

Sign in to comment.

Answers (1)

Shishir Singhal
Shishir Singhal on 7 Apr 2020
May be you are facing the problem of unsual split.
You can also do like:
  1. split your data into 5 subsets each belongs to one class.
  2. Then from each subset take random train(40%, 40%), valid(10%), and test(10%) datapoints.
  3. With this way, your train, test and validation set will definetly contains sample for all 5 classes.
Now you can train your model. But make sure that your classses should be balaced otherwise your model will get biased and does not give good results.





Community Treasure Hunt

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

Start Hunting!