How to divide image datastore into training set, validation set and test set for training a CNN network with k-fold cross validation?
이전 댓글 표시
I have a image datastore:
filefolder=fullfile("D:\folder");
Images = imageDatastore(filefolder,...
'IncludeSubfolders',true,...
'LabelSource','foldernames');
How can I divide this image datastore into training set, validation set and test set for training a CNN network with k-fold cross validation?
splitEachLabel is a command where I can split the labels accordingly but there was no option for cross validation.
Thanking You in advance.
채택된 답변
추가 답변 (1개)
Govind KM
2024년 9월 4일
Hi Bipin,
The “splitEachLabel” function can be used to initially split the image datastore into training and test sets. Following this, the “cvpartition” function can be used to create a partition for k-fold cross validation on the training set. The training, validation and testing sets can then be accessed as needed for model training, testing and validation. A sample code for this is provided below:
% Load the image datastore
filefolder=fullfile("D:\folder");
Images = imageDatastore(filefolder,'IncludeSubfolders',true,'LabelSource','foldernames');
% Split the datastore into training and test sets
[trainImds, testImds] = splitEachLabel(Images, 0.8, 'randomized');
% Split the training set into training and validation sets for k-fold cross-validation
k = 5; % Number of folds
cvp = cvpartition(trainImds.Labels, 'KFold', k);
% Access the training, validation, and test sets
for fold = 1:k
trainIdx = training(cvp, fold);
valIdx = test(cvp, fold);
trainFoldImds = subset(trainImds, trainIdx);
valFoldImds = subset(trainImds, valIdx);
% Train your CNN network using trainFoldImds and validate using valFoldImds
end
% Test your CNN network using testImds
You can refer to the documentation for more details regarding the “splitEachLabel” function, and performing cross-validation using “cvpartition”:
https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.imagedatastore.spliteachlabel.html
For help in training a neural network, you can refer to this example, which uses the “trainnet” function with Image datastores:
Hope this helps!
카테고리
도움말 센터 및 File Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!