modelinitializerloader.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 921 行 · 第 1/2 页
JAVA
921 行
// TODO: this should be flexible, but we're hardwiring for now int numStreams = 1; // Since we're initializing, we start simple. int numGaussiansPerState = 1; String version; boolean sameSizedModels; boolean tmatSkip; ExtendedStreamTokenizer est = new ExtendedStreamTokenizer (inputStream, '#', false); // Pool pool = new Pool(path); // Initialize the pools we'll need. meansPool = new Pool("means"); variancePool = new Pool("variances"); mixtureWeightsPool = new Pool("mixtureweights"); matrixPool = new Pool("transitionmatrices"); senonePool = new Pool("senones"); float distFloor = props.getFloat(TiedStateAcousticModel.PROP_MC_FLOOR, TiedStateAcousticModel.PROP_MC_FLOOR_DEFAULT); float mixtureWeightFloor = props.getFloat(TiedStateAcousticModel.PROP_MW_FLOOR, TiedStateAcousticModel.PROP_MW_FLOOR_DEFAULT); float transitionProbabilityFloor = props.getFloat(TiedStateAcousticModel.PROP_TP_FLOOR, TiedStateAcousticModel.PROP_TP_FLOOR_DEFAULT); float varianceFloor = props.getFloat(TiedStateAcousticModel.PROP_VARIANCE_FLOOR, TiedStateAcousticModel.PROP_VARIANCE_FLOOR_DEFAULT); logger.info("Loading phone list file from: "); logger.info(path); // At this point, we only accept version 0.1 version = "0.1"; est.expectString("version"); est.expectString(version); est.expectString("same_sized_models"); sameSizedModels = est.getString().equals("yes"); if (sameSizedModels) { est.expectString("n_state"); numState = est.getInt("numBase"); } // for this phone list version, let's assume left-to-right // models, with optional state skip. est.expectString("tmat_skip"); tmatSkip = est.getString().equals("yes"); // Load the phones with sizes // stateIndex contains the absolute state index, that is, a // unique index in the senone pool. int stateIndex; int unitCount; String attribute; for (stateIndex = 0, unitCount = 0;;) { String phone = est.getString(); if (est.isEOF()) { break; } int size = numState; if (!sameSizedModels) { size = est.getInt("ModelSize"); } phoneList.put(phone, new Integer(size)); logger.fine("Phone: " + phone + " size: " + size); int[] stid = new int[size]; String position = "-"; for (int j = 0; j < size; j++, stateIndex++) { stid[j] = stateIndex; } // The first filler if (phone.equals(SILENCE_CIPHONE)) { attribute = FILLER; } else { attribute = "-"; } Unit unit = Unit.getUnit(phone, attribute.equals(FILLER)); contextIndependentUnits.put(unit.getName(), unit); if (logger.isLoggable(Level.FINE)) { logger.fine("Loaded " + unit + " with " + size + " states"); } // The first filler if (unit.isFiller() && unit.getName().equals(SILENCE_CIPHONE)) { unit = Unit.SILENCE; } // Means addModelToDensityPool(meansPool, stid, numStreams, numGaussiansPerState); // Variances addModelToDensityPool(variancePool, stid, numStreams, numGaussiansPerState); // Mixture weights addModelToMixtureWeightPool(mixtureWeightsPool, stid, numStreams, numGaussiansPerState, mixtureWeightFloor); // Transition matrix addModelToTransitionMatricesPool(matrixPool, unitCount, stid.length, transitionProbabilityFloor, tmatSkip); // After creating all pools, we create the senone pool. addModelToSenonePool(senonePool, stid, distFloor, varianceFloor); // With the senone pool in place, we go through all units, and // create the HMMs. // Create tmat float[][] transitionMatrix = (float[][]) matrixPool.get(unitCount); SenoneSequence ss = getSenoneSequence(stid); HMM hmm = new SenoneHMM(unit, ss, transitionMatrix, HMMPosition.lookup(position)); hmmManager.put(hmm); unitCount++; } // If we want to use this code to load sizes/create models for // CD units, we need to find another way of establishing the // number of CI models, instead of just reading until the end // of file. est.close(); } /** * Gets the senone sequence representing the given senones * * @param stateid is the array of senone state ids * * @return the senone sequence associated with the states */ private SenoneSequence getSenoneSequence(int[] stateid) { Senone[] senones = new Senone[stateid.length]; for (int i=0; i<stateid.length; i++){ senones[i] = (Senone) senonePool.get(stateid[i]); } // TODO: Is there any advantage in trying to pool these? return new SenoneSequence(senones); } /** * Adds model to the mixture weights * * @param pool the pool to add models to * @param stateID vector containing state ids for hmm * @param numStreams the number of streams * @param numGaussiansPerState the number of Gaussians per state * @param floor the minimum mixture weight allowed * * @throws IOException if an error occurs while loading the data */ private void addModelToMixtureWeightPool(Pool pool, int[] stateID, int numStreams, int numGaussiansPerState, float floor) throws IOException { int numStates = stateID.length; int numInPool; assert pool != null; numInPool = pool.getFeature(NUM_SENONES, 0); pool.setFeature(NUM_SENONES, numStates + numInPool); numInPool = pool.getFeature(NUM_STREAMS, -1); if (numInPool == -1) { pool.setFeature(NUM_STREAMS, numStreams); } else { assert numInPool == numStreams; } numInPool = pool.getFeature(NUM_GAUSSIANS_PER_STATE, -1); if (numInPool == -1) { pool.setFeature(NUM_GAUSSIANS_PER_STATE, numGaussiansPerState); } else { assert numInPool == numGaussiansPerState; } // TODO: allow any number for numStreams assert numStreams == 1; for (int i = 0; i < numStates; i++) { int state = stateID[i]; float[] logMixtureWeight = new float[numGaussiansPerState]; // Initialize the weights with the same value, e.g. floor floorData(logMixtureWeight, floor); // Normalize, so the numbers are not all too low normalize(logMixtureWeight); convertToLogMath(logMixtureWeight); pool.put(state, logMixtureWeight); } } /** * Adds transition matrix to the transition matrices pool * * @param pool the pool to add matrix to * @param hmmId current HMM's id * @param numEmittingStates number of states in current HMM * @param floor the transition probability floor * @param skip if true, states can be skipped * * @throws IOException if an error occurs while loading the data */ private void addModelToTransitionMatricesPool(Pool pool, int hmmId, int numEmittingStates, float floor, boolean skip) throws IOException { assert pool != null; // Add one to account for the last, non-emitting, state int numStates = numEmittingStates + 1; float[][] tmat = new float[numStates][numStates]; for (int j = 0; j < numStates ; j++) { for (int k = 0; k < numStates ; k++) { // Just to be sure... tmat[j][k] = 0.0f; // the last row is just zeros, so we just do // the first (numStates - 1) rows // The value assigned could be anything, provided // we normalize it. if (j < numStates - 1) { // Usual case: state can transition to itself // or the next state. if (k == j || k == j + 1) { tmat[j][k] = floor; } // If we can skip, we can also transition to // the next state if (skip) { if (k == j + 2) { tmat[j][k] = floor; } } } } normalize(tmat[j]); convertToLogMath(tmat[j]); } pool.put(hmmId, tmat); } /** * Creates a pool with a single identity matrix in it. * * @param name the name of the pool * * @return the pool with the matrix */ private Pool createDummyMatrixPool(String name) { Pool pool = new Pool(name); float[][] matrix = new float[vectorLength][vectorLength]; logger.info("creating dummy matrix pool " + name); for (int i = 0; i < vectorLength; i++) { for (int j = 0; j < vectorLength; j++) { if (i == j) { matrix[i][j] = 1.0F; } else { matrix[i][j] = 0.0F; } } } pool.put(0, matrix); return pool; } /** * Creates a pool with a single zero vector in it. * * @param name the name of the pool * * @return the pool with the vector */ private Pool createDummyVectorPool(String name) { logger.info("creating dummy vector pool " + name); Pool pool = new Pool(name); float[] vector = new float[vectorLength]; for (int i = 0; i < vectorLength; i++) { vector[i] = 0.0f; } pool.put(0, vector); return pool; } /** * Returns the properties of the loaded AcousticModel. * * @return the properties of the loaded AcousticModel, or null if * it has no properties */ public SphinxProperties getModelProperties() { return acousticProperties; } /** * Gets the pool of means for this loader * * @return the pool */ public Pool getMeansPool() { return meansPool; } /** * Gets the pool of means transformation matrices for this loader * * @return the pool */ public Pool getMeansTransformationMatrixPool() { return meanTransformationMatrixPool; } /** * Gets the pool of means transformation vectors for this loader * * @return the pool */ public Pool getMeansTransformationVectorPool() { return meanTransformationVectorPool; } /* * Gets the variance pool * * @return the pool */ public Pool getVariancePool() { return variancePool; } /** * Gets the variance transformation matrix pool * * @return the pool */ public Pool getVarianceTransformationMatrixPool() { return varianceTransformationMatrixPool; } /** * Gets the pool of variance transformation vectors for this loader * * @return the pool */ public Pool getVarianceTransformationVectorPool() { return varianceTransformationVectorPool; } /* * Gets the mixture weight pool * * @return the pool */ public Pool getMixtureWeightPool() { return mixtureWeightsPool; } /* * Gets the transition matrix pool * * @return the pool */ public Pool getTransitionMatrixPool() { return matrixPool; } /* * Gets the senone pool for this loader * * @return the pool */ public Pool getSenonePool() { return senonePool; } /** * Returns the size of the left context for context dependent * units * * @return the left context size */ public int getLeftContextSize() { return CONTEXT_SIZE; } /** * Returns the size of the right context for context dependent * units * * @return the left context size */ public int getRightContextSize() { return CONTEXT_SIZE; } /** * Returns the hmm manager associated with this loader * * @return the hmm Manager */ public HMMManager getHMMManager() { return hmmManager; } /** * Log info about this loader */ public void logInfo() { logger.info("Sphinx3Loader"); meansPool.logInfo(logger); variancePool.logInfo(logger); matrixPool.logInfo(logger); senonePool.logInfo(logger); meanTransformationMatrixPool.logInfo(logger); meanTransformationVectorPool.logInfo(logger); varianceTransformationMatrixPool.logInfo(logger); varianceTransformationVectorPool.logInfo(logger); mixtureWeightsPool.logInfo(logger); senonePool.logInfo(logger); logger.info("Context Independent Unit Entries: " + contextIndependentUnits.size()); hmmManager.logInfo(logger); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?