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

6 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Deep Learning Toolbox in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by