Sid*_*Sid 5 matlab classification libsvm
我正在尝试使用 LIBSVM 实现一对一多类分类。
\n此链接很有用http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/ovr_multiclass/ \n但我在函数 \'ovrpredict()\' 中收到错误。
\n函数如下:
\nfunction [pred, ac, decv] = ovrpredict(y, x, model)\n\nlabelSet = model.labelSet;\nlabelSetSize = length(labelSet);\nmodels = model.models;\ndecv= zeros(size(y, 1), labelSetSize);\n\nfor i=1:labelSetSize\n [l,a,d] = svmpredict(double(y == labelSet(i)), x, models{i});\n decv(:, i) = d * (2 * models{i}.Label(1) - 1); % ERROR IN THIS LINE\nend\n[tmp,pred] = max(decv, [], 2);\npred = labelSet(pred);\nac = sum(y==pred) / size(x, 1);\nRun Code Online (Sandbox Code Playgroud)\n我收到的错误消息是Reference to non-existent field \'Label\'。
任何建议都会非常有帮助。
\n用于调用函数的代码:\\
\n[trainY trainX]=libsvmread(\'libfacecombine.train\');\n[testY testX]=libsvmread(\'libfacetest.train\');\nmodel=ovrtrain(trainY,trainX,\'-c 8 -g 4\');\n[~,accuracy,~]=ovrpredict(testY,testX,model);\nRun Code Online (Sandbox Code Playgroud)\n训练和测试数据即“libfacecombine.train”和“libfacetest.train”是从 .csv 文件中获得的:
\nf1=createdabase(f); % where createdatabase is a function to read various images from a folder and arrange into 1D array\n[sig1 mn1]=pcam(f1); % where pcam is a function to find \'pca\'(sig1) and \'mean\'(mn1) of the data\n\n%labelling is done this way:\n%Positive class\n label=[];\n for i=1:length(sig1)\n for j=1:1\n label(i,j)=+1;\n end\n end\n csvwrite(\'face1.csv\',[label sig1]);\n\n%Negative class\n label1=[];\n for i=1:length(sig2) % sig2 obtained in same way as sig1\n for j=1:1\n label1(i,j)=-1;\n end\n end\n csvwrite(\'face2.csv\',[label sig2]);\nRun Code Online (Sandbox Code Playgroud)\n使用“追加”模式,这两个文件都会连接并转换为 .train 文件。\n对测试数据执行相同的操作。
\n我有5节课。标签的完成方式如下:\n类别 1:+1 包含来自面部 1 的 4 个图像的特征,-1 包含来自非面部 1 的 4 个图像(面部 2、3、4 和 5)的特征。类别 2:+2 包含来自脸部 2 的 4 个图像的特征,-2 包含来自脸部 2 的 4 个图像的特征(脸部 1、3、4 和 5).... 类别 5:+5 包含来自脸部 2 的 4 个图像的特征Face 5 和-5 包含来自非 Face 5 的 4 个图像(面部 1、2、3 和 4)的特征。所有这些特征以及标签都按照上面给定的顺序写入 .csv 文件,然后转换为 .train 格式。因此我获得了培训文件。
\n对于测试图像,我拍摄一张脸部 1 的图像并给出其真实标签,即 +1 并写入 .csv 文件,然后转换为 .train。因此我获得了测试文件。当我运行该程序时,我得到的结果如下:
\nAccuracy=92%(12/13)classification; \nAccuracy=61%(8/13)classification;\nAccuracy=100%(13/13)classification;\nAccuracy=100\xe2\x80\x8c\xe2\x80\x8b%(13/13)classification;\nAccuracy=100%(13/13)classification;\nAccuracy=100%(13/13)cla\xe2\x80\x8c\xe2\x80\x8bssification; \nRun Code Online (Sandbox Code Playgroud)\n当我只有 5 个类别时,为什么我会获得 6 个准确度值?
\n在互联网上找到类似问题的解决方案很棘手,但让我们尝试一下。这篇文章由问题而不是答案组成。然而,我相信,如果你回答了所有问题,你就会在没有进一步帮助的情况下找到你的错误 \xe2\x80\x93 或至少是 90%。
\n\n所有这些步骤对于调试任何类型的 MATLAB 程序都是有意义的。
\n\n工作区中残留的旧版本变量可能会导致调试变得困难。变量名称拼写错误可能会导致意外使用旧版本。clear在调试开始时使用 清除工作区。
我已经编译了 libsvm,添加了ovr_multiclass插件,并且可以成功运行我编写的以下示例脚本:
clear\n\n% random train and test data\ntrainX = rand(10, 4);\ntrainY = randi(4, 10, 1);\ntestX = rand(10, 4);\ntestY = randi(4, 10, 1);\n\nmodel=ovrtrain(trainY,trainX,'-c 8 -g 4'); \n[~,accuracy,~]=ovrpredict(testY,testX,model);\nRun Code Online (Sandbox Code Playgroud)\n\n您可以运行它,还是会得到与以前相同的错误?\n这种最小工作示例对于调试非常有用。\n使用用户生成的小型数据可确保错误不会来自意外来源,并有助于缩小原因范围。
\n\nmodels元胞数组您指出错误发生在以下位置:
\n\ndecv(:, i) = d * (2 * models{i}.Label(1) - 1); % ERROR IN THIS LINE\nRun Code Online (Sandbox Code Playgroud)\n\n该行的关键部分是models{i}.Label(1)。这是获取一个元胞数组models,并提取第ith 项。这i一项预计是一个结构体,其中有一个名为 的字段Label。Label预期是一个非空数组,可以从中提取第一个元素。元models胞数组是结构体中的一个字段model,它作为第三个参数传递给ovrpredict.
运行上面非常简单的测试脚本后,我在 MATLAB 命令窗口中运行以下诊断:
\n\n>> models = model.models\n\nmodels = \n\n [1x1 struct]\n [1x1 struct]\n [1x1 struct]\n [1x1 struct]\n\n>> models{1}\n\nans = \n\n Parameters: [5x1 double]\n nr_class: 2\n totalSV: 6\n rho: -1.2122\n Label: [2x1 double]\n sv_indices: [6x1 double]\n ProbA: []\n ProbB: []\n nSV: [2x1 double]\n sv_coef: [6x1 double]\n SVs: [6x4 double]\n\n>> models{1}.Label\n\nans =\n\n 0\n 1\nRun Code Online (Sandbox Code Playgroud)\n\n如果你做同样的事情,你会得到同样的结果吗?如果没有,请将您的输出发布到编辑您的问题时的这些命令。
\n\n如果模型看起来不错,但仍然收到错误,请通过在命令窗口中键入以下内容来打开错误调试器:
\n\ndbstop if error\nRun Code Online (Sandbox Code Playgroud)\n\n当 MATLAB 在函数中遇到错误时,它现在会暂停并允许您检查所有变量。
\n\n再次运行你的程序(或者我的程序,如果你在我发布的最小工作示例中遇到错误)。当错误发生时,程序应该暂停。您的命令提示符应从 更改>>为K>>。
运行上述步骤来示例model单元格数组。然后尝试复制、粘贴并运行在命令窗口中出现错误的代码行。尝试运行其中的一小部分,例如models{i}.Label(1)then 2 * models{i}.Label(1) - 1。
键入dbquit退出调试器,并dbclear if error关闭错误时的自动调试。
(See also my question below about the error message \xe2\x80\x93 make sure that the error is actually occuring where you think it is!)
\n\nWhat version of MATLAB are you using? e.g. R2013a
If you type which ovrpredict into the MATLAB command line, do you see the path to the file that you expect? (i.e. the correct path to ovrpredict.m as you have it saved on your computer)
Does your ovrpredict.m file (as pointed to by which ovrpredict) contain exactly what you have pasted in your question? The error you are getting suggests their might be a tiny difference, e.g. an extra space.