├── README.md ├── cmvnpdf.m ├── covfixer2.m ├── gauss.m ├── getDCT.m ├── getargs.m ├── gmm.m ├── main.m ├── model.mat └── pictures ├── 1080.jpeg ├── 1081.jpeg ├── 1082.jpeg ├── 1083.jpeg ├── 1084.jpeg ├── 1085.jpeg ├── 1086.jpeg ├── 1087.jpeg ├── 1088.jpeg ├── 1089.jpeg ├── 1090.jpeg ├── 1091.jpeg ├── 1092.jpeg ├── 1093.jpeg └── 1094.jpeg /README.md: -------------------------------------------------------------------------------- 1 | # SML 2 | class model for Supervised Multiclass Labeling(SML) which is a text annotation algorithm for images. 3 | corel5k中部分图像基于SML算法的类模型 4 | 5 | getDCT.m 实现单个图像的YBR空间转换,分块抽取DCT系数,并对系数进行降维 6 | 7 | gauss.m 为main.m的子函数,计算高斯分布密度函数 8 | 9 | cmnpdf.m 为gmm.m的子函数,计算多元正态概率密度函数 10 | covfixer2.m 为gmm.m的子函数,使协方差矩阵强制转换为有效的协方差矩阵 11 | getargs.m 为gmm.m的子函数,对函数的默认参数进行处理 12 | 13 | gmm.m 实现将每一幅图像聚类成8个混合高斯分布 14 | 15 | main.m 为主文件,结合上述文件中函数功能,读取文件夹中所有的图像,实现将多个高斯分布聚类成64个混合高斯分布 16 | 17 | 最终聚类的混合高斯分布参数存储在model.mat文件中。 18 | 19 | mat文件中包含64个高斯分布的μ值,sigma值,和π值。 20 | 21 | 22 | 说明:一个图像块的数据是192维的,为了使得提高程序的运行速率本程序中将数据压缩到30维。 23 | 24 | 具体原理详见博客: 25 | 有监督的多类标注(SML)的原理及matlab实现:http://blog.csdn.net/vivian_ll/article/details/79004473 26 | 使用EM算法估计GMM参数的原理及matlab实现:http://blog.csdn.net/vivian_ll/article/details/78793293 27 | -------------------------------------------------------------------------------- /cmvnpdf.m: -------------------------------------------------------------------------------- 1 | function y = cmvnpdf(X, Mu, Sigma) 2 | %CMVNPDF - (Complex range) multivariate normal probability density function (pdf). 3 | % Y = CMVNPDF(X) returns the n-by-1 vector Y, containing the probability 4 | % density of the multivariate normal distribution with zero mean and 5 | % identity covariance matrix, evaluated at each row of the n-by-d matrix 6 | % X. Rows of X correspond to observations and columns correspond to 7 | % variables or coordinates. 8 | % 9 | % Y = CMVNPDF(X,MU) returns the density of the multivariate normal 10 | % distribution with mean MU and identity covariance matrix, evaluated 11 | % at each row of X. MU is a 1-by-d vector, or an n-by-d matrix, in which 12 | % case the density is evaluated for each row of X with the corresponding 13 | % row of MU. MU can also be a scalar value, which CMVNPDF replicates to 14 | % match the size of X. 15 | % 16 | % Y = CMVNPDF(X,MU,SIGMA) returns the density of the multivariate normal 17 | % distribution with mean MU and covariance SIGMA, evaluated at each row 18 | % of X. SIGMA is a d-by-d matrix, or an d-by-d-by-n array, in which case 19 | % the density is evaluated for each row of X with the corresponding page 20 | % of SIGMA, i.e., CMVNPDF computes Y(I) using X(I,:) and SIGMA(:,:,I). 21 | % Pass in the empty matrix for MU to use its default value when you want 22 | % to only specify SIGMA. 23 | % 24 | % If X is a 1-by-d vector, CMVNPDF replicates it to match the leading 25 | % dimension of MU or the trailing dimension of SIGMA. 26 | % 27 | % Example: 28 | % 29 | % mu = [1 -1]; 30 | % Sigma = [.9 .4; .4 .3]; 31 | % X = mvnrnd(mu, Sigma, 10); 32 | % p = cmvnpdf(X, mu, Sigma); 33 | % 34 | % See also MVNRND, NORMPDF. 35 | 36 | % Copyright 1993-2002 The MathWorks, Inc. 37 | % Revision: 1.2 Date: 2002/03/28 16:51:27 38 | 39 | % Modified by Pekka Paalanen, LUT, 2003 40 | % 41 | % 42 | % $Name: $ 43 | % $Revision: 1.1 $ $Date: 2004/08/16 15:06:44 $ 44 | 45 | if nargin < 1 | isempty(X) 46 | error('Requires the input argument X.'); 47 | elseif ndims(X) > 2 48 | error('X must be a matrix.'); 49 | end 50 | 51 | % Get size of data. Column vectors provisionally interpreted as multiple scalar data. 52 | [n,d] = size(X); 53 | 54 | % Assume zero mean, data are already centered 55 | if nargin < 2 | isempty(Mu) 56 | X0 = X; 57 | 58 | % Get scalar mean, and use it to center data 59 | elseif prod(size(Mu)) == 1 60 | X0 = X - Mu; 61 | 62 | % Get vector mean, and use it to center data 63 | elseif ndims(Mu) == 2 64 | [n2,d2] = size(Mu); 65 | if d2 ~= d % has to have same number of coords as X 66 | error('X and MU must have the same number of columns.'); 67 | elseif n2 == n % lengths match 68 | X0 = X - Mu; 69 | elseif n2 == 1 % mean is a single row, rep it out to match data 70 | X0 = X - repmat(Mu,n,1); 71 | elseif n == 1 % data is a single row, rep it out to match mean 72 | n = n2; 73 | X0 = repmat(X,n2,1) - Mu; 74 | else % sizes don't match 75 | error('X or MU must be a row vector, or X and MU must have the same number of rows.'); 76 | end 77 | 78 | else 79 | error('MU must be a matrix.'); 80 | end 81 | 82 | % Assume identity covariance, data are already standardized 83 | if nargin < 3 | isempty(Sigma) 84 | % Special case: if Sigma isn't supplied, then interpret X 85 | % and Mu as row vectors if they were both column vectors 86 | if d == 1 & prod(size(X)) > 1 87 | X0 = X0.'; 88 | [n,d] = size(X0); 89 | end 90 | xRinv = X0; 91 | sqrtInvDetSigma = 1; 92 | 93 | % Single covariance matrix 94 | elseif ndims(Sigma) == 2 95 | % Special case: if Sigma is supplied, then use it to try to interpret 96 | % X and Mu as row vectors if they were both column vectors. 97 | if (d == 1 & prod(size(X)) > 1) & size(Sigma,1) == n 98 | X0 = X0.'; 99 | [n,d] = size(X0); 100 | end 101 | 102 | % Make sure Sigma is the right size 103 | if size(Sigma,1) ~= d | size(Sigma,2) ~= d 104 | error('SIGMA must be a square matrix with size equal to the number of columns in X.'); 105 | else 106 | % Make sure Sigma is a valid covariance matrix 107 | [spd,R] = isspd(Sigma); 108 | if spd 109 | % Create array of standardized data, vector of inverse det 110 | xRinv = X0 / R; 111 | sqrtInvDetSigma = 1 / prod(diag(R)); 112 | else 113 | error('SIGMA must be symmetric and positive definite.'); 114 | end 115 | end 116 | 117 | % Multiple covariance matrices 118 | elseif ndims(Sigma) == 3 119 | % Special case: if Sigma is supplied, then use it to try to interpret 120 | % X and Mu as row vectors if they were both column vectors. 121 | if (d == 1 & prod(size(X)) > 1) & size(Sigma,1) == n 122 | X0 = X0.'; 123 | [n,d] = size(X0); 124 | end 125 | 126 | % Data and mean are a single row, rep them out to match covariance 127 | if n == 1 % already know size(Sigma,3) > 1 128 | n = size(Sigma,3); 129 | X0 = repmat(X0,n,1); % rep centered data out to match cov 130 | end 131 | 132 | % Make sure Sigma is the right size 133 | if size(Sigma,1) ~= d | size(Sigma,2) ~= d 134 | error('Each page of SIGMA must be a square matrix with size equal to the number of columns in X.'); 135 | elseif size(Sigma,3) ~= n 136 | error('SIGMA must have one page for each row of X.'); 137 | else 138 | 139 | % Create array of standardized data, vector of inverse det 140 | xRinv = zeros(n,d); 141 | sqrtInvDetSigma = zeros(n,1); 142 | for i = 1:n 143 | % Make sure Sigma is a valid covariance matrix 144 | [spd,R] = isspd(Sigma(:,:,i)); 145 | if spd 146 | xRinv(i,:) = X0(i,:) / R; 147 | sqrtInvDetSigma(i) = 1 / prod(diag(R)); 148 | else 149 | error('SIGMA must be symmetric and positive definite.'); 150 | end 151 | end 152 | end 153 | 154 | elseif ndims(Sigma) > 3 155 | error('SIGMA must be a matrix or a 3 dimensional array.'); 156 | end 157 | 158 | % Exponents in pdf are the inner products of the standardized data 159 | %quadform = sum(xRinv.^2, 2); 160 | quadform = sum(xRinv.*conj(xRinv), 2); 161 | y = sqrt((2*pi)^(-d)) * sqrtInvDetSigma .* exp(-0.5*quadform); 162 | 163 | 164 | function [t,R] = isspd(Sigma) 165 | %ISPDS Test if a matrix is positive definite symmetric 166 | % T = ISPDS(SIGMA) returns a logical indicating whether the matrix SIGMA is 167 | % square, symmetric, and positive definite, i.e., it is a valid full rank 168 | % covariance matrix. 169 | % 170 | % [T,R] = ISPDS(SIGMA) returns the cholesky factor of SIGMA in R. If SIGMA 171 | % is not square symmetric, ISPDS returns [] in R. 172 | 173 | % Test for square, symmetric 174 | [n,m] = size(Sigma); 175 | if (n == m) & all(all(abs(Sigma - Sigma') < 10*eps*max(abs(diag(Sigma))))) 176 | % Test for positive definiteness 177 | [R,p] = chol(Sigma); 178 | if p == 0 179 | t = 1; 180 | else 181 | t = 0; 182 | end 183 | else 184 | R = []; 185 | t = 0; 186 | end -------------------------------------------------------------------------------- /covfixer2.m: -------------------------------------------------------------------------------- 1 | function [nsigma, varargout] = covfixer2(sigma); 2 | %COVFIXER2 - force matrix to be a valid covariance matrix 3 | % 4 | % covmatrix = COVFIXER2(matrix) 5 | % Matrix is forced (complex conjugate) symmetric, 6 | % positive definite and its diagonal real valued. 7 | % 8 | % [covmatrix, loops] = COVFIXER2(...) 9 | % loops - number of rounds the positive definite fixer had to run. 10 | % 11 | % [covmatrix, loops, symerr] = COVFIXER2(...) 12 | % symerr - symmetry error matrix 13 | 14 | % 15 | % $Name: $ 16 | % $Id: covfixer2.m,v 1.2 2004/08/18 07:52:12 paalanen Exp $ 17 | % Copyright 2003, Pekka Paalanen 18 | 19 | % except isspd() function which is from The MathWorks Matlab mvnpdf.m. 20 | 21 | D = size(sigma, 1); 22 | fixrate = 0.01; 23 | covfixmat = ones(D) + fixrate*eye(D); 24 | loops = 0; 25 | min_limit = eps*10; 26 | 27 | if ~all( isfinite(sigma(:)) ) 28 | error('covariance matrix is not finite'); 29 | end 30 | 31 | % Running imagfixer is not counted as covariance fixing, 32 | % the changes are assumed to be so small. 33 | nsigma = imagfixer(sigma); 34 | 35 | if nargout>2 36 | varargout(2) = {(sigma-nsigma)}; 37 | end 38 | 39 | while isspd(nsigma) == 0 40 | % covariance matrix is not positive definite 41 | % fix it 42 | loops = loops+1; 43 | d = diag(nsigma); 44 | if any(d <= min_limit) 45 | % negative or zero (1 66 | varargout(1) = {loops}; 67 | end 68 | 69 | 70 | % ------------------ 71 | 72 | function [t,R] = isspd(Sigma) 73 | %ISPDS Test if a matrix is positive definite symmetric 74 | % T = ISPDS(SIGMA) returns a logical indicating whether the matrix SIGMA is 75 | % square, symmetric, and positive definite, i.e., it is a valid full rank 76 | % covariance matrix. 77 | % 78 | % [T,R] = ISPDS(SIGMA) returns the cholesky factor of SIGMA in R. If SIGMA 79 | % is not square symmetric, ISPDS returns [] in R. 80 | 81 | % Copyright 1993-2002 The MathWorks, Inc. 82 | % Revision: 1.2 Date: 2002/03/28 16:51:27 83 | 84 | % Test for square, symmetric 85 | % NOTE: imagfixer already enforces squareness and symmetricity, 86 | % and fixing affects only the diagonal, so this is not necessary 87 | %[n,m] = size(Sigma); 88 | %if (n == m) & all(all(abs(Sigma - Sigma') < 10*eps*max(abs(diag(Sigma))))); 89 | 90 | % Test for positive definiteness 91 | [R,p] = chol(Sigma); 92 | if p == 0 93 | t = 1; 94 | else 95 | t = 0; 96 | end 97 | 98 | %else 99 | % R = []; 100 | % t = 0; 101 | %end 102 | 103 | % ------------------ 104 | 105 | function nsigma = imagfixer(sigma); 106 | 107 | % force symmetric 108 | nsigma = sigma - (sigma - sigma')/2; 109 | % purge imag 110 | purge = imag(diag(nsigma)); 111 | nsigma = nsigma - diag(purge)*1i; 112 | 113 | if max(purge) > 1e-4 114 | warning_wrap('gmmbayes:covfixer2:imagfixer', 'Quite big imaginary components removed from the diagonal'); 115 | end -------------------------------------------------------------------------------- /gauss.m: -------------------------------------------------------------------------------- 1 | function b = gauss(x,mu,sigma) 2 | d = length(mu); 3 | c = (2*pi)^(d/2); 4 | c = c*(det(sigma)^(1/2)); 5 | b= exp((-1/2)*((x-mu)'*inv(sigma)*(x-mu))); 6 | b = b/c; 7 | return 8 | end -------------------------------------------------------------------------------- /getDCT.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/getDCT.m -------------------------------------------------------------------------------- /getargs.m: -------------------------------------------------------------------------------- 1 | function S = getargs(defaultS, varglist); 2 | 3 | if mod(length(varglist),2) ~=0 4 | error('Odd number of variable parameters'); 5 | end 6 | 7 | S = defaultS; 8 | i=1; 9 | while i <= length(varglist) 10 | if isfield(S, varglist{i}) 11 | % for Matlab R12 12 | %S = setfield(S, varglist{i}, varglist{i+1}); 13 | 14 | % for Matlab R13 and above 15 | S.(varglist{i}) = varglist{i+1}; 16 | else 17 | warning_wrap('getargs:unknown_param', ... 18 | ['Unknown parameter "' varglist{i} '"']); 19 | end 20 | i = i+2; 21 | end -------------------------------------------------------------------------------- /gmm.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/gmm.m -------------------------------------------------------------------------------- /main.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/main.m -------------------------------------------------------------------------------- /model.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/model.mat -------------------------------------------------------------------------------- /pictures/1080.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1080.jpeg -------------------------------------------------------------------------------- /pictures/1081.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1081.jpeg -------------------------------------------------------------------------------- /pictures/1082.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1082.jpeg -------------------------------------------------------------------------------- /pictures/1083.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1083.jpeg -------------------------------------------------------------------------------- /pictures/1084.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1084.jpeg -------------------------------------------------------------------------------- /pictures/1085.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1085.jpeg -------------------------------------------------------------------------------- /pictures/1086.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1086.jpeg -------------------------------------------------------------------------------- /pictures/1087.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1087.jpeg -------------------------------------------------------------------------------- /pictures/1088.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1088.jpeg -------------------------------------------------------------------------------- /pictures/1089.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1089.jpeg -------------------------------------------------------------------------------- /pictures/1090.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1090.jpeg -------------------------------------------------------------------------------- /pictures/1091.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1091.jpeg -------------------------------------------------------------------------------- /pictures/1092.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1092.jpeg -------------------------------------------------------------------------------- /pictures/1093.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1093.jpeg -------------------------------------------------------------------------------- /pictures/1094.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vivianLL/SML/4462b76bd8e27c6a9232b740a70820e1fe7107c3/pictures/1094.jpeg --------------------------------------------------------------------------------