支持向量机SVM的详细原理
SVM的定义
SVM理论
SVM应用实例,SVM图像多分类
代码
结果分析
展望
参考
支持向量机(support vector machines, SVM)是一种二分类模型,它的基本模型是定义在特征空间上的间隔最大的线性分类器,间隔最大使它有别于感知机;SVM还包括核技巧,这使它成为实质上的非线性分类器。SVM的的学习策略就是间隔最大化,可形式化为一个求解凸二次规划的问题,也等价于正则化的合页损失函数的最小化问题。SVM的的学习算法就是求解凸二次规划的最优化算法。
(1)支持向量机(Support Vector Machine, SVM)是一种对数据进行二分类的广义线性分类器,其分类边界是对学习样本求解的最大间隔超平面。
(2)SVM使用铰链损失函数计算经验风险并在求解系统中加入了正则化项以优化结构风险,是一个具有稀疏性和稳健性的分类器 。
(3)SVM可以通过引入核函数进行非线性分类。
1,线性可分性
2,损失函数
3,核函数
部分数据如下
function Fun3_Test_SVMs()
close all;clear all;clc;
%%%说明:下面这些句子是将当前目录下所有的子目录加为可搜索路径%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
files = dir(cd);
for i=1:length(files)
if files(i).isdir & strcmp(files(i).name,‘.’) == 0 && strcmp(files(i).name,‘…’) == 0
addpath([cd ‘/’ files(i).name]);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%功能说明:给定一幅图像,用Fun2训练的SVM分类器,对它进行识别
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%图像类别名
ClassName={‘非洲人’,‘马匹’,‘花杂’,‘大象’,‘恐龙’,‘公交车’,‘古建筑’}; %这些名字,你可以换上具体的名
%1 指定待识别的图像名 (自己改变文件名,观察识别结果是不是正确)
%ImgFileName=‘图像库\测试图像\第1类图像\50.jpg’;
ImgFileName=‘图像库\测试图像\第2类图像\765.jpg’;
%2 对待识别的图像,进行特征提取
Img=imread(ImgFileName);
figure(1),imshow(Img)
[TestData]=ImgFea(Img); %调用此子函数,对图像进行特征提取
%3 图像特征,写入到文件中
[r dim]=size(TestData);
fid=fopen(‘tmp\SvmTest.txt’,‘w’);
for n=1:r
%写入特征
for x=1:dim-1
fprintf(fid, '%d:%0.8f ',x,TestData(n,x));
end
fprintf(fid, ‘%d:%0.8f’,dim,TestData(n,dim));
fprintf(fid, ‘\n’); %换行
end
fclose(fid);
disp(‘SVM测试文件构造成功…’)
%4 SVM测试
%4.1 训练数据归一化处理
system([‘svm\svm-scale.exe -r tmp\尺度模板 tmp\SvmTest.txt > tmp\SvmTest.scale’]);
%4.2 预测
system([‘svm\svm-predict.exe tmp\SvmTest.scale tmp\SVM分类模型.mdl tmp\预测结果.txt’]);
%4.3 读出预测结果
[ypred] = textread(‘tmp\预测结果.txt’, ‘%d’, r);
%5 显示识别结果
disp([‘预测标签:’ num2str(ypred)]);
disp([‘预测类别名:’ char(ClassName(ypred))]);
%在图像上显示识别结果
t=[‘预测标签:’ num2str(ypred) '; ’ char(ClassName(ypred))];
title(t);
%主程序结束
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%功能:提取的特征
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [FeaBuff]=ImgFea(Img)
[F1]=GLM_Texture(Img); %灰度共生纹理
[F2]=Lab_Histt(Img); %Lab直方图
FeaBuff=[F1 F2]; %FeaBuff为返回的特征向量 26+9=35维向量
从效果图看,SVM能把数据映射到高维空间,进行回归分析,准确识别出来图片类型,为马匹
下面是部分SVM可以应用的方面,如果有需要欢迎扫描二维码联系
百科