Facing matlab error "grouping variable must be vector, character array, string array"

6 次查看(过去 30 天)
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.

采纳的回答

Prateek Rai
Prateek Rai 2021-9-14
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);
You can refer to cvpartition MathWorks documentation page to find more on cvpartition function .

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Deep Learning Toolbox 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by