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

2 次查看(过去 30 天)
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 %
valset1=subset(imds,(1:(y-1)));
t2=countEachLabel(valset1);
trainset1=subset(imds,((y):(y+z-1)));
t1=countEachLabel(trainset1); % # for training part
testset1=subset(imds,((z):(z+m-1))); % floor: round the fraction number to nearest small inger
t3=countEachLabel(testset1);
trainset2=subset(imds,((m):(m+w-1)));
t4=countEachLabel(trainset2);
  3 个评论
Mohammad Sami
Mohammad Sami 2020-4-8
Is there a reason to further divide the training data into 40% and 40% ?
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);
This first portion of your code, the training data will already contain all classes in train, validation and test imds.
Mohammad Sami
Mohammad Sami 2020-4-8
编辑:Mohammad Sami 2020-4-8
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);

请先登录,再进行评论。

回答(1 个)

Shishir Singhal
Shishir Singhal 2020-4-7
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.

标签

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by