如何标记PCA获得的培训预测用于培训SVM进行分类? MATLAB

时间:2014-01-29 09:25:05

标签: matlab libsvm pca

我有一套图像的“训练集”。我已经形成了'Eigenspace'。现在我需要标记投影来训练SVM。 “面1”对特征空间的投影必须标记为+1,并且所有其他面向特征空间的投影必须标记为-1。

我不知道该怎么做。任何建议都会非常有用!

我使用以下内容形成了本征空间:

    function [signals,V] = pca2(data)
    [M,N] = size(data); 
    data = reshape(data, M*N,1); % subtract off the mean for each dimension 
    mn = mean(data,2); 
    data = bsxfun(@minus, data, mean(data,1)); 
    % construct the matrix Y 
    Y = data'*data / (M*N-1); 
    [V D] = eigs(Y, 10); % reduce to 10 dimension 
    % project the original data 
    signals = data * V; 

3 个答案:

答案 0 :(得分:1)

label = ones(N,1);% N samples in total, +1 represents face 1
for i=1:N 
    % For each face image, you run
    [signals,V] = pca2(data); % ith data
    if ....  % other faces than face 1
        label(i) = -1;
    end
    face(i,:) = reshape(signals,1,[]);
end
model = svmtrain(label,face);

答案 1 :(得分:0)

似乎你无法训练SVM ...这是一个关于如何训练Matlab SVM的例子:

%Generate 100 positive points (the data is a circle)
r = sqrt(rand(100,1)); % radius
t = 2*pi*rand(100,1); % angle
dataP = [r.*cos(t), r.*sin(t)]; % points

%Generate 100 negative points (the data is a circle)
r2 = sqrt(3*rand(100,1)+1); % radius
t2 = 2*pi*rand(100,1); % angle
dataN = [r2.*cos(t2), r2.*sin(t2)]; % points

data3 = [dataN;dataP];
theclass = ones(200,1);
theclass(1:100) = -1; %First 100 points are negative

%Train the SVM
cl = svmtrain(data3,theclass,'Kernel_Function','rbf');

答案 2 :(得分:0)

如果您要识别多个人,则必须为每个人创建一个单独的数据文件,并为每个人创建一个SVM。这是因为SVM专注于两级分离。

这是一个使用libsvm for Matlab的例子(here是完整的代码),假设你有一个文件中的数据:

[person1_label, person1_inst] = libsvmread('../person1');
[person2_label, person2_inst] = libsvmread('../person2');
[person3_label, person3_inst] = libsvmread('../person3');

model1 = svmtrain(person1_label, person1_inst, '-c 1 -g 0.07 -b 1');
model2 = svmtrain(person2_label, person2_inst, '-c 1 -g 0.07 -b 1');
model3 = svmtrain(person3_label, person3_inst, '-c 1 -g 0.07 -b 1');

要测试一张脸,您需要应用所有模型并获得最大输出(使用svmpredict时,您必须使用'-b 1'来获取概率估算值。

此外,在Matlab中您不需要使用svmreadsvmwrite,您可以直接传递数据:

training_data = [];%Your matrix that contains 4 feature vectors
person1_label =[1,1,-1,-1];
person2_label = [-1,-1,1,-1];
person3_label = [-1,-1,-1,1];

model1 = svmtrain(person1_label, person_inst, '-c 1 -g 0.07 -b 1');
model2 = svmtrain(person2_label, person_inst, '-c 1 -g 0.07 -b 1');
model3 = svmtrain(person3_label, person_inst, '-c 1 -g 0.07 -b 1');