Clear Filters
Clear Filters

split Each Data store

4 views (last 30 days)
shehryar shuaib
shehryar shuaib on 9 Dec 2019
Answered: Lokesh on 25 Apr 2024
How we can Split Each data store while training neural network with not disturbing the sequence of Images and labels so that it can train images and corresponding labels accordingly.
Thanks in advance.

Answers (1)

Lokesh
Lokesh on 25 Apr 2024
Hi shehryar,
It is my understanding that you would like to split datastore maintaining the alignment of images and their corresponding labels.
This can be efficiently achieved in MATLAB using the 'splitEachLabel' method. This method ensures that the split is done maintaining the correspondence between images and labels, and allows you to specify the proportion of data you want in each set. To split the datastore into training and validation sets, where 70% is designated for training and 30% for validation, you can use the following code:
[imdsTrain, imdsValidation] = splitEachLabel(imds, 0.7);
For sequential data where the order and sequence are crucial, consider manually dividing the dataset while keeping track of indices. You can then create new imageDatastore objects for training and validation sets using subsets of the original datastore:
% Calculate the number of images for training
numTrainFiles = floor(0.7 * numel(imds.Files));
% Manually create training and validation datastores
imdsTrain = imageDatastore(imds.Files(1:numTrainFiles), 'Labels', imds.Labels(1:numTrainFiles));
imdsValidation = imageDatastore(imds.Files(numTrainFiles+1:end), 'Labels', imds.Labels(numTrainFiles+1:end));
Refer to the following documentation for more information on 'splitEachLabel':

Community Treasure Hunt

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

Start Hunting!