├── Dataset └── IndiaP.mat ├── MSTV.pdf ├── MSTV_Demo.asv ├── MSTV_Demo.m ├── Readme.md ├── a.jpg ├── functions ├── MSTV.m ├── ToVector.m ├── average_fusion.m ├── centering.m ├── confusion.m ├── cross_validation_svm.m ├── kernelize_test.m ├── kernelize_training.m ├── kpca.m ├── label2colord.m ├── randsample.m ├── scale_func.m ├── scale_new.m └── tsmooth.m ├── libsvm-3.22 ├── COPYRIGHT ├── FAQ.html ├── Makefile ├── Makefile.win ├── README ├── heart_scale ├── java │ ├── Makefile │ ├── libsvm.jar │ ├── libsvm │ │ ├── svm.java │ │ ├── svm.m4 │ │ ├── svm_model.java │ │ ├── svm_node.java │ │ ├── svm_parameter.java │ │ ├── svm_print_interface.java │ │ └── svm_problem.java │ ├── svm_predict.java │ ├── svm_scale.java │ ├── svm_toy.java │ ├── svm_train.java │ └── test_applet.html ├── matlab │ ├── Makefile │ ├── README │ ├── libsvmread.c │ ├── libsvmread.mexw64 │ ├── libsvmwrite.c │ ├── libsvmwrite.mexw64 │ ├── make.m │ ├── svm_model_matlab.c │ ├── svm_model_matlab.h │ ├── svmpredict.c │ ├── svmpredict.mexw64 │ ├── svmtrain.c │ └── svmtrain.mexw64 ├── python │ ├── Makefile │ ├── README │ ├── svm.py │ └── svmutil.py ├── svm-predict.c ├── svm-scale.c ├── svm-toy │ ├── gtk │ │ ├── Makefile │ │ ├── callbacks.cpp │ │ ├── callbacks.h │ │ ├── interface.c │ │ ├── interface.h │ │ ├── main.c │ │ └── svm-toy.glade │ ├── qt │ │ ├── Makefile │ │ └── svm-toy.cpp │ └── windows │ │ └── svm-toy.cpp ├── svm-train.c ├── svm.cpp ├── svm.def ├── svm.h ├── tools │ ├── README │ ├── checkdata.py │ ├── easy.py │ ├── grid.py │ └── subset.py └── windows │ ├── libsvm.dll │ ├── libsvmread.mexw64 │ ├── libsvmwrite.mexw64 │ ├── svm-predict.exe │ ├── svm-scale.exe │ ├── svm-toy.exe │ ├── svm-train.exe │ ├── svmpredict.mexw64 │ └── svmtrain.mexw64 └── training_indexes └── in_1.mat /Dataset/IndiaP.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/Dataset/IndiaP.mat -------------------------------------------------------------------------------- /MSTV.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/MSTV.pdf -------------------------------------------------------------------------------- /MSTV_Demo.asv: -------------------------------------------------------------------------------- 1 | %% Name: MSTV_Demo 2 | % 3 | % Generate the classification results of MSTV method as follows: 4 | % 5 | % P. Duan et al.,"Noise-Robust Hyperspectral Image Classification via Multi-Scale Total Variation", accepted to 6 | % IEEE Journal of Selected Topics in Applied Earth Observations and Remote Sensing, 2019. 7 | % 8 | % URL: https://ieeexplore.ieee.org/document/8725896 9 | % The SVM toolbox which can be downloaded at: 10 | % http://www.csie.ntu.edu.tw/~cjlin/libsvm/ 11 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 12 | % Author: Puhong Duan (puhong_duan@hnu.edu.cn) 13 | % 2019 14 | 15 | %% 16 | 17 | %% 18 | clc,clear 19 | close all 20 | %%%%% 21 | addpath(genpath('functions')) 22 | addpath(genpath('libsvm-3.22')) 23 | %% Load hyperspectral image 24 | path='.\Dataset\'; 25 | inputs = 'IndiaP';% Indian dataset 26 | location = [path,inputs]; 27 | load (location); 28 | %% Multi-scale total variation method 29 | [OA,AA,kappa,CA]=MSTV(img,GroundT); 30 | %% Evaluation 31 | OA_mean=mean(OA); 32 | AA_mean=mean(AA); 33 | kappa_mean=mean(kappa); 34 | CA_mean=mean(CA,2); 35 | disp('%%%%%%%%%%%%%%%%%%% Classification Results of MSTV Method %%%%%%%%%%%%%%%%') 36 | disp(['OA',' = ',num2str(OA_mean),' || ','AA',' = ',num2str(AA_mean),' || ','Kappa',' = ',num2str(kappa_mean)]) 37 | 38 | -------------------------------------------------------------------------------- /MSTV_Demo.m: -------------------------------------------------------------------------------- 1 | 2 | %% Name: MSTV 3 | % MSTV for Hyperspectral image classification 4 | % Copyright(c) 2018 Puhong Duan 5 | % All Rights Reserved. 6 | % 7 | % ---------------------------------------------------------------------- 8 | % Permission to use, copy, or modify this software and its documentation 9 | % for educational and research purposes only and without fee is here 10 | % granted, provided that this copyright notice and the original authors' 11 | % names appear on all copies and supporting documentation. This program 12 | % shall not be used, rewritten, or adapted as the basis of a commercial 13 | % software or hardware product without first obtaining permission of the 14 | % authors. The authors make no representations about the suitability of 15 | % this software for any purpose. It is provided "as is" without express 16 | % or implied warranty. 17 | %---------------------------------------------------------------------- 18 | % 19 | % This is an implementation of the method for Hyperspectral image 20 | % classification which is robust to Gaussian noise via multi-scale total variation 21 | 22 | % if you use this code, Please cite the following paper: 23 | % 24 | % P. Duan et al.,"Noise-Robust Hyperspectral Image Classification via Multi-Scale Total Variation" 25 | % IEEE Journal of Selected Topics in Applied Earth Observations and Remote Sensing, 2019. 26 | %% 27 | clc,clear 28 | close all 29 | %%%%% 30 | addpath(genpath('functions')) 31 | addpath(genpath('libsvm-3.22')) 32 | %% Load hyperspectral image 33 | path='.\Dataset\'; 34 | inputs = 'IndiaP';% Indian dataset 35 | location = [path,inputs]; 36 | load (location); 37 | %% Multi-scale total variation method 38 | [OA,AA,kappa,CA]=MSTV(img,GroundT); 39 | %% Evaluation 40 | OA_mean=mean(OA); 41 | AA_mean=mean(AA); 42 | kappa_mean=mean(kappa); 43 | CA_mean=mean(CA,2); 44 | disp('%%%%%%%%%%%%%%%%%%% Classification Results of MSTV Method %%%%%%%%%%%%%%%%') 45 | disp(['OA',' = ',num2str(OA_mean),' || ','AA',' = ',num2str(AA_mean),' || ','Kappa',' = ',num2str(kappa_mean)]) 46 | 47 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Matlab Code for Noise-Robust Hyperspectral Image Classification via Multi-Scale Total Variation 2 | 3 | [Puhong Duan](https://scholar.google.ch/citations?hl=en&user=IYUlx_8AAAAJ&view_op=list_works&sortby=pubdate), [Xudong Kang](https://scholar.google.ch/citations?user=5XOeLZYAAAAJ&hl=en), [Shutao Li](https://scholar.google.ch/citations?user=PlBq8n8AAAAJ&hl=en), [Pedram Ghamisi](https://scholar.google.ch/citations?user=Gr9afd0AAAAJ&hl=en) 4 | 5 | ___________ 6 | 7 | The code in this toolbox implements the ["Noise-Robust Hyperspectral Image Classification via Multi-Scale Total Variation"](https://ieeexplore.ieee.org/document/8725896). More specifically, it is detailed as follow. 8 | 9 | ![alt text](./a.jpg) 10 | 11 | Citation 12 | --------------------- 13 | 14 | **Please kindly cite the papers if this code is useful and helpful for your research.** 15 | 16 | P. Duan, X. Kang, S. Li and P. Ghamisi, "Noise-Robust Hyperspectral Image Classification via Multi-Scale Total Variation," in IEEE Journal of Selected Topics in Applied Earth Observations and Remote Sensing, vol. 12, no. 6, pp. 1948-1962, June 2019, doi: 10.1109/JSTARS.2019.2915272. 17 | 18 | @article{MSTV, 19 | title = {Noise-Robust Hyperspectral Image Classification via Multi-Scale Total Variation}, 20 | author = {Duan, Puhong and Kang, Xudong and Li, Shutao and Ghamisi, Pedram}, 21 | journal = {IEEE Transactions on Geoscience and Remote Sensing}, 22 | volume = {12}, 23 | number = {6}, 24 | pages = {1948-1962}, 25 | year = {2019}, 26 | publisher = {IEEE} 27 | } 28 | 29 | # Usage 30 | run demo.m in Matlab 31 | 32 | 33 | The SVM toolbox can be downloaded at: http://www.csie.ntu.edu.tw/~cjlin/libsvm/ 34 | -------------------------------------------------------------------------------- /a.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/a.jpg -------------------------------------------------------------------------------- /functions/MSTV.m: -------------------------------------------------------------------------------- 1 | function [ OA,AA,kappa,CA ] = MSTV( img,GroundT ) 2 | %% size of image 3 | [no_lines, no_rows, no_bands] = size(img); 4 | GroundT=GroundT'; 5 | OA=[];AA=[];kappa=[];w=10;CA=[]; 6 | load (['.\training_indexes\in_1.mat']) 7 | img2=average_fusion(img,20); 8 | %% normalization 9 | no_bands=size(img2,3); 10 | fimg=reshape(img2,[no_lines*no_rows no_bands]); 11 | [fimg] = scale_new(fimg); 12 | fimg=reshape(fimg,[no_lines no_rows no_bands]); 13 | %% feature extraction 14 | fimg1 = tsmooth(fimg,0.003,2); 15 | fimg2 = tsmooth(fimg,0.02,1); 16 | fimg3 = tsmooth(fimg,0.01,3); 17 | f_fimg=cat(3,fimg1,fimg2,fimg3); 18 | fimg =kpca(f_fimg, 1000,30, 'Gaussian',1000); 19 | 20 | %% SVM classification 21 | fimg = ToVector(fimg); 22 | fimg = fimg'; 23 | fimg=double(fimg); 24 | for i=1:w 25 | indexes=dph(:,i); 26 | %%% traing and test samples 27 | train_SL = GroundT(:,indexes); 28 | train_samples = fimg(:,train_SL(1,:))'; 29 | train_labels= train_SL(2,:)'; 30 | % 31 | test_SL = GroundT; 32 | test_SL(:,indexes) = []; 33 | test_samples = fimg(:,test_SL(1,:))'; 34 | test_labels = test_SL(2,:)'; 35 | 36 | [train_samples,M,m] = scale_func(train_samples); 37 | [fimg11 ] = scale_func(fimg',M,m); 38 | % Selecting the paramter for SVM 39 | [Ccv Gcv cv cv_t]=cross_validation_svm(train_labels,train_samples); 40 | % Training using a Gaussian RBF kernel 41 | parameter=sprintf('-c %f -g %f -m 500 -t 2 -q',Ccv,Gcv); 42 | model=svmtrain(train_labels,train_samples,parameter); 43 | % Testing 44 | Result = svmpredict(ones(no_lines*no_rows,1),fimg11,model); 45 | % Evaluation 46 | GroudTest = double(test_labels(:,1)); 47 | ResultTest = Result(test_SL(1,:),:); 48 | [OA_i,AA_i,kappa_i,CA_i]=confusion(GroudTest,ResultTest); 49 | Result = reshape(Result,no_lines,no_rows); 50 | VClassMap=label2colord(Result,'india'); 51 | figure,imshow(VClassMap); 52 | OA=[OA OA_i]; 53 | AA=[AA AA_i]; 54 | kappa=[kappa kappa_i]; 55 | CA=[CA CA_i]; 56 | end 57 | 58 | 59 | end 60 | 61 | -------------------------------------------------------------------------------- /functions/ToVector.m: -------------------------------------------------------------------------------- 1 | function v = ToVector(im) 2 | % takes MxNx3 picture and returns (MN)x3 vector 3 | sz = size(im); 4 | v = reshape(im, [prod(sz(1:2)) sz(3)]); -------------------------------------------------------------------------------- /functions/average_fusion.m: -------------------------------------------------------------------------------- 1 | function [ R ] = average_fusion( img,n ) 2 | %PCA_FUSION Summary of this function goes here 3 | % Detailed explanation goes here 4 | no_bands=size(img,3); 5 | for i=1:n 6 | R(:,:,i)= mean(img(:,:,1+floor(no_bands/n)*(i-1):floor(no_bands/n)*i),3); 7 | if (floor(no_bands/n)*i~=no_bands)&(i==n)% 8 | R(:,:,i+1)=mean(img(:,:,0+floor(no_bands/n)*i:no_bands),3); 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /functions/centering.m: -------------------------------------------------------------------------------- 1 | function K = centering(K) 2 | 3 | % K = centering(K) 4 | % 5 | % Center a kernel matrix 6 | % 7 | % Copyright 2009-2011 8 | % Wenzhi Liao 9 | % wliao@telin.ugent.be, http://telin.ugent.be/~wliao/ 10 | % 2 Oct 2010 11 | 12 | 13 | [nrow nclom] = size(K); 14 | if nrow ~= nclom 15 | error('input matrix must be symmetric matrax') 16 | end 17 | 18 | 19 | D = sum(K)/nrow; 20 | E = sum(D)/nrow; 21 | J = ones(nrow,1)*D; 22 | K = K-J-J'+E; 23 | -------------------------------------------------------------------------------- /functions/confusion.m: -------------------------------------------------------------------------------- 1 | function [oa, aa, K, ua]=confusion(true_label,estim_label) 2 | % 3 | % function confusion(true_label,estim_label) 4 | % 5 | % This function compute the confusion matrix and extract the OA, AA 6 | % and the Kappa coefficient. 7 | % 8 | % INPUT 9 | % easy! just read 10 | 11 | l=length(true_label); 12 | nb_c=max(true_label); 13 | 14 | confu=zeros(nb_c,nb_c); 15 | 16 | for i=1:l 17 | confu(estim_label(i),true_label(i))= confu(estim_label(i),true_label(i))+1; 18 | end 19 | 20 | oa=trace(confu)/sum(confu(:)); %overall accuracy 21 | ua=diag(confu)./sum(confu,2); %class accuracy 22 | ua(isnan(ua))=0; 23 | number=size(ua,1); 24 | 25 | aa=sum(ua)/number; 26 | 27 | 28 | Po=oa; 29 | Pe=(sum(confu)*sum(confu,2))/(sum(confu(:))^2); 30 | 31 | K=(Po-Pe)/(1-Pe);%kappa coefficient 32 | 33 | 34 | %http://kappa.chez-alice.fr/kappa_intro.htm -------------------------------------------------------------------------------- /functions/cross_validation_svm.m: -------------------------------------------------------------------------------- 1 | function [C, G, cv, cv_t]=cross_validation_svm(train_label,train_set) 2 | 3 | % 4 | % function [C G cv cv_t]=cross_validation(train_label,train_set,kernel) 5 | % 6 | % This function performs a cross_validation to select goods 7 | % parameter for the training of a binary svm 8 | % 9 | % INPUT 10 | % train_label: the label in row column 11 | % train_set: the sample corresponding to the label 12 | % 13 | % OUPUT 14 | % C: the optimal value of c coressponding to the best cv 15 | % g: the optimal value of g coressponding to the best cv 16 | % cv: the best cross validation accuracy 17 | % cv_t: the cross validation grid 18 | 19 | h = waitbar(0,'cross_validation...'); 20 | c=10.^(-2:4); 21 | g=2.^(-3:1:4); 22 | 23 | c_s=length(c); 24 | g_s=length(g); 25 | 26 | k=0; 27 | for i=1:g_s 28 | for j=1:c_s 29 | k=k+1; 30 | waitbar(k/(g_s*c_s)); 31 | parameter=sprintf('-c %f -g %f -m 1000 -v 5 -q',c(j),g(i)); 32 | cv_t(i,j)=svmtrain(train_label, train_set,parameter); 33 | end 34 | end 35 | 36 | [li, co]=find(max(max(cv_t))==cv_t); 37 | C=c(co(1)); 38 | G=g(li(1)); 39 | cv=max(max(cv_t)); 40 | close(h) -------------------------------------------------------------------------------- /functions/kernelize_test.m: -------------------------------------------------------------------------------- 1 | function K = kernelize_test(kernel, Xtrain, Xtest, parameter, Xtrainsum) 2 | 3 | % 4 | % K = kernelize_test(kernel, Xtrain, Xtest, parameter, Xtrainsum); 5 | % 6 | % Kernelize the test data with training data 7 | % 8 | % Input: 9 | % 10 | % kernel - kernel type, 'Gaussian' etc. 11 | % Xtrain - training data 12 | % Xtest - new test data to be kernelized with training data 13 | % parameter - scale parameter for kernel 14 | % Xtrainsum - aux variable for training data 15 | % (row or columns sum for symmetric kernel matrix) 16 | % 17 | % Output: 18 | % 19 | % K - kernel matrix for test data with training data 20 | % 21 | % 22 | % Copyright 2009-2011 23 | % Wenzhi Liao 24 | % wliao@telin.ugent.be, http://telin.ugent.be/~wliao/ 25 | % 2 Oct 2010 26 | 27 | if nargin<4, error('not enough input'); end 28 | 29 | [ntrain dimtrain] = size(Xtrain); 30 | [ntest dimtest] = size(Xtest); 31 | if dimtrain~=dimtest, error('Xtrain and Xtest must have same dimension'); end 32 | 33 | if ~strncmp(kernel,'linear',1) 34 | if nargin<5, Xtrainsum = sum(Xtrain.*Xtrain,2); end 35 | Xtestsum = sum(Xtest.*Xtest,2); 36 | K0 = repmat(Xtestsum',ntrain,1); 37 | Ki = repmat(Xtrainsum',ntest,1); 38 | K = K0 + Ki' - 2*Xtrain*Xtest'; 39 | clear K0 Ki 40 | end 41 | 42 | switch kernel 43 | case 'linear' 44 | K = Xtrain*Xtest'; % linear kernel 45 | case 'Gaussian' 46 | sigma2 = 2*parameter^2; 47 | K = exp(-K/sigma2); % Gaussian 48 | case 'poly' 49 | param1 = parameter(1); param2 = parameter(2); 50 | K = (K + param1) .^ param2; % poly kernel 51 | otherwise 52 | error('Unknown kernel function.'); 53 | end 54 | -------------------------------------------------------------------------------- /functions/kernelize_training.m: -------------------------------------------------------------------------------- 1 | function [K, scale, Xtrainsum] = kernelize_training(kernel, X, parameter) 2 | 3 | % 4 | % [K scale Xtrainsum] = kernelize_training(kernel, X, parameter); 5 | % 6 | % Kernelize the training data 7 | % 8 | % Input 9 | % kernel - kernel type, 'Gaussian' etc. 10 | % X - training data 11 | % parameter - parameters for kernel 12 | % 13 | % Output 14 | % K - kernel for training data 15 | % scale - scale parameter for kernel testing 16 | % Xtrainsum - row or columns sum for symmetric kernel matrix (symmetric) 17 | % 18 | % 19 | % Copyright 2009-2011 20 | % Wenzhi Liao 21 | % wliao@telin.ugent.be, http://telin.ugent.be/~wliao/ 22 | % 2 Oct 2010 23 | 24 | 25 | if nargin<3, error('not enough input'); end 26 | [ntrain dimtrain] = size(X); 27 | Xtrainsum = NaN; 28 | 29 | if ~strncmp(kernel,'l',1) 30 | Xtrainsum = sum(X.*X,2); 31 | K = repmat(Xtrainsum',ntrain,1); 32 | K = K + K' - 2*X*X'; % squared distances beetween training smaples, rows of X 33 | end 34 | 35 | switch kernel 36 | case 'linear' 37 | K = X*X'; % linear kernel 38 | scale = NaN; 39 | case 'Gaussian' 40 | scale = parameter*sum(real(sqrt(K(:))))/(ntrain*ntrain); 41 | sigma2 = 2*scale^2; 42 | K = exp(-K/sigma2); % Gaussian 43 | case 'poly' 44 | param1 = parameter(1); param2 = parameter(2); 45 | scale=[param1, param2]; 46 | K = (K + param1) .^ param2; % ploy kernel 47 | otherwise 48 | error('Unknown kernel function.'); 49 | end 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /functions/kpca.m: -------------------------------------------------------------------------------- 1 | function [out, idxtrain,eigvector, eigvalue] = kpca(spectraldata, No_Train, dimension, kernel, parameter) 2 | 3 | % 4 | % Kernel principal component analysis, KPCA 5 | % 6 | % [out, eigvector, eigvalue] = kpca(spectraldata, No_Train, dimension, kernel, parameter) 7 | % 8 | % Input 9 | % 10 | % spectraldata - input hyperspectral data with 3-D, ncols by nrows by nbands 11 | % No_Train - randomly selected samples for training (<= 5000), 12 | % depending on the memory of your PC 13 | % dimension - the dimension of extracted kernelized PCs (with largest eigen values) 14 | % kernel - kernel function 15 | % parameter - parameters for kernel fuction 16 | % - kernel = 'linear';% linear kernel 17 | % - kernel = 'Gaussian'; parameter=1; %Gausian kernel 18 | % - kernel = 'poly'; parameter=[1,3];% third order polynomial 19 | % 20 | % 21 | % Output 22 | % 23 | % out - the extracted kernelized PCs 24 | % eigvector - the eigenvectors divided by square root of corresponding eigenvalues 25 | % eigvalue - the first dimension largest eigenvalues 26 | 27 | if nargin < 3, error('not enough input'); end 28 | if nargin < 4 29 | if strncmp(kernel,'Gaussian',1) 30 | parameter=1; 31 | elseif strncmp(kernel,'poly',1) 32 | parameter=[1, 3]; 33 | end 34 | end 35 | 36 | [nrows,ncols,nbands] = size(spectraldata); 37 | X0 = reshape(spectraldata,nrows*ncols,nbands); 38 | clear spectraldata 39 | 40 | %% sub-sample for training, select No_Train samples randomly 41 | rand('state',4711007);% initialization of rand 42 | if No_Train>nrows*ncols, No_Train = nrows*ncols; end 43 | idxtrain = randsample(nrows*ncols, No_Train); 44 | X = double(X0(idxtrain,:)); 45 | ntrain = size(X,1); 46 | 47 | Xtest = X0; 48 | ntest = size(Xtest,1); 49 | clear X0; 50 | 51 | %% kernelized training data and centering the kernel matrix 52 | [K scale sums] = kernelize_training(kernel, X, parameter); 53 | meanK = mean(K(:)); 54 | meanrowsK = mean(K); 55 | K = centering(K); 56 | 57 | %% select the first dimension eigvectors 58 | dimout = ntrain; 59 | if dimout>dimension, dimout=dimension; end 60 | [eigvector,eigvalue,flagk] = eigs(K, dimout, 'LM'); 61 | 62 | if flagk~=0, warning('*** Convergence problems in eigs ***'); end 63 | eigvalue = diag(abs(eigvalue))'; 64 | eigvector = sqrt(ntrain-1)*eigvector*diag(1./sqrt(eigvalue)); 65 | clear K 66 | 67 | out = NaN(ntest,dimout); 68 | %% kernelized the test samples, center kernel and calculate kernel PCs 69 | for rr=1:nrows 70 | idx = (rr-1)*ncols+1; 71 | idx = idx:(idx+ncols-1); 72 | Xk = kernelize_test(kernel, X, Xtest(idx,:), scale, sums); 73 | Xk = Xk - repmat(meanrowsK,ncols,1)' - repmat(mean(Xk),ntrain,1) + meanK; 74 | out(idx,:) = Xk'*eigvector; 75 | end 76 | 77 | out = reshape(out,nrows,ncols,dimout); 78 | -------------------------------------------------------------------------------- /functions/label2colord.m: -------------------------------------------------------------------------------- 1 | function classif=label2colord(label,data_name) 2 | 3 | [w h]=size(label); 4 | 5 | im=zeros(w,h,3); 6 | 7 | switch lower(data_name) 8 | 9 | case 'uni' 10 | map=[192 192 192;0 255 0;0 255 255;0 128 0; 255 0 255;165 82 41;128 0 128;255 0 0;255 255 0]; 11 | for i=1:w 12 | for j=1:h 13 | switch(label(i,j)) 14 | case(1) 15 | im(i,j,:)=uint8(map(1,:)); 16 | case(2) 17 | im(i,j,:)=uint8(map(2,:)); 18 | case(3) 19 | im(i,j,:)=uint8(map(3,:)); 20 | case(4) 21 | im(i,j,:)=uint8(map(4,:)); 22 | case(5) 23 | im(i,j,:)=uint8(map(5,:)); 24 | case(6) 25 | im(i,j,:)=uint8(map(6,:)); 26 | case(7) 27 | im(i,j,:)=uint8(map(7,:)); 28 | case(8) 29 | im(i,j,:)=uint8(map(8,:)); 30 | case(9) 31 | im(i,j,:)=uint8(map(9,:)); 32 | end 33 | end 34 | end 35 | case 'center' 36 | map=[0 0 255;0 128 0;0 255 0;255 0 0;142 71 2;192 192 192;0 255 255;246 110 0; 255 255 0]; 37 | for i=1:w 38 | for j=1:h 39 | switch(label(i,j)) 40 | case(1) 41 | im(i,j,:)=uint8(map(1,:)); 42 | case(2) 43 | im(i,j,:)=uint8(map(2,:)); 44 | case(3) 45 | im(i,j,:)=uint8(map(3,:)); 46 | case(4) 47 | im(i,j,:)=uint8(map(4,:)); 48 | case(5) 49 | im(i,j,:)=uint8(map(5,:)); 50 | case(6) 51 | im(i,j,:)=uint8(map(6,:)); 52 | case(7) 53 | im(i,j,:)=uint8(map(7,:)); 54 | case(8) 55 | im(i,j,:)=uint8(map(8,:)); 56 | case(9) 57 | im(i,j,:)=uint8(map(9,:)); 58 | end 59 | end 60 | end 61 | 62 | case 'india' 63 | % map=[0 0 255;255 100 0;0 255 134;150 70 150; 100 150 255;60 90 114;255 255 125;255 0 255;100 0 255;1 170 255;0 255 0;175 175 82;100 190 56;140 67 46;115 255 172;255 255 0]; 64 | map=[140 67 46;0 0 255;255 100 0;0 255 123;164 75 155;101 174 255;118 254 172; 60 91 112;255,255,0;255 255 125;255 0 255;100 0 255;0 172 254;0 255 0;171 175 80;101 193 60]; 65 | for i=1:w 66 | for j=1:h 67 | switch(label(i,j)) 68 | case(1) 69 | im(i,j,:)=uint8(map(1,:)); 70 | case(2) 71 | im(i,j,:)=uint8(map(2,:)); 72 | case(3) 73 | im(i,j,:)=uint8(map(3,:)); 74 | case(4) 75 | im(i,j,:)=uint8(map(4,:)); 76 | case(5) 77 | im(i,j,:)=uint8(map(5,:)); 78 | case(6) 79 | im(i,j,:)=uint8(map(6,:)); 80 | case(7) 81 | im(i,j,:)=uint8(map(7,:)); 82 | case(8) 83 | im(i,j,:)=uint8(map(8,:)); 84 | case(9) 85 | im(i,j,:)=uint8(map(9,:)); 86 | case(10) 87 | im(i,j,:)=uint8(map(10,:)); 88 | case(11) 89 | im(i,j,:)=uint8(map(11,:)); 90 | case(12) 91 | im(i,j,:)=uint8(map(12,:)); 92 | case(13) 93 | im(i,j,:)=uint8(map(13,:)); 94 | case(14) 95 | im(i,j,:)=uint8(map(14,:)); 96 | case(15) 97 | im(i,j,:)=uint8(map(15,:)); 98 | case(16) 99 | im(i,j,:)=uint8(map(16,:)); 100 | end 101 | end 102 | end 103 | 104 | 105 | end 106 | 107 | name=sprintf('classif_%s.tif',data_name); 108 | im=uint8(im); 109 | classif=uint8(zeros(w,h,3)); 110 | classif(:,:,1)=im(:,:,1); 111 | classif(:,:,2)=im(:,:,2); 112 | classif(:,:,3)=im(:,:,3); 113 | %imwrite(classif,name); -------------------------------------------------------------------------------- /functions/randsample.m: -------------------------------------------------------------------------------- 1 | function y = randsample(n, k) 2 | %RANDSAMPLE Random sampling, without replacement 3 | % Y = RANDSAMPLE(N,K) returns K values sampled at random, without 4 | % replacement, from the integers 1:N. 5 | 6 | % Copyright 1993-2002 The MathWorks, Inc. 7 | % $Revision: 1.1 $ $Date: 2002/03/13 23:15:54 $ 8 | 9 | % RANDSAMPLE does not (yet) implement weighted sampling. 10 | 11 | if nargin < 2 12 | error('Requires two input arguments.'); 13 | end 14 | 15 | % If the sample is a sizeable fraction of the population, just 16 | % randomize the whole population (which involves a full sort 17 | % of n random values), and take the first k. 18 | if 4*k > n 19 | rp = randperm(n); 20 | y = rp(1:k); 21 | 22 | % If the sample is a small fraction of the population, a full 23 | % sort is wasteful. Repeatedly sample with replacement until 24 | % there are k unique values. 25 | else 26 | x = zeros(1,n); % flags 27 | sumx = 0; 28 | while sumx < k 29 | x(ceil(n * rand(1,k-sumx))) = 1; % sample w/replacement 30 | sumx = sum(x); % count how many unique elements so far 31 | end 32 | y = find(x > 0); 33 | y = y(randperm(k)); 34 | end 35 | 36 | % a scalar loop version 37 | % 38 | % x = 1:n; 39 | % n = n:(-1):(n-k+1); 40 | % y = zeros(1,k); 41 | % j = ceil(n .* rand(1,k)); 42 | % for i = 1:k 43 | % y(i) = x(j(i)); 44 | % x(j(i)) = x(n(i)); 45 | % end 46 | -------------------------------------------------------------------------------- /functions/scale_func.m: -------------------------------------------------------------------------------- 1 | function [data M m] =scale_func(data,M,m) 2 | % 3 | % function data =rescale(data) 4 | % 5 | % This function rescale the input data between -1 and 1 6 | % 7 | % INPUT 8 | % 9 | % data: the data to rescale 10 | % max: the maximum value of the ouput data 11 | % min: the minimum value of the output data 12 | % 13 | % OUTPUT 14 | % 15 | % data: the rescaled data 16 | [Nb_s Nb_b]=size(data); 17 | if nargin==1 18 | M=max(data,[],1); 19 | m=min(data,[],1); 20 | end 21 | 22 | data = 2*(data-repmat(m,Nb_s,1))./(repmat(M-m,Nb_s,1))-1; -------------------------------------------------------------------------------- /functions/scale_new.m: -------------------------------------------------------------------------------- 1 | function [data M m] =scale_new(data,M,m) 2 | % 3 | % function data =rescale(data) 4 | % 5 | % This function rescale the input data between 0 and 1 6 | % 7 | % INPUT 8 | % 9 | % data: the data to rescale 10 | % max: the maximum value of the ouput data 11 | % min: the minimum value of the output data 12 | % 13 | % OUTPUT 14 | % 15 | % data: the rescaled data 16 | [Nb_s Nb_b]=size(data); 17 | if nargin==1 18 | M=max(data,[],1); 19 | m=min(data,[],1); 20 | end 21 | 22 | data = (data-repmat(m,Nb_s,1))./(repmat(M-m,Nb_s,1)); 23 | 24 | -------------------------------------------------------------------------------- /functions/tsmooth.m: -------------------------------------------------------------------------------- 1 | function S = tsmooth(I,lambda,sigma,sharpness,maxIter) 2 | %tsmooth - Structure Extraction from Texture via Relative Total Variation 3 | % S = tsmooth(I, lambda, sigma, maxIter) extracts structure S from 4 | % structure+texture input I, with smoothness weight lambda, scale 5 | % parameter sigma and iteration number maxIter. 6 | % 7 | % Paras: 8 | % @I : Input UINT8 image, both grayscale and color images are acceptable. 9 | % @lambda : Parameter controlling the degree of smooth. 10 | % Range (0, 0.05], 0.01 by default. 11 | % @sigma : Parameter specifying the maximum size of texture elements. 12 | % Range (0, 6], 3 by defalut. 13 | % @sharpness : Parameter controlling the sharpness of the final results, 14 | % which corresponds to \epsilon_s in the paper [1]. The smaller the value, the sharper the result. 15 | % Range (1e-3, 0.03], 0.02 by defalut. 16 | % @maxIter : Number of itearations, 4 by default. 17 | % 18 | % Example 19 | % ========== 20 | % I = imread('Bishapur_zan.jpg'); 21 | % S = tsmooth(I); % Default Parameters (lambda = 0.01, sigma = 3, sharpness = 0.02, maxIter = 4) 22 | % figure, imshow(I), figure, imshow(S); 23 | % 24 | % ========== 25 | % The Code is created based on the method described in the following paper 26 | % [1] "Structure Extraction from Texture via Relative Total Variation", Li Xu, Qiong Yan, Yang Xia, Jiaya Jia, ACM Transactions on Graphics, 27 | % (SIGGRAPH Asia 2012), 2012. 28 | % The code and the algorithm are for non-comercial use only. 29 | % 30 | % Author: Li Xu (xuli@cse.cuhk.edu.hk) 31 | % Date : 08/25/2012 32 | % Version : 1.0 33 | % Copyright 2012, The Chinese University of Hong Kong. 34 | % 35 | 36 | if (~exist('lambda','var')) 37 | lambda=0.01; 38 | end 39 | if (~exist('sigma','var')) 40 | sigma=3.0; 41 | end 42 | if (~exist('sharpness','var')) 43 | sharpness = 0.02; 44 | end 45 | if (~exist('maxIter','var')) 46 | maxIter=4; 47 | end 48 | I = im2double(I); 49 | x = I; 50 | sigma_iter = sigma; 51 | lambda = lambda/2.0; 52 | dec=2.0; 53 | for iter = 1:maxIter 54 | [wx, wy] = computeTextureWeights(x, sigma_iter, sharpness); 55 | x = solveLinearEquation(I, wx, wy, lambda); 56 | sigma_iter = sigma_iter/dec; 57 | if sigma_iter < 0.5 58 | sigma_iter = 0.5; 59 | end 60 | end 61 | S = x; 62 | end 63 | 64 | function [retx, rety] = computeTextureWeights(fin, sigma,sharpness) 65 | 66 | fx = diff(fin,1,2); 67 | fx = padarray(fx, [0 1 0], 'post'); 68 | fy = diff(fin,1,1); 69 | fy = padarray(fy, [1 0 0], 'post'); 70 | 71 | vareps_s = sharpness; 72 | vareps = 0.001; 73 | 74 | wto = max(sum(sqrt(fx.^2+fy.^2),3)/size(fin,3),vareps_s).^(-1); 75 | fbin = lpfilter(fin, sigma); 76 | gfx = diff(fbin,1,2); 77 | gfx = padarray(gfx, [0 1], 'post'); 78 | gfy = diff(fbin,1,1); 79 | gfy = padarray(gfy, [1 0], 'post'); 80 | wtbx = max(sum(abs(gfx),3)/size(fin,3),vareps).^(-1); 81 | wtby = max(sum(abs(gfy),3)/size(fin,3),vareps).^(-1); 82 | retx = wtbx.*wto; 83 | rety = wtby.*wto; 84 | 85 | retx(:,end) = 0; 86 | rety(end,:) = 0; 87 | 88 | end 89 | 90 | function ret = conv2_sep(im, sigma) 91 | ksize = bitor(round(5*sigma),1); 92 | g = fspecial('gaussian', [1,ksize], sigma); 93 | ret = conv2(im,g,'same'); 94 | ret = conv2(ret,g','same'); 95 | end 96 | 97 | function FBImg = lpfilter(FImg, sigma) 98 | FBImg = FImg; 99 | for ic = 1:size(FBImg,3) 100 | FBImg(:,:,ic) = conv2_sep(FImg(:,:,ic), sigma); 101 | end 102 | end 103 | 104 | function OUT = solveLinearEquation(IN, wx, wy, lambda) 105 | % 106 | % The code for constructing inhomogenious Laplacian is adapted from 107 | % the implementaion of the wlsFilter. 108 | % 109 | % For color images, we enforce wx and wy be same for three channels 110 | % and thus the pre-conditionar only need to be computed once. 111 | % 112 | [r,c,ch] = size(IN); 113 | k = r*c; 114 | dx = -lambda*wx(:); 115 | dy = -lambda*wy(:); 116 | B(:,1) = dx; 117 | B(:,2) = dy; 118 | d = [-r,-1]; 119 | A = spdiags(B,d,k,k); 120 | e = dx; 121 | w = padarray(dx, r, 'pre'); w = w(1:end-r); 122 | s = dy; 123 | n = padarray(dy, 1, 'pre'); n = n(1:end-1); 124 | D = 1-(e+w+s+n); 125 | A = A + A' + spdiags(D, 0, k, k); 126 | if exist('ichol','builtin') 127 | L = ichol(A,struct('michol','on')); 128 | OUT = IN; 129 | for ii=1:ch 130 | tin = IN(:,:,ii); 131 | [tout, flag] = pcg(A, tin(:),0.1,100, L, L'); 132 | OUT(:,:,ii) = reshape(tout, r, c); 133 | end 134 | else 135 | OUT = IN; 136 | for ii=1:ch 137 | tin = IN(:,:,ii); 138 | tout = A\tin(:); 139 | OUT(:,:,ii) = reshape(tout, r, c); 140 | end 141 | end 142 | 143 | end -------------------------------------------------------------------------------- /libsvm-3.22/COPYRIGHT: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2000-2014 Chih-Chung Chang and Chih-Jen Lin 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 16 | 3. Neither name of copyright holders nor the names of its contributors 17 | may be used to endorse or promote products derived from this software 18 | without specific prior written permission. 19 | 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 28 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | -------------------------------------------------------------------------------- /libsvm-3.22/Makefile: -------------------------------------------------------------------------------- 1 | CXX ?= g++ 2 | CFLAGS = -Wall -Wconversion -O3 -fPIC 3 | SHVER = 2 4 | OS = $(shell uname) 5 | 6 | all: svm-train svm-predict svm-scale 7 | 8 | lib: svm.o 9 | if [ "$(OS)" = "Darwin" ]; then \ 10 | SHARED_LIB_FLAG="-dynamiclib -Wl,-install_name,libsvm.so.$(SHVER)"; \ 11 | else \ 12 | SHARED_LIB_FLAG="-shared -Wl,-soname,libsvm.so.$(SHVER)"; \ 13 | fi; \ 14 | $(CXX) $${SHARED_LIB_FLAG} svm.o -o libsvm.so.$(SHVER) 15 | 16 | svm-predict: svm-predict.c svm.o 17 | $(CXX) $(CFLAGS) svm-predict.c svm.o -o svm-predict -lm 18 | svm-train: svm-train.c svm.o 19 | $(CXX) $(CFLAGS) svm-train.c svm.o -o svm-train -lm 20 | svm-scale: svm-scale.c 21 | $(CXX) $(CFLAGS) svm-scale.c -o svm-scale 22 | svm.o: svm.cpp svm.h 23 | $(CXX) $(CFLAGS) -c svm.cpp 24 | clean: 25 | rm -f *~ svm.o svm-train svm-predict svm-scale libsvm.so.$(SHVER) 26 | -------------------------------------------------------------------------------- /libsvm-3.22/Makefile.win: -------------------------------------------------------------------------------- 1 | #You must ensure nmake.exe, cl.exe, link.exe are in system path. 2 | #VCVARS64.bat 3 | #Under dosbox prompt 4 | #nmake -f Makefile.win 5 | 6 | ########################################## 7 | CXX = cl.exe 8 | CFLAGS = /nologo /O2 /EHsc /I. /D _WIN64 /D _CRT_SECURE_NO_DEPRECATE 9 | TARGET = windows 10 | 11 | all: $(TARGET)\svm-train.exe $(TARGET)\svm-predict.exe $(TARGET)\svm-scale.exe $(TARGET)\svm-toy.exe lib 12 | 13 | $(TARGET)\svm-predict.exe: svm.h svm-predict.c svm.obj 14 | $(CXX) $(CFLAGS) svm-predict.c svm.obj -Fe$(TARGET)\svm-predict.exe 15 | 16 | $(TARGET)\svm-train.exe: svm.h svm-train.c svm.obj 17 | $(CXX) $(CFLAGS) svm-train.c svm.obj -Fe$(TARGET)\svm-train.exe 18 | 19 | $(TARGET)\svm-scale.exe: svm.h svm-scale.c 20 | $(CXX) $(CFLAGS) svm-scale.c -Fe$(TARGET)\svm-scale.exe 21 | 22 | $(TARGET)\svm-toy.exe: svm.h svm.obj svm-toy\windows\svm-toy.cpp 23 | $(CXX) $(CFLAGS) svm-toy\windows\svm-toy.cpp svm.obj user32.lib gdi32.lib comdlg32.lib -Fe$(TARGET)\svm-toy.exe 24 | 25 | svm.obj: svm.cpp svm.h 26 | $(CXX) $(CFLAGS) -c svm.cpp 27 | 28 | lib: svm.cpp svm.h svm.def 29 | $(CXX) $(CFLAGS) -LD svm.cpp -Fe$(TARGET)\libsvm -link -DEF:svm.def 30 | 31 | clean: 32 | -erase /Q *.obj $(TARGET)\*.exe $(TARGET)\*.dll $(TARGET)\*.exp $(TARGET)\*.lib 33 | 34 | -------------------------------------------------------------------------------- /libsvm-3.22/java/Makefile: -------------------------------------------------------------------------------- 1 | .SUFFIXES: .class .java 2 | FILES = libsvm/svm.class libsvm/svm_model.class libsvm/svm_node.class \ 3 | libsvm/svm_parameter.class libsvm/svm_problem.class \ 4 | libsvm/svm_print_interface.class \ 5 | svm_train.class svm_predict.class svm_toy.class svm_scale.class 6 | 7 | #JAVAC = jikes 8 | JAVAC_FLAGS = -target 1.7 -source 1.7 9 | JAVAC = javac 10 | # JAVAC_FLAGS = 11 | export CLASSPATH := .:$(CLASSPATH) 12 | 13 | all: $(FILES) 14 | jar cvf libsvm.jar *.class libsvm/*.class 15 | 16 | .java.class: 17 | $(JAVAC) $(JAVAC_FLAGS) $< 18 | 19 | libsvm/svm.java: libsvm/svm.m4 20 | m4 libsvm/svm.m4 > libsvm/svm.java 21 | 22 | clean: 23 | rm -f libsvm/*.class *.class *.jar libsvm/*~ *~ libsvm/svm.java 24 | 25 | dist: clean all 26 | rm *.class libsvm/*.class 27 | -------------------------------------------------------------------------------- /libsvm-3.22/java/libsvm.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/java/libsvm.jar -------------------------------------------------------------------------------- /libsvm-3.22/java/libsvm/svm_model.java: -------------------------------------------------------------------------------- 1 | // 2 | // svm_model 3 | // 4 | package libsvm; 5 | public class svm_model implements java.io.Serializable 6 | { 7 | public svm_parameter param; // parameter 8 | public int nr_class; // number of classes, = 2 in regression/one class svm 9 | public int l; // total #SV 10 | public svm_node[][] SV; // SVs (SV[l]) 11 | public double[][] sv_coef; // coefficients for SVs in decision functions (sv_coef[k-1][l]) 12 | public double[] rho; // constants in decision functions (rho[k*(k-1)/2]) 13 | public double[] probA; // pariwise probability information 14 | public double[] probB; 15 | public int[] sv_indices; // sv_indices[0,...,nSV-1] are values in [1,...,num_traning_data] to indicate SVs in the training set 16 | 17 | // for classification only 18 | 19 | public int[] label; // label of each class (label[k]) 20 | public int[] nSV; // number of SVs for each class (nSV[k]) 21 | // nSV[0] + nSV[1] + ... + nSV[k-1] = l 22 | }; 23 | -------------------------------------------------------------------------------- /libsvm-3.22/java/libsvm/svm_node.java: -------------------------------------------------------------------------------- 1 | package libsvm; 2 | public class svm_node implements java.io.Serializable 3 | { 4 | public int index; 5 | public double value; 6 | } 7 | -------------------------------------------------------------------------------- /libsvm-3.22/java/libsvm/svm_parameter.java: -------------------------------------------------------------------------------- 1 | package libsvm; 2 | public class svm_parameter implements Cloneable,java.io.Serializable 3 | { 4 | /* svm_type */ 5 | public static final int C_SVC = 0; 6 | public static final int NU_SVC = 1; 7 | public static final int ONE_CLASS = 2; 8 | public static final int EPSILON_SVR = 3; 9 | public static final int NU_SVR = 4; 10 | 11 | /* kernel_type */ 12 | public static final int LINEAR = 0; 13 | public static final int POLY = 1; 14 | public static final int RBF = 2; 15 | public static final int SIGMOID = 3; 16 | public static final int PRECOMPUTED = 4; 17 | 18 | public int svm_type; 19 | public int kernel_type; 20 | public int degree; // for poly 21 | public double gamma; // for poly/rbf/sigmoid 22 | public double coef0; // for poly/sigmoid 23 | 24 | // these are for training only 25 | public double cache_size; // in MB 26 | public double eps; // stopping criteria 27 | public double C; // for C_SVC, EPSILON_SVR and NU_SVR 28 | public int nr_weight; // for C_SVC 29 | public int[] weight_label; // for C_SVC 30 | public double[] weight; // for C_SVC 31 | public double nu; // for NU_SVC, ONE_CLASS, and NU_SVR 32 | public double p; // for EPSILON_SVR 33 | public int shrinking; // use the shrinking heuristics 34 | public int probability; // do probability estimates 35 | 36 | public Object clone() 37 | { 38 | try 39 | { 40 | return super.clone(); 41 | } catch (CloneNotSupportedException e) 42 | { 43 | return null; 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /libsvm-3.22/java/libsvm/svm_print_interface.java: -------------------------------------------------------------------------------- 1 | package libsvm; 2 | public interface svm_print_interface 3 | { 4 | public void print(String s); 5 | } 6 | -------------------------------------------------------------------------------- /libsvm-3.22/java/libsvm/svm_problem.java: -------------------------------------------------------------------------------- 1 | package libsvm; 2 | public class svm_problem implements java.io.Serializable 3 | { 4 | public int l; 5 | public double[] y; 6 | public svm_node[][] x; 7 | } 8 | -------------------------------------------------------------------------------- /libsvm-3.22/java/svm_predict.java: -------------------------------------------------------------------------------- 1 | import libsvm.*; 2 | import java.io.*; 3 | import java.util.*; 4 | 5 | class svm_predict { 6 | private static svm_print_interface svm_print_null = new svm_print_interface() 7 | { 8 | public void print(String s) {} 9 | }; 10 | 11 | private static svm_print_interface svm_print_stdout = new svm_print_interface() 12 | { 13 | public void print(String s) 14 | { 15 | System.out.print(s); 16 | } 17 | }; 18 | 19 | private static svm_print_interface svm_print_string = svm_print_stdout; 20 | 21 | static void info(String s) 22 | { 23 | svm_print_string.print(s); 24 | } 25 | 26 | private static double atof(String s) 27 | { 28 | return Double.valueOf(s).doubleValue(); 29 | } 30 | 31 | private static int atoi(String s) 32 | { 33 | return Integer.parseInt(s); 34 | } 35 | 36 | private static void predict(BufferedReader input, DataOutputStream output, svm_model model, int predict_probability) throws IOException 37 | { 38 | int correct = 0; 39 | int total = 0; 40 | double error = 0; 41 | double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; 42 | 43 | int svm_type=svm.svm_get_svm_type(model); 44 | int nr_class=svm.svm_get_nr_class(model); 45 | double[] prob_estimates=null; 46 | 47 | if(predict_probability == 1) 48 | { 49 | if(svm_type == svm_parameter.EPSILON_SVR || 50 | svm_type == svm_parameter.NU_SVR) 51 | { 52 | svm_predict.info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma="+svm.svm_get_svr_probability(model)+"\n"); 53 | } 54 | else 55 | { 56 | int[] labels=new int[nr_class]; 57 | svm.svm_get_labels(model,labels); 58 | prob_estimates = new double[nr_class]; 59 | output.writeBytes("labels"); 60 | for(int j=0;j=argv.length-2) 155 | exit_with_help(); 156 | try 157 | { 158 | BufferedReader input = new BufferedReader(new FileReader(argv[i])); 159 | DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(argv[i+2]))); 160 | svm_model model = svm.svm_load_model(argv[i+1]); 161 | if (model == null) 162 | { 163 | System.err.print("can't open model file "+argv[i+1]+"\n"); 164 | System.exit(1); 165 | } 166 | if(predict_probability == 1) 167 | { 168 | if(svm.svm_check_probability_model(model)==0) 169 | { 170 | System.err.print("Model does not support probabiliy estimates\n"); 171 | System.exit(1); 172 | } 173 | } 174 | else 175 | { 176 | if(svm.svm_check_probability_model(model)!=0) 177 | { 178 | svm_predict.info("Model supports probability estimates, but disabled in prediction.\n"); 179 | } 180 | } 181 | predict(input,output,model,predict_probability); 182 | input.close(); 183 | output.close(); 184 | } 185 | catch(FileNotFoundException e) 186 | { 187 | exit_with_help(); 188 | } 189 | catch(ArrayIndexOutOfBoundsException e) 190 | { 191 | exit_with_help(); 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /libsvm-3.22/java/svm_scale.java: -------------------------------------------------------------------------------- 1 | import libsvm.*; 2 | import java.io.*; 3 | import java.util.*; 4 | import java.text.DecimalFormat; 5 | 6 | class svm_scale 7 | { 8 | private String line = null; 9 | private double lower = -1.0; 10 | private double upper = 1.0; 11 | private double y_lower; 12 | private double y_upper; 13 | private boolean y_scaling = false; 14 | private double[] feature_max; 15 | private double[] feature_min; 16 | private double y_max = -Double.MAX_VALUE; 17 | private double y_min = Double.MAX_VALUE; 18 | private int max_index; 19 | private long num_nonzeros = 0; 20 | private long new_num_nonzeros = 0; 21 | 22 | private static void exit_with_help() 23 | { 24 | System.out.print( 25 | "Usage: svm-scale [options] data_filename\n" 26 | +"options:\n" 27 | +"-l lower : x scaling lower limit (default -1)\n" 28 | +"-u upper : x scaling upper limit (default +1)\n" 29 | +"-y y_lower y_upper : y scaling limits (default: no y scaling)\n" 30 | +"-s save_filename : save scaling parameters to save_filename\n" 31 | +"-r restore_filename : restore scaling parameters from restore_filename\n" 32 | ); 33 | System.exit(1); 34 | } 35 | 36 | private BufferedReader rewind(BufferedReader fp, String filename) throws IOException 37 | { 38 | fp.close(); 39 | return new BufferedReader(new FileReader(filename)); 40 | } 41 | 42 | private void output_target(double value) 43 | { 44 | if(y_scaling) 45 | { 46 | if(value == y_min) 47 | value = y_lower; 48 | else if(value == y_max) 49 | value = y_upper; 50 | else 51 | value = y_lower + (y_upper-y_lower) * 52 | (value-y_min) / (y_max-y_min); 53 | } 54 | 55 | System.out.print(value + " "); 56 | } 57 | 58 | private void output(int index, double value) 59 | { 60 | /* skip single-valued attribute */ 61 | if(feature_max[index] == feature_min[index]) 62 | return; 63 | 64 | if(value == feature_min[index]) 65 | value = lower; 66 | else if(value == feature_max[index]) 67 | value = upper; 68 | else 69 | value = lower + (upper-lower) * 70 | (value-feature_min[index])/ 71 | (feature_max[index]-feature_min[index]); 72 | 73 | if(value != 0) 74 | { 75 | System.out.print(index + ":" + value + " "); 76 | new_num_nonzeros++; 77 | } 78 | } 79 | 80 | private String readline(BufferedReader fp) throws IOException 81 | { 82 | line = fp.readLine(); 83 | return line; 84 | } 85 | 86 | private void run(String []argv) throws IOException 87 | { 88 | int i,index; 89 | BufferedReader fp = null, fp_restore = null; 90 | String save_filename = null; 91 | String restore_filename = null; 92 | String data_filename = null; 93 | 94 | 95 | for(i=0;i lower) || (y_scaling && !(y_upper > y_lower))) 118 | { 119 | System.err.println("inconsistent lower/upper specification"); 120 | System.exit(1); 121 | } 122 | if(restore_filename != null && save_filename != null) 123 | { 124 | System.err.println("cannot use -r and -s simultaneously"); 125 | System.exit(1); 126 | } 127 | 128 | if(argv.length != i+1) 129 | exit_with_help(); 130 | 131 | data_filename = argv[i]; 132 | try { 133 | fp = new BufferedReader(new FileReader(data_filename)); 134 | } catch (Exception e) { 135 | System.err.println("can't open file " + data_filename); 136 | System.exit(1); 137 | } 138 | 139 | /* assumption: min index of attributes is 1 */ 140 | /* pass 1: find out max index of attributes */ 141 | max_index = 0; 142 | 143 | if(restore_filename != null) 144 | { 145 | int idx, c; 146 | 147 | try { 148 | fp_restore = new BufferedReader(new FileReader(restore_filename)); 149 | } 150 | catch (Exception e) { 151 | System.err.println("can't open file " + restore_filename); 152 | System.exit(1); 153 | } 154 | if((c = fp_restore.read()) == 'y') 155 | { 156 | fp_restore.readLine(); 157 | fp_restore.readLine(); 158 | fp_restore.readLine(); 159 | } 160 | fp_restore.readLine(); 161 | fp_restore.readLine(); 162 | 163 | String restore_line = null; 164 | while((restore_line = fp_restore.readLine())!=null) 165 | { 166 | StringTokenizer st2 = new StringTokenizer(restore_line); 167 | idx = Integer.parseInt(st2.nextToken()); 168 | max_index = Math.max(max_index, idx); 169 | } 170 | fp_restore = rewind(fp_restore, restore_filename); 171 | } 172 | 173 | while (readline(fp) != null) 174 | { 175 | StringTokenizer st = new StringTokenizer(line," \t\n\r\f:"); 176 | st.nextToken(); 177 | while(st.hasMoreTokens()) 178 | { 179 | index = Integer.parseInt(st.nextToken()); 180 | max_index = Math.max(max_index, index); 181 | st.nextToken(); 182 | num_nonzeros++; 183 | } 184 | } 185 | 186 | try { 187 | feature_max = new double[(max_index+1)]; 188 | feature_min = new double[(max_index+1)]; 189 | } catch(OutOfMemoryError e) { 190 | System.err.println("can't allocate enough memory"); 191 | System.exit(1); 192 | } 193 | 194 | for(i=0;i<=max_index;i++) 195 | { 196 | feature_max[i] = -Double.MAX_VALUE; 197 | feature_min[i] = Double.MAX_VALUE; 198 | } 199 | 200 | fp = rewind(fp, data_filename); 201 | 202 | /* pass 2: find out min/max value */ 203 | while(readline(fp) != null) 204 | { 205 | int next_index = 1; 206 | double target; 207 | double value; 208 | 209 | StringTokenizer st = new StringTokenizer(line," \t\n\r\f:"); 210 | target = Double.parseDouble(st.nextToken()); 211 | y_max = Math.max(y_max, target); 212 | y_min = Math.min(y_min, target); 213 | 214 | while (st.hasMoreTokens()) 215 | { 216 | index = Integer.parseInt(st.nextToken()); 217 | value = Double.parseDouble(st.nextToken()); 218 | 219 | for (i = next_index; i num_nonzeros) 337 | System.err.print( 338 | "WARNING: original #nonzeros " + num_nonzeros+"\n" 339 | +" new #nonzeros " + new_num_nonzeros+"\n" 340 | +"Use -l 0 if many original feature values are zeros\n"); 341 | 342 | fp.close(); 343 | } 344 | 345 | public static void main(String argv[]) throws IOException 346 | { 347 | svm_scale s = new svm_scale(); 348 | s.run(argv); 349 | } 350 | } 351 | -------------------------------------------------------------------------------- /libsvm-3.22/java/svm_train.java: -------------------------------------------------------------------------------- 1 | import libsvm.*; 2 | import java.io.*; 3 | import java.util.*; 4 | 5 | class svm_train { 6 | private svm_parameter param; // set by parse_command_line 7 | private svm_problem prob; // set by read_problem 8 | private svm_model model; 9 | private String input_file_name; // set by parse_command_line 10 | private String model_file_name; // set by parse_command_line 11 | private String error_msg; 12 | private int cross_validation; 13 | private int nr_fold; 14 | 15 | private static svm_print_interface svm_print_null = new svm_print_interface() 16 | { 17 | public void print(String s) {} 18 | }; 19 | 20 | private static void exit_with_help() 21 | { 22 | System.out.print( 23 | "Usage: svm_train [options] training_set_file [model_file]\n" 24 | +"options:\n" 25 | +"-s svm_type : set type of SVM (default 0)\n" 26 | +" 0 -- C-SVC (multi-class classification)\n" 27 | +" 1 -- nu-SVC (multi-class classification)\n" 28 | +" 2 -- one-class SVM\n" 29 | +" 3 -- epsilon-SVR (regression)\n" 30 | +" 4 -- nu-SVR (regression)\n" 31 | +"-t kernel_type : set type of kernel function (default 2)\n" 32 | +" 0 -- linear: u'*v\n" 33 | +" 1 -- polynomial: (gamma*u'*v + coef0)^degree\n" 34 | +" 2 -- radial basis function: exp(-gamma*|u-v|^2)\n" 35 | +" 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n" 36 | +" 4 -- precomputed kernel (kernel values in training_set_file)\n" 37 | +"-d degree : set degree in kernel function (default 3)\n" 38 | +"-g gamma : set gamma in kernel function (default 1/num_features)\n" 39 | +"-r coef0 : set coef0 in kernel function (default 0)\n" 40 | +"-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n" 41 | +"-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n" 42 | +"-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n" 43 | +"-m cachesize : set cache memory size in MB (default 100)\n" 44 | +"-e epsilon : set tolerance of termination criterion (default 0.001)\n" 45 | +"-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n" 46 | +"-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n" 47 | +"-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n" 48 | +"-v n : n-fold cross validation mode\n" 49 | +"-q : quiet mode (no outputs)\n" 50 | ); 51 | System.exit(1); 52 | } 53 | 54 | private void do_cross_validation() 55 | { 56 | int i; 57 | int total_correct = 0; 58 | double total_error = 0; 59 | double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; 60 | double[] target = new double[prob.l]; 61 | 62 | svm.svm_cross_validation(prob,param,nr_fold,target); 63 | if(param.svm_type == svm_parameter.EPSILON_SVR || 64 | param.svm_type == svm_parameter.NU_SVR) 65 | { 66 | for(i=0;i=argv.length) 166 | exit_with_help(); 167 | switch(argv[i-1].charAt(1)) 168 | { 169 | case 's': 170 | param.svm_type = atoi(argv[i]); 171 | break; 172 | case 't': 173 | param.kernel_type = atoi(argv[i]); 174 | break; 175 | case 'd': 176 | param.degree = atoi(argv[i]); 177 | break; 178 | case 'g': 179 | param.gamma = atof(argv[i]); 180 | break; 181 | case 'r': 182 | param.coef0 = atof(argv[i]); 183 | break; 184 | case 'n': 185 | param.nu = atof(argv[i]); 186 | break; 187 | case 'm': 188 | param.cache_size = atof(argv[i]); 189 | break; 190 | case 'c': 191 | param.C = atof(argv[i]); 192 | break; 193 | case 'e': 194 | param.eps = atof(argv[i]); 195 | break; 196 | case 'p': 197 | param.p = atof(argv[i]); 198 | break; 199 | case 'h': 200 | param.shrinking = atoi(argv[i]); 201 | break; 202 | case 'b': 203 | param.probability = atoi(argv[i]); 204 | break; 205 | case 'q': 206 | print_func = svm_print_null; 207 | i--; 208 | break; 209 | case 'v': 210 | cross_validation = 1; 211 | nr_fold = atoi(argv[i]); 212 | if(nr_fold < 2) 213 | { 214 | System.err.print("n-fold cross validation: n must >= 2\n"); 215 | exit_with_help(); 216 | } 217 | break; 218 | case 'w': 219 | ++param.nr_weight; 220 | { 221 | int[] old = param.weight_label; 222 | param.weight_label = new int[param.nr_weight]; 223 | System.arraycopy(old,0,param.weight_label,0,param.nr_weight-1); 224 | } 225 | 226 | { 227 | double[] old = param.weight; 228 | param.weight = new double[param.nr_weight]; 229 | System.arraycopy(old,0,param.weight,0,param.nr_weight-1); 230 | } 231 | 232 | param.weight_label[param.nr_weight-1] = atoi(argv[i-1].substring(2)); 233 | param.weight[param.nr_weight-1] = atof(argv[i]); 234 | break; 235 | default: 236 | System.err.print("Unknown option: " + argv[i-1] + "\n"); 237 | exit_with_help(); 238 | } 239 | } 240 | 241 | svm.svm_set_print_string_function(print_func); 242 | 243 | // determine filenames 244 | 245 | if(i>=argv.length) 246 | exit_with_help(); 247 | 248 | input_file_name = argv[i]; 249 | 250 | if(i vy = new Vector(); 266 | Vector vx = new Vector(); 267 | int max_index = 0; 268 | 269 | while(true) 270 | { 271 | String line = fp.readLine(); 272 | if(line == null) break; 273 | 274 | StringTokenizer st = new StringTokenizer(line," \t\n\r\f:"); 275 | 276 | vy.addElement(atof(st.nextToken())); 277 | int m = st.countTokens()/2; 278 | svm_node[] x = new svm_node[m]; 279 | for(int j=0;j0) max_index = Math.max(max_index, x[m-1].index); 286 | vx.addElement(x); 287 | } 288 | 289 | prob = new svm_problem(); 290 | prob.l = vy.size(); 291 | prob.x = new svm_node[prob.l][]; 292 | for(int i=0;i 0) 299 | param.gamma = 1.0/max_index; 300 | 301 | if(param.kernel_type == svm_parameter.PRECOMPUTED) 302 | for(int i=0;i max_index) 310 | { 311 | System.err.print("Wrong input format: sample_serial_number out of range\n"); 312 | System.exit(1); 313 | } 314 | } 315 | 316 | fp.close(); 317 | } 318 | } 319 | -------------------------------------------------------------------------------- /libsvm-3.22/java/test_applet.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /libsvm-3.22/matlab/Makefile: -------------------------------------------------------------------------------- 1 | # This Makefile is used under Linux 2 | 3 | MATLABDIR ?= /usr/local/matlab 4 | # for Mac 5 | # MATLABDIR ?= /opt/local/matlab 6 | 7 | CXX ?= g++ 8 | #CXX = g++-4.1 9 | CFLAGS = -Wall -Wconversion -O3 -fPIC -I$(MATLABDIR)/extern/include -I.. 10 | 11 | MEX = $(MATLABDIR)/bin/mex 12 | MEX_OPTION = CC="$(CXX)" CXX="$(CXX)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" 13 | # comment the following line if you use MATLAB on 32-bit computer 14 | MEX_OPTION += -largeArrayDims 15 | MEX_EXT = $(shell $(MATLABDIR)/bin/mexext) 16 | 17 | all: matlab 18 | 19 | matlab: binary 20 | 21 | octave: 22 | @echo "please type make under Octave" 23 | 24 | binary: svmpredict.$(MEX_EXT) svmtrain.$(MEX_EXT) libsvmread.$(MEX_EXT) libsvmwrite.$(MEX_EXT) 25 | 26 | svmpredict.$(MEX_EXT): svmpredict.c ../svm.h ../svm.o svm_model_matlab.o 27 | $(MEX) $(MEX_OPTION) svmpredict.c ../svm.o svm_model_matlab.o 28 | 29 | svmtrain.$(MEX_EXT): svmtrain.c ../svm.h ../svm.o svm_model_matlab.o 30 | $(MEX) $(MEX_OPTION) svmtrain.c ../svm.o svm_model_matlab.o 31 | 32 | libsvmread.$(MEX_EXT): libsvmread.c 33 | $(MEX) $(MEX_OPTION) libsvmread.c 34 | 35 | libsvmwrite.$(MEX_EXT): libsvmwrite.c 36 | $(MEX) $(MEX_OPTION) libsvmwrite.c 37 | 38 | svm_model_matlab.o: svm_model_matlab.c ../svm.h 39 | $(CXX) $(CFLAGS) -c svm_model_matlab.c 40 | 41 | ../svm.o: ../svm.cpp ../svm.h 42 | make -C .. svm.o 43 | 44 | clean: 45 | rm -f *~ *.o *.mex* *.obj ../svm.o 46 | -------------------------------------------------------------------------------- /libsvm-3.22/matlab/README: -------------------------------------------------------------------------------- 1 | ----------------------------------------- 2 | --- MATLAB/OCTAVE interface of LIBSVM --- 3 | ----------------------------------------- 4 | 5 | Table of Contents 6 | ================= 7 | 8 | - Introduction 9 | - Installation 10 | - Usage 11 | - Returned Model Structure 12 | - Other Utilities 13 | - Examples 14 | - Additional Information 15 | 16 | 17 | Introduction 18 | ============ 19 | 20 | This tool provides a simple interface to LIBSVM, a library for support vector 21 | machines (http://www.csie.ntu.edu.tw/~cjlin/libsvm). It is very easy to use as 22 | the usage and the way of specifying parameters are the same as that of LIBSVM. 23 | 24 | Installation 25 | ============ 26 | 27 | On Windows systems, pre-built binary files are already in the 28 | directory '..\windows', so no need to conduct installation. Now we 29 | provide binary files only for 64bit MATLAB on Windows. If you would 30 | like to re-build the package, please rely on the following steps. 31 | 32 | We recommend using make.m on both MATLAB and OCTAVE. Just type 'make' 33 | to build 'libsvmread.mex', 'libsvmwrite.mex', 'svmtrain.mex', and 34 | 'svmpredict.mex'. 35 | 36 | On MATLAB or Octave: 37 | 38 | >> make 39 | 40 | If make.m does not work on MATLAB (especially for Windows), try 'mex 41 | -setup' to choose a suitable compiler for mex. Make sure your compiler 42 | is accessible and workable. Then type 'make' to start the 43 | installation. 44 | 45 | Example: 46 | 47 | matlab>> mex -setup 48 | (ps: MATLAB will show the following messages to setup default compiler.) 49 | Please choose your compiler for building external interface (MEX) files: 50 | Would you like mex to locate installed compilers [y]/n? y 51 | Select a compiler: 52 | [1] Microsoft Visual C/C++ version 7.1 in C:\Program Files\Microsoft Visual Studio 53 | [0] None 54 | Compiler: 1 55 | Please verify your choices: 56 | Compiler: Microsoft Visual C/C++ 7.1 57 | Location: C:\Program Files\Microsoft Visual Studio 58 | Are these correct?([y]/n): y 59 | 60 | matlab>> make 61 | 62 | On Unix systems, if neither make.m nor 'mex -setup' works, please use 63 | Makefile and type 'make' in a command window. Note that we assume 64 | your MATLAB is installed in '/usr/local/matlab'. If not, please change 65 | MATLABDIR in Makefile. 66 | 67 | Example: 68 | linux> make 69 | 70 | To use octave, type 'make octave': 71 | 72 | Example: 73 | linux> make octave 74 | 75 | For a list of supported/compatible compilers for MATLAB, please check 76 | the following page: 77 | 78 | http://www.mathworks.com/support/compilers/current_release/ 79 | 80 | Usage 81 | ===== 82 | 83 | matlab> model = svmtrain(training_label_vector, training_instance_matrix [, 'libsvm_options']); 84 | 85 | -training_label_vector: 86 | An m by 1 vector of training labels (type must be double). 87 | -training_instance_matrix: 88 | An m by n matrix of m training instances with n features. 89 | It can be dense or sparse (type must be double). 90 | -libsvm_options: 91 | A string of training options in the same format as that of LIBSVM. 92 | 93 | matlab> [predicted_label, accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector, testing_instance_matrix, model [, 'libsvm_options']); 94 | matlab> [predicted_label] = svmpredict(testing_label_vector, testing_instance_matrix, model [, 'libsvm_options']); 95 | 96 | -testing_label_vector: 97 | An m by 1 vector of prediction labels. If labels of test 98 | data are unknown, simply use any random values. (type must be double) 99 | -testing_instance_matrix: 100 | An m by n matrix of m testing instances with n features. 101 | It can be dense or sparse. (type must be double) 102 | -model: 103 | The output of svmtrain. 104 | -libsvm_options: 105 | A string of testing options in the same format as that of LIBSVM. 106 | 107 | Returned Model Structure 108 | ======================== 109 | 110 | The 'svmtrain' function returns a model which can be used for future 111 | prediction. It is a structure and is organized as [Parameters, nr_class, 112 | totalSV, rho, Label, ProbA, ProbB, nSV, sv_coef, SVs]: 113 | 114 | -Parameters: parameters 115 | -nr_class: number of classes; = 2 for regression/one-class svm 116 | -totalSV: total #SV 117 | -rho: -b of the decision function(s) wx+b 118 | -Label: label of each class; empty for regression/one-class SVM 119 | -sv_indices: values in [1,...,num_traning_data] to indicate SVs in the training set 120 | -ProbA: pairwise probability information; empty if -b 0 or in one-class SVM 121 | -ProbB: pairwise probability information; empty if -b 0 or in one-class SVM 122 | -nSV: number of SVs for each class; empty for regression/one-class SVM 123 | -sv_coef: coefficients for SVs in decision functions 124 | -SVs: support vectors 125 | 126 | If you do not use the option '-b 1', ProbA and ProbB are empty 127 | matrices. If the '-v' option is specified, cross validation is 128 | conducted and the returned model is just a scalar: cross-validation 129 | accuracy for classification and mean-squared error for regression. 130 | 131 | More details about this model can be found in LIBSVM FAQ 132 | (http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html) and LIBSVM 133 | implementation document 134 | (http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf). 135 | 136 | Result of Prediction 137 | ==================== 138 | 139 | The function 'svmpredict' has three outputs. The first one, 140 | predictd_label, is a vector of predicted labels. The second output, 141 | accuracy, is a vector including accuracy (for classification), mean 142 | squared error, and squared correlation coefficient (for regression). 143 | The third is a matrix containing decision values or probability 144 | estimates (if '-b 1' is specified). If k is the number of classes 145 | in training data, for decision values, each row includes results of 146 | predicting k(k-1)/2 binary-class SVMs. For classification, k = 1 is a 147 | special case. Decision value +1 is returned for each testing instance, 148 | instead of an empty vector. For probabilities, each row contains k values 149 | indicating the probability that the testing instance is in each class. 150 | Note that the order of classes here is the same as 'Label' field 151 | in the model structure. 152 | 153 | Other Utilities 154 | =============== 155 | 156 | A matlab function libsvmread reads files in LIBSVM format: 157 | 158 | [label_vector, instance_matrix] = libsvmread('data.txt'); 159 | 160 | Two outputs are labels and instances, which can then be used as inputs 161 | of svmtrain or svmpredict. 162 | 163 | A matlab function libsvmwrite writes Matlab matrix to a file in LIBSVM format: 164 | 165 | libsvmwrite('data.txt', label_vector, instance_matrix) 166 | 167 | The instance_matrix must be a sparse matrix. (type must be double) 168 | For 32bit and 64bit MATLAB on Windows, pre-built binary files are ready 169 | in the directory `..\windows', but in future releases, we will only 170 | include 64bit MATLAB binary files. 171 | 172 | These codes are prepared by Rong-En Fan and Kai-Wei Chang from National 173 | Taiwan University. 174 | 175 | Examples 176 | ======== 177 | 178 | Train and test on the provided data heart_scale: 179 | 180 | matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale'); 181 | matlab> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07'); 182 | matlab> [predict_label, accuracy, dec_values] = svmpredict(heart_scale_label, heart_scale_inst, model); % test the training data 183 | 184 | For probability estimates, you need '-b 1' for training and testing: 185 | 186 | matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale'); 187 | matlab> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07 -b 1'); 188 | matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale'); 189 | matlab> [predict_label, accuracy, prob_estimates] = svmpredict(heart_scale_label, heart_scale_inst, model, '-b 1'); 190 | 191 | To use precomputed kernel, you must include sample serial number as 192 | the first column of the training and testing data (assume your kernel 193 | matrix is K, # of instances is n): 194 | 195 | matlab> K1 = [(1:n)', K]; % include sample serial number as first column 196 | matlab> model = svmtrain(label_vector, K1, '-t 4'); 197 | matlab> [predict_label, accuracy, dec_values] = svmpredict(label_vector, K1, model); % test the training data 198 | 199 | We give the following detailed example by splitting heart_scale into 200 | 150 training and 120 testing data. Constructing a linear kernel 201 | matrix and then using the precomputed kernel gives exactly the same 202 | testing error as using the LIBSVM built-in linear kernel. 203 | 204 | matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale'); 205 | matlab> 206 | matlab> % Split Data 207 | matlab> train_data = heart_scale_inst(1:150,:); 208 | matlab> train_label = heart_scale_label(1:150,:); 209 | matlab> test_data = heart_scale_inst(151:270,:); 210 | matlab> test_label = heart_scale_label(151:270,:); 211 | matlab> 212 | matlab> % Linear Kernel 213 | matlab> model_linear = svmtrain(train_label, train_data, '-t 0'); 214 | matlab> [predict_label_L, accuracy_L, dec_values_L] = svmpredict(test_label, test_data, model_linear); 215 | matlab> 216 | matlab> % Precomputed Kernel 217 | matlab> model_precomputed = svmtrain(train_label, [(1:150)', train_data*train_data'], '-t 4'); 218 | matlab> [predict_label_P, accuracy_P, dec_values_P] = svmpredict(test_label, [(1:120)', test_data*train_data'], model_precomputed); 219 | matlab> 220 | matlab> accuracy_L % Display the accuracy using linear kernel 221 | matlab> accuracy_P % Display the accuracy using precomputed kernel 222 | 223 | Note that for testing, you can put anything in the 224 | testing_label_vector. For more details of precomputed kernels, please 225 | read the section ``Precomputed Kernels'' in the README of the LIBSVM 226 | package. 227 | 228 | Additional Information 229 | ====================== 230 | 231 | This interface was initially written by Jun-Cheng Chen, Kuan-Jen Peng, 232 | Chih-Yuan Yang and Chih-Huai Cheng from Department of Computer 233 | Science, National Taiwan University. The current version was prepared 234 | by Rong-En Fan and Ting-Fan Wu. If you find this tool useful, please 235 | cite LIBSVM as follows 236 | 237 | Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for support 238 | vector machines. ACM Transactions on Intelligent Systems and 239 | Technology, 2:27:1--27:27, 2011. Software available at 240 | http://www.csie.ntu.edu.tw/~cjlin/libsvm 241 | 242 | For any question, please contact Chih-Jen Lin , 243 | or check the FAQ page: 244 | 245 | http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#/Q10:_MATLAB_interface 246 | -------------------------------------------------------------------------------- /libsvm-3.22/matlab/libsvmread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "mex.h" 8 | 9 | #ifdef MX_API_VER 10 | #if MX_API_VER < 0x07030000 11 | typedef int mwIndex; 12 | #endif 13 | #endif 14 | #ifndef max 15 | #define max(x,y) (((x)>(y))?(x):(y)) 16 | #endif 17 | #ifndef min 18 | #define min(x,y) (((x)<(y))?(x):(y)) 19 | #endif 20 | 21 | void exit_with_help() 22 | { 23 | mexPrintf( 24 | "Usage: [label_vector, instance_matrix] = libsvmread('filename');\n" 25 | ); 26 | } 27 | 28 | static void fake_answer(int nlhs, mxArray *plhs[]) 29 | { 30 | int i; 31 | for(i=0;i start from 0 86 | strtok(line," \t"); // label 87 | while (1) 88 | { 89 | idx = strtok(NULL,":"); // index:value 90 | val = strtok(NULL," \t"); 91 | if(val == NULL) 92 | break; 93 | 94 | errno = 0; 95 | index = (int) strtol(idx,&endptr,10); 96 | if(endptr == idx || errno != 0 || *endptr != '\0' || index <= inst_max_index) 97 | { 98 | mexPrintf("Wrong input format at line %d\n",l+1); 99 | fake_answer(nlhs, plhs); 100 | return; 101 | } 102 | else 103 | inst_max_index = index; 104 | 105 | min_index = min(min_index, index); 106 | elements++; 107 | } 108 | max_index = max(max_index, inst_max_index); 109 | l++; 110 | } 111 | rewind(fp); 112 | 113 | // y 114 | plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL); 115 | // x^T 116 | if (min_index <= 0) 117 | plhs[1] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL); 118 | else 119 | plhs[1] = mxCreateSparse(max_index, l, elements, mxREAL); 120 | 121 | labels = mxGetPr(plhs[0]); 122 | samples = mxGetPr(plhs[1]); 123 | ir = mxGetIr(plhs[1]); 124 | jc = mxGetJc(plhs[1]); 125 | 126 | k=0; 127 | for(i=0;i start from 0 158 | 159 | errno = 0; 160 | samples[k] = strtod(val,&endptr); 161 | if (endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) 162 | { 163 | mexPrintf("Wrong input format at line %d\n",i+1); 164 | fake_answer(nlhs, plhs); 165 | return; 166 | } 167 | ++k; 168 | } 169 | } 170 | jc[l] = k; 171 | 172 | fclose(fp); 173 | free(line); 174 | 175 | { 176 | mxArray *rhs[1], *lhs[1]; 177 | rhs[0] = plhs[1]; 178 | if(mexCallMATLAB(1, lhs, 1, rhs, "transpose")) 179 | { 180 | mexPrintf("Error: cannot transpose problem\n"); 181 | fake_answer(nlhs, plhs); 182 | return; 183 | } 184 | plhs[1] = lhs[0]; 185 | } 186 | } 187 | 188 | void mexFunction( int nlhs, mxArray *plhs[], 189 | int nrhs, const mxArray *prhs[] ) 190 | { 191 | char filename[256]; 192 | 193 | if(nrhs != 1 || nlhs != 2) 194 | { 195 | exit_with_help(); 196 | fake_answer(nlhs, plhs); 197 | return; 198 | } 199 | 200 | mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1); 201 | 202 | if(filename == NULL) 203 | { 204 | mexPrintf("Error: filename is NULL\n"); 205 | return; 206 | } 207 | 208 | read_problem(filename, nlhs, plhs); 209 | 210 | return; 211 | } 212 | 213 | -------------------------------------------------------------------------------- /libsvm-3.22/matlab/libsvmread.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/matlab/libsvmread.mexw64 -------------------------------------------------------------------------------- /libsvm-3.22/matlab/libsvmwrite.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "mex.h" 5 | 6 | #ifdef MX_API_VER 7 | #if MX_API_VER < 0x07030000 8 | typedef int mwIndex; 9 | #endif 10 | #endif 11 | 12 | void exit_with_help() 13 | { 14 | mexPrintf( 15 | "Usage: libsvmwrite('filename', label_vector, instance_matrix);\n" 16 | ); 17 | } 18 | 19 | static void fake_answer(int nlhs, mxArray *plhs[]) 20 | { 21 | int i; 22 | for(i=0;i 0) 88 | { 89 | exit_with_help(); 90 | fake_answer(nlhs, plhs); 91 | return; 92 | } 93 | 94 | // Transform the input Matrix to libsvm format 95 | if(nrhs == 3) 96 | { 97 | char filename[256]; 98 | if(!mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2])) 99 | { 100 | mexPrintf("Error: label vector and instance matrix must be double\n"); 101 | return; 102 | } 103 | 104 | mxGetString(prhs[0], filename, mxGetN(prhs[0])+1); 105 | 106 | if(mxIsSparse(prhs[2])) 107 | libsvmwrite(filename, prhs[1], prhs[2]); 108 | else 109 | { 110 | mexPrintf("Instance_matrix must be sparse\n"); 111 | return; 112 | } 113 | } 114 | else 115 | { 116 | exit_with_help(); 117 | return; 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /libsvm-3.22/matlab/libsvmwrite.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/matlab/libsvmwrite.mexw64 -------------------------------------------------------------------------------- /libsvm-3.22/matlab/make.m: -------------------------------------------------------------------------------- 1 | % This make.m is for MATLAB and OCTAVE under Windows, Mac, and Unix 2 | function make() 3 | try 4 | % This part is for OCTAVE 5 | if (exist ('OCTAVE_VERSION', 'builtin')) 6 | mex libsvmread.c 7 | mex libsvmwrite.c 8 | mex -I.. svmtrain.c ../svm.cpp svm_model_matlab.c 9 | mex -I.. svmpredict.c ../svm.cpp svm_model_matlab.c 10 | % This part is for MATLAB 11 | % Add -largeArrayDims on 64-bit machines of MATLAB 12 | else 13 | mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims libsvmread.c 14 | mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims libsvmwrite.c 15 | mex CFLAGS="\$CFLAGS -std=c99" -I.. -largeArrayDims svmtrain.c ../svm.cpp svm_model_matlab.c 16 | mex CFLAGS="\$CFLAGS -std=c99" -I.. -largeArrayDims svmpredict.c ../svm.cpp svm_model_matlab.c 17 | end 18 | catch err 19 | fprintf('Error: %s failed (line %d)\n', err.stack(1).file, err.stack(1).line); 20 | disp(err.message); 21 | fprintf('=> Please check README for detailed instructions.\n'); 22 | end 23 | -------------------------------------------------------------------------------- /libsvm-3.22/matlab/svm_model_matlab.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "svm.h" 4 | 5 | #include "mex.h" 6 | 7 | #ifdef MX_API_VER 8 | #if MX_API_VER < 0x07030000 9 | typedef int mwIndex; 10 | #endif 11 | #endif 12 | 13 | #define NUM_OF_RETURN_FIELD 11 14 | 15 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 16 | 17 | static const char *field_names[] = { 18 | "Parameters", 19 | "nr_class", 20 | "totalSV", 21 | "rho", 22 | "Label", 23 | "sv_indices", 24 | "ProbA", 25 | "ProbB", 26 | "nSV", 27 | "sv_coef", 28 | "SVs" 29 | }; 30 | 31 | const char *model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model) 32 | { 33 | int i, j, n; 34 | double *ptr; 35 | mxArray *return_model, **rhs; 36 | int out_id = 0; 37 | 38 | rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD); 39 | 40 | // Parameters 41 | rhs[out_id] = mxCreateDoubleMatrix(5, 1, mxREAL); 42 | ptr = mxGetPr(rhs[out_id]); 43 | ptr[0] = model->param.svm_type; 44 | ptr[1] = model->param.kernel_type; 45 | ptr[2] = model->param.degree; 46 | ptr[3] = model->param.gamma; 47 | ptr[4] = model->param.coef0; 48 | out_id++; 49 | 50 | // nr_class 51 | rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); 52 | ptr = mxGetPr(rhs[out_id]); 53 | ptr[0] = model->nr_class; 54 | out_id++; 55 | 56 | // total SV 57 | rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); 58 | ptr = mxGetPr(rhs[out_id]); 59 | ptr[0] = model->l; 60 | out_id++; 61 | 62 | // rho 63 | n = model->nr_class*(model->nr_class-1)/2; 64 | rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL); 65 | ptr = mxGetPr(rhs[out_id]); 66 | for(i = 0; i < n; i++) 67 | ptr[i] = model->rho[i]; 68 | out_id++; 69 | 70 | // Label 71 | if(model->label) 72 | { 73 | rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL); 74 | ptr = mxGetPr(rhs[out_id]); 75 | for(i = 0; i < model->nr_class; i++) 76 | ptr[i] = model->label[i]; 77 | } 78 | else 79 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 80 | out_id++; 81 | 82 | // sv_indices 83 | if(model->sv_indices) 84 | { 85 | rhs[out_id] = mxCreateDoubleMatrix(model->l, 1, mxREAL); 86 | ptr = mxGetPr(rhs[out_id]); 87 | for(i = 0; i < model->l; i++) 88 | ptr[i] = model->sv_indices[i]; 89 | } 90 | else 91 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 92 | out_id++; 93 | 94 | // probA 95 | if(model->probA != NULL) 96 | { 97 | rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL); 98 | ptr = mxGetPr(rhs[out_id]); 99 | for(i = 0; i < n; i++) 100 | ptr[i] = model->probA[i]; 101 | } 102 | else 103 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 104 | out_id ++; 105 | 106 | // probB 107 | if(model->probB != NULL) 108 | { 109 | rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL); 110 | ptr = mxGetPr(rhs[out_id]); 111 | for(i = 0; i < n; i++) 112 | ptr[i] = model->probB[i]; 113 | } 114 | else 115 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 116 | out_id++; 117 | 118 | // nSV 119 | if(model->nSV) 120 | { 121 | rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL); 122 | ptr = mxGetPr(rhs[out_id]); 123 | for(i = 0; i < model->nr_class; i++) 124 | ptr[i] = model->nSV[i]; 125 | } 126 | else 127 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 128 | out_id++; 129 | 130 | // sv_coef 131 | rhs[out_id] = mxCreateDoubleMatrix(model->l, model->nr_class-1, mxREAL); 132 | ptr = mxGetPr(rhs[out_id]); 133 | for(i = 0; i < model->nr_class-1; i++) 134 | for(j = 0; j < model->l; j++) 135 | ptr[(i*(model->l))+j] = model->sv_coef[i][j]; 136 | out_id++; 137 | 138 | // SVs 139 | { 140 | int ir_index, nonzero_element; 141 | mwIndex *ir, *jc; 142 | mxArray *pprhs[1], *pplhs[1]; 143 | 144 | if(model->param.kernel_type == PRECOMPUTED) 145 | { 146 | nonzero_element = model->l; 147 | num_of_feature = 1; 148 | } 149 | else 150 | { 151 | nonzero_element = 0; 152 | for(i = 0; i < model->l; i++) { 153 | j = 0; 154 | while(model->SV[i][j].index != -1) 155 | { 156 | nonzero_element++; 157 | j++; 158 | } 159 | } 160 | } 161 | 162 | // SV in column, easier accessing 163 | rhs[out_id] = mxCreateSparse(num_of_feature, model->l, nonzero_element, mxREAL); 164 | ir = mxGetIr(rhs[out_id]); 165 | jc = mxGetJc(rhs[out_id]); 166 | ptr = mxGetPr(rhs[out_id]); 167 | jc[0] = ir_index = 0; 168 | for(i = 0;i < model->l; i++) 169 | { 170 | if(model->param.kernel_type == PRECOMPUTED) 171 | { 172 | // make a (1 x model->l) matrix 173 | ir[ir_index] = 0; 174 | ptr[ir_index] = model->SV[i][0].value; 175 | ir_index++; 176 | jc[i+1] = jc[i] + 1; 177 | } 178 | else 179 | { 180 | int x_index = 0; 181 | while (model->SV[i][x_index].index != -1) 182 | { 183 | ir[ir_index] = model->SV[i][x_index].index - 1; 184 | ptr[ir_index] = model->SV[i][x_index].value; 185 | ir_index++, x_index++; 186 | } 187 | jc[i+1] = jc[i] + x_index; 188 | } 189 | } 190 | // transpose back to SV in row 191 | pprhs[0] = rhs[out_id]; 192 | if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) 193 | return "cannot transpose SV matrix"; 194 | rhs[out_id] = pplhs[0]; 195 | out_id++; 196 | } 197 | 198 | /* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */ 199 | return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names); 200 | 201 | /* Fill struct matrix with input arguments */ 202 | for(i = 0; i < NUM_OF_RETURN_FIELD; i++) 203 | mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i])); 204 | /* return */ 205 | plhs[0] = return_model; 206 | mxFree(rhs); 207 | 208 | return NULL; 209 | } 210 | 211 | struct svm_model *matlab_matrix_to_model(const mxArray *matlab_struct, const char **msg) 212 | { 213 | int i, j, n, num_of_fields; 214 | double *ptr; 215 | int id = 0; 216 | struct svm_node *x_space; 217 | struct svm_model *model; 218 | mxArray **rhs; 219 | 220 | num_of_fields = mxGetNumberOfFields(matlab_struct); 221 | if(num_of_fields != NUM_OF_RETURN_FIELD) 222 | { 223 | *msg = "number of return field is not correct"; 224 | return NULL; 225 | } 226 | rhs = (mxArray **) mxMalloc(sizeof(mxArray *)*num_of_fields); 227 | 228 | for(i=0;irho = NULL; 233 | model->probA = NULL; 234 | model->probB = NULL; 235 | model->label = NULL; 236 | model->sv_indices = NULL; 237 | model->nSV = NULL; 238 | model->free_sv = 1; // XXX 239 | 240 | ptr = mxGetPr(rhs[id]); 241 | model->param.svm_type = (int)ptr[0]; 242 | model->param.kernel_type = (int)ptr[1]; 243 | model->param.degree = (int)ptr[2]; 244 | model->param.gamma = ptr[3]; 245 | model->param.coef0 = ptr[4]; 246 | id++; 247 | 248 | ptr = mxGetPr(rhs[id]); 249 | model->nr_class = (int)ptr[0]; 250 | id++; 251 | 252 | ptr = mxGetPr(rhs[id]); 253 | model->l = (int)ptr[0]; 254 | id++; 255 | 256 | // rho 257 | n = model->nr_class * (model->nr_class-1)/2; 258 | model->rho = (double*) malloc(n*sizeof(double)); 259 | ptr = mxGetPr(rhs[id]); 260 | for(i=0;irho[i] = ptr[i]; 262 | id++; 263 | 264 | // label 265 | if(mxIsEmpty(rhs[id]) == 0) 266 | { 267 | model->label = (int*) malloc(model->nr_class*sizeof(int)); 268 | ptr = mxGetPr(rhs[id]); 269 | for(i=0;inr_class;i++) 270 | model->label[i] = (int)ptr[i]; 271 | } 272 | id++; 273 | 274 | // sv_indices 275 | if(mxIsEmpty(rhs[id]) == 0) 276 | { 277 | model->sv_indices = (int*) malloc(model->l*sizeof(int)); 278 | ptr = mxGetPr(rhs[id]); 279 | for(i=0;il;i++) 280 | model->sv_indices[i] = (int)ptr[i]; 281 | } 282 | id++; 283 | 284 | // probA 285 | if(mxIsEmpty(rhs[id]) == 0) 286 | { 287 | model->probA = (double*) malloc(n*sizeof(double)); 288 | ptr = mxGetPr(rhs[id]); 289 | for(i=0;iprobA[i] = ptr[i]; 291 | } 292 | id++; 293 | 294 | // probB 295 | if(mxIsEmpty(rhs[id]) == 0) 296 | { 297 | model->probB = (double*) malloc(n*sizeof(double)); 298 | ptr = mxGetPr(rhs[id]); 299 | for(i=0;iprobB[i] = ptr[i]; 301 | } 302 | id++; 303 | 304 | // nSV 305 | if(mxIsEmpty(rhs[id]) == 0) 306 | { 307 | model->nSV = (int*) malloc(model->nr_class*sizeof(int)); 308 | ptr = mxGetPr(rhs[id]); 309 | for(i=0;inr_class;i++) 310 | model->nSV[i] = (int)ptr[i]; 311 | } 312 | id++; 313 | 314 | // sv_coef 315 | ptr = mxGetPr(rhs[id]); 316 | model->sv_coef = (double**) malloc((model->nr_class-1)*sizeof(double)); 317 | for( i=0 ; i< model->nr_class -1 ; i++ ) 318 | model->sv_coef[i] = (double*) malloc((model->l)*sizeof(double)); 319 | for(i = 0; i < model->nr_class - 1; i++) 320 | for(j = 0; j < model->l; j++) 321 | model->sv_coef[i][j] = ptr[i*(model->l)+j]; 322 | id++; 323 | 324 | // SV 325 | { 326 | int sr, elements; 327 | int num_samples; 328 | mwIndex *ir, *jc; 329 | mxArray *pprhs[1], *pplhs[1]; 330 | 331 | // transpose SV 332 | pprhs[0] = rhs[id]; 333 | if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) 334 | { 335 | svm_free_and_destroy_model(&model); 336 | *msg = "cannot transpose SV matrix"; 337 | return NULL; 338 | } 339 | rhs[id] = pplhs[0]; 340 | 341 | sr = (int)mxGetN(rhs[id]); 342 | 343 | ptr = mxGetPr(rhs[id]); 344 | ir = mxGetIr(rhs[id]); 345 | jc = mxGetJc(rhs[id]); 346 | 347 | num_samples = (int)mxGetNzmax(rhs[id]); 348 | 349 | elements = num_samples + sr; 350 | 351 | model->SV = (struct svm_node **) malloc(sr * sizeof(struct svm_node *)); 352 | x_space = (struct svm_node *)malloc(elements * sizeof(struct svm_node)); 353 | 354 | // SV is in column 355 | for(i=0;iSV[i] = &x_space[low+i]; 360 | for(j=low;jSV[i][x_index].index = (int)ir[j] + 1; 363 | model->SV[i][x_index].value = ptr[j]; 364 | x_index++; 365 | } 366 | model->SV[i][x_index].index = -1; 367 | } 368 | 369 | id++; 370 | } 371 | mxFree(rhs); 372 | 373 | return model; 374 | } 375 | -------------------------------------------------------------------------------- /libsvm-3.22/matlab/svm_model_matlab.h: -------------------------------------------------------------------------------- 1 | const char *model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model); 2 | struct svm_model *matlab_matrix_to_model(const mxArray *matlab_struct, const char **error_message); 3 | -------------------------------------------------------------------------------- /libsvm-3.22/matlab/svmpredict.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "svm.h" 5 | 6 | #include "mex.h" 7 | #include "svm_model_matlab.h" 8 | 9 | #ifdef MX_API_VER 10 | #if MX_API_VER < 0x07030000 11 | typedef int mwIndex; 12 | #endif 13 | #endif 14 | 15 | #define CMD_LEN 2048 16 | 17 | int print_null(const char *s,...) {} 18 | int (*info)(const char *fmt,...) = &mexPrintf; 19 | 20 | void read_sparse_instance(const mxArray *prhs, int index, struct svm_node *x) 21 | { 22 | int i, j, low, high; 23 | mwIndex *ir, *jc; 24 | double *samples; 25 | 26 | ir = mxGetIr(prhs); 27 | jc = mxGetJc(prhs); 28 | samples = mxGetPr(prhs); 29 | 30 | // each column is one instance 31 | j = 0; 32 | low = (int)jc[index], high = (int)jc[index+1]; 33 | for(i=low;iparam.kernel_type == PRECOMPUTED) 95 | { 96 | // precomputed kernel requires dense matrix, so we make one 97 | mxArray *rhs[1], *lhs[1]; 98 | rhs[0] = mxDuplicateArray(prhs[1]); 99 | if(mexCallMATLAB(1, lhs, 1, rhs, "full")) 100 | { 101 | mexPrintf("Error: cannot full testing instance matrix\n"); 102 | fake_answer(nlhs, plhs); 103 | return; 104 | } 105 | ptr_instance = mxGetPr(lhs[0]); 106 | mxDestroyArray(rhs[0]); 107 | } 108 | else 109 | { 110 | mxArray *pprhs[1]; 111 | pprhs[0] = mxDuplicateArray(prhs[1]); 112 | if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) 113 | { 114 | mexPrintf("Error: cannot transpose testing instance matrix\n"); 115 | fake_answer(nlhs, plhs); 116 | return; 117 | } 118 | } 119 | } 120 | 121 | if(predict_probability) 122 | { 123 | if(svm_type==NU_SVR || svm_type==EPSILON_SVR) 124 | info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model)); 125 | else 126 | prob_estimates = (double *) malloc(nr_class*sizeof(double)); 127 | } 128 | 129 | tplhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); 130 | if(predict_probability) 131 | { 132 | // prob estimates are in plhs[2] 133 | if(svm_type==C_SVC || svm_type==NU_SVC) 134 | tplhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL); 135 | else 136 | tplhs[2] = mxCreateDoubleMatrix(0, 0, mxREAL); 137 | } 138 | else 139 | { 140 | // decision values are in plhs[2] 141 | if(svm_type == ONE_CLASS || 142 | svm_type == EPSILON_SVR || 143 | svm_type == NU_SVR || 144 | nr_class == 1) // if only one class in training data, decision values are still returned. 145 | tplhs[2] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); 146 | else 147 | tplhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class*(nr_class-1)/2, mxREAL); 148 | } 149 | 150 | ptr_predict_label = mxGetPr(tplhs[0]); 151 | ptr_prob_estimates = mxGetPr(tplhs[2]); 152 | ptr_dec_values = mxGetPr(tplhs[2]); 153 | x = (struct svm_node*)malloc((feature_number+1)*sizeof(struct svm_node) ); 154 | for(instance_index=0;instance_indexparam.kernel_type != PRECOMPUTED) // prhs[1]^T is still sparse 162 | read_sparse_instance(pplhs[0], instance_index, x); 163 | else 164 | { 165 | for(i=0;i 3 || nrhs > 4 || nrhs < 3) 280 | { 281 | exit_with_help(); 282 | fake_answer(nlhs, plhs); 283 | return; 284 | } 285 | 286 | if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { 287 | mexPrintf("Error: label vector and instance matrix must be double\n"); 288 | fake_answer(nlhs, plhs); 289 | return; 290 | } 291 | 292 | if(mxIsStruct(prhs[2])) 293 | { 294 | const char *error_msg; 295 | 296 | // parse options 297 | if(nrhs==4) 298 | { 299 | int i, argc = 1; 300 | char cmd[CMD_LEN], *argv[CMD_LEN/2]; 301 | 302 | // put options in argv[] 303 | mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1); 304 | if((argv[argc] = strtok(cmd, " ")) != NULL) 305 | while((argv[++argc] = strtok(NULL, " ")) != NULL) 306 | ; 307 | 308 | for(i=1;i=argc) && argv[i-1][1] != 'q') 312 | { 313 | exit_with_help(); 314 | fake_answer(nlhs, plhs); 315 | return; 316 | } 317 | switch(argv[i-1][1]) 318 | { 319 | case 'b': 320 | prob_estimate_flag = atoi(argv[i]); 321 | break; 322 | case 'q': 323 | i--; 324 | info = &print_null; 325 | break; 326 | default: 327 | mexPrintf("Unknown option: -%c\n", argv[i-1][1]); 328 | exit_with_help(); 329 | fake_answer(nlhs, plhs); 330 | return; 331 | } 332 | } 333 | } 334 | 335 | model = matlab_matrix_to_model(prhs[2], &error_msg); 336 | if (model == NULL) 337 | { 338 | mexPrintf("Error: can't read model: %s\n", error_msg); 339 | fake_answer(nlhs, plhs); 340 | return; 341 | } 342 | 343 | if(prob_estimate_flag) 344 | { 345 | if(svm_check_probability_model(model)==0) 346 | { 347 | mexPrintf("Model does not support probabiliy estimates\n"); 348 | fake_answer(nlhs, plhs); 349 | svm_free_and_destroy_model(&model); 350 | return; 351 | } 352 | } 353 | else 354 | { 355 | if(svm_check_probability_model(model)!=0) 356 | info("Model supports probability estimates, but disabled in predicton.\n"); 357 | } 358 | 359 | predict(nlhs, plhs, prhs, model, prob_estimate_flag); 360 | // destroy model 361 | svm_free_and_destroy_model(&model); 362 | } 363 | else 364 | { 365 | mexPrintf("model file should be a struct array\n"); 366 | fake_answer(nlhs, plhs); 367 | } 368 | 369 | return; 370 | } 371 | -------------------------------------------------------------------------------- /libsvm-3.22/matlab/svmpredict.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/matlab/svmpredict.mexw64 -------------------------------------------------------------------------------- /libsvm-3.22/matlab/svmtrain.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/matlab/svmtrain.mexw64 -------------------------------------------------------------------------------- /libsvm-3.22/python/Makefile: -------------------------------------------------------------------------------- 1 | all = lib 2 | 3 | lib: 4 | make -C .. lib 5 | -------------------------------------------------------------------------------- /libsvm-3.22/python/svm.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from ctypes import * 4 | from ctypes.util import find_library 5 | from os import path 6 | import sys 7 | 8 | if sys.version_info[0] >= 3: 9 | xrange = range 10 | 11 | __all__ = ['libsvm', 'svm_problem', 'svm_parameter', 12 | 'toPyModel', 'gen_svm_nodearray', 'print_null', 'svm_node', 'C_SVC', 13 | 'EPSILON_SVR', 'LINEAR', 'NU_SVC', 'NU_SVR', 'ONE_CLASS', 14 | 'POLY', 'PRECOMPUTED', 'PRINT_STRING_FUN', 'RBF', 15 | 'SIGMOID', 'c_double', 'svm_model'] 16 | 17 | try: 18 | dirname = path.dirname(path.abspath(__file__)) 19 | if sys.platform == 'win32': 20 | libsvm = CDLL(path.join(dirname, r'..\windows\libsvm.dll')) 21 | else: 22 | libsvm = CDLL(path.join(dirname, '../libsvm.so.2')) 23 | except: 24 | # For unix the prefix 'lib' is not considered. 25 | if find_library('svm'): 26 | libsvm = CDLL(find_library('svm')) 27 | elif find_library('libsvm'): 28 | libsvm = CDLL(find_library('libsvm')) 29 | else: 30 | raise Exception('LIBSVM library not found.') 31 | 32 | C_SVC = 0 33 | NU_SVC = 1 34 | ONE_CLASS = 2 35 | EPSILON_SVR = 3 36 | NU_SVR = 4 37 | 38 | LINEAR = 0 39 | POLY = 1 40 | RBF = 2 41 | SIGMOID = 3 42 | PRECOMPUTED = 4 43 | 44 | PRINT_STRING_FUN = CFUNCTYPE(None, c_char_p) 45 | def print_null(s): 46 | return 47 | 48 | def genFields(names, types): 49 | return list(zip(names, types)) 50 | 51 | def fillprototype(f, restype, argtypes): 52 | f.restype = restype 53 | f.argtypes = argtypes 54 | 55 | class svm_node(Structure): 56 | _names = ["index", "value"] 57 | _types = [c_int, c_double] 58 | _fields_ = genFields(_names, _types) 59 | 60 | def __str__(self): 61 | return '%d:%g' % (self.index, self.value) 62 | 63 | def gen_svm_nodearray(xi, feature_max=None, isKernel=None): 64 | if isinstance(xi, dict): 65 | index_range = xi.keys() 66 | elif isinstance(xi, (list, tuple)): 67 | if not isKernel: 68 | xi = [0] + xi # idx should start from 1 69 | index_range = range(len(xi)) 70 | else: 71 | raise TypeError('xi should be a dictionary, list or tuple') 72 | 73 | if feature_max: 74 | assert(isinstance(feature_max, int)) 75 | index_range = filter(lambda j: j <= feature_max, index_range) 76 | if not isKernel: 77 | index_range = filter(lambda j:xi[j] != 0, index_range) 78 | 79 | index_range = sorted(index_range) 80 | ret = (svm_node * (len(index_range)+1))() 81 | ret[-1].index = -1 82 | for idx, j in enumerate(index_range): 83 | ret[idx].index = j 84 | ret[idx].value = xi[j] 85 | max_idx = 0 86 | if index_range: 87 | max_idx = index_range[-1] 88 | return ret, max_idx 89 | 90 | class svm_problem(Structure): 91 | _names = ["l", "y", "x"] 92 | _types = [c_int, POINTER(c_double), POINTER(POINTER(svm_node))] 93 | _fields_ = genFields(_names, _types) 94 | 95 | def __init__(self, y, x, isKernel=None): 96 | if len(y) != len(x): 97 | raise ValueError("len(y) != len(x)") 98 | self.l = l = len(y) 99 | 100 | max_idx = 0 101 | x_space = self.x_space = [] 102 | for i, xi in enumerate(x): 103 | tmp_xi, tmp_idx = gen_svm_nodearray(xi,isKernel=isKernel) 104 | x_space += [tmp_xi] 105 | max_idx = max(max_idx, tmp_idx) 106 | self.n = max_idx 107 | 108 | self.y = (c_double * l)() 109 | for i, yi in enumerate(y): self.y[i] = yi 110 | 111 | self.x = (POINTER(svm_node) * l)() 112 | for i, xi in enumerate(self.x_space): self.x[i] = xi 113 | 114 | class svm_parameter(Structure): 115 | _names = ["svm_type", "kernel_type", "degree", "gamma", "coef0", 116 | "cache_size", "eps", "C", "nr_weight", "weight_label", "weight", 117 | "nu", "p", "shrinking", "probability"] 118 | _types = [c_int, c_int, c_int, c_double, c_double, 119 | c_double, c_double, c_double, c_int, POINTER(c_int), POINTER(c_double), 120 | c_double, c_double, c_int, c_int] 121 | _fields_ = genFields(_names, _types) 122 | 123 | def __init__(self, options = None): 124 | if options == None: 125 | options = '' 126 | self.parse_options(options) 127 | 128 | def __str__(self): 129 | s = '' 130 | attrs = svm_parameter._names + list(self.__dict__.keys()) 131 | values = map(lambda attr: getattr(self, attr), attrs) 132 | for attr, val in zip(attrs, values): 133 | s += (' %s: %s\n' % (attr, val)) 134 | s = s.strip() 135 | 136 | return s 137 | 138 | def set_to_default_values(self): 139 | self.svm_type = C_SVC; 140 | self.kernel_type = RBF 141 | self.degree = 3 142 | self.gamma = 0 143 | self.coef0 = 0 144 | self.nu = 0.5 145 | self.cache_size = 100 146 | self.C = 1 147 | self.eps = 0.001 148 | self.p = 0.1 149 | self.shrinking = 1 150 | self.probability = 0 151 | self.nr_weight = 0 152 | self.weight_label = None 153 | self.weight = None 154 | self.cross_validation = False 155 | self.nr_fold = 0 156 | self.print_func = cast(None, PRINT_STRING_FUN) 157 | 158 | def parse_options(self, options): 159 | if isinstance(options, list): 160 | argv = options 161 | elif isinstance(options, str): 162 | argv = options.split() 163 | else: 164 | raise TypeError("arg 1 should be a list or a str.") 165 | self.set_to_default_values() 166 | self.print_func = cast(None, PRINT_STRING_FUN) 167 | weight_label = [] 168 | weight = [] 169 | 170 | i = 0 171 | while i < len(argv): 172 | if argv[i] == "-s": 173 | i = i + 1 174 | self.svm_type = int(argv[i]) 175 | elif argv[i] == "-t": 176 | i = i + 1 177 | self.kernel_type = int(argv[i]) 178 | elif argv[i] == "-d": 179 | i = i + 1 180 | self.degree = int(argv[i]) 181 | elif argv[i] == "-g": 182 | i = i + 1 183 | self.gamma = float(argv[i]) 184 | elif argv[i] == "-r": 185 | i = i + 1 186 | self.coef0 = float(argv[i]) 187 | elif argv[i] == "-n": 188 | i = i + 1 189 | self.nu = float(argv[i]) 190 | elif argv[i] == "-m": 191 | i = i + 1 192 | self.cache_size = float(argv[i]) 193 | elif argv[i] == "-c": 194 | i = i + 1 195 | self.C = float(argv[i]) 196 | elif argv[i] == "-e": 197 | i = i + 1 198 | self.eps = float(argv[i]) 199 | elif argv[i] == "-p": 200 | i = i + 1 201 | self.p = float(argv[i]) 202 | elif argv[i] == "-h": 203 | i = i + 1 204 | self.shrinking = int(argv[i]) 205 | elif argv[i] == "-b": 206 | i = i + 1 207 | self.probability = int(argv[i]) 208 | elif argv[i] == "-q": 209 | self.print_func = PRINT_STRING_FUN(print_null) 210 | elif argv[i] == "-v": 211 | i = i + 1 212 | self.cross_validation = 1 213 | self.nr_fold = int(argv[i]) 214 | if self.nr_fold < 2: 215 | raise ValueError("n-fold cross validation: n must >= 2") 216 | elif argv[i].startswith("-w"): 217 | i = i + 1 218 | self.nr_weight += 1 219 | weight_label += [int(argv[i-1][2:])] 220 | weight += [float(argv[i])] 221 | else: 222 | raise ValueError("Wrong options") 223 | i += 1 224 | 225 | libsvm.svm_set_print_string_function(self.print_func) 226 | self.weight_label = (c_int*self.nr_weight)() 227 | self.weight = (c_double*self.nr_weight)() 228 | for i in range(self.nr_weight): 229 | self.weight[i] = weight[i] 230 | self.weight_label[i] = weight_label[i] 231 | 232 | class svm_model(Structure): 233 | _names = ['param', 'nr_class', 'l', 'SV', 'sv_coef', 'rho', 234 | 'probA', 'probB', 'sv_indices', 'label', 'nSV', 'free_sv'] 235 | _types = [svm_parameter, c_int, c_int, POINTER(POINTER(svm_node)), 236 | POINTER(POINTER(c_double)), POINTER(c_double), 237 | POINTER(c_double), POINTER(c_double), POINTER(c_int), 238 | POINTER(c_int), POINTER(c_int), c_int] 239 | _fields_ = genFields(_names, _types) 240 | 241 | def __init__(self): 242 | self.__createfrom__ = 'python' 243 | 244 | def __del__(self): 245 | # free memory created by C to avoid memory leak 246 | if hasattr(self, '__createfrom__') and self.__createfrom__ == 'C': 247 | libsvm.svm_free_and_destroy_model(pointer(self)) 248 | 249 | def get_svm_type(self): 250 | return libsvm.svm_get_svm_type(self) 251 | 252 | def get_nr_class(self): 253 | return libsvm.svm_get_nr_class(self) 254 | 255 | def get_svr_probability(self): 256 | return libsvm.svm_get_svr_probability(self) 257 | 258 | def get_labels(self): 259 | nr_class = self.get_nr_class() 260 | labels = (c_int * nr_class)() 261 | libsvm.svm_get_labels(self, labels) 262 | return labels[:nr_class] 263 | 264 | def get_sv_indices(self): 265 | total_sv = self.get_nr_sv() 266 | sv_indices = (c_int * total_sv)() 267 | libsvm.svm_get_sv_indices(self, sv_indices) 268 | return sv_indices[:total_sv] 269 | 270 | def get_nr_sv(self): 271 | return libsvm.svm_get_nr_sv(self) 272 | 273 | def is_probability_model(self): 274 | return (libsvm.svm_check_probability_model(self) == 1) 275 | 276 | def get_sv_coef(self): 277 | return [tuple(self.sv_coef[j][i] for j in xrange(self.nr_class - 1)) 278 | for i in xrange(self.l)] 279 | 280 | def get_SV(self): 281 | result = [] 282 | for sparse_sv in self.SV[:self.l]: 283 | row = dict() 284 | 285 | i = 0 286 | while True: 287 | row[sparse_sv[i].index] = sparse_sv[i].value 288 | if sparse_sv[i].index == -1: 289 | break 290 | i += 1 291 | 292 | result.append(row) 293 | return result 294 | 295 | def toPyModel(model_ptr): 296 | """ 297 | toPyModel(model_ptr) -> svm_model 298 | 299 | Convert a ctypes POINTER(svm_model) to a Python svm_model 300 | """ 301 | if bool(model_ptr) == False: 302 | raise ValueError("Null pointer") 303 | m = model_ptr.contents 304 | m.__createfrom__ = 'C' 305 | return m 306 | 307 | fillprototype(libsvm.svm_train, POINTER(svm_model), [POINTER(svm_problem), POINTER(svm_parameter)]) 308 | fillprototype(libsvm.svm_cross_validation, None, [POINTER(svm_problem), POINTER(svm_parameter), c_int, POINTER(c_double)]) 309 | 310 | fillprototype(libsvm.svm_save_model, c_int, [c_char_p, POINTER(svm_model)]) 311 | fillprototype(libsvm.svm_load_model, POINTER(svm_model), [c_char_p]) 312 | 313 | fillprototype(libsvm.svm_get_svm_type, c_int, [POINTER(svm_model)]) 314 | fillprototype(libsvm.svm_get_nr_class, c_int, [POINTER(svm_model)]) 315 | fillprototype(libsvm.svm_get_labels, None, [POINTER(svm_model), POINTER(c_int)]) 316 | fillprototype(libsvm.svm_get_sv_indices, None, [POINTER(svm_model), POINTER(c_int)]) 317 | fillprototype(libsvm.svm_get_nr_sv, c_int, [POINTER(svm_model)]) 318 | fillprototype(libsvm.svm_get_svr_probability, c_double, [POINTER(svm_model)]) 319 | 320 | fillprototype(libsvm.svm_predict_values, c_double, [POINTER(svm_model), POINTER(svm_node), POINTER(c_double)]) 321 | fillprototype(libsvm.svm_predict, c_double, [POINTER(svm_model), POINTER(svm_node)]) 322 | fillprototype(libsvm.svm_predict_probability, c_double, [POINTER(svm_model), POINTER(svm_node), POINTER(c_double)]) 323 | 324 | fillprototype(libsvm.svm_free_model_content, None, [POINTER(svm_model)]) 325 | fillprototype(libsvm.svm_free_and_destroy_model, None, [POINTER(POINTER(svm_model))]) 326 | fillprototype(libsvm.svm_destroy_param, None, [POINTER(svm_parameter)]) 327 | 328 | fillprototype(libsvm.svm_check_parameter, c_char_p, [POINTER(svm_problem), POINTER(svm_parameter)]) 329 | fillprototype(libsvm.svm_check_probability_model, c_int, [POINTER(svm_model)]) 330 | fillprototype(libsvm.svm_set_print_string_function, None, [PRINT_STRING_FUN]) 331 | -------------------------------------------------------------------------------- /libsvm-3.22/python/svmutil.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os 4 | import sys 5 | from svm import * 6 | from svm import __all__ as svm_all 7 | 8 | 9 | __all__ = ['evaluations', 'svm_load_model', 'svm_predict', 'svm_read_problem', 10 | 'svm_save_model', 'svm_train'] + svm_all 11 | 12 | sys.path = [os.path.dirname(os.path.abspath(__file__))] + sys.path 13 | 14 | def svm_read_problem(data_file_name): 15 | """ 16 | svm_read_problem(data_file_name) -> [y, x] 17 | 18 | Read LIBSVM-format data from data_file_name and return labels y 19 | and data instances x. 20 | """ 21 | prob_y = [] 22 | prob_x = [] 23 | for line in open(data_file_name): 24 | line = line.split(None, 1) 25 | # In case an instance with all zero features 26 | if len(line) == 1: line += [''] 27 | label, features = line 28 | xi = {} 29 | for e in features.split(): 30 | ind, val = e.split(":") 31 | xi[int(ind)] = float(val) 32 | prob_y += [float(label)] 33 | prob_x += [xi] 34 | return (prob_y, prob_x) 35 | 36 | def svm_load_model(model_file_name): 37 | """ 38 | svm_load_model(model_file_name) -> model 39 | 40 | Load a LIBSVM model from model_file_name and return. 41 | """ 42 | model = libsvm.svm_load_model(model_file_name.encode()) 43 | if not model: 44 | print("can't open model file %s" % model_file_name) 45 | return None 46 | model = toPyModel(model) 47 | return model 48 | 49 | def svm_save_model(model_file_name, model): 50 | """ 51 | svm_save_model(model_file_name, model) -> None 52 | 53 | Save a LIBSVM model to the file model_file_name. 54 | """ 55 | libsvm.svm_save_model(model_file_name.encode(), model) 56 | 57 | def evaluations(ty, pv): 58 | """ 59 | evaluations(ty, pv) -> (ACC, MSE, SCC) 60 | 61 | Calculate accuracy, mean squared error and squared correlation coefficient 62 | using the true values (ty) and predicted values (pv). 63 | """ 64 | if len(ty) != len(pv): 65 | raise ValueError("len(ty) must equal to len(pv)") 66 | total_correct = total_error = 0 67 | sumv = sumy = sumvv = sumyy = sumvy = 0 68 | for v, y in zip(pv, ty): 69 | if y == v: 70 | total_correct += 1 71 | total_error += (v-y)*(v-y) 72 | sumv += v 73 | sumy += y 74 | sumvv += v*v 75 | sumyy += y*y 76 | sumvy += v*y 77 | l = len(ty) 78 | ACC = 100.0*total_correct/l 79 | MSE = total_error/l 80 | try: 81 | SCC = ((l*sumvy-sumv*sumy)*(l*sumvy-sumv*sumy))/((l*sumvv-sumv*sumv)*(l*sumyy-sumy*sumy)) 82 | except: 83 | SCC = float('nan') 84 | return (ACC, MSE, SCC) 85 | 86 | def svm_train(arg1, arg2=None, arg3=None): 87 | """ 88 | svm_train(y, x [, options]) -> model | ACC | MSE 89 | svm_train(prob [, options]) -> model | ACC | MSE 90 | svm_train(prob, param) -> model | ACC| MSE 91 | 92 | Train an SVM model from data (y, x) or an svm_problem prob using 93 | 'options' or an svm_parameter param. 94 | If '-v' is specified in 'options' (i.e., cross validation) 95 | either accuracy (ACC) or mean-squared error (MSE) is returned. 96 | options: 97 | -s svm_type : set type of SVM (default 0) 98 | 0 -- C-SVC (multi-class classification) 99 | 1 -- nu-SVC (multi-class classification) 100 | 2 -- one-class SVM 101 | 3 -- epsilon-SVR (regression) 102 | 4 -- nu-SVR (regression) 103 | -t kernel_type : set type of kernel function (default 2) 104 | 0 -- linear: u'*v 105 | 1 -- polynomial: (gamma*u'*v + coef0)^degree 106 | 2 -- radial basis function: exp(-gamma*|u-v|^2) 107 | 3 -- sigmoid: tanh(gamma*u'*v + coef0) 108 | 4 -- precomputed kernel (kernel values in training_set_file) 109 | -d degree : set degree in kernel function (default 3) 110 | -g gamma : set gamma in kernel function (default 1/num_features) 111 | -r coef0 : set coef0 in kernel function (default 0) 112 | -c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1) 113 | -n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5) 114 | -p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1) 115 | -m cachesize : set cache memory size in MB (default 100) 116 | -e epsilon : set tolerance of termination criterion (default 0.001) 117 | -h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1) 118 | -b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0) 119 | -wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1) 120 | -v n: n-fold cross validation mode 121 | -q : quiet mode (no outputs) 122 | """ 123 | prob, param = None, None 124 | if isinstance(arg1, (list, tuple)): 125 | assert isinstance(arg2, (list, tuple)) 126 | y, x, options = arg1, arg2, arg3 127 | param = svm_parameter(options) 128 | prob = svm_problem(y, x, isKernel=(param.kernel_type == PRECOMPUTED)) 129 | elif isinstance(arg1, svm_problem): 130 | prob = arg1 131 | if isinstance(arg2, svm_parameter): 132 | param = arg2 133 | else: 134 | param = svm_parameter(arg2) 135 | if prob == None or param == None: 136 | raise TypeError("Wrong types for the arguments") 137 | 138 | if param.kernel_type == PRECOMPUTED: 139 | for xi in prob.x_space: 140 | idx, val = xi[0].index, xi[0].value 141 | if xi[0].index != 0: 142 | raise ValueError('Wrong input format: first column must be 0:sample_serial_number') 143 | if val <= 0 or val > prob.n: 144 | raise ValueError('Wrong input format: sample_serial_number out of range') 145 | 146 | if param.gamma == 0 and prob.n > 0: 147 | param.gamma = 1.0 / prob.n 148 | libsvm.svm_set_print_string_function(param.print_func) 149 | err_msg = libsvm.svm_check_parameter(prob, param) 150 | if err_msg: 151 | raise ValueError('Error: %s' % err_msg) 152 | 153 | if param.cross_validation: 154 | l, nr_fold = prob.l, param.nr_fold 155 | target = (c_double * l)() 156 | libsvm.svm_cross_validation(prob, param, nr_fold, target) 157 | ACC, MSE, SCC = evaluations(prob.y[:l], target[:l]) 158 | if param.svm_type in [EPSILON_SVR, NU_SVR]: 159 | print("Cross Validation Mean squared error = %g" % MSE) 160 | print("Cross Validation Squared correlation coefficient = %g" % SCC) 161 | return MSE 162 | else: 163 | print("Cross Validation Accuracy = %g%%" % ACC) 164 | return ACC 165 | else: 166 | m = libsvm.svm_train(prob, param) 167 | m = toPyModel(m) 168 | 169 | # If prob is destroyed, data including SVs pointed by m can remain. 170 | m.x_space = prob.x_space 171 | return m 172 | 173 | def svm_predict(y, x, m, options=""): 174 | """ 175 | svm_predict(y, x, m [, options]) -> (p_labels, p_acc, p_vals) 176 | 177 | Predict data (y, x) with the SVM model m. 178 | options: 179 | -b probability_estimates: whether to predict probability estimates, 180 | 0 or 1 (default 0); for one-class SVM only 0 is supported. 181 | -q : quiet mode (no outputs). 182 | 183 | The return tuple contains 184 | p_labels: a list of predicted labels 185 | p_acc: a tuple including accuracy (for classification), mean-squared 186 | error, and squared correlation coefficient (for regression). 187 | p_vals: a list of decision values or probability estimates (if '-b 1' 188 | is specified). If k is the number of classes, for decision values, 189 | each element includes results of predicting k(k-1)/2 binary-class 190 | SVMs. For probabilities, each element contains k values indicating 191 | the probability that the testing instance is in each class. 192 | Note that the order of classes here is the same as 'model.label' 193 | field in the model structure. 194 | """ 195 | 196 | def info(s): 197 | print(s) 198 | 199 | predict_probability = 0 200 | argv = options.split() 201 | i = 0 202 | while i < len(argv): 203 | if argv[i] == '-b': 204 | i += 1 205 | predict_probability = int(argv[i]) 206 | elif argv[i] == '-q': 207 | info = print_null 208 | else: 209 | raise ValueError("Wrong options") 210 | i+=1 211 | 212 | svm_type = m.get_svm_type() 213 | is_prob_model = m.is_probability_model() 214 | nr_class = m.get_nr_class() 215 | pred_labels = [] 216 | pred_values = [] 217 | 218 | if predict_probability: 219 | if not is_prob_model: 220 | raise ValueError("Model does not support probabiliy estimates") 221 | 222 | if svm_type in [NU_SVR, EPSILON_SVR]: 223 | info("Prob. model for test data: target value = predicted value + z,\n" 224 | "z: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g" % m.get_svr_probability()); 225 | nr_class = 0 226 | 227 | prob_estimates = (c_double * nr_class)() 228 | for xi in x: 229 | xi, idx = gen_svm_nodearray(xi, isKernel=(m.param.kernel_type == PRECOMPUTED)) 230 | label = libsvm.svm_predict_probability(m, xi, prob_estimates) 231 | values = prob_estimates[:nr_class] 232 | pred_labels += [label] 233 | pred_values += [values] 234 | else: 235 | if is_prob_model: 236 | info("Model supports probability estimates, but disabled in predicton.") 237 | if svm_type in (ONE_CLASS, EPSILON_SVR, NU_SVC): 238 | nr_classifier = 1 239 | else: 240 | nr_classifier = nr_class*(nr_class-1)//2 241 | dec_values = (c_double * nr_classifier)() 242 | for xi in x: 243 | xi, idx = gen_svm_nodearray(xi, isKernel=(m.param.kernel_type == PRECOMPUTED)) 244 | label = libsvm.svm_predict_values(m, xi, dec_values) 245 | if(nr_class == 1): 246 | values = [1] 247 | else: 248 | values = dec_values[:nr_classifier] 249 | pred_labels += [label] 250 | pred_values += [values] 251 | 252 | ACC, MSE, SCC = evaluations(y, pred_labels) 253 | l = len(y) 254 | if svm_type in [EPSILON_SVR, NU_SVR]: 255 | info("Mean squared error = %g (regression)" % MSE) 256 | info("Squared correlation coefficient = %g (regression)" % SCC) 257 | else: 258 | info("Accuracy = %g%% (%d/%d) (classification)" % (ACC, int(l*ACC/100), l)) 259 | 260 | return pred_labels, (ACC, MSE, SCC), pred_values 261 | 262 | 263 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-predict.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "svm.h" 7 | 8 | int print_null(const char *s,...) {return 0;} 9 | 10 | static int (*info)(const char *fmt,...) = &printf; 11 | 12 | struct svm_node *x; 13 | int max_nr_attr = 64; 14 | 15 | struct svm_model* model; 16 | int predict_probability=0; 17 | 18 | static char *line = NULL; 19 | static int max_line_len; 20 | 21 | static char* readline(FILE *input) 22 | { 23 | int len; 24 | 25 | if(fgets(line,max_line_len,input) == NULL) 26 | return NULL; 27 | 28 | while(strrchr(line,'\n') == NULL) 29 | { 30 | max_line_len *= 2; 31 | line = (char *) realloc(line,max_line_len); 32 | len = (int) strlen(line); 33 | if(fgets(line+len,max_line_len-len,input) == NULL) 34 | break; 35 | } 36 | return line; 37 | } 38 | 39 | void exit_input_error(int line_num) 40 | { 41 | fprintf(stderr,"Wrong input format at line %d\n", line_num); 42 | exit(1); 43 | } 44 | 45 | void predict(FILE *input, FILE *output) 46 | { 47 | int correct = 0; 48 | int total = 0; 49 | double error = 0; 50 | double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0; 51 | 52 | int svm_type=svm_get_svm_type(model); 53 | int nr_class=svm_get_nr_class(model); 54 | double *prob_estimates=NULL; 55 | int j; 56 | 57 | if(predict_probability) 58 | { 59 | if (svm_type==NU_SVR || svm_type==EPSILON_SVR) 60 | info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model)); 61 | else 62 | { 63 | int *labels=(int *) malloc(nr_class*sizeof(int)); 64 | svm_get_labels(model,labels); 65 | prob_estimates = (double *) malloc(nr_class*sizeof(double)); 66 | fprintf(output,"labels"); 67 | for(j=0;j start from 0 82 | 83 | label = strtok(line," \t\n"); 84 | if(label == NULL) // empty line 85 | exit_input_error(total+1); 86 | 87 | target_label = strtod(label,&endptr); 88 | if(endptr == label || *endptr != '\0') 89 | exit_input_error(total+1); 90 | 91 | while(1) 92 | { 93 | if(i>=max_nr_attr-1) // need one more for index = -1 94 | { 95 | max_nr_attr *= 2; 96 | x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node)); 97 | } 98 | 99 | idx = strtok(NULL,":"); 100 | val = strtok(NULL," \t"); 101 | 102 | if(val == NULL) 103 | break; 104 | errno = 0; 105 | x[i].index = (int) strtol(idx,&endptr,10); 106 | if(endptr == idx || errno != 0 || *endptr != '\0' || x[i].index <= inst_max_index) 107 | exit_input_error(total+1); 108 | else 109 | inst_max_index = x[i].index; 110 | 111 | errno = 0; 112 | x[i].value = strtod(val,&endptr); 113 | if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) 114 | exit_input_error(total+1); 115 | 116 | ++i; 117 | } 118 | x[i].index = -1; 119 | 120 | if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC)) 121 | { 122 | predict_label = svm_predict_probability(model,x,prob_estimates); 123 | fprintf(output,"%g",predict_label); 124 | for(j=0;j=argc-2) 195 | exit_with_help(); 196 | 197 | input = fopen(argv[i],"r"); 198 | if(input == NULL) 199 | { 200 | fprintf(stderr,"can't open input file %s\n",argv[i]); 201 | exit(1); 202 | } 203 | 204 | output = fopen(argv[i+2],"w"); 205 | if(output == NULL) 206 | { 207 | fprintf(stderr,"can't open output file %s\n",argv[i+2]); 208 | exit(1); 209 | } 210 | 211 | if((model=svm_load_model(argv[i+1]))==0) 212 | { 213 | fprintf(stderr,"can't open model file %s\n",argv[i+1]); 214 | exit(1); 215 | } 216 | 217 | x = (struct svm_node *) malloc(max_nr_attr*sizeof(struct svm_node)); 218 | if(predict_probability) 219 | { 220 | if(svm_check_probability_model(model)==0) 221 | { 222 | fprintf(stderr,"Model does not support probabiliy estimates\n"); 223 | exit(1); 224 | } 225 | } 226 | else 227 | { 228 | if(svm_check_probability_model(model)!=0) 229 | info("Model supports probability estimates, but disabled in prediction.\n"); 230 | } 231 | 232 | predict(input,output); 233 | svm_free_and_destroy_model(&model); 234 | free(x); 235 | free(line); 236 | fclose(input); 237 | fclose(output); 238 | return 0; 239 | } 240 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-scale.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void exit_with_help() 8 | { 9 | printf( 10 | "Usage: svm-scale [options] data_filename\n" 11 | "options:\n" 12 | "-l lower : x scaling lower limit (default -1)\n" 13 | "-u upper : x scaling upper limit (default +1)\n" 14 | "-y y_lower y_upper : y scaling limits (default: no y scaling)\n" 15 | "-s save_filename : save scaling parameters to save_filename\n" 16 | "-r restore_filename : restore scaling parameters from restore_filename\n" 17 | ); 18 | exit(1); 19 | } 20 | 21 | char *line = NULL; 22 | int max_line_len = 1024; 23 | double lower=-1.0,upper=1.0,y_lower,y_upper; 24 | int y_scaling = 0; 25 | double *feature_max; 26 | double *feature_min; 27 | double y_max = -DBL_MAX; 28 | double y_min = DBL_MAX; 29 | int max_index; 30 | int min_index; 31 | long int num_nonzeros = 0; 32 | long int new_num_nonzeros = 0; 33 | 34 | #define max(x,y) (((x)>(y))?(x):(y)) 35 | #define min(x,y) (((x)<(y))?(x):(y)) 36 | 37 | void output_target(double value); 38 | void output(int index, double value); 39 | char* readline(FILE *input); 40 | int clean_up(FILE *fp_restore, FILE *fp, const char *msg); 41 | 42 | int main(int argc,char **argv) 43 | { 44 | int i,index; 45 | FILE *fp, *fp_restore = NULL; 46 | char *save_filename = NULL; 47 | char *restore_filename = NULL; 48 | 49 | for(i=1;i lower) || (y_scaling && !(y_upper > y_lower))) 72 | { 73 | fprintf(stderr,"inconsistent lower/upper specification\n"); 74 | exit(1); 75 | } 76 | 77 | if(restore_filename && save_filename) 78 | { 79 | fprintf(stderr,"cannot use -r and -s simultaneously\n"); 80 | exit(1); 81 | } 82 | 83 | if(argc != i+1) 84 | exit_with_help(); 85 | 86 | fp=fopen(argv[i],"r"); 87 | 88 | if(fp==NULL) 89 | { 90 | fprintf(stderr,"can't open file %s\n", argv[i]); 91 | exit(1); 92 | } 93 | 94 | line = (char *) malloc(max_line_len*sizeof(char)); 95 | 96 | #define SKIP_TARGET\ 97 | while(isspace(*p)) ++p;\ 98 | while(!isspace(*p)) ++p; 99 | 100 | #define SKIP_ELEMENT\ 101 | while(*p!=':') ++p;\ 102 | ++p;\ 103 | while(isspace(*p)) ++p;\ 104 | while(*p && !isspace(*p)) ++p; 105 | 106 | /* assumption: min index of attributes is 1 */ 107 | /* pass 1: find out max index of attributes */ 108 | max_index = 0; 109 | min_index = 1; 110 | 111 | if(restore_filename) 112 | { 113 | int idx, c; 114 | 115 | fp_restore = fopen(restore_filename,"r"); 116 | if(fp_restore==NULL) 117 | { 118 | fprintf(stderr,"can't open file %s\n", restore_filename); 119 | exit(1); 120 | } 121 | 122 | c = fgetc(fp_restore); 123 | if(c == 'y') 124 | { 125 | readline(fp_restore); 126 | readline(fp_restore); 127 | readline(fp_restore); 128 | } 129 | readline(fp_restore); 130 | readline(fp_restore); 131 | 132 | while(fscanf(fp_restore,"%d %*f %*f\n",&idx) == 1) 133 | max_index = max(idx,max_index); 134 | rewind(fp_restore); 135 | } 136 | 137 | while(readline(fp)!=NULL) 138 | { 139 | char *p=line; 140 | 141 | SKIP_TARGET 142 | 143 | while(sscanf(p,"%d:%*f",&index)==1) 144 | { 145 | max_index = max(max_index, index); 146 | min_index = min(min_index, index); 147 | SKIP_ELEMENT 148 | num_nonzeros++; 149 | } 150 | } 151 | 152 | if(min_index < 1) 153 | fprintf(stderr, 154 | "WARNING: minimal feature index is %d, but indices should start from 1\n", min_index); 155 | 156 | rewind(fp); 157 | 158 | feature_max = (double *)malloc((max_index+1)* sizeof(double)); 159 | feature_min = (double *)malloc((max_index+1)* sizeof(double)); 160 | 161 | if(feature_max == NULL || feature_min == NULL) 162 | { 163 | fprintf(stderr,"can't allocate enough memory\n"); 164 | exit(1); 165 | } 166 | 167 | for(i=0;i<=max_index;i++) 168 | { 169 | feature_max[i]=-DBL_MAX; 170 | feature_min[i]=DBL_MAX; 171 | } 172 | 173 | /* pass 2: find out min/max value */ 174 | while(readline(fp)!=NULL) 175 | { 176 | char *p=line; 177 | int next_index=1; 178 | double target; 179 | double value; 180 | 181 | if (sscanf(p,"%lf",&target) != 1) 182 | return clean_up(fp_restore, fp, "ERROR: failed to read labels\n"); 183 | y_max = max(y_max,target); 184 | y_min = min(y_min,target); 185 | 186 | SKIP_TARGET 187 | 188 | while(sscanf(p,"%d:%lf",&index,&value)==2) 189 | { 190 | for(i=next_index;i num_nonzeros) 319 | fprintf(stderr, 320 | "WARNING: original #nonzeros %ld\n" 321 | " > new #nonzeros %ld\n" 322 | "If feature values are non-negative and sparse, use -l 0 rather than the default -l -1\n", 323 | num_nonzeros, new_num_nonzeros); 324 | 325 | free(line); 326 | free(feature_max); 327 | free(feature_min); 328 | fclose(fp); 329 | return 0; 330 | } 331 | 332 | char* readline(FILE *input) 333 | { 334 | int len; 335 | 336 | if(fgets(line,max_line_len,input) == NULL) 337 | return NULL; 338 | 339 | while(strrchr(line,'\n') == NULL) 340 | { 341 | max_line_len *= 2; 342 | line = (char *) realloc(line, max_line_len); 343 | len = (int) strlen(line); 344 | if(fgets(line+len,max_line_len-len,input) == NULL) 345 | break; 346 | } 347 | return line; 348 | } 349 | 350 | void output_target(double value) 351 | { 352 | if(y_scaling) 353 | { 354 | if(value == y_min) 355 | value = y_lower; 356 | else if(value == y_max) 357 | value = y_upper; 358 | else value = y_lower + (y_upper-y_lower) * 359 | (value - y_min)/(y_max-y_min); 360 | } 361 | printf("%g ",value); 362 | } 363 | 364 | void output(int index, double value) 365 | { 366 | /* skip single-valued attribute */ 367 | if(feature_max[index] == feature_min[index]) 368 | return; 369 | 370 | if(value == feature_min[index]) 371 | value = lower; 372 | else if(value == feature_max[index]) 373 | value = upper; 374 | else 375 | value = lower + (upper-lower) * 376 | (value-feature_min[index])/ 377 | (feature_max[index]-feature_min[index]); 378 | 379 | if(value != 0) 380 | { 381 | printf("%d:%g ",index, value); 382 | new_num_nonzeros++; 383 | } 384 | } 385 | 386 | int clean_up(FILE *fp_restore, FILE *fp, const char* msg) 387 | { 388 | fprintf(stderr, "%s", msg); 389 | free(line); 390 | free(feature_max); 391 | free(feature_min); 392 | fclose(fp); 393 | if (fp_restore) 394 | fclose(fp_restore); 395 | return -1; 396 | } 397 | 398 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-toy/gtk/Makefile: -------------------------------------------------------------------------------- 1 | CC? = gcc 2 | CXX? = g++ 3 | CFLAGS = -Wall -O3 -g `pkg-config --cflags gtk+-2.0` 4 | LIBS = `pkg-config --libs gtk+-2.0` 5 | 6 | svm-toy: main.o interface.o callbacks.o ../../svm.o 7 | $(CXX) $(CFLAGS) main.o interface.o callbacks.o ../../svm.o -o svm-toy $(LIBS) 8 | 9 | main.o: main.c 10 | $(CC) $(CFLAGS) -c main.c 11 | 12 | interface.o: interface.c interface.h 13 | $(CC) $(CFLAGS) -c interface.c 14 | 15 | callbacks.o: callbacks.cpp callbacks.h 16 | $(CXX) $(CFLAGS) -c callbacks.cpp 17 | 18 | ../../svm.o: ../../svm.cpp ../../svm.h 19 | make -C ../.. svm.o 20 | 21 | clean: 22 | rm -f *~ callbacks.o svm-toy main.o interface.o callbacks.o ../../svm.o 23 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-toy/gtk/callbacks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "callbacks.h" 8 | #include "interface.h" 9 | #include "../../svm.h" 10 | using namespace std; 11 | 12 | #define DEFAULT_PARAM "-t 2 -c 100" 13 | #define XLEN 500 14 | #define YLEN 500 15 | 16 | GdkColor colors[] = 17 | { 18 | {0,0,0,0}, 19 | {0,0,120<<8,120<<8}, 20 | {0,120<<8,120<<8,0}, 21 | {0,120<<8,0,120<<8}, 22 | {0,0,200<<8,200<<8}, 23 | {0,200<<8,200<<8,0}, 24 | {0,200<<8,0,200<<8}, 25 | }; 26 | 27 | GdkGC *gc; 28 | GdkPixmap *pixmap; 29 | extern "C" GtkWidget *draw_main; 30 | GtkWidget *draw_main; 31 | extern "C" GtkWidget *entry_option; 32 | GtkWidget *entry_option; 33 | 34 | typedef struct { 35 | double x, y; 36 | signed char value; 37 | } point; 38 | 39 | list point_list; 40 | int current_value = 1; 41 | 42 | extern "C" void svm_toy_initialize() 43 | { 44 | gboolean success[7]; 45 | 46 | gdk_colormap_alloc_colors( 47 | gdk_colormap_get_system(), 48 | colors, 49 | 7, 50 | FALSE, 51 | TRUE, 52 | success); 53 | 54 | gc = gdk_gc_new(draw_main->window); 55 | pixmap = gdk_pixmap_new(draw_main->window,XLEN,YLEN,-1); 56 | gdk_gc_set_foreground(gc,&colors[0]); 57 | gdk_draw_rectangle(pixmap,gc,TRUE,0,0,XLEN,YLEN); 58 | gtk_entry_set_text(GTK_ENTRY(entry_option),DEFAULT_PARAM); 59 | } 60 | 61 | void redraw_area(GtkWidget* widget, int x, int y, int w, int h) 62 | { 63 | gdk_draw_pixmap(widget->window, 64 | gc, 65 | pixmap, 66 | x,y,x,y,w,h); 67 | } 68 | 69 | void draw_point(const point& p) 70 | { 71 | gdk_gc_set_foreground(gc,&colors[p.value+3]); 72 | gdk_draw_rectangle(pixmap, gc, TRUE,int(p.x*XLEN),int(p.y*YLEN),4,4); 73 | gdk_draw_rectangle(draw_main->window, gc, TRUE,int(p.x*XLEN),int(p.y*YLEN),4,4); 74 | } 75 | 76 | void draw_all_points() 77 | { 78 | for(list::iterator p = point_list.begin(); p != point_list.end();p++) 79 | draw_point(*p); 80 | } 81 | 82 | void clear_all() 83 | { 84 | point_list.clear(); 85 | gdk_gc_set_foreground(gc,&colors[0]); 86 | gdk_draw_rectangle(pixmap,gc,TRUE,0,0,XLEN,YLEN); 87 | redraw_area(draw_main,0,0,XLEN,YLEN); 88 | } 89 | 90 | void 91 | on_button_change_clicked (GtkButton *button, 92 | gpointer user_data) 93 | { 94 | ++current_value; 95 | if(current_value > 3) current_value = 1; 96 | } 97 | 98 | void 99 | on_button_run_clicked (GtkButton *button, 100 | gpointer user_data) 101 | { 102 | // guard 103 | if(point_list.empty()) return; 104 | 105 | svm_parameter param; 106 | int i,j; 107 | 108 | // default values 109 | param.svm_type = C_SVC; 110 | param.kernel_type = RBF; 111 | param.degree = 3; 112 | param.gamma = 0; 113 | param.coef0 = 0; 114 | param.nu = 0.5; 115 | param.cache_size = 100; 116 | param.C = 1; 117 | param.eps = 1e-3; 118 | param.p = 0.1; 119 | param.shrinking = 1; 120 | param.probability = 0; 121 | param.nr_weight = 0; 122 | param.weight_label = NULL; 123 | param.weight = NULL; 124 | 125 | // parse options 126 | const char *p = gtk_entry_get_text(GTK_ENTRY(entry_option)); 127 | 128 | while (1) { 129 | while (*p && *p != '-') 130 | p++; 131 | 132 | if (*p == '\0') 133 | break; 134 | 135 | p++; 136 | switch (*p++) { 137 | case 's': 138 | param.svm_type = atoi(p); 139 | break; 140 | case 't': 141 | param.kernel_type = atoi(p); 142 | break; 143 | case 'd': 144 | param.degree = atoi(p); 145 | break; 146 | case 'g': 147 | param.gamma = atof(p); 148 | break; 149 | case 'r': 150 | param.coef0 = atof(p); 151 | break; 152 | case 'n': 153 | param.nu = atof(p); 154 | break; 155 | case 'm': 156 | param.cache_size = atof(p); 157 | break; 158 | case 'c': 159 | param.C = atof(p); 160 | break; 161 | case 'e': 162 | param.eps = atof(p); 163 | break; 164 | case 'p': 165 | param.p = atof(p); 166 | break; 167 | case 'h': 168 | param.shrinking = atoi(p); 169 | break; 170 | case 'b': 171 | param.probability = atoi(p); 172 | break; 173 | case 'w': 174 | ++param.nr_weight; 175 | param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight); 176 | param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight); 177 | param.weight_label[param.nr_weight-1] = atoi(p); 178 | while(*p && !isspace(*p)) ++p; 179 | param.weight[param.nr_weight-1] = atof(p); 180 | break; 181 | } 182 | } 183 | 184 | // build problem 185 | svm_problem prob; 186 | 187 | prob.l = point_list.size(); 188 | prob.y = new double[prob.l]; 189 | 190 | if(param.kernel_type == PRECOMPUTED) 191 | { 192 | } 193 | else if(param.svm_type == EPSILON_SVR || 194 | param.svm_type == NU_SVR) 195 | { 196 | if(param.gamma == 0) param.gamma = 1; 197 | svm_node *x_space = new svm_node[2 * prob.l]; 198 | prob.x = new svm_node *[prob.l]; 199 | 200 | i = 0; 201 | for (list ::iterator q = point_list.begin(); q != point_list.end(); q++, i++) 202 | { 203 | x_space[2 * i].index = 1; 204 | x_space[2 * i].value = q->x; 205 | x_space[2 * i + 1].index = -1; 206 | prob.x[i] = &x_space[2 * i]; 207 | prob.y[i] = q->y; 208 | } 209 | 210 | // build model & classify 211 | svm_model *model = svm_train(&prob, ¶m); 212 | svm_node x[2]; 213 | x[0].index = 1; 214 | x[1].index = -1; 215 | int *j = new int[XLEN]; 216 | 217 | for (i = 0; i < XLEN; i++) 218 | { 219 | x[0].value = (double) i / XLEN; 220 | j[i] = (int)(YLEN*svm_predict(model, x)); 221 | } 222 | 223 | gdk_gc_set_foreground(gc,&colors[0]); 224 | gdk_draw_line(pixmap,gc,0,0,0,YLEN-1); 225 | gdk_draw_line(draw_main->window,gc,0,0,0,YLEN-1); 226 | 227 | int p = (int)(param.p * YLEN); 228 | for(i = 1; i < XLEN; i++) 229 | { 230 | gdk_gc_set_foreground(gc,&colors[0]); 231 | gdk_draw_line(pixmap,gc,i,0,i,YLEN-1); 232 | gdk_draw_line(draw_main->window,gc,i,0,i,YLEN-1); 233 | 234 | gdk_gc_set_foreground(gc,&colors[5]); 235 | gdk_draw_line(pixmap,gc,i-1,j[i-1],i,j[i]); 236 | gdk_draw_line(draw_main->window,gc,i-1,j[i-1],i,j[i]); 237 | 238 | if(param.svm_type == EPSILON_SVR) 239 | { 240 | gdk_gc_set_foreground(gc,&colors[2]); 241 | gdk_draw_line(pixmap,gc,i-1,j[i-1]+p,i,j[i]+p); 242 | gdk_draw_line(draw_main->window,gc,i-1,j[i-1]+p,i,j[i]+p); 243 | 244 | gdk_gc_set_foreground(gc,&colors[2]); 245 | gdk_draw_line(pixmap,gc,i-1,j[i-1]-p,i,j[i]-p); 246 | gdk_draw_line(draw_main->window,gc,i-1,j[i-1]-p,i,j[i]-p); 247 | } 248 | } 249 | 250 | svm_free_and_destroy_model(&model); 251 | delete[] j; 252 | delete[] x_space; 253 | delete[] prob.x; 254 | delete[] prob.y; 255 | } 256 | else 257 | { 258 | if(param.gamma == 0) param.gamma = 0.5; 259 | svm_node *x_space = new svm_node[3 * prob.l]; 260 | prob.x = new svm_node *[prob.l]; 261 | 262 | i = 0; 263 | for (list ::iterator q = point_list.begin(); q != point_list.end(); q++, i++) 264 | { 265 | x_space[3 * i].index = 1; 266 | x_space[3 * i].value = q->x; 267 | x_space[3 * i + 1].index = 2; 268 | x_space[3 * i + 1].value = q->y; 269 | x_space[3 * i + 2].index = -1; 270 | prob.x[i] = &x_space[3 * i]; 271 | prob.y[i] = q->value; 272 | } 273 | 274 | // build model & classify 275 | svm_model *model = svm_train(&prob, ¶m); 276 | svm_node x[3]; 277 | x[0].index = 1; 278 | x[1].index = 2; 279 | x[2].index = -1; 280 | 281 | for (i = 0; i < XLEN; i++) 282 | for (j = 0; j < YLEN; j++) { 283 | x[0].value = (double) i / XLEN; 284 | x[1].value = (double) j / YLEN; 285 | double d = svm_predict(model, x); 286 | if (param.svm_type == ONE_CLASS && d<0) d=2; 287 | gdk_gc_set_foreground(gc,&colors[(int)d]); 288 | gdk_draw_point(pixmap,gc,i,j); 289 | gdk_draw_point(draw_main->window,gc,i,j); 290 | } 291 | 292 | svm_free_and_destroy_model(&model); 293 | delete[] x_space; 294 | delete[] prob.x; 295 | delete[] prob.y; 296 | } 297 | free(param.weight_label); 298 | free(param.weight); 299 | draw_all_points(); 300 | } 301 | 302 | void 303 | on_button_clear_clicked (GtkButton *button, 304 | gpointer user_data) 305 | { 306 | clear_all(); 307 | } 308 | 309 | void 310 | on_window1_destroy (GtkObject *object, 311 | gpointer user_data) 312 | { 313 | gtk_exit(0); 314 | } 315 | 316 | gboolean 317 | on_draw_main_button_press_event (GtkWidget *widget, 318 | GdkEventButton *event, 319 | gpointer user_data) 320 | { 321 | point p = {(double)event->x/XLEN, (double)event->y/YLEN, current_value}; 322 | point_list.push_back(p); 323 | draw_point(p); 324 | return FALSE; 325 | } 326 | 327 | gboolean 328 | on_draw_main_expose_event (GtkWidget *widget, 329 | GdkEventExpose *event, 330 | gpointer user_data) 331 | { 332 | redraw_area(widget, 333 | event->area.x, event->area.y, 334 | event->area.width, event->area.height); 335 | return FALSE; 336 | } 337 | 338 | GtkWidget *fileselection; 339 | static enum { SAVE, LOAD } fileselection_flag; 340 | 341 | void show_fileselection() 342 | { 343 | fileselection = create_fileselection(); 344 | gtk_signal_connect_object( 345 | GTK_OBJECT(GTK_FILE_SELECTION(fileselection)->ok_button), 346 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), 347 | (GtkObject *) fileselection); 348 | 349 | gtk_signal_connect_object (GTK_OBJECT 350 | (GTK_FILE_SELECTION(fileselection)->cancel_button), 351 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), 352 | (GtkObject *) fileselection); 353 | 354 | gtk_widget_show(fileselection); 355 | } 356 | 357 | void 358 | on_button_save_clicked (GtkButton *button, 359 | gpointer user_data) 360 | { 361 | fileselection_flag = SAVE; 362 | show_fileselection(); 363 | } 364 | 365 | 366 | void 367 | on_button_load_clicked (GtkButton *button, 368 | gpointer user_data) 369 | { 370 | fileselection_flag = LOAD; 371 | show_fileselection(); 372 | } 373 | 374 | void 375 | on_filesel_ok_clicked (GtkButton *button, 376 | gpointer user_data) 377 | { 378 | gtk_widget_hide(fileselection); 379 | const char *filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(fileselection)); 380 | 381 | if(fileselection_flag == SAVE) 382 | { 383 | FILE *fp = fopen(filename,"w"); 384 | 385 | const char *p = gtk_entry_get_text(GTK_ENTRY(entry_option)); 386 | const char* svm_type_str = strstr(p, "-s "); 387 | int svm_type = C_SVC; 388 | if(svm_type_str != NULL) 389 | sscanf(svm_type_str, "-s %d", &svm_type); 390 | 391 | if(fp) 392 | { 393 | if(svm_type == EPSILON_SVR || svm_type == NU_SVR) 394 | { 395 | for(list::iterator p = point_list.begin(); p != point_list.end();p++) 396 | fprintf(fp,"%f 1:%f\n", p->y, p->x); 397 | } 398 | else 399 | { 400 | for(list::iterator p = point_list.begin(); p != point_list.end();p++) 401 | fprintf(fp,"%d 1:%f 2:%f\n", p->value, p->x, p->y); 402 | } 403 | fclose(fp); 404 | } 405 | 406 | } 407 | else if(fileselection_flag == LOAD) 408 | { 409 | FILE *fp = fopen(filename,"r"); 410 | if(fp) 411 | { 412 | clear_all(); 413 | char buf[4096]; 414 | while(fgets(buf,sizeof(buf),fp)) 415 | { 416 | int v; 417 | double x,y; 418 | if(sscanf(buf,"%d%*d:%lf%*d:%lf",&v,&x,&y)==3) 419 | { 420 | point p = {x,y,v}; 421 | point_list.push_back(p); 422 | } 423 | else if(sscanf(buf,"%lf%*d:%lf",&y,&x)==2) 424 | { 425 | point p = {x,y,current_value}; 426 | point_list.push_back(p); 427 | } 428 | else 429 | break; 430 | } 431 | fclose(fp); 432 | draw_all_points(); 433 | } 434 | } 435 | } 436 | 437 | void 438 | on_fileselection_destroy (GtkObject *object, 439 | gpointer user_data) 440 | { 441 | } 442 | 443 | void 444 | on_filesel_cancel_clicked (GtkButton *button, 445 | gpointer user_data) 446 | { 447 | } 448 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-toy/gtk/callbacks.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | void 8 | on_window1_destroy (GtkObject *object, 9 | gpointer user_data); 10 | 11 | gboolean 12 | on_draw_main_button_press_event (GtkWidget *widget, 13 | GdkEventButton *event, 14 | gpointer user_data); 15 | 16 | gboolean 17 | on_draw_main_expose_event (GtkWidget *widget, 18 | GdkEventExpose *event, 19 | gpointer user_data); 20 | 21 | void 22 | on_button_change_clicked (GtkButton *button, 23 | gpointer user_data); 24 | 25 | void 26 | on_button_run_clicked (GtkButton *button, 27 | gpointer user_data); 28 | 29 | void 30 | on_button_clear_clicked (GtkButton *button, 31 | gpointer user_data); 32 | 33 | void 34 | on_button_save_clicked (GtkButton *button, 35 | gpointer user_data); 36 | 37 | void 38 | on_button_load_clicked (GtkButton *button, 39 | gpointer user_data); 40 | 41 | void 42 | on_fileselection_destroy (GtkObject *object, 43 | gpointer user_data); 44 | 45 | void 46 | on_filesel_ok_clicked (GtkButton *button, 47 | gpointer user_data); 48 | 49 | void 50 | on_filesel_cancel_clicked (GtkButton *button, 51 | gpointer user_data); 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-toy/gtk/interface.c: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT EDIT THIS FILE - it is generated by Glade. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include "callbacks.h" 14 | #include "interface.h" 15 | 16 | GtkWidget* 17 | create_window (void) 18 | { 19 | GtkWidget *window; 20 | GtkWidget *vbox1; 21 | extern GtkWidget *draw_main; 22 | GtkWidget *hbox1; 23 | GtkWidget *button_change; 24 | GtkWidget *button_run; 25 | GtkWidget *button_clear; 26 | GtkWidget *button_save; 27 | GtkWidget *button_load; 28 | extern GtkWidget *entry_option; 29 | 30 | window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 31 | gtk_object_set_data (GTK_OBJECT (window), "window", window); 32 | gtk_window_set_title (GTK_WINDOW (window), "SVM Toy"); 33 | 34 | vbox1 = gtk_vbox_new (FALSE, 0); 35 | gtk_widget_ref (vbox1); 36 | gtk_object_set_data_full (GTK_OBJECT (window), "vbox1", vbox1, 37 | (GtkDestroyNotify) gtk_widget_unref); 38 | gtk_widget_show (vbox1); 39 | gtk_container_add (GTK_CONTAINER (window), vbox1); 40 | 41 | draw_main = gtk_drawing_area_new (); 42 | gtk_widget_ref (draw_main); 43 | gtk_object_set_data_full (GTK_OBJECT (window), "draw_main", draw_main, 44 | (GtkDestroyNotify) gtk_widget_unref); 45 | gtk_widget_show (draw_main); 46 | gtk_box_pack_start (GTK_BOX (vbox1), draw_main, TRUE, TRUE, 0); 47 | gtk_widget_set_usize (draw_main, 500, 500); 48 | gtk_widget_set_events (draw_main, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK); 49 | 50 | hbox1 = gtk_hbox_new (FALSE, 0); 51 | gtk_widget_ref (hbox1); 52 | gtk_object_set_data_full (GTK_OBJECT (window), "hbox1", hbox1, 53 | (GtkDestroyNotify) gtk_widget_unref); 54 | gtk_widget_show (hbox1); 55 | gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0); 56 | 57 | button_change = gtk_button_new_with_label ("Change"); 58 | gtk_widget_ref (button_change); 59 | gtk_object_set_data_full (GTK_OBJECT (window), "button_change", button_change, 60 | (GtkDestroyNotify) gtk_widget_unref); 61 | gtk_widget_show (button_change); 62 | gtk_box_pack_start (GTK_BOX (hbox1), button_change, FALSE, FALSE, 0); 63 | 64 | button_run = gtk_button_new_with_label ("Run"); 65 | gtk_widget_ref (button_run); 66 | gtk_object_set_data_full (GTK_OBJECT (window), "button_run", button_run, 67 | (GtkDestroyNotify) gtk_widget_unref); 68 | gtk_widget_show (button_run); 69 | gtk_box_pack_start (GTK_BOX (hbox1), button_run, FALSE, FALSE, 0); 70 | 71 | button_clear = gtk_button_new_with_label ("Clear"); 72 | gtk_widget_ref (button_clear); 73 | gtk_object_set_data_full (GTK_OBJECT (window), "button_clear", button_clear, 74 | (GtkDestroyNotify) gtk_widget_unref); 75 | gtk_widget_show (button_clear); 76 | gtk_box_pack_start (GTK_BOX (hbox1), button_clear, FALSE, FALSE, 0); 77 | 78 | button_save = gtk_button_new_with_label ("Save"); 79 | gtk_widget_ref (button_save); 80 | gtk_object_set_data_full (GTK_OBJECT (window), "button_save", button_save, 81 | (GtkDestroyNotify) gtk_widget_unref); 82 | gtk_widget_show (button_save); 83 | gtk_box_pack_start (GTK_BOX (hbox1), button_save, FALSE, FALSE, 0); 84 | 85 | button_load = gtk_button_new_with_label ("Load"); 86 | gtk_widget_ref (button_load); 87 | gtk_object_set_data_full (GTK_OBJECT (window), "button_load", button_load, 88 | (GtkDestroyNotify) gtk_widget_unref); 89 | gtk_widget_show (button_load); 90 | gtk_box_pack_start (GTK_BOX (hbox1), button_load, FALSE, FALSE, 0); 91 | 92 | entry_option = gtk_entry_new (); 93 | gtk_widget_ref (entry_option); 94 | gtk_object_set_data_full (GTK_OBJECT (window), "entry_option", entry_option, 95 | (GtkDestroyNotify) gtk_widget_unref); 96 | gtk_widget_show (entry_option); 97 | gtk_box_pack_start (GTK_BOX (hbox1), entry_option, TRUE, TRUE, 0); 98 | 99 | gtk_signal_connect (GTK_OBJECT (window), "destroy", 100 | GTK_SIGNAL_FUNC (on_window1_destroy), 101 | NULL); 102 | gtk_signal_connect (GTK_OBJECT (draw_main), "button_press_event", 103 | GTK_SIGNAL_FUNC (on_draw_main_button_press_event), 104 | NULL); 105 | gtk_signal_connect (GTK_OBJECT (draw_main), "expose_event", 106 | GTK_SIGNAL_FUNC (on_draw_main_expose_event), 107 | NULL); 108 | gtk_signal_connect (GTK_OBJECT (button_change), "clicked", 109 | GTK_SIGNAL_FUNC (on_button_change_clicked), 110 | NULL); 111 | gtk_signal_connect (GTK_OBJECT (button_run), "clicked", 112 | GTK_SIGNAL_FUNC (on_button_run_clicked), 113 | NULL); 114 | gtk_signal_connect (GTK_OBJECT (button_clear), "clicked", 115 | GTK_SIGNAL_FUNC (on_button_clear_clicked), 116 | NULL); 117 | gtk_signal_connect (GTK_OBJECT (button_save), "clicked", 118 | GTK_SIGNAL_FUNC (on_button_save_clicked), 119 | NULL); 120 | gtk_signal_connect (GTK_OBJECT (button_load), "clicked", 121 | GTK_SIGNAL_FUNC (on_button_load_clicked), 122 | NULL); 123 | gtk_signal_connect (GTK_OBJECT (entry_option), "activate", 124 | GTK_SIGNAL_FUNC (on_button_run_clicked), 125 | NULL); 126 | 127 | return window; 128 | } 129 | 130 | GtkWidget* 131 | create_fileselection (void) 132 | { 133 | GtkWidget *fileselection; 134 | GtkWidget *filesel_ok; 135 | GtkWidget *filesel_cancel; 136 | 137 | fileselection = gtk_file_selection_new ("Select File"); 138 | gtk_object_set_data (GTK_OBJECT (fileselection), "fileselection", fileselection); 139 | gtk_container_set_border_width (GTK_CONTAINER (fileselection), 10); 140 | gtk_window_set_modal (GTK_WINDOW (fileselection), TRUE); 141 | 142 | filesel_ok = GTK_FILE_SELECTION (fileselection)->ok_button; 143 | gtk_object_set_data (GTK_OBJECT (fileselection), "filesel_ok", filesel_ok); 144 | gtk_widget_show (filesel_ok); 145 | GTK_WIDGET_SET_FLAGS (filesel_ok, GTK_CAN_DEFAULT); 146 | 147 | filesel_cancel = GTK_FILE_SELECTION (fileselection)->cancel_button; 148 | gtk_object_set_data (GTK_OBJECT (fileselection), "filesel_cancel", filesel_cancel); 149 | gtk_widget_show (filesel_cancel); 150 | GTK_WIDGET_SET_FLAGS (filesel_cancel, GTK_CAN_DEFAULT); 151 | 152 | gtk_signal_connect (GTK_OBJECT (fileselection), "destroy", 153 | GTK_SIGNAL_FUNC (on_fileselection_destroy), 154 | NULL); 155 | gtk_signal_connect (GTK_OBJECT (filesel_ok), "clicked", 156 | GTK_SIGNAL_FUNC (on_filesel_ok_clicked), 157 | NULL); 158 | gtk_signal_connect (GTK_OBJECT (filesel_cancel), "clicked", 159 | GTK_SIGNAL_FUNC (on_filesel_cancel_clicked), 160 | NULL); 161 | 162 | return fileselection; 163 | } 164 | 165 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-toy/gtk/interface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT EDIT THIS FILE - it is generated by Glade. 3 | */ 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | GtkWidget* create_window (void); 10 | GtkWidget* create_fileselection (void); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-toy/gtk/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Initial main.c file generated by Glade. Edit as required. 3 | * Glade will not overwrite this file. 4 | */ 5 | 6 | #include 7 | #include "interface.h" 8 | void svm_toy_initialize(); 9 | 10 | int main (int argc, char *argv[]) 11 | { 12 | GtkWidget *window; 13 | 14 | gtk_set_locale (); 15 | gtk_init (&argc, &argv); 16 | 17 | window = create_window (); 18 | gtk_widget_show (window); 19 | 20 | svm_toy_initialize(); 21 | gtk_main (); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-toy/gtk/svm-toy.glade: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | svm-toy 6 | svm-toy 7 | 8 | src 9 | pixmaps 10 | C 11 | False 12 | False 13 | False 14 | True 15 | True 16 | True 17 | False 18 | interface.c 19 | interface.h 20 | callbacks.c 21 | callbacks.h 22 | support.c 23 | support.h 24 | 25 | 26 | 27 | 28 | GtkWindow 29 | window 30 | 31 | destroy 32 | on_window1_destroy 33 | Sun, 16 Apr 2000 09:47:10 GMT 34 | 35 | SVM Toy 36 | GTK_WINDOW_TOPLEVEL 37 | GTK_WIN_POS_NONE 38 | False 39 | False 40 | True 41 | False 42 | 43 | 44 | GtkVBox 45 | vbox1 46 | False 47 | 0 48 | 49 | 50 | GtkDrawingArea 51 | draw_main 52 | 500 53 | 500 54 | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK 55 | 56 | button_press_event 57 | on_draw_main_button_press_event 58 | Sun, 16 Apr 2000 13:02:05 GMT 59 | 60 | 61 | expose_event 62 | on_draw_main_expose_event 63 | Sun, 16 Apr 2000 14:27:05 GMT 64 | 65 | 66 | 0 67 | True 68 | True 69 | 70 | 71 | 72 | 73 | GtkHBox 74 | hbox1 75 | False 76 | 0 77 | 78 | 0 79 | False 80 | False 81 | 82 | 83 | 84 | GtkButton 85 | button_change 86 | True 87 | 88 | clicked 89 | on_button_change_clicked 90 | Sun, 16 Apr 2000 09:40:18 GMT 91 | 92 | 93 | 94 | 0 95 | False 96 | False 97 | 98 | 99 | 100 | 101 | GtkButton 102 | button_run 103 | True 104 | 105 | clicked 106 | on_button_run_clicked 107 | Sun, 16 Apr 2000 09:40:37 GMT 108 | 109 | 110 | 111 | 0 112 | False 113 | False 114 | 115 | 116 | 117 | 118 | GtkButton 119 | button_clear 120 | True 121 | 122 | clicked 123 | on_button_clear_clicked 124 | Sun, 16 Apr 2000 09:40:44 GMT 125 | 126 | 127 | 128 | 0 129 | False 130 | False 131 | 132 | 133 | 134 | 135 | GtkButton 136 | button_save 137 | True 138 | 139 | clicked 140 | on_button_save_clicked 141 | Fri, 16 Jun 2000 18:23:46 GMT 142 | 143 | 144 | 145 | 0 146 | False 147 | False 148 | 149 | 150 | 151 | 152 | GtkButton 153 | button_load 154 | True 155 | 156 | clicked 157 | on_button_load_clicked 158 | Fri, 16 Jun 2000 18:23:56 GMT 159 | 160 | 161 | 162 | 0 163 | False 164 | False 165 | 166 | 167 | 168 | 169 | GtkEntry 170 | entry_option 171 | True 172 | 173 | activate 174 | on_button_run_clicked 175 | Sun, 16 Apr 2000 09:42:46 GMT 176 | 177 | True 178 | True 179 | 0 180 | 181 | 182 | 0 183 | True 184 | True 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | GtkFileSelection 193 | fileselection 194 | 10 195 | 196 | destroy 197 | on_fileselection_destroy 198 | Fri, 16 Jun 2000 18:11:28 GMT 199 | 200 | Select File 201 | GTK_WINDOW_TOPLEVEL 202 | GTK_WIN_POS_NONE 203 | True 204 | False 205 | True 206 | False 207 | True 208 | 209 | 210 | GtkButton 211 | FileSel:ok_button 212 | filesel_ok 213 | True 214 | True 215 | 216 | clicked 217 | on_filesel_ok_clicked 218 | Fri, 16 Jun 2000 18:09:56 GMT 219 | 220 | 221 | 222 | 223 | 224 | GtkButton 225 | FileSel:cancel_button 226 | filesel_cancel 227 | True 228 | True 229 | 230 | clicked 231 | on_filesel_cancel_clicked 232 | Fri, 16 Jun 2000 18:09:46 GMT 233 | 234 | 235 | 236 | 237 | 238 | 239 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-toy/qt/Makefile: -------------------------------------------------------------------------------- 1 | CXX? = g++ 2 | INCLUDE = /usr/include/qt4 3 | CFLAGS = -Wall -O3 -I$(INCLUDE) -I$(INCLUDE)/QtGui -I$(INCLUDE)/QtCore 4 | LIB = -lQtGui -lQtCore 5 | MOC = /usr/bin/moc-qt4 6 | 7 | svm-toy: svm-toy.cpp svm-toy.moc ../../svm.o 8 | $(CXX) $(CFLAGS) svm-toy.cpp ../../svm.o -o svm-toy $(LIB) 9 | 10 | svm-toy.moc: svm-toy.cpp 11 | $(MOC) svm-toy.cpp -o svm-toy.moc 12 | 13 | ../../svm.o: ../../svm.cpp ../../svm.h 14 | make -C ../.. svm.o 15 | 16 | clean: 17 | rm -f *~ svm-toy svm-toy.moc ../../svm.o 18 | 19 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-toy/qt/svm-toy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "../../svm.h" 8 | using namespace std; 9 | 10 | #define DEFAULT_PARAM "-t 2 -c 100" 11 | #define XLEN 500 12 | #define YLEN 500 13 | 14 | QRgb colors[] = 15 | { 16 | qRgb(0,0,0), 17 | qRgb(0,120,120), 18 | qRgb(120,120,0), 19 | qRgb(120,0,120), 20 | qRgb(0,200,200), 21 | qRgb(200,200,0), 22 | qRgb(200,0,200) 23 | }; 24 | 25 | class SvmToyWindow : public QWidget 26 | { 27 | 28 | Q_OBJECT 29 | 30 | public: 31 | SvmToyWindow(); 32 | ~SvmToyWindow(); 33 | protected: 34 | virtual void mousePressEvent( QMouseEvent* ); 35 | virtual void paintEvent( QPaintEvent* ); 36 | 37 | private: 38 | QPixmap buffer; 39 | QPixmap icon1; 40 | QPixmap icon2; 41 | QPixmap icon3; 42 | QPushButton button_change_icon; 43 | QPushButton button_run; 44 | QPushButton button_clear; 45 | QPushButton button_save; 46 | QPushButton button_load; 47 | QLineEdit input_line; 48 | QPainter buffer_painter; 49 | struct point { 50 | double x, y; 51 | signed char value; 52 | }; 53 | list point_list; 54 | int current_value; 55 | const QPixmap& choose_icon(int v) 56 | { 57 | if(v==1) return icon1; 58 | else if(v==2) return icon2; 59 | else return icon3; 60 | } 61 | void clear_all() 62 | { 63 | point_list.clear(); 64 | buffer.fill(Qt::black); 65 | repaint(); 66 | } 67 | void draw_point(const point& p) 68 | { 69 | const QPixmap& icon = choose_icon(p.value); 70 | buffer_painter.drawPixmap((int)(p.x*XLEN),(int)(p.y*YLEN),icon); 71 | repaint(); 72 | } 73 | void draw_all_points() 74 | { 75 | for(list::iterator p = point_list.begin(); p != point_list.end();p++) 76 | draw_point(*p); 77 | } 78 | private slots: 79 | void button_change_icon_clicked() 80 | { 81 | ++current_value; 82 | if(current_value > 3) current_value = 1; 83 | button_change_icon.setIcon(choose_icon(current_value)); 84 | } 85 | void button_run_clicked() 86 | { 87 | // guard 88 | if(point_list.empty()) return; 89 | 90 | svm_parameter param; 91 | int i,j; 92 | 93 | // default values 94 | param.svm_type = C_SVC; 95 | param.kernel_type = RBF; 96 | param.degree = 3; 97 | param.gamma = 0; 98 | param.coef0 = 0; 99 | param.nu = 0.5; 100 | param.cache_size = 100; 101 | param.C = 1; 102 | param.eps = 1e-3; 103 | param.p = 0.1; 104 | param.shrinking = 1; 105 | param.probability = 0; 106 | param.nr_weight = 0; 107 | param.weight_label = NULL; 108 | param.weight = NULL; 109 | 110 | // parse options 111 | const char *p = input_line.text().toAscii().constData(); 112 | 113 | while (1) { 114 | while (*p && *p != '-') 115 | p++; 116 | 117 | if (*p == '\0') 118 | break; 119 | 120 | p++; 121 | switch (*p++) { 122 | case 's': 123 | param.svm_type = atoi(p); 124 | break; 125 | case 't': 126 | param.kernel_type = atoi(p); 127 | break; 128 | case 'd': 129 | param.degree = atoi(p); 130 | break; 131 | case 'g': 132 | param.gamma = atof(p); 133 | break; 134 | case 'r': 135 | param.coef0 = atof(p); 136 | break; 137 | case 'n': 138 | param.nu = atof(p); 139 | break; 140 | case 'm': 141 | param.cache_size = atof(p); 142 | break; 143 | case 'c': 144 | param.C = atof(p); 145 | break; 146 | case 'e': 147 | param.eps = atof(p); 148 | break; 149 | case 'p': 150 | param.p = atof(p); 151 | break; 152 | case 'h': 153 | param.shrinking = atoi(p); 154 | break; 155 | case 'b': 156 | param.probability = atoi(p); 157 | break; 158 | case 'w': 159 | ++param.nr_weight; 160 | param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight); 161 | param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight); 162 | param.weight_label[param.nr_weight-1] = atoi(p); 163 | while(*p && !isspace(*p)) ++p; 164 | param.weight[param.nr_weight-1] = atof(p); 165 | break; 166 | } 167 | } 168 | 169 | // build problem 170 | svm_problem prob; 171 | 172 | prob.l = point_list.size(); 173 | prob.y = new double[prob.l]; 174 | 175 | if(param.kernel_type == PRECOMPUTED) 176 | { 177 | } 178 | else if(param.svm_type == EPSILON_SVR || 179 | param.svm_type == NU_SVR) 180 | { 181 | if(param.gamma == 0) param.gamma = 1; 182 | svm_node *x_space = new svm_node[2 * prob.l]; 183 | prob.x = new svm_node *[prob.l]; 184 | 185 | i = 0; 186 | for (list ::iterator q = point_list.begin(); q != point_list.end(); q++, i++) 187 | { 188 | x_space[2 * i].index = 1; 189 | x_space[2 * i].value = q->x; 190 | x_space[2 * i + 1].index = -1; 191 | prob.x[i] = &x_space[2 * i]; 192 | prob.y[i] = q->y; 193 | } 194 | 195 | // build model & classify 196 | svm_model *model = svm_train(&prob, ¶m); 197 | svm_node x[2]; 198 | x[0].index = 1; 199 | x[1].index = -1; 200 | int *j = new int[XLEN]; 201 | 202 | for (i = 0; i < XLEN; i++) 203 | { 204 | x[0].value = (double) i / XLEN; 205 | j[i] = (int)(YLEN*svm_predict(model, x)); 206 | } 207 | 208 | buffer_painter.setPen(colors[0]); 209 | buffer_painter.drawLine(0,0,0,YLEN-1); 210 | 211 | int p = (int)(param.p * YLEN); 212 | for(i = 1; i < XLEN; i++) 213 | { 214 | buffer_painter.setPen(colors[0]); 215 | buffer_painter.drawLine(i,0,i,YLEN-1); 216 | 217 | buffer_painter.setPen(colors[5]); 218 | buffer_painter.drawLine(i-1,j[i-1],i,j[i]); 219 | 220 | if(param.svm_type == EPSILON_SVR) 221 | { 222 | buffer_painter.setPen(colors[2]); 223 | buffer_painter.drawLine(i-1,j[i-1]+p,i,j[i]+p); 224 | 225 | buffer_painter.setPen(colors[2]); 226 | buffer_painter.drawLine(i-1,j[i-1]-p,i,j[i]-p); 227 | } 228 | } 229 | 230 | svm_free_and_destroy_model(&model); 231 | delete[] j; 232 | delete[] x_space; 233 | delete[] prob.x; 234 | delete[] prob.y; 235 | } 236 | else 237 | { 238 | if(param.gamma == 0) param.gamma = 0.5; 239 | svm_node *x_space = new svm_node[3 * prob.l]; 240 | prob.x = new svm_node *[prob.l]; 241 | 242 | i = 0; 243 | for (list ::iterator q = point_list.begin(); q != point_list.end(); q++, i++) 244 | { 245 | x_space[3 * i].index = 1; 246 | x_space[3 * i].value = q->x; 247 | x_space[3 * i + 1].index = 2; 248 | x_space[3 * i + 1].value = q->y; 249 | x_space[3 * i + 2].index = -1; 250 | prob.x[i] = &x_space[3 * i]; 251 | prob.y[i] = q->value; 252 | } 253 | 254 | // build model & classify 255 | svm_model *model = svm_train(&prob, ¶m); 256 | svm_node x[3]; 257 | x[0].index = 1; 258 | x[1].index = 2; 259 | x[2].index = -1; 260 | 261 | for (i = 0; i < XLEN; i++) 262 | for (j = 0; j < YLEN ; j++) { 263 | x[0].value = (double) i / XLEN; 264 | x[1].value = (double) j / YLEN; 265 | double d = svm_predict(model, x); 266 | if (param.svm_type == ONE_CLASS && d<0) d=2; 267 | buffer_painter.setPen(colors[(int)d]); 268 | buffer_painter.drawPoint(i,j); 269 | } 270 | 271 | svm_free_and_destroy_model(&model); 272 | delete[] x_space; 273 | delete[] prob.x; 274 | delete[] prob.y; 275 | } 276 | free(param.weight_label); 277 | free(param.weight); 278 | draw_all_points(); 279 | } 280 | void button_clear_clicked() 281 | { 282 | clear_all(); 283 | } 284 | void button_save_clicked() 285 | { 286 | QString filename = QFileDialog::getSaveFileName(); 287 | if(!filename.isNull()) 288 | { 289 | FILE *fp = fopen(filename.toAscii().constData(),"w"); 290 | 291 | const char *p = input_line.text().toAscii().constData(); 292 | const char* svm_type_str = strstr(p, "-s "); 293 | int svm_type = C_SVC; 294 | if(svm_type_str != NULL) 295 | sscanf(svm_type_str, "-s %d", &svm_type); 296 | 297 | if(fp) 298 | { 299 | if(svm_type == EPSILON_SVR || svm_type == NU_SVR) 300 | { 301 | for(list::iterator p = point_list.begin(); p != point_list.end();p++) 302 | fprintf(fp,"%f 1:%f\n", p->y, p->x); 303 | } 304 | else 305 | { 306 | for(list::iterator p = point_list.begin(); p != point_list.end();p++) 307 | fprintf(fp,"%d 1:%f 2:%f\n", p->value, p->x, p->y); 308 | } 309 | fclose(fp); 310 | } 311 | } 312 | } 313 | void button_load_clicked() 314 | { 315 | QString filename = QFileDialog::getOpenFileName(); 316 | if(!filename.isNull()) 317 | { 318 | FILE *fp = fopen(filename.toAscii().constData(),"r"); 319 | if(fp) 320 | { 321 | clear_all(); 322 | char buf[4096]; 323 | while(fgets(buf,sizeof(buf),fp)) 324 | { 325 | int v; 326 | double x,y; 327 | if(sscanf(buf,"%d%*d:%lf%*d:%lf",&v,&x,&y)==3) 328 | { 329 | point p = {x,y,v}; 330 | point_list.push_back(p); 331 | } 332 | else if(sscanf(buf,"%lf%*d:%lf",&y,&x)==2) 333 | { 334 | point p = {x,y,current_value}; 335 | point_list.push_back(p); 336 | } 337 | else 338 | break; 339 | } 340 | fclose(fp); 341 | draw_all_points(); 342 | } 343 | } 344 | 345 | } 346 | }; 347 | 348 | #include "svm-toy.moc" 349 | 350 | SvmToyWindow::SvmToyWindow() 351 | :button_change_icon(this) 352 | ,button_run("Run",this) 353 | ,button_clear("Clear",this) 354 | ,button_save("Save",this) 355 | ,button_load("Load",this) 356 | ,input_line(this) 357 | ,current_value(1) 358 | { 359 | buffer = QPixmap(XLEN,YLEN); 360 | buffer.fill(Qt::black); 361 | 362 | buffer_painter.begin(&buffer); 363 | 364 | QObject::connect(&button_change_icon, SIGNAL(clicked()), this, 365 | SLOT(button_change_icon_clicked())); 366 | QObject::connect(&button_run, SIGNAL(clicked()), this, 367 | SLOT(button_run_clicked())); 368 | QObject::connect(&button_clear, SIGNAL(clicked()), this, 369 | SLOT(button_clear_clicked())); 370 | QObject::connect(&button_save, SIGNAL(clicked()), this, 371 | SLOT(button_save_clicked())); 372 | QObject::connect(&button_load, SIGNAL(clicked()), this, 373 | SLOT(button_load_clicked())); 374 | QObject::connect(&input_line, SIGNAL(returnPressed()), this, 375 | SLOT(button_run_clicked())); 376 | 377 | // don't blank the window before repainting 378 | setAttribute(Qt::WA_NoBackground); 379 | 380 | icon1 = QPixmap(4,4); 381 | icon2 = QPixmap(4,4); 382 | icon3 = QPixmap(4,4); 383 | 384 | 385 | QPainter painter; 386 | painter.begin(&icon1); 387 | painter.fillRect(0,0,4,4,QBrush(colors[4])); 388 | painter.end(); 389 | 390 | painter.begin(&icon2); 391 | painter.fillRect(0,0,4,4,QBrush(colors[5])); 392 | painter.end(); 393 | 394 | painter.begin(&icon3); 395 | painter.fillRect(0,0,4,4,QBrush(colors[6])); 396 | painter.end(); 397 | 398 | button_change_icon.setGeometry( 0, YLEN, 50, 25 ); 399 | button_run.setGeometry( 50, YLEN, 50, 25 ); 400 | button_clear.setGeometry( 100, YLEN, 50, 25 ); 401 | button_save.setGeometry( 150, YLEN, 50, 25); 402 | button_load.setGeometry( 200, YLEN, 50, 25); 403 | input_line.setGeometry( 250, YLEN, 250, 25); 404 | 405 | input_line.setText(DEFAULT_PARAM); 406 | button_change_icon.setIcon(icon1); 407 | } 408 | 409 | SvmToyWindow::~SvmToyWindow() 410 | { 411 | buffer_painter.end(); 412 | } 413 | 414 | void SvmToyWindow::mousePressEvent( QMouseEvent* event ) 415 | { 416 | point p = {(double)event->x()/XLEN, (double)event->y()/YLEN, current_value}; 417 | point_list.push_back(p); 418 | draw_point(p); 419 | } 420 | 421 | void SvmToyWindow::paintEvent( QPaintEvent* ) 422 | { 423 | // copy the image from the buffer pixmap to the window 424 | QPainter p(this); 425 | p.drawPixmap(0, 0, buffer); 426 | } 427 | 428 | int main( int argc, char* argv[] ) 429 | { 430 | QApplication myapp( argc, argv ); 431 | 432 | SvmToyWindow* mywidget = new SvmToyWindow(); 433 | mywidget->setGeometry( 100, 100, XLEN, YLEN+25 ); 434 | 435 | mywidget->show(); 436 | return myapp.exec(); 437 | } 438 | -------------------------------------------------------------------------------- /libsvm-3.22/svm-train.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "svm.h" 7 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 8 | 9 | void print_null(const char *s) {} 10 | 11 | void exit_with_help() 12 | { 13 | printf( 14 | "Usage: svm-train [options] training_set_file [model_file]\n" 15 | "options:\n" 16 | "-s svm_type : set type of SVM (default 0)\n" 17 | " 0 -- C-SVC (multi-class classification)\n" 18 | " 1 -- nu-SVC (multi-class classification)\n" 19 | " 2 -- one-class SVM\n" 20 | " 3 -- epsilon-SVR (regression)\n" 21 | " 4 -- nu-SVR (regression)\n" 22 | "-t kernel_type : set type of kernel function (default 2)\n" 23 | " 0 -- linear: u'*v\n" 24 | " 1 -- polynomial: (gamma*u'*v + coef0)^degree\n" 25 | " 2 -- radial basis function: exp(-gamma*|u-v|^2)\n" 26 | " 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n" 27 | " 4 -- precomputed kernel (kernel values in training_set_file)\n" 28 | "-d degree : set degree in kernel function (default 3)\n" 29 | "-g gamma : set gamma in kernel function (default 1/num_features)\n" 30 | "-r coef0 : set coef0 in kernel function (default 0)\n" 31 | "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n" 32 | "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n" 33 | "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n" 34 | "-m cachesize : set cache memory size in MB (default 100)\n" 35 | "-e epsilon : set tolerance of termination criterion (default 0.001)\n" 36 | "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n" 37 | "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n" 38 | "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n" 39 | "-v n: n-fold cross validation mode\n" 40 | "-q : quiet mode (no outputs)\n" 41 | ); 42 | exit(1); 43 | } 44 | 45 | void exit_input_error(int line_num) 46 | { 47 | fprintf(stderr,"Wrong input format at line %d\n", line_num); 48 | exit(1); 49 | } 50 | 51 | void parse_command_line(int argc, char **argv, char *input_file_name, char *model_file_name); 52 | void read_problem(const char *filename); 53 | void do_cross_validation(); 54 | 55 | struct svm_parameter param; // set by parse_command_line 56 | struct svm_problem prob; // set by read_problem 57 | struct svm_model *model; 58 | struct svm_node *x_space; 59 | int cross_validation; 60 | int nr_fold; 61 | 62 | static char *line = NULL; 63 | static int max_line_len; 64 | 65 | static char* readline(FILE *input) 66 | { 67 | int len; 68 | 69 | if(fgets(line,max_line_len,input) == NULL) 70 | return NULL; 71 | 72 | while(strrchr(line,'\n') == NULL) 73 | { 74 | max_line_len *= 2; 75 | line = (char *) realloc(line,max_line_len); 76 | len = (int) strlen(line); 77 | if(fgets(line+len,max_line_len-len,input) == NULL) 78 | break; 79 | } 80 | return line; 81 | } 82 | 83 | int main(int argc, char **argv) 84 | { 85 | char input_file_name[1024]; 86 | char model_file_name[1024]; 87 | const char *error_msg; 88 | 89 | parse_command_line(argc, argv, input_file_name, model_file_name); 90 | read_problem(input_file_name); 91 | error_msg = svm_check_parameter(&prob,¶m); 92 | 93 | if(error_msg) 94 | { 95 | fprintf(stderr,"ERROR: %s\n",error_msg); 96 | exit(1); 97 | } 98 | 99 | if(cross_validation) 100 | { 101 | do_cross_validation(); 102 | } 103 | else 104 | { 105 | model = svm_train(&prob,¶m); 106 | if(svm_save_model(model_file_name,model)) 107 | { 108 | fprintf(stderr, "can't save model to file %s\n", model_file_name); 109 | exit(1); 110 | } 111 | svm_free_and_destroy_model(&model); 112 | } 113 | svm_destroy_param(¶m); 114 | free(prob.y); 115 | free(prob.x); 116 | free(x_space); 117 | free(line); 118 | 119 | return 0; 120 | } 121 | 122 | void do_cross_validation() 123 | { 124 | int i; 125 | int total_correct = 0; 126 | double total_error = 0; 127 | double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; 128 | double *target = Malloc(double,prob.l); 129 | 130 | svm_cross_validation(&prob,¶m,nr_fold,target); 131 | if(param.svm_type == EPSILON_SVR || 132 | param.svm_type == NU_SVR) 133 | { 134 | for(i=0;i=argc) 189 | exit_with_help(); 190 | switch(argv[i-1][1]) 191 | { 192 | case 's': 193 | param.svm_type = atoi(argv[i]); 194 | break; 195 | case 't': 196 | param.kernel_type = atoi(argv[i]); 197 | break; 198 | case 'd': 199 | param.degree = atoi(argv[i]); 200 | break; 201 | case 'g': 202 | param.gamma = atof(argv[i]); 203 | break; 204 | case 'r': 205 | param.coef0 = atof(argv[i]); 206 | break; 207 | case 'n': 208 | param.nu = atof(argv[i]); 209 | break; 210 | case 'm': 211 | param.cache_size = atof(argv[i]); 212 | break; 213 | case 'c': 214 | param.C = atof(argv[i]); 215 | break; 216 | case 'e': 217 | param.eps = atof(argv[i]); 218 | break; 219 | case 'p': 220 | param.p = atof(argv[i]); 221 | break; 222 | case 'h': 223 | param.shrinking = atoi(argv[i]); 224 | break; 225 | case 'b': 226 | param.probability = atoi(argv[i]); 227 | break; 228 | case 'q': 229 | print_func = &print_null; 230 | i--; 231 | break; 232 | case 'v': 233 | cross_validation = 1; 234 | nr_fold = atoi(argv[i]); 235 | if(nr_fold < 2) 236 | { 237 | fprintf(stderr,"n-fold cross validation: n must >= 2\n"); 238 | exit_with_help(); 239 | } 240 | break; 241 | case 'w': 242 | ++param.nr_weight; 243 | param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight); 244 | param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight); 245 | param.weight_label[param.nr_weight-1] = atoi(&argv[i-1][2]); 246 | param.weight[param.nr_weight-1] = atof(argv[i]); 247 | break; 248 | default: 249 | fprintf(stderr,"Unknown option: -%c\n", argv[i-1][1]); 250 | exit_with_help(); 251 | } 252 | } 253 | 254 | svm_set_print_string_function(print_func); 255 | 256 | // determine filenames 257 | 258 | if(i>=argc) 259 | exit_with_help(); 260 | 261 | strcpy(input_file_name, argv[i]); 262 | 263 | if(i start from 0 323 | readline(fp); 324 | prob.x[i] = &x_space[j]; 325 | label = strtok(line," \t\n"); 326 | if(label == NULL) // empty line 327 | exit_input_error(i+1); 328 | 329 | prob.y[i] = strtod(label,&endptr); 330 | if(endptr == label || *endptr != '\0') 331 | exit_input_error(i+1); 332 | 333 | while(1) 334 | { 335 | idx = strtok(NULL,":"); 336 | val = strtok(NULL," \t"); 337 | 338 | if(val == NULL) 339 | break; 340 | 341 | errno = 0; 342 | x_space[j].index = (int) strtol(idx,&endptr,10); 343 | if(endptr == idx || errno != 0 || *endptr != '\0' || x_space[j].index <= inst_max_index) 344 | exit_input_error(i+1); 345 | else 346 | inst_max_index = x_space[j].index; 347 | 348 | errno = 0; 349 | x_space[j].value = strtod(val,&endptr); 350 | if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) 351 | exit_input_error(i+1); 352 | 353 | ++j; 354 | } 355 | 356 | if(inst_max_index > max_index) 357 | max_index = inst_max_index; 358 | x_space[j++].index = -1; 359 | } 360 | 361 | if(param.gamma == 0 && max_index > 0) 362 | param.gamma = 1.0/max_index; 363 | 364 | if(param.kernel_type == PRECOMPUTED) 365 | for(i=0;i max_index) 373 | { 374 | fprintf(stderr,"Wrong input format: sample_serial_number out of range\n"); 375 | exit(1); 376 | } 377 | } 378 | 379 | fclose(fp); 380 | } 381 | -------------------------------------------------------------------------------- /libsvm-3.22/svm.def: -------------------------------------------------------------------------------- 1 | LIBRARY libsvm 2 | EXPORTS 3 | svm_train @1 4 | svm_cross_validation @2 5 | svm_save_model @3 6 | svm_load_model @4 7 | svm_get_svm_type @5 8 | svm_get_nr_class @6 9 | svm_get_labels @7 10 | svm_get_svr_probability @8 11 | svm_predict_values @9 12 | svm_predict @10 13 | svm_predict_probability @11 14 | svm_free_model_content @12 15 | svm_free_and_destroy_model @13 16 | svm_destroy_param @14 17 | svm_check_parameter @15 18 | svm_check_probability_model @16 19 | svm_set_print_string_function @17 20 | svm_get_sv_indices @18 21 | svm_get_nr_sv @19 22 | -------------------------------------------------------------------------------- /libsvm-3.22/svm.h: -------------------------------------------------------------------------------- 1 | #ifndef _LIBSVM_H 2 | #define _LIBSVM_H 3 | 4 | #define LIBSVM_VERSION 322 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | extern int libsvm_version; 11 | 12 | struct svm_node 13 | { 14 | int index; 15 | double value; 16 | }; 17 | 18 | struct svm_problem 19 | { 20 | int l; 21 | double *y; 22 | struct svm_node **x; 23 | }; 24 | 25 | enum { C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR }; /* svm_type */ 26 | enum { LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED }; /* kernel_type */ 27 | 28 | struct svm_parameter 29 | { 30 | int svm_type; 31 | int kernel_type; 32 | int degree; /* for poly */ 33 | double gamma; /* for poly/rbf/sigmoid */ 34 | double coef0; /* for poly/sigmoid */ 35 | 36 | /* these are for training only */ 37 | double cache_size; /* in MB */ 38 | double eps; /* stopping criteria */ 39 | double C; /* for C_SVC, EPSILON_SVR and NU_SVR */ 40 | int nr_weight; /* for C_SVC */ 41 | int *weight_label; /* for C_SVC */ 42 | double* weight; /* for C_SVC */ 43 | double nu; /* for NU_SVC, ONE_CLASS, and NU_SVR */ 44 | double p; /* for EPSILON_SVR */ 45 | int shrinking; /* use the shrinking heuristics */ 46 | int probability; /* do probability estimates */ 47 | }; 48 | 49 | // 50 | // svm_model 51 | // 52 | struct svm_model 53 | { 54 | struct svm_parameter param; /* parameter */ 55 | int nr_class; /* number of classes, = 2 in regression/one class svm */ 56 | int l; /* total #SV */ 57 | struct svm_node **SV; /* SVs (SV[l]) */ 58 | double **sv_coef; /* coefficients for SVs in decision functions (sv_coef[k-1][l]) */ 59 | double *rho; /* constants in decision functions (rho[k*(k-1)/2]) */ 60 | double *probA; /* pariwise probability information */ 61 | double *probB; 62 | int *sv_indices; /* sv_indices[0,...,nSV-1] are values in [1,...,num_traning_data] to indicate SVs in the training set */ 63 | 64 | /* for classification only */ 65 | 66 | int *label; /* label of each class (label[k]) */ 67 | int *nSV; /* number of SVs for each class (nSV[k]) */ 68 | /* nSV[0] + nSV[1] + ... + nSV[k-1] = l */ 69 | /* XXX */ 70 | int free_sv; /* 1 if svm_model is created by svm_load_model*/ 71 | /* 0 if svm_model is created by svm_train */ 72 | }; 73 | 74 | struct svm_model *svm_train(const struct svm_problem *prob, const struct svm_parameter *param); 75 | void svm_cross_validation(const struct svm_problem *prob, const struct svm_parameter *param, int nr_fold, double *target); 76 | 77 | int svm_save_model(const char *model_file_name, const struct svm_model *model); 78 | struct svm_model *svm_load_model(const char *model_file_name); 79 | 80 | int svm_get_svm_type(const struct svm_model *model); 81 | int svm_get_nr_class(const struct svm_model *model); 82 | void svm_get_labels(const struct svm_model *model, int *label); 83 | void svm_get_sv_indices(const struct svm_model *model, int *sv_indices); 84 | int svm_get_nr_sv(const struct svm_model *model); 85 | double svm_get_svr_probability(const struct svm_model *model); 86 | 87 | double svm_predict_values(const struct svm_model *model, const struct svm_node *x, double* dec_values); 88 | double svm_predict(const struct svm_model *model, const struct svm_node *x); 89 | double svm_predict_probability(const struct svm_model *model, const struct svm_node *x, double* prob_estimates); 90 | 91 | void svm_free_model_content(struct svm_model *model_ptr); 92 | void svm_free_and_destroy_model(struct svm_model **model_ptr_ptr); 93 | void svm_destroy_param(struct svm_parameter *param); 94 | 95 | const char *svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param); 96 | int svm_check_probability_model(const struct svm_model *model); 97 | 98 | void svm_set_print_string_function(void (*print_func)(const char *)); 99 | 100 | #ifdef __cplusplus 101 | } 102 | #endif 103 | 104 | #endif /* _LIBSVM_H */ 105 | -------------------------------------------------------------------------------- /libsvm-3.22/tools/README: -------------------------------------------------------------------------------- 1 | This directory includes some useful codes: 2 | 3 | 1. subset selection tools. 4 | 2. parameter selection tools. 5 | 3. LIBSVM format checking tools 6 | 7 | Part I: Subset selection tools 8 | 9 | Introduction 10 | ============ 11 | 12 | Training large data is time consuming. Sometimes one should work on a 13 | smaller subset first. The python script subset.py randomly selects a 14 | specified number of samples. For classification data, we provide a 15 | stratified selection to ensure the same class distribution in the 16 | subset. 17 | 18 | Usage: subset.py [options] dataset number [output1] [output2] 19 | 20 | This script selects a subset of the given data set. 21 | 22 | options: 23 | -s method : method of selection (default 0) 24 | 0 -- stratified selection (classification only) 25 | 1 -- random selection 26 | 27 | output1 : the subset (optional) 28 | output2 : the rest of data (optional) 29 | 30 | If output1 is omitted, the subset will be printed on the screen. 31 | 32 | Example 33 | ======= 34 | 35 | > python subset.py heart_scale 100 file1 file2 36 | 37 | From heart_scale 100 samples are randomly selected and stored in 38 | file1. All remaining instances are stored in file2. 39 | 40 | 41 | Part II: Parameter Selection Tools 42 | 43 | Introduction 44 | ============ 45 | 46 | grid.py is a parameter selection tool for C-SVM classification using 47 | the RBF (radial basis function) kernel. It uses cross validation (CV) 48 | technique to estimate the accuracy of each parameter combination in 49 | the specified range and helps you to decide the best parameters for 50 | your problem. 51 | 52 | grid.py directly executes libsvm binaries (so no python binding is needed) 53 | for cross validation and then draw contour of CV accuracy using gnuplot. 54 | You must have libsvm and gnuplot installed before using it. The package 55 | gnuplot is available at http://www.gnuplot.info/ 56 | 57 | On Mac OSX, the precompiled gnuplot file needs the library Aquarterm, 58 | which thus must be installed as well. In addition, this version of 59 | gnuplot does not support png, so you need to change "set term png 60 | transparent small" and use other image formats. For example, you may 61 | have "set term pbm small color". 62 | 63 | Usage: grid.py [grid_options] [svm_options] dataset 64 | 65 | grid_options : 66 | -log2c {begin,end,step | "null"} : set the range of c (default -5,15,2) 67 | begin,end,step -- c_range = 2^{begin,...,begin+k*step,...,end} 68 | "null" -- do not grid with c 69 | -log2g {begin,end,step | "null"} : set the range of g (default 3,-15,-2) 70 | begin,end,step -- g_range = 2^{begin,...,begin+k*step,...,end} 71 | "null" -- do not grid with g 72 | -v n : n-fold cross validation (default 5) 73 | -svmtrain pathname : set svm executable path and name 74 | -gnuplot {pathname | "null"} : 75 | pathname -- set gnuplot executable path and name 76 | "null" -- do not plot 77 | -out {pathname | "null"} : (default dataset.out) 78 | pathname -- set output file path and name 79 | "null" -- do not output file 80 | -png pathname : set graphic output file path and name (default dataset.png) 81 | -resume [pathname] : resume the grid task using an existing output file (default pathname is dataset.out) 82 | Use this option only if some parameters have been checked for the SAME data. 83 | 84 | svm_options : additional options for svm-train 85 | 86 | The program conducts v-fold cross validation using parameter C (and gamma) 87 | = 2^begin, 2^(begin+step), ..., 2^end. 88 | 89 | You can specify where the libsvm executable and gnuplot are using the 90 | -svmtrain and -gnuplot parameters. 91 | 92 | For windows users, please use pgnuplot.exe. If you are using gnuplot 93 | 3.7.1, please upgrade to version 3.7.3 or higher. The version 3.7.1 94 | has a bug. If you use cygwin on windows, please use gunplot-x11. 95 | 96 | If the task is terminated accidentally or you would like to change the 97 | range of parameters, you can apply '-resume' to save time by re-using 98 | previous results. You may specify the output file of a previous run 99 | or use the default (i.e., dataset.out) without giving a name. Please 100 | note that the same condition must be used in two runs. For example, 101 | you cannot use '-v 10' earlier and resume the task with '-v 5'. 102 | 103 | The value of some options can be "null." For example, `-log2c -1,0,1 104 | -log2 "null"' means that C=2^-1,2^0,2^1 and g=LIBSVM's default gamma 105 | value. That is, you do not conduct parameter selection on gamma. 106 | 107 | Example 108 | ======= 109 | 110 | > python grid.py -log2c -5,5,1 -log2g -4,0,1 -v 5 -m 300 heart_scale 111 | 112 | Users (in particular MS Windows users) may need to specify the path of 113 | executable files. You can either change paths in the beginning of 114 | grid.py or specify them in the command line. For example, 115 | 116 | > grid.py -log2c -5,5,1 -svmtrain "c:\Program Files\libsvm\windows\svm-train.exe" -gnuplot c:\tmp\gnuplot\binary\pgnuplot.exe -v 10 heart_scale 117 | 118 | Output: two files 119 | dataset.png: the CV accuracy contour plot generated by gnuplot 120 | dataset.out: the CV accuracy at each (log2(C),log2(gamma)) 121 | 122 | The following example saves running time by loading the output file of a previous run. 123 | 124 | > python grid.py -log2c -7,7,1 -log2g -5,2,1 -v 5 -resume heart_scale.out heart_scale 125 | 126 | Parallel grid search 127 | ==================== 128 | 129 | You can conduct a parallel grid search by dispatching jobs to a 130 | cluster of computers which share the same file system. First, you add 131 | machine names in grid.py: 132 | 133 | ssh_workers = ["linux1", "linux5", "linux5"] 134 | 135 | and then setup your ssh so that the authentication works without 136 | asking a password. 137 | 138 | The same machine (e.g., linux5 here) can be listed more than once if 139 | it has multiple CPUs or has more RAM. If the local machine is the 140 | best, you can also enlarge the nr_local_worker. For example: 141 | 142 | nr_local_worker = 2 143 | 144 | Example: 145 | 146 | > python grid.py heart_scale 147 | [local] -1 -1 78.8889 (best c=0.5, g=0.5, rate=78.8889) 148 | [linux5] -1 -7 83.3333 (best c=0.5, g=0.0078125, rate=83.3333) 149 | [linux5] 5 -1 77.037 (best c=0.5, g=0.0078125, rate=83.3333) 150 | [linux1] 5 -7 83.3333 (best c=0.5, g=0.0078125, rate=83.3333) 151 | . 152 | . 153 | . 154 | 155 | If -log2c, -log2g, or -v is not specified, default values are used. 156 | 157 | If your system uses telnet instead of ssh, you list the computer names 158 | in telnet_workers. 159 | 160 | Calling grid in Python 161 | ====================== 162 | 163 | In addition to using grid.py as a command-line tool, you can use it as a 164 | Python module. 165 | 166 | >>> rate, param = find_parameters(dataset, options) 167 | 168 | You need to specify `dataset' and `options' (default ''). See the following example. 169 | 170 | > python 171 | 172 | >>> from grid import * 173 | >>> rate, param = find_parameters('../heart_scale', '-log2c -1,1,1 -log2g -1,1,1') 174 | [local] 0.0 0.0 rate=74.8148 (best c=1.0, g=1.0, rate=74.8148) 175 | [local] 0.0 -1.0 rate=77.037 (best c=1.0, g=0.5, rate=77.037) 176 | . 177 | . 178 | [local] -1.0 -1.0 rate=78.8889 (best c=0.5, g=0.5, rate=78.8889) 179 | . 180 | . 181 | >>> rate 182 | 78.8889 183 | >>> param 184 | {'c': 0.5, 'g': 0.5} 185 | 186 | 187 | Part III: LIBSVM format checking tools 188 | 189 | Introduction 190 | ============ 191 | 192 | `svm-train' conducts only a simple check of the input data. To do a 193 | detailed check, we provide a python script `checkdata.py.' 194 | 195 | Usage: checkdata.py dataset 196 | 197 | Exit status (returned value): 1 if there are errors, 0 otherwise. 198 | 199 | This tool is written by Rong-En Fan at National Taiwan University. 200 | 201 | Example 202 | ======= 203 | 204 | > cat bad_data 205 | 1 3:1 2:4 206 | > python checkdata.py bad_data 207 | line 1: feature indices must be in an ascending order, previous/current features 3:1 2:4 208 | Found 1 lines with error. 209 | 210 | 211 | -------------------------------------------------------------------------------- /libsvm-3.22/tools/checkdata.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # 4 | # A format checker for LIBSVM 5 | # 6 | 7 | # 8 | # Copyright (c) 2007, Rong-En Fan 9 | # 10 | # All rights reserved. 11 | # 12 | # This program is distributed under the same license of the LIBSVM package. 13 | # 14 | 15 | from sys import argv, exit 16 | import os.path 17 | 18 | def err(line_no, msg): 19 | print("line {0}: {1}".format(line_no, msg)) 20 | 21 | # works like float() but does not accept nan and inf 22 | def my_float(x): 23 | if x.lower().find("nan") != -1 or x.lower().find("inf") != -1: 24 | raise ValueError 25 | 26 | return float(x) 27 | 28 | def main(): 29 | if len(argv) != 2: 30 | print("Usage: {0} dataset".format(argv[0])) 31 | exit(1) 32 | 33 | dataset = argv[1] 34 | 35 | if not os.path.exists(dataset): 36 | print("dataset {0} not found".format(dataset)) 37 | exit(1) 38 | 39 | line_no = 1 40 | error_line_count = 0 41 | for line in open(dataset, 'r'): 42 | line_error = False 43 | 44 | # each line must end with a newline character 45 | if line[-1] != '\n': 46 | err(line_no, "missing a newline character in the end") 47 | line_error = True 48 | 49 | nodes = line.split() 50 | 51 | # check label 52 | try: 53 | label = nodes.pop(0) 54 | 55 | if label.find(',') != -1: 56 | # multi-label format 57 | try: 58 | for l in label.split(','): 59 | l = my_float(l) 60 | except: 61 | err(line_no, "label {0} is not a valid multi-label form".format(label)) 62 | line_error = True 63 | else: 64 | try: 65 | label = my_float(label) 66 | except: 67 | err(line_no, "label {0} is not a number".format(label)) 68 | line_error = True 69 | except: 70 | err(line_no, "missing label, perhaps an empty line?") 71 | line_error = True 72 | 73 | # check features 74 | prev_index = -1 75 | for i in range(len(nodes)): 76 | try: 77 | (index, value) = nodes[i].split(':') 78 | 79 | index = int(index) 80 | value = my_float(value) 81 | 82 | # precomputed kernel's index starts from 0 and LIBSVM 83 | # checks it. Hence, don't treat index 0 as an error. 84 | if index < 0: 85 | err(line_no, "feature index must be positive; wrong feature {0}".format(nodes[i])) 86 | line_error = True 87 | elif index <= prev_index: 88 | err(line_no, "feature indices must be in an ascending order, previous/current features {0} {1}".format(nodes[i-1], nodes[i])) 89 | line_error = True 90 | prev_index = index 91 | except: 92 | err(line_no, "feature '{0}' not an : pair, integer, real number ".format(nodes[i])) 93 | line_error = True 94 | 95 | line_no += 1 96 | 97 | if line_error: 98 | error_line_count += 1 99 | 100 | if error_line_count > 0: 101 | print("Found {0} lines with error.".format(error_line_count)) 102 | return 1 103 | else: 104 | print("No error.") 105 | return 0 106 | 107 | if __name__ == "__main__": 108 | exit(main()) 109 | -------------------------------------------------------------------------------- /libsvm-3.22/tools/easy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import os 5 | from subprocess import * 6 | 7 | if len(sys.argv) <= 1: 8 | print('Usage: {0} training_file [testing_file]'.format(sys.argv[0])) 9 | raise SystemExit 10 | 11 | # svm, grid, and gnuplot executable files 12 | 13 | is_win32 = (sys.platform == 'win32') 14 | if not is_win32: 15 | svmscale_exe = "../svm-scale" 16 | svmtrain_exe = "../svm-train" 17 | svmpredict_exe = "../svm-predict" 18 | grid_py = "./grid.py" 19 | gnuplot_exe = "/usr/bin/gnuplot" 20 | else: 21 | # example for windows 22 | svmscale_exe = r"..\windows\svm-scale.exe" 23 | svmtrain_exe = r"..\windows\svm-train.exe" 24 | svmpredict_exe = r"..\windows\svm-predict.exe" 25 | gnuplot_exe = r"c:\tmp\gnuplot\binary\pgnuplot.exe" 26 | grid_py = r".\grid.py" 27 | 28 | assert os.path.exists(svmscale_exe),"svm-scale executable not found" 29 | assert os.path.exists(svmtrain_exe),"svm-train executable not found" 30 | assert os.path.exists(svmpredict_exe),"svm-predict executable not found" 31 | assert os.path.exists(gnuplot_exe),"gnuplot executable not found" 32 | assert os.path.exists(grid_py),"grid.py not found" 33 | 34 | train_pathname = sys.argv[1] 35 | assert os.path.exists(train_pathname),"training file not found" 36 | file_name = os.path.split(train_pathname)[1] 37 | scaled_file = file_name + ".scale" 38 | model_file = file_name + ".model" 39 | range_file = file_name + ".range" 40 | 41 | if len(sys.argv) > 2: 42 | test_pathname = sys.argv[2] 43 | file_name = os.path.split(test_pathname)[1] 44 | assert os.path.exists(test_pathname),"testing file not found" 45 | scaled_test_file = file_name + ".scale" 46 | predict_test_file = file_name + ".predict" 47 | 48 | cmd = '{0} -s "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, train_pathname, scaled_file) 49 | print('Scaling training data...') 50 | Popen(cmd, shell = True, stdout = PIPE).communicate() 51 | 52 | cmd = '{0} -svmtrain "{1}" -gnuplot "{2}" "{3}"'.format(grid_py, svmtrain_exe, gnuplot_exe, scaled_file) 53 | print('Cross validation...') 54 | f = Popen(cmd, shell = True, stdout = PIPE).stdout 55 | 56 | line = '' 57 | while True: 58 | last_line = line 59 | line = f.readline() 60 | if not line: break 61 | c,g,rate = map(float,last_line.split()) 62 | 63 | print('Best c={0}, g={1} CV rate={2}'.format(c,g,rate)) 64 | 65 | cmd = '{0} -c {1} -g {2} "{3}" "{4}"'.format(svmtrain_exe,c,g,scaled_file,model_file) 66 | print('Training...') 67 | Popen(cmd, shell = True, stdout = PIPE).communicate() 68 | 69 | print('Output model: {0}'.format(model_file)) 70 | if len(sys.argv) > 2: 71 | cmd = '{0} -r "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, test_pathname, scaled_test_file) 72 | print('Scaling testing data...') 73 | Popen(cmd, shell = True, stdout = PIPE).communicate() 74 | 75 | cmd = '{0} "{1}" "{2}" "{3}"'.format(svmpredict_exe, scaled_test_file, model_file, predict_test_file) 76 | print('Testing...') 77 | Popen(cmd, shell = True).communicate() 78 | 79 | print('Output prediction: {0}'.format(predict_test_file)) 80 | -------------------------------------------------------------------------------- /libsvm-3.22/tools/subset.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os, sys, math, random 4 | from collections import defaultdict 5 | 6 | if sys.version_info[0] >= 3: 7 | xrange = range 8 | 9 | def exit_with_help(argv): 10 | print("""\ 11 | Usage: {0} [options] dataset subset_size [output1] [output2] 12 | 13 | This script randomly selects a subset of the dataset. 14 | 15 | options: 16 | -s method : method of selection (default 0) 17 | 0 -- stratified selection (classification only) 18 | 1 -- random selection 19 | 20 | output1 : the subset (optional) 21 | output2 : rest of the data (optional) 22 | If output1 is omitted, the subset will be printed on the screen.""".format(argv[0])) 23 | exit(1) 24 | 25 | def process_options(argv): 26 | argc = len(argv) 27 | if argc < 3: 28 | exit_with_help(argv) 29 | 30 | # default method is stratified selection 31 | method = 0 32 | subset_file = sys.stdout 33 | rest_file = None 34 | 35 | i = 1 36 | while i < argc: 37 | if argv[i][0] != "-": 38 | break 39 | if argv[i] == "-s": 40 | i = i + 1 41 | method = int(argv[i]) 42 | if method not in [0,1]: 43 | print("Unknown selection method {0}".format(method)) 44 | exit_with_help(argv) 45 | i = i + 1 46 | 47 | dataset = argv[i] 48 | subset_size = int(argv[i+1]) 49 | if i+2 < argc: 50 | subset_file = open(argv[i+2],'w') 51 | if i+3 < argc: 52 | rest_file = open(argv[i+3],'w') 53 | 54 | return dataset, subset_size, method, subset_file, rest_file 55 | 56 | def random_selection(dataset, subset_size): 57 | l = sum(1 for line in open(dataset,'r')) 58 | return sorted(random.sample(xrange(l), subset_size)) 59 | 60 | def stratified_selection(dataset, subset_size): 61 | labels = [line.split(None,1)[0] for line in open(dataset)] 62 | label_linenums = defaultdict(list) 63 | for i, label in enumerate(labels): 64 | label_linenums[label] += [i] 65 | 66 | l = len(labels) 67 | remaining = subset_size 68 | ret = [] 69 | 70 | # classes with fewer data are sampled first; otherwise 71 | # some rare classes may not be selected 72 | for label in sorted(label_linenums, key=lambda x: len(label_linenums[x])): 73 | linenums = label_linenums[label] 74 | label_size = len(linenums) 75 | # at least one instance per class 76 | s = int(min(remaining, max(1, math.ceil(label_size*(float(subset_size)/l))))) 77 | if s == 0: 78 | sys.stderr.write('''\ 79 | Error: failed to have at least one instance per class 80 | 1. You may have regression data. 81 | 2. Your classification data is unbalanced or too small. 82 | Please use -s 1. 83 | ''') 84 | sys.exit(-1) 85 | remaining -= s 86 | ret += [linenums[i] for i in random.sample(xrange(label_size), s)] 87 | return sorted(ret) 88 | 89 | def main(argv=sys.argv): 90 | dataset, subset_size, method, subset_file, rest_file = process_options(argv) 91 | #uncomment the following line to fix the random seed 92 | #random.seed(0) 93 | selected_lines = [] 94 | 95 | if method == 0: 96 | selected_lines = stratified_selection(dataset, subset_size) 97 | elif method == 1: 98 | selected_lines = random_selection(dataset, subset_size) 99 | 100 | #select instances based on selected_lines 101 | dataset = open(dataset,'r') 102 | prev_selected_linenum = -1 103 | for i in xrange(len(selected_lines)): 104 | for cnt in xrange(selected_lines[i]-prev_selected_linenum-1): 105 | line = dataset.readline() 106 | if rest_file: 107 | rest_file.write(line) 108 | subset_file.write(dataset.readline()) 109 | prev_selected_linenum = selected_lines[i] 110 | subset_file.close() 111 | 112 | if rest_file: 113 | for line in dataset: 114 | rest_file.write(line) 115 | rest_file.close() 116 | dataset.close() 117 | 118 | if __name__ == '__main__': 119 | main(sys.argv) 120 | 121 | -------------------------------------------------------------------------------- /libsvm-3.22/windows/libsvm.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/windows/libsvm.dll -------------------------------------------------------------------------------- /libsvm-3.22/windows/libsvmread.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/windows/libsvmread.mexw64 -------------------------------------------------------------------------------- /libsvm-3.22/windows/libsvmwrite.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/windows/libsvmwrite.mexw64 -------------------------------------------------------------------------------- /libsvm-3.22/windows/svm-predict.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/windows/svm-predict.exe -------------------------------------------------------------------------------- /libsvm-3.22/windows/svm-scale.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/windows/svm-scale.exe -------------------------------------------------------------------------------- /libsvm-3.22/windows/svm-toy.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/windows/svm-toy.exe -------------------------------------------------------------------------------- /libsvm-3.22/windows/svm-train.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/windows/svm-train.exe -------------------------------------------------------------------------------- /libsvm-3.22/windows/svmpredict.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/windows/svmpredict.mexw64 -------------------------------------------------------------------------------- /libsvm-3.22/windows/svmtrain.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/libsvm-3.22/windows/svmtrain.mexw64 -------------------------------------------------------------------------------- /training_indexes/in_1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PuhongDuan/MSTV-Noise-Robust-Hyperspectral-Image-Classification-via-Multi-Scale-Total-Variation/68d4e5d388de643550e090bb20644a134134aeb0/training_indexes/in_1.mat --------------------------------------------------------------------------------