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

6 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Prateek Rai
Prateek Rai el 14 de Sept. de 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 .

Más respuestas (0)

Categorías

Más información sobre Deep Learning Toolbox en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by