def _constructHmmNetwork(self, numStates, ALPHA, withModels): ''' top level-function: costruct self.hmmNEtwork that confirms to guyz's code ''' # sequencePhonemes = sequencePhonemes[0:4] ######## construct transition matrix ####### if not WITH_DURATIONS: transMAtrix = self._constructTransMatrixHMMNetwork( self.lyricsWithModels.phonemesNetwork) # DEBUG # writeListOfListToTextFile(transMAtrix, None , '/Users/joro/Documents/Phd/UPF/voxforge/myScripts/AlignmentStep/transMatrix') # construct means, covars, and all the rest params ######### if numStates == None: numStates = len(self.lyricsWithModels.statesNetwork) means, covars, weights, pi = self._constructHMMNetworkParameters( numStates, withModels) if WITH_DURATIONS: self.hmmNetwork = GMHMM(numStates, numMixtures, numDimensions, None, means, covars, weights, pi, init_type='user', verbose=True) self.hmmNetwork.setALPHA(ALPHA) else: self.hmmNetwork = GMHMM(numStates, numMixtures, numDimensions, transMAtrix, means, covars, weights, pi, init_type='user', verbose=True)
def test_rand(): n = 5 m = 4 d = 2 atmp = numpy.random.random_sample((n, n)) row_sums = atmp.sum(axis=1) a = numpy.array(atmp / row_sums[:, numpy.newaxis], dtype=numpy.double) wtmp = numpy.random.random_sample((n, m)) row_sums = wtmp.sum(axis=1) w = numpy.array(wtmp / row_sums[:, numpy.newaxis], dtype=numpy.double) means = numpy.array((0.6 * numpy.random.random_sample((n, m, d)) - 0.3), dtype=numpy.double) covars = numpy.zeros( (n,m,d,d) ) for i in range(n): for j in range(m): for k in range(d): covars[i][j][k][k] = 1 pitmp = numpy.random.random_sample((n)) pi = numpy.array(pitmp / sum(pitmp), dtype=numpy.double) gmmhmm = GMHMM(n,m,d,a,means,covars,w,pi,init_type='user',verbose=True) obs = numpy.array((0.6 * numpy.random.random_sample((40,d)) - 0.3), dtype=numpy.double) print("Doing Baum-welch") gmmhmm.train(obs,1000) print() print("Pi",gmmhmm.pi) print("A",gmmhmm.A) print("weights", gmmhmm.w) print("means", gmmhmm.means) print("covars", gmmhmm.covars)
def test_simple(): n = 2 m = 2 d = 2 pi = numpy.array([0.5, 0.5]) A = numpy.ones((n, n), dtype=numpy.double) / float(n) w = numpy.ones((n, m), dtype=numpy.double) means = numpy.ones((n, m, d), dtype=numpy.double) covars = [[numpy.matrix(numpy.eye(d, d)) for j in xrange(m)] for i in xrange(n)] w[0][0] = 0.5 w[0][1] = 0.5 w[1][0] = 0.5 w[1][1] = 0.5 means[0][0][0] = 0.5 means[0][0][1] = 0.5 means[0][1][0] = 0.5 means[0][1][1] = 0.5 means[1][0][0] = 0.5 means[1][0][1] = 0.5 means[1][1][0] = 0.5 means[1][1][1] = 0.5 gmmhmm = GMHMM(n, m, d, A, means, covars, w, pi, init_type='user', verbose=True) obs = numpy.array([[0.3, 0.3], [0.1, 0.1], [0.2, 0.2]]) print "Doing Baum-welch" gmmhmm.train(obs, 10) print print "Pi", gmmhmm.pi print "A", gmmhmm.A print "weights", gmmhmm.w print "means", gmmhmm.means print "covars", gmmhmm.covars
def trainModel(self, obs): pi = numpy.array([0.2, 0.2, 0.2, 0.2, 0.2]) A = numpy.ones((self.n, self.n), dtype=numpy.double) / float(self.n) w = numpy.ones((self.n, self.m), dtype=numpy.double) means = numpy.ones((self.n, self.m, self.d), dtype=numpy.double) covars = [[ numpy.matrix(numpy.eye(self.d, self.d)) for j in xrange(self.m) ] for i in xrange(self.n)] n_iter = 20 '''w[0][0] = 0.5 w[0][1] = 0.5 w[1][0] = 0.5 w[1][1] = 0.5 means[0][0][0] = 0.5 means[0][0][1] = 0.5 means[0][1][0] = 0.5 means[0][1][1] = 0.5 means[1][0][0] = 0.5 means[1][0][1] = 0.5 means[1][1][0] = 0.5 means[1][1][1] = 0.5 ''' gmmhmm = GMHMM(self.n, self.m, self.d, A, means, covars, w, pi, init_type='user', verbose=True) print "Doing Baum-welch" #gmmhmm.train(obs,10) if len(obs.shape) == 2: gmmhmm.train(obs) return self elif len(obs.shape) == 3: count = obs.shape[0] for n in range(count): gmmhmm.train(obs[n, :, :]) return self
# w[1][0] = 0.5 # w[1][1] = 0.5 # btmp = np.random.random_sample((N, M)) # row_sums = btmp.sum(axis=1) # B = btmp / row_sums[:, np.newaxis] # print("Initial B: ", B) print("HMM node started... Call a service") #print("--- Check rows sum up to 1 ---") HMM_model = GMHMM(N, M, D, A, means, covars, w, pi, init_type='user', verbose=True) def get_training_set_srv(req): print("Start training requested") print("Run a bag...") global train_hmm train_hmm = True return def start_training_srv(req):
w = numpy.ones((n, m), dtype=numpy.double) hmm_means = numpy.ones((n, m, d), dtype=numpy.double) hmm_means[0][0] = model.means_[0] hmm_means[1][0] = model.means_[1] hmm_means[2][0] = model.means_[2] hmm_covars = numpy.array( [[numpy.matrix(numpy.eye(d, d)) for j in xrange(m)] for i in xrange(n)]) hmm_covars[0][0] = model.covars_[0] hmm_covars[1][0] = model.covars_[1] hmm_covars[2][0] = model.covars_[2] gmmhmm = GMHMM(n, m, d, A, hmm_means, hmm_covars, w, pi, init_type='user', verbose=False) # hidden_state = model.predict(obs) hidden_state = gmmhmm.decode(obs) mean_sequence = [None] * len(obs) var_sequence = [None] * len(obs) for i in range(len(obs)): mean_sequence[i] = model.means_[hidden_state[i]] var_sequence[i] = numpy.diag(model.covars_[hidden_state[i]]) means.append(mean_sequence)