//------------------------------------------------------------------------- void A::deaccumulate(const Feature& f) { const unsigned long vectSize = f.getVectSize(); if (!_vectSizeDefined) { return; } else if (vectSize != _vectSize) throw Exception("Incompatible vectSize (" + String::valueOf(vectSize) + "/" + String::valueOf(_vectSize) + ")", __FILE__, __LINE__); const double* dataVect = f.getDataVector(); for (unsigned long i=0; i<_vectSize; i++) { const double v = dataVect[i]; _accVect[i] -= v; _xaccVect[i] -= v*v; } _count--; _computed = false; _stdComputed = false; }
void FactorAnalysisStat::computeAndAccumulateGeneralFAStats(SegCluster &selectedSegments,FeatureServer &fs,Config & config){ if (verbose) cout <<"(FactorAnalysisStat) Compute General FA Stats (Complete)" << endl; double *N_h, *N, *S_X_h, *S_X,*ff; _matN_h.setAllValues(0.0); _matN.setAllValues(0.0); _matS_X_h.setAllValues(0.0); _matS_X.setAllValues(0.0); N_h=_matN_h.getArray(); N=_matN.getArray(); S_X_h=_matS_X_h.getArray();S_X=_matS_X.getArray(); MixtureGD & UBM=_ms.getMixtureGD((unsigned long) 1); MixtureGDStat &acc=_ss.createAndStoreMixtureStat(UBM); // Compute Occupations and Statistics acc.resetOcc(); Seg *seg; selectedSegments.rewind(); String currentSource="";unsigned long loc=0;unsigned long sent=0; while((seg=selectedSegments.getSeg())!=NULL){ unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName()); // Idx of the first frame of the current file in the feature server if (currentSource!=seg->sourceName()) { currentSource=seg->sourceName(); loc=_ndxTable.locNb(currentSource); sent=_ndxTable.sessionNb(currentSource); if (verbose)cout << "Processing speaker["<<currentSource<<"]"<< endl; } fs.seekFeature(begin); Feature f; if (!_topGauss) { for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){ fs.readFeature(f); acc.computeAndAccumulateOcc(f); RealVector <double> aPost=acc.getOccVect(); ff=f.getDataVector(); for(unsigned long k=0;k<_mixsize;k++) { N_h[sent*_mixsize+k]+=aPost[k]; N[loc*_mixsize+k] +=aPost[k]; for (unsigned long i=0;i<_vsize;i++) { S_X_h[sent*_supervsize+(k*_vsize+i)]+=aPost[k]*ff[i]; S_X[loc*_supervsize+(k*_vsize+i)] +=aPost[k]*ff[i]; } } } } else throw Exception("ComputeGeneralStats TopGauss not done at this level",__FILE__,__LINE__); } };
/// Normalize features with a smooth mixture transformation o't=ot-sum(P(c|ot)Uc.x) void FactorAnalysisStat::normalizeFeatures(SegCluster &selectedSegments,FeatureServer &fs,Config & config){ if (verbose) cout << "(FactorAnalysisStat) Normalize Features" << endl; MixtureGD & clientMixture=_ms.getMixtureGD(1); // copy the UBM mixture unsigned long nt=0; RealVector <double> m_xh_1; m_xh_1.setSize(_supervsize); double *_m_xh_1=m_xh_1.getArray(); Seg *seg; // current selectd segment selectedSegments.rewind(); String currentSource=""; while((seg=selectedSegments.getSeg())!=NULL){ unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName()); if (currentSource!=seg->sourceName()) { currentSource=seg->sourceName(); this->getUX(m_xh_1,currentSource); this->getSpeakerModel(clientMixture,currentSource); if (verbose)cout << "Processing speaker["<<currentSource<<"]"<< endl; } fs.seekFeature(begin); Feature f; if (!_topGauss) { for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){ fs.readFeature(f,0); double *ff=f.getDataVector(); double sum=0.0; RealVector <double> P; P.setSize(_mixsize); double *Prob=P.getArray(); for(unsigned long k=0;k<_mixsize;k++) { Prob[k]=clientMixture.weight(k)*clientMixture.getDistrib(k).computeLK(f); sum+=Prob[k]; } for(unsigned long k=0;k<_mixsize;k++) Prob[k]/=sum; for(unsigned long k=0;k<_mixsize;k++) { for (unsigned long i=0;i<_vsize;i++) ff[i]-= Prob[k]*_m_xh_1[k*_vsize+i]; } fs.writeFeature(f); nt++; } } else { throw Exception("no topgauss yet",__FILE__,__LINE__); } } };
//------------------------------------------------------------------------- // TODO : A OPTIMISER !! //------------------------------------------------------------------------- lk_t DistribGD::computeLK(const Feature& frame) const { if (frame.getVectSize() != _vectSize) throw Exception("distrib vectSize (" + String::valueOf(_vectSize) + ") != feature vectSize (" + String::valueOf(frame.getVectSize()) + ")", __FILE__, __LINE__); real_t tmp = 0.0; real_t* m = _meanVect.getArray(); real_t* c = _covInvVect.getArray(); Feature::data_t* f = frame.getDataVector(); for (unsigned long i=0; i<_vectSize; i++) tmp += (f[i] - m[i]) * (f[i] - m[i]) * c[i]; tmp = _cst * exp(-0.5*tmp); if (ISNAN(tmp)) return EPS_LK; return tmp; }
//------------------------------------------------------------------------- bool M::writeFeature(const Feature& f, unsigned long step) { bool ok; if (!_useMask) ok = _pInput->writeFeature(f, step); else { if (_selectionSize != f.getVectSize()) throw Exception("Invalid feature mask : " + _mask, __FILE__, __LINE__); _feature.setVectSize(K::k, _pInput->getVectSize()); Feature::data_t* outputVect = _feature.getDataVector(); Feature::data_t* inputVect = f.getDataVector(); for (unsigned long i=0; i<_selectionSize; i++) outputVect[_selection[i]] = inputVect[i]; ok = _pInput->writeFeature(_feature, step); } _error = _pInput->getError(); return ok; }