Facing matlab error "grouping variable must be vector, character array, string array"
6 views (last 30 days)
Show older comments
Hi,
i facing some error in matlab while using both CNN and SVM for classification. However when i try run the program the error occur. The coding is as below
imds = imageDatastore('MerchData', 'IncludeSubfolders',true, 'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7);
testnet = resnet18
inputSize = testnet.Layers(1).InputSize
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain)
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation)
layer = 'pool5';
featuresTrain = activations(testnet,augimdsTrain,layer,'OutputAs','rows')
featuresTest = activations(testnet,augimdsValidation,layer,'OutputAs','rows');
whos featuresTrain
YTrain = imdsTrain.Labels;
YValidation = imdsValidation.Labels;
cvpt = cvpartition(featuresTrain,"KFold",5)
opt = struct("CVPartition",cvpt)
classifier = fitcecoc(featuresTrain,YTrain,"OptimizeHyperparameters","auto","HyperparameterOptimizationOptions",opt);
Below is the error that occur
Hope someone could help me on this. Thank you very much.
0 Comments
Accepted Answer
Prateek Rai
on 14 Sep 2021
To my understanding, you are getting error in matlab while using both CNN and SVM for classification.
The error is mainly arising in the line:
cvpt = cvpartition(featuresTrain,"KFold",5)
In 'cvpartition' function, the first argument should correspond to number of observations in the sample data but you are passing featuresTrain which causes an error. Instead pass the number of observations which can be done by:
cvpt = cvpartition(augimdsTrain.NumObservations,"KFold",5)
So the whole code can be rewritten as:
imds = imageDatastore('MerchData', 'IncludeSubfolders',true, 'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7);
testnet = resnet18
inputSize = testnet.Layers(1).InputSize
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain)
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation)
layer = 'pool5';
featuresTrain = activations(testnet,augimdsTrain,layer,'OutputAs','rows')
featuresTest = activations(testnet,augimdsValidation,layer,'OutputAs','rows');
whos featuresTrain
YTrain = imdsTrain.Labels;
YValidation = imdsValidation.Labels;
%% -- Modified code -- %%
cvpt = cvpartition(augimdsTrain.NumObservations,"KFold",5)
%% -- %%
opt = struct("CVPartition",cvpt)
classifier = fitcecoc(featuresTrain,YTrain,"OptimizeHyperparameters","auto","HyperparameterOptimizationOptions",opt);
More Answers (0)
See Also
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!