1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
/*!
* \file
* \brief Implementation of a Gaussian Mixture Model Class
* \author Thomas Eriksson
*
* -------------------------------------------------------------------------
*
* IT++ - C++ library of mathematical, signal processing, speech processing,
* and communications classes and functions
*
* Copyright (C) 1995-2008 (see AUTHORS file for a list of contributors)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* -------------------------------------------------------------------------
*/
#include <itpp/srccode/gmm.h>
#include <itpp/srccode/vqtrain.h>
#include <itpp/base/math/elem_math.h>
#include <itpp/base/matfunc.h>
#include <itpp/base/specmat.h>
#include <itpp/base/random.h>
#include <itpp/base/timing.h>
#include <iostream>
#include <fstream>
//! \cond
namespace itpp {
GMM::GMM()
{
d=0;
M=0;
}
GMM::GMM(std::string filename)
{
load(filename);
}
GMM::GMM(int M_in, int d_in)
{
M=M_in;
d=d_in;
m=zeros(M*d);
sigma=zeros(M*d);
w=1./M*ones(M);
for (int i=0;i<M;i++) {
w(i)=1.0/M;
}
compute_internals();
}
void GMM::init_from_vq(const vec &codebook, int dim)
{
mat C(dim,dim);
int i;
vec v;
d=dim;
M=codebook.length()/dim;
m=codebook;
w=ones(M)/double(M);
C.clear();
for (i=0;i<M;i++) {
v=codebook.mid(i*d,d);
C=C+outer_product(v,v);
}
C=1./M*C;
sigma.set_length(M*d);
for (i=0;i<M;i++) {
sigma.replace_mid(i*d,diag(C));
}
compute_internals();
}
void GMM::init(const vec &w_in, const mat &m_in, const mat &sigma_in)
{
int i,j;
d=m_in.rows();
M=m_in.cols();
m.set_length(M*d);
sigma.set_length(M*d);
for (i=0;i<M;i++) {
for (j=0;j<d;j++) {
m(i*d+j)=m_in(j,i);
sigma(i*d+j)=sigma_in(j,i);
}
}
w=w_in;
compute_internals();
}
void GMM::set_mean(const mat &m_in)
{
int i,j;
d=m_in.rows();
M=m_in.cols();
m.set_length(M*d);
for (i=0;i<M;i++) {
for (j=0;j<d;j++) {
m(i*d+j)=m_in(j,i);
}
}
compute_internals();
}
void GMM::set_mean(int i, const vec &means, bool compflag)
{
m.replace_mid(i*length(means),means);
if (compflag) compute_internals();
}
void GMM::set_covariance(const mat &sigma_in)
{
int i,j;
d=sigma_in.rows();
M=sigma_in.cols();
sigma.set_length(M*d);
for (i=0;i<M;i++) {
for (j=0;j<d;j++) {
sigma(i*d+j)=sigma_in(j,i);
}
}
compute_internals();
}
void GMM::set_covariance(int i, const vec &covariances, bool compflag)
{
sigma.replace_mid(i*length(covariances),covariances);
if (compflag) compute_internals();
}
void GMM::marginalize(int d_new)
{
it_error_if(d_new>d,"GMM.marginalize: cannot change to a larger dimension");
vec mnew(d_new*M),sigmanew(d_new*M);
int i,j;
for (i=0;i<M;i++) {
for (j=0;j<d_new;j++) {
mnew(i*d_new+j)=m(i*d+j);
sigmanew(i*d_new+j)=sigma(i*d+j);
}
}
m=mnew;
sigma=sigmanew;
d=d_new;
compute_internals();
}
void GMM::join(const GMM &newgmm)
{
if (d==0) {
w=newgmm.w;
m=newgmm.m;
sigma=newgmm.sigma;
d=newgmm.d;
M=newgmm.M;
} else {
it_error_if( d!=newgmm.d,"GMM.join: cannot join GMMs of different dimension");
w=concat(double(M)/(M+newgmm.M)*w,double(newgmm.M)/(M+newgmm.M)*newgmm.w);
w=w/sum(w);
m=concat(m,newgmm.m);
sigma=concat(sigma,newgmm.sigma);
M=M+newgmm.M;
}
compute_internals();
}
void GMM::clear()
{
w.set_length(0);
m.set_length(0);
sigma.set_length(0);
d=0;
M=0;
}
void GMM::save(std::string filename)
{
std::ofstream f(filename.c_str());
int i,j;
f << M << " " << d << std::endl ;
for (i=0;i<w.length();i++) {
f << w(i) << std::endl ;
}
for (i=0;i<M;i++) {
f << m(i*d) ;
for (j=1;j<d;j++) {
f << " " << m(i*d+j) ;
}
f << std::endl ;
}
for (i=0;i<M;i++) {
f << sigma(i*d) ;
for (j=1;j<d;j++) {
f << " " << sigma(i*d+j) ;
}
f << std::endl ;
}
}
void GMM::load(std::string filename)
{
std::ifstream GMMFile(filename.c_str());
int i,j;
it_error_if(!GMMFile,std::string("GMM::load : cannot open file ")+filename);
GMMFile >> M >> d ;
w.set_length(M);
for (i=0;i<M;i++) {
GMMFile >> w(i) ;
}
m.set_length(M*d);
for (i=0;i<M;i++) {
for (j=0;j<d;j++) {
GMMFile >> m(i*d+j) ;
}
}
sigma.set_length(M*d);
for (i=0;i<M;i++) {
for (j=0;j<d;j++) {
GMMFile >> sigma(i*d+j) ;
}
}
compute_internals();
std::cout << " mixtures:" << M << " dim:" << d << std::endl ;
}
double GMM::likelihood(const vec &x)
{
double fx=0;
int i;
for (i=0;i<M;i++) {
fx+=w(i)*likelihood_aposteriori(x, i);
}
return fx;
}
vec GMM::likelihood_aposteriori(const vec &x)
{
vec v(M);
int i;
for (i=0;i<M;i++) {
v(i)=w(i)*likelihood_aposteriori(x, i);
}
return v;
}
double GMM::likelihood_aposteriori(const vec &x, int mixture)
{
int j;
double s;
it_error_if(d!=x.length(),"GMM::likelihood_aposteriori : dimensions does not match");
s=0;
for (j=0;j<d;j++) {
s+=normexp(mixture*d+j)*sqr(x(j)-m(mixture*d+j));
}
return normweight(mixture)*std::exp(s);;
}
void GMM::compute_internals()
{
int i,j;
double s;
double constant=1.0/std::pow(2*pi,d/2.0);
normweight.set_length(M);
normexp.set_length(M*d);
for (i=0;i<M;i++) {
s=1;
for (j=0;j<d;j++) {
normexp(i*d+j)=-0.5/sigma(i*d+j); // check time
s*=sigma(i*d+j);
}
normweight(i) = constant/std::sqrt(s);
}
}
vec GMM::draw_sample()
{
static bool first=true;
static vec cumweight;
double u=randu();
int k;
if (first) {
first=false;
cumweight=cumsum(w);
it_error_if(std::abs(cumweight(length(cumweight)-1)-1)>1e-6,"weight does not sum to 0");
cumweight(length(cumweight)-1)=1;
}
k=0;
while (u>cumweight(k)) k++;
return elem_mult(sqrt(sigma.mid(k*d,d)),randn(d))+m.mid(k*d,d);
}
GMM gmmtrain(Array<vec> &TrainingData, int M, int NOITER, bool VERBOSE)
{
mat mean;
int i,j,d=TrainingData(0).length();
vec sig;
GMM gmm(M,d);
vec m(d*M);
vec sigma(d*M);
vec w(M);
vec normweight(M);
vec normexp(d*M);
double LL=0,LLold,fx;
double constant=1.0/std::pow(2*pi,d/2.0);
int T=TrainingData.length();
vec x1;
int t,n;
vec msum(d*M);
vec sigmasum(d*M);
vec wsum(M);
vec p_aposteriori(M);
vec x2;
double s;
vec temp1,temp2;
//double MINIMUM_VARIANCE=0.03;
//-----------initialization-----------------------------------
mean=vqtrain(TrainingData,M,200000,0.5,VERBOSE);
for (i=0;i<M;i++) gmm.set_mean(i,mean.get_col(i),false);
// for (i=0;i<M;i++) gmm.set_mean(i,TrainingData(randi(0,TrainingData.length()-1)),false);
sig=zeros(d);
for (i=0;i<TrainingData.length();i++) sig+=sqr(TrainingData(i));
sig/=TrainingData.length();
for (i=0;i<M;i++) gmm.set_covariance(i,0.5*sig,false);
gmm.set_weight(1.0/M*ones(M));
//-----------optimization-----------------------------------
tic();
for (i=0;i<M;i++) {
temp1=gmm.get_mean(i);
temp2=gmm.get_covariance(i);
for (j=0;j<d;j++) {
m(i*d+j)=temp1(j);
sigma(i*d+j)=temp2(j);
}
w(i)=gmm.get_weight(i);
}
for (n=0;n<NOITER;n++) {
for (i=0;i<M;i++) {
s=1;
for (j=0;j<d;j++) {
normexp(i*d+j)=-0.5/sigma(i*d+j); // check time
s*=sigma(i*d+j);
}
normweight(i) = constant*w(i)/std::sqrt(s);
}
LLold=LL;
wsum.clear();
msum.clear();
sigmasum.clear();
LL=0;
for (t=0;t<T;t++) {
x1=TrainingData(t);
x2=sqr(x1);
fx=0;
for (i=0;i<M;i++) {
s=0;
for (j=0;j<d;j++) {
s+=normexp(i*d+j)*sqr(x1(j)-m(i*d+j));
}
p_aposteriori(i)=normweight(i)*std::exp(s);
fx+=p_aposteriori(i);
}
p_aposteriori/=fx;
LL=LL+std::log(fx);
for (i=0;i<M;i++) {
wsum(i)+=p_aposteriori(i);
for (j=0;j<d;j++) {
msum(i*d+j)+=p_aposteriori(i)*x1(j);
sigmasum(i*d+j)+=p_aposteriori(i)*x2(j);
}
}
}
for (i=0;i<M;i++) {
for (j=0;j<d;j++) {
m(i*d+j)=msum(i*d+j)/wsum(i);
sigma(i*d+j)=sigmasum(i*d+j)/wsum(i)-sqr(m(i*d+j));
}
w(i)=wsum(i)/T;
}
LL=LL/T;
if (std::abs((LL-LLold)/LL) < 1e-6) break;
if (VERBOSE) {
std::cout << n << ": " << LL << " " << std::abs((LL-LLold)/LL) << " " << toc() << std::endl ;
std::cout << "---------------------------------------" << std::endl ;
tic();
} else {
std::cout << n << ": LL = " << LL << " " << std::abs((LL-LLold)/LL) << "\r" ;std::cout.flush();
}
}
for (i=0;i<M;i++) {
gmm.set_mean(i,m.mid(i*d,d),false);
gmm.set_covariance(i,sigma.mid(i*d,d),false);
}
gmm.set_weight(w);
return gmm;
}
} // namespace itpp
//! \endcond
|