下载
libsvm下载地址:LIBSM下载,下载libsvm-3.23.tar.gz
文件,解压缩,将解压好的文件剪贴到安装好的matlab/toolbox
下,例如我的路径是:
/usr/local/MATLAB/R2015b/toolbox
编译安装
在libsvm-3.23所在目录/usr/local/MATLAB/R2015b/toolbox
打开终端:
sudo chmod 777 libsvm-3.23
cd libsvm-3.23
make
可以使用./svm-train heart_scale
来训练进行检验,会输出如下,并生成heart_scale.model文件:
optimization finished, #iter = 162
nu = 0.431029
obj = -100.877288, rho = 0.424462
nSV = 132, nBSV = 107
Total nSV = 132
接着进行预测:./svm-predict heart_scale heart_scale.model heart_scale.out
(其中heart_scale为test file, heart_scale.model是由svm-train训练出来对模型文件,heart_scale.out为预测输出文件),会输出如下,并生成heart_scale.out文件:
Accuracy = 86.6667% (234/270) (classification)
编译完成
与Matlab接口的实现
进入/usr/local/MATLAB/R2015b/toolbox/libsvm-3.23/matlab
中,打开终端,make
一下
错误一
这个部分忘记截图,错误大概就是mexext not found
,主要是想说在libsvm-3.23/matlab
中有一个README
,里面有写到:
翻译:
在Unix系统上,如果make.m和“mex-setup”都不起作用,请使用Makefile并在命令窗口中键入“make”。
请注意,我们假设您的MATLAB安装在'/usr/local/MATLAB'中。
如果没有,请更换Makefile中的MATLABDIR。
请注意,我们假设您的MATLAB安装在’/usr/local/MATLAB’中。如果没有,请更换Makefile中的MATLABDIR。
- 原始的Makefile文件中
而我的Matlab并不是这个安装路径,所以需要更改为自己的MATLAB路径,如下:
错误二
问题:
/usr/local/MATLAB/R2015b/bin/glnxa64/mex: /usr/local/MATLAB/R2015b/bin/glnxa64/libtiff.so.5: no version information available (required by /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4)
libopencv_highgui.so.2.4需要调用 /usr/local/MATLAB/R2015b/bin/glnxa64/libtiff.so.5:。但该版本与libopencv_highgui.so.2.4所能用的版本不对应
- 解决方法:
sudo rm -rf /usr/local/MATLAB/R2015b/bin/glnxa64/libtiff.so.5
sudo ln -s /usr/lib/x86_64-linux-gnu/libtiff.so.5.2.4 /usr/local/MATLAB/R2015b/bin/glnxa64/libtiff.so.5
linux下Matlab调用及库链接问题no version information available
再重新make
一下
/usr/local/MATLAB/R2015b/bin/mex CC="g++" CXX="g++" CFLAGS="-Wall -Wconversion -O3 -fPIC -I/usr/local/MATLAB/R2015b/extern/include -I.." CXXFLAGS="-Wall -Wconversion -O3 -fPIC -I/usr/local/MATLAB/R2015b/extern/include -I.." -largeArrayDims svmpredict.c ../svm.o svm_model_matlab.o
Building with 'gcc'.
Warning: You are using gcc version '5.4.0'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
Warning: You are using gcc version '5.4.0-6ubuntu1~16.04.12)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
/usr/local/MATLAB/R2015b/toolbox/libsvm-3.23/matlab/svmpredict.c: In function ‘int print_null(const char*, ...)’:
/usr/local/MATLAB/R2015b/toolbox/libsvm-3.23/matlab/svmpredict.c:17:36: warning: no return statement in function returning non-void [-Wreturn-type]
int print_null(const char *s,...) {}
^
MEX completed successfully.
/usr/local/MATLAB/R2015b/bin/mex CC="g++" CXX="g++" CFLAGS="-Wall -Wconversion -O3 -fPIC -I/usr/local/MATLAB/R2015b/extern/include -I.." CXXFLAGS="-Wall -Wconversion -O3 -fPIC -I/usr/local/MATLAB/R2015b/extern/include -I.." -largeArrayDims svmtrain.c ../svm.o svm_model_matlab.o
Building with 'gcc'.
Warning: You are using gcc version '5.4.0'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
Warning: You are using gcc version '5.4.0-6ubuntu1~16.04.12)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
/usr/local/MATLAB/R2015b/toolbox/libsvm-3.23/matlab/svmtrain.c: In function ‘int read_problem_dense(const mxArray*, const mxArray*)’:
/usr/local/MATLAB/R2015b/toolbox/libsvm-3.23/matlab/svmtrain.c:287:30: warning: conversion to ‘double’ from ‘size_t {aka long unsigned int}’ may alter its value [-Wconversion]
param.gamma = (double)(1.0/max_index);
^
/usr/local/MATLAB/R2015b/toolbox/libsvm-3.23/matlab/svmtrain.c: In function ‘int read_problem_sparse(const mxArray*, const mxArray*)’:
/usr/local/MATLAB/R2015b/toolbox/libsvm-3.23/matlab/svmtrain.c:370:30: warning: conversion to ‘double’ from ‘size_t {aka long unsigned int}’ may alter its value [-Wconversion]
param.gamma = (double)(1.0/max_index);
^
MEX completed successfully.
/usr/local/MATLAB/R2015b/bin/mex CC="g++" CXX="g++" CFLAGS="-Wall -Wconversion -O3 -fPIC -I/usr/local/MATLAB/R2015b/extern/include -I.." CXXFLAGS="-Wall -Wconversion -O3 -fPIC -I/usr/local/MATLAB/R2015b/extern/include -I.." -largeArrayDims libsvmread.c
Building with 'gcc'.
Warning: You are using gcc version '5.4.0'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
Warning: You are using gcc version '5.4.0-6ubuntu1~16.04.12)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
MEX completed successfully.
/usr/local/MATLAB/R2015b/bin/mex CC="g++" CXX="g++" CFLAGS="-Wall -Wconversion -O3 -fPIC -I/usr/local/MATLAB/R2015b/extern/include -I.." CXXFLAGS="-Wall -Wconversion -O3 -fPIC -I/usr/local/MATLAB/R2015b/extern/include -I.." -largeArrayDims libsvmwrite.c
Building with 'gcc'.
Warning: You are using gcc version '5.4.0'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
Warning: You are using gcc version '5.4.0-6ubuntu1~16.04.12)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
MEX completed successfully.
添加路径
打开MATLAB,把/usr/local/MATLAB/R2015b/toolbox/libsvm-3.23
文件夹路径添加到Matlab的搜索路径(添加并包含子文件夹)
将Matlab的路径设置在/usr/local/MATLAB/R2015b/toolbox/libsvm-3.23
下,输入如下测试代码:
[heart_scale_label,heart_scale_inst]=libsvmread('heart_scale');
model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07');
[predict_label, accuracy, dec_values] =svmpredict(heart_scale_label, heart_scale_inst, model); % test the traindata
显示如下:
完成。