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.
0 件のコメント
採用された回答
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);
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Deep Learning Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!