I am a beginner in svm. Getting error when trying to run for the example given. Please lend me a hand. Thanks

1 次查看(过去 30 天)
Code: traindatasetDir = fullfile(toolboxdir('vision'), 'visiondata','digits','synthetic'); testdatasetDir = fullfile(toolboxdir('vision'), 'visiondata','digits','handwritten');
% imageDatastore recursively scans the directory tree containing the % images. Folder names are automatically used as labels for each image. trainingSet = imageDatastore(traindatasetDir, 'IncludeSubfolders', true, 'LabelSource', 'foldernames'); testSet = imageDatastore(testdatasetDir, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
countEachLabel(trainingSet) countEachLabel(testSet)
figure;
subplot(2,3,1); imshow(trainingSet.Files{8});
subplot(2,3,2); imshow(trainingSet.Files{13});
subplot(2,3,3); imshow(trainingSet.Files{29});
subplot(2,3,4); imshow(testSet.Files{6});
subplot(2,3,5); imshow(testSet.Files{17});
subplot(2,3,6); imshow(testSet.Files{22});
% Show pre-processing results exTestImage = readimage(testSet,37); processedImage = imbinarize(rgb2gray(exTestImage));
figure;
subplot(1,2,1) imshow(exTestImage)
subplot(1,2,2) imshow(processedImage)
img = readimage(trainingSet, 3);
% Extract HOG features and HOG visualization [hog_2x2, vis2x2] = extractHOGFeatures(img,'CellSize',[2 2]); [hog_4x4, vis4x4] = extractHOGFeatures(img,'CellSize',[4 4]); [hog_8x8, vis8x8] = extractHOGFeatures(img,'CellSize',[8 8]);
% Show the original image figure; subplot(2,3,1:3); imshow(img);
% Visualize the HOG features subplot(2,3,4); plot(vis2x2); title({'CellSize = [2 2]'; ['Length = ' num2str(length(hog_2x2))]});
subplot(2,3,5); plot(vis4x4); title({'CellSize = [4 4]'; ['Length = ' num2str(length(hog_4x4))]});
subplot(2,3,6); plot(vis8x8); title({'CellSize = [8 8]'; ['Length = ' num2str(length(hog_8x8))]});
cellSize = [4 4]; hogFeatureSize = length(hog_4x4);
% Loop over the trainingSet and extract HOG features from each image. A % similar procedure will be used to extract features from the testSet.
numImages = numel(trainingSet.Files); trainingFeatures = cell(numImages, 1);
for i = 1:numImages img = readimage(trainingSet, i);
img = rgb2gray(img);
% Apply pre-processing steps
img = imbinarize(img);
trainingFeatures{i} = extractHOGFeatures(img, 'CellSize', cellSize);
end
% Get labels for each image. trainingLabels = trainingSet.Labels;
% fitcecoc uses SVM learners and a 'One-vs-One' encoding scheme. classifier = fitcecoc(trainingFeatures, trainingLabels); % Extract HOG features from the test set. The procedure is similar to what % was shown earlier and is encapsulated as a helper function for brevity. [testFeatures, testLabels] = helperExtractHOGFeaturesFromImageSet(testSet, hogFeatureSize, cellSize);
% Make class predictions using the test features. predictedLabels = predict(classifier, testFeatures);
% Tabulate the results using a confusion matrix. confMat = confusionmat(testLabels, predictedLabels);
helperDisplayConfusionMatrix(confMat)
Error: Error using classreg.learning.FullClassificationRegressionModel.prepareDataCR (line 171) X must be a numeric matrix.
Error in classreg.learning.classif.FullClassificationModel.prepareData (line 487) classreg.learning.FullClassificationRegressionModel.prepareDataCR(...
Error in classreg.learning.FitTemplate/fit (line 213) this.PrepareData(X,Y,this.BaseFitObjectArgs{:});
Error in ClassificationECOC.fit (line 115) this = fit(temp,X,Y);
Error in fitcecoc (line 289) obj = ClassificationECOC.fit(X,Y,ecocArgs{:});
Error in HOG_train (line 92) classifier = fitcecoc(trainingFeatures, trainingLabels);

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by