svmtrain中的错误,“Y和训练必须有相同数量的行”

时间:2021-01-15 19:17:15

I'm using svmtrain function to classify the images. And I'm getting an error like this.

我使用svmtrain函数对图像进行分类。我得到一个这样的错误。

Error using svmtrain (line 253)
Y and TRAINING must have the same number of rows.

Error in svm5 (line 80)
SVMStruct = svmtrain(Training_Set , train_label, 'kernel_function', 'linear');

Training_Set contains sets of images and train_lable is the class to which to identify the input image. The complete code for the reference

Training_Set包含图像集,train_lable是用来识别输入图像的类。完整的代码供参考。

clc
clear all

% Load Datasets

Dataset = 'D:\majorproject\image\traindata\';   
Testset  = 'D:\majorproject\image\testset\';


% we need to process the images first.
% Convert your images into grayscale
% Resize the images

width=100; height=100;
DataSet      = cell([], 1);

 for i=1:length(dir(fullfile(Dataset,'*.jpg')))

     % Training set process
     k = dir(fullfile(Dataset,'*.jpg'));
     k = {k(~[k.isdir]).name};
     for j=1:length(k)
        tempImage       = imread(horzcat(Dataset,filesep,k{j}));
        imgInfo         = imfinfo(horzcat(Dataset,filesep,k{j}));

         % Image transformation
         if strcmp(imgInfo.ColorType,'grayscale')
            % array of images
            DataSet{j}   = double(imresize(tempImage,[width height])); 
         else
            % array of images
            DataSet{j}   = double(imresize((tempImage),[width height])); 
         end
     end
 end
TestSet =  cell([], 1);
  for i=1:length(dir(fullfile(Testset,'*.jpg')))

     % Training set process
     k = dir(fullfile(Testset,'*.jpg'));
     k = {k(~[k.isdir]).name};
     for j=1:length(k)
        tempImage       = imread(horzcat(Testset,filesep,k{j}));
        imgInfo         = imfinfo(horzcat(Testset,filesep,k{j}));

         % Image transformation
         if strcmp(imgInfo.ColorType,'grayscale')
            % array of images
            TestSet{j}   = double(imresize(tempImage,[width height])); 
         else
            % array of images
            TestSet{j}   = double(imresize(tempImage,[width height])); 
         end
     end
  end

    % Prepare class label for first run of svm
% I have arranged labels 1 & 2 as per my convenience.

% It is always better to label your images numerically

% Note that for every image in our Dataset we need to provide one label.

% we have 10 images and we divided it into two label groups here.

    train_label               = zeros(size(10,1),1);
    train_label(1:4,1)   = 1;         % 1 = naa
    train_label(5:10,1)  = 2;         % 2 = ta


% Prepare numeric matrix for svmtrain

    Training_Set=[];
    for i=1:length(DataSet)
    b = imresize(DataSet{i},[100 100]);
    Training_Set_tmp= b(:);
    %Training_Set_tmp   = reshape(DataSet{i},1, 100*100);
    Training_Set=[Training_Set;Training_Set_tmp];
    end

    Test_Set=[];
    for j=1:length(TestSet)
    b = imresize(TestSet{j},[100 100]);
    Test_set_tmp= b(:);
    %Test_set_tmp   = reshape(TestSet{j},1, 100*100);
    Test_Set=[Test_Set;Test_set_tmp];
    end


% Perform first run of svm

SVMStruct = svmtrain(Training_Set, train_label, 'kernel_function', 'linear');
Group     = svmclassify(SVMStruct, Test_Set);

please guide me to overcome from this. Thank you.

请引导我克服这一点。谢谢你!

1 个解决方案

#1


0  

I have recently bumped into this similar problem and after hours of checking, here is the problem I found:

我最近遇到了类似的问题,经过几个小时的检查,我发现了一个问题:

% we have 10 images and we divided it into two label groups here.

train_label               = zeros(size(10,1),1);
train_label(1:4,1)   = 1;         % 1 = naa
train_label(5:10,1)  = 2;         % 2 = ta

Where I have only 499 images, while instead, I set my size to 500 (Accidentally deleted 1), and thus resulting in the exact same error as above. You did not provide your data set here so maybe you can consider checking it again.

在我只有499张图片的地方,我将我的尺寸设置为500(不小心删除了1),从而导致了与上面相同的错误。您没有提供您的数据集,所以也许您可以考虑再次检查它。

#1


0  

I have recently bumped into this similar problem and after hours of checking, here is the problem I found:

我最近遇到了类似的问题,经过几个小时的检查,我发现了一个问题:

% we have 10 images and we divided it into two label groups here.

train_label               = zeros(size(10,1),1);
train_label(1:4,1)   = 1;         % 1 = naa
train_label(5:10,1)  = 2;         % 2 = ta

Where I have only 499 images, while instead, I set my size to 500 (Accidentally deleted 1), and thus resulting in the exact same error as above. You did not provide your data set here so maybe you can consider checking it again.

在我只有499张图片的地方,我将我的尺寸设置为500(不小心删除了1),从而导致了与上面相同的错误。您没有提供您的数据集,所以也许您可以考虑再次检查它。