⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 read_model.m

📁 CU SVM Classifier Matlab Toolbox
💻 M
字号:
function [output]=Read_Model(filename)
%

fid=fopen(filename,'r');
if(fid==-1) 
    fprintf('Error open file! \n');
    return;
end

while 1
    r=fgetl(fid);
    if r==-1
        break;
    end
    
    spaces=strfind(r, ' ');
    if isempty(spaces)
        cmd=r;
        if (strcmp(cmd,'SV')==1)
            break;
        elseif (strcmp(cmd,'alpha')==1)
            for I=1:output.nr_binary
                value=fgetl(fid);
                spaces=strfind(value, ' ');
                temp=value(1:spaces(1)-1);
                output.sv_ind(1,I)=str2num(temp);
                for J=2:output.nSV_binary(I)
                    temp=value(spaces(J-1)+1:spaces(J)-1);
                    output.sv_ind(J,I)=str2num(temp);
                end
                value=fgetl(fid);
                spaces=strfind(value, ' ');
                temp=value(1:spaces(1)-1);
                output.sv_coef(1,I)=str2num(temp);
                for J=2:output.nSV_binary(I)
                    temp=value(spaces(J-1)+1:spaces(J)-1);
                    output.sv_coef(J,I)=str2num(temp);
                end
            end
        end
    else
        cmd=r(1:spaces(1)-1);
        value=r(spaces(1)+1:size(r,2));
        if(strcmp(cmd,'svm_type')==1)
            if (strcmp(value,'c_svc')==1)
                output.svm_type=0;
            elseif (strcmp(value,'nu_svc')==1)
                output.svm_type=1;
            elseif (strcmp(value,'one_class')==1)
                output.svm_type=2;
            elseif (strcmp(value,'epsilon_svr')==1)
                output.svm_type=3;
            elseif (strcmp(value,'nu_svr')==1)
                output.svm_type=4;
            else
                fprintf('Unknown text in model file!\n');
                fclose(fid);
                return;
            end
        elseif (strcmp(cmd,'kernel_type')==1)
            if (strcmp(value,'linear')==1)
                output.kernel_type=0;
            elseif (strcmp(value,'polynomial')==1)
                output.kernel_type=1;
            elseif (strcmp(value,'rbf')==1)
                output.kernel_type=2;
            elseif (strcmp(value,'sigmoid')==1)
                output.kernel_type=3;
            else
                fprintf('Unknown text in model file!\n');
                fclose(fid);
                return;
            end
        elseif (strcmp(cmd,'degree')==1)
            output.degree=str2num(value);
        elseif (strcmp(cmd,'gamma')==1)
            output.gamma=str2num(value);
        elseif (strcmp(cmd,'coef0')==1)
            output.coef0=str2num(value);
        elseif (strcmp(cmd,'nr_class')==1)
            output.nr_class=str2num(value);
        elseif (strcmp(cmd,'nr_binary')==1)
            output.nr_binary=str2num(value);    
        elseif (strcmp(cmd,'total_sv')==1)
            output.total_sv=str2num(value);
        elseif (strcmp(cmd,'rho')==1)
            n=output.nr_binary;
            sep=strfind(value, ' ');
            if size(sep,2)~=n-1;
                fprintf('Error load rho!\n');
                fclose(fid);
                return;
            end
            
            if ~isempty(sep)
                temp=value(1:sep(1)-1);
                output.rhos(1)=str2num(temp);
                if (n-2)>=1
                    for I=1:n-2
                        temp=value(sep(I)+1:sep(I+1)-1);
                        output.rhos(I+1)=str2num(temp);
                    end
                end
                temp=value(sep(n-1)+1:size(value,2));
                output.rhos(n)=str2num(temp);
            else
                output.rhos(1)=str2num(value);
            end
        elseif (strcmp(cmd,'label')==1)
            n=output.nr_class;
            sep=strfind(value, ' ');
            if size(sep,2)~=n-1;
                fprintf('Error load label!\n');
                fclose(fid);
                return;
            end
            
            if ~isempty(sep)
                temp=value(1:sep(1)-1);
                output.labels(1)=str2num(temp);
                if (n-2)>=1
                    for I=1:n-2
                        temp=value(sep(I)+1:sep(I+1)-1);
                        output.labels(I+1)=str2num(temp);
                    end
                end
                temp=value(sep(n-1)+1:size(value,2));
                output.labels(n)=str2num(temp);
            else
                output.labels(1)=str2num(value);
            end
        elseif (strcmp(cmd,'probA')==1)
            n=output.nr_binary;
            sep=strfind(value, ' ');
            if size(sep,2)~=n-1;
                fprintf('Error load probA!\n');
                fclose(fid);
                return;
            end
            
            if ~isempty(sep)
                temp=value(1:sep(1)-1);
                output.probA(1)=str2num(temp);
                if (n-2)>=1
                    for I=1:n-2
                        temp=value(sep(I)+1:sep(I+1)-1);
                        output.probA(I+1)=str2num(temp);
                    end
                end
                temp=value(sep(n-1)+1:size(value,2));
                output.probA(n)=str2num(temp);
            else
                output.probA(1)=str2num(value);
            end
        elseif (strcmp(cmd,'probB')==1)
            n=output.nr_binary;
            sep=strfind(value, ' ');
            if size(sep,2)~=n-1;
                fprintf('Error load probB!\n');
                fclose(fid);
                return;
            end
            
            if ~isempty(sep)
                temp=value(1:sep(1)-1);
                output.probB(1)=str2num(temp);
                if (n-2)>=1
                    for I=1:n-2
                        temp=value(sep(I)+1:sep(I+1)-1);
                        output.probB(I+1)=str2num(temp);
                    end
                end
                temp=value(sep(n-1)+1:size(value,2));
                output.probB(n)=str2num(temp);
            else
                output.probB(1)=str2num(value);
            end
        elseif (strcmp(cmd,'nr_sv')==1)
            n=output.nr_class;
            sep=strfind(value, ' ');
            if size(sep,2)~=n-1;
                fprintf('Error load nSV!\n');
                fclose(fid);
                return;
            end
            
            if ~isempty(sep)
                temp=value(1:sep(1)-1);
                output.nSV(1)=str2num(temp);
                if (n-2)>=1
                    for I=1:n-2
                        temp=value(sep(I)+1:sep(I+1)-1);
                        output.nSV(I+1)=str2num(temp);
                    end
                end
                temp=value(sep(n-1)+1:size(value,2));
                output.nSV(n)=str2num(temp);
            else
                output.nSV(1)=str2num(value);
            end
         elseif (strcmp(cmd,'nr_sv_binary')==1)
            n=output.nr_binary;
            sep=strfind(value, ' ');
            if size(sep,2)~=n-1;
                fprintf('Error load nSV_binary!\n');
                fclose(fid);
                return;
            end
            
            if ~isempty(sep)
                temp=value(1:sep(1)-1);
                output.nSV_binary(1)=str2num(temp);
                if (n-2)>=1
                    for I=1:n-2
                        temp=value(sep(I)+1:sep(I+1)-1);
                        output.nSV_binary(I+1)=str2num(temp);
                    end
                end
                temp=value(sep(n-1)+1:size(value,2));
                output.nSV_binary(n)=str2num(temp);
            else
                output.nSV_binary(1)=str2num(value);
            end
        elseif (strcmp(cmd,'I')==1)
            output.multiclass_type=str2num(value);
            for I=1:output.nr_binary
                value=fgetl(fid);
                spaces=strfind(value, ' ');
                if ~isempty(spaces)
                    nr_Ip=str2num(value(1:spaces(1)-1));
                else
                    nr_Ip=str2num(value);
                end
                value=value(spaces(1)+1:size(value,2));
                n=nr_Ip;
                sep=strfind(value, ' ');
                if size(sep,2)~=n-1;
                    fprintf('Error load I!\n');
                    fclose(fid);
                    return;
                end
                if ~isempty(sep)
                    temp=value(1:sep(1)-1);
                    output.I(str2num(temp)+1,I)=1;
                    if (n-2)>=1
                        for J=1:n-2
                            temp=value(sep(J)+1:sep(J+1)-1);
                            output.I(str2num(temp)+1,I)=1;
                        end
                    end
                    temp=value(sep(n-1)+1:size(value,2));
                    output.I(str2num(temp)+1,I)=1;
                else
                    output.I(str2num(value)+1,I)=1;
                end
                
                value=fgetl(fid);
                spaces=strfind(value, ' ');
                if ~isempty(spaces)
                    nr_In=str2num(value(1:spaces(1)-1));
                else
                    nr_In=str2num(value);
                end
                value=value(spaces(1)+1:size(value,2));
                n=nr_In;
                sep=strfind(value, ' ');
                if size(sep,2)~=n-1;
                    fprintf('Error load I!\n');
                    fclose(fid);
                    return;
                end
                if ~isempty(sep)
                    temp=value(1:sep(1)-1);
                    output.I(str2num(temp)+1,I)=-1;
                    if (n-2)>=1
                        for J=1:n-2
                            temp=value(sep(J)+1:sep(J+1)-1);
                            output.I(str2num(temp)+1,I)=-1;
                        end
                    end
                    temp=value(sep(n-1)+1:size(value,2));
                    output.I(str2num(temp)+1,I)=-1;
                else
                    output.I(str2num(value)+1,I)=-1;
                end
            end
        else
            fprintf('Unknown text in model file2!\n');
            fclose(fid);
            return;
        end
    end
end

J=0;
while 1
    r=fgetl(fid);
    if r==-1
        break;
    end
    J=J+1;
    spaces=strfind(r, ' ');
    colons=strfind(r, ':');
    spacenum=size(spaces,2);
    colonnum=size(colons,2);
    
    if spacenum~=colonnum
            fprintf('Error read SV!\n');
            fclose(fid);
            return;
    end
    
    temp=r(1:colons(1)-1);
    idx=str2num(temp);
    temp=r(colons(1)+1:spaces(1)-1);
    output.SV(idx,J)=str2num(temp);
    for I=1:(colonnum-1)
        temp=r(spaces(I)+1:colons(I+1)-1);
        idx=str2num(temp);
        temp=r(colons(I+1)+1:spaces(I+1)-1);
        output.SV(idx,J)=str2num(temp);
    end
end
output.data_dim=size(output.SV,1);
fclose(fid);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -