Example #1
0
def test8_helper(num_obs, num_passes):
    """
    This tests mimics a run ChrisW did with HTK.  The models are 2-D single-mode Gaussians embedded
    in a 3-state Hmm.  Each observation is a sequence of length 11, taken by sampling 2, 3, and 6
    times, respectively, from three target distributions.  This is identical to test5 except that
    here I have built the Hmm with only one Gmm, which is shared by all three states.
    """
    import pprint
    num_states = 3
    dimension = 2
    
    # Data generator setup
    target_means = ((1,1), (2,2), (3,3))
    target_vars = ((0.1,0.1), (0.2,0.2), (0.3,0.3))
    target_durations = (2, 3, 6)
    num_steps = sum(target_durations)
    generators = [SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE) for i in xrange(num_states)]
    [m.set_model(tm, tv) for (m, tm, tv) in izip(generators, target_means, target_vars)]
    SimpleGaussianModel.seed(0)

    # Gmm setup
    num_states = 3

    gmm = GaussianMixtureModel(dimension, GaussianMixtureModel.DIAGONAL_COVARIANCE, 1)
    gmm.set_weights(array((1.0,)))
    mu = array(((0.0,0.0),))
    v = array(((1.0,1.0),))
    gmm.set_model(mu, v)
    models = (gmm,)
    
    mm = GmmMgr(models)
    # Here's where we're using the same Gmm in all three states of this Hmm.
    models = (0, 0, 0)

    # Hmm setup
    trans = array(((0.0, 1.0, 0.0, 0.0, 0.0),
                   (0.0, 0.5, 0.5, 0.0, 0.0),
                   (0.0, 0.0, 0.5, 0.5, 0.0),
                   (0.0, 0.0, 0.0, 0.5, 0.5),
                   (0.0, 0.0, 0.0, 0.0, 0.0)))
    hmm0 = Hmm(num_states, log_domain=True)
    hmm0.build_model(mm, models, 1, 1, trans)
    print hmm0.to_string(True)

    for p in xrange(num_passes):
        # Reseeding here ensures we are repeating the same observations in each pass
        SimpleGaussianModel.seed(0)
        mm.set_adaptation_state("INITIALIZING")
        mm.clear_all_accumulators()
        hmm0.begin_adapt("STANDALONE")
        mm.set_adaptation_state("ACCUMULATING")
        obs_gen = obs_generator(generators, target_durations)
        for i in xrange(num_obs):
            obs = obs_gen.next()
            hmm0.adapt_one_sequence(obs)
        mm.set_adaptation_state("APPLYING")
        hmm0.end_adapt()
        mm.apply_all_accumulators()
        mm.set_adaptation_state("NOT_ADAPTING")
        print hmm0.to_string(True)
Example #2
0
def read_gmm(stream, *_):
    v, covariance_type = stream.read_singleton("covariance_type")
    v, dimension = stream.read_scalar("dimension", int)
    v, num_components = stream.read_scalar("num_components", int)
    v, relevances = stream.read_list("relevances", float)
    v, gmm_weights = stream.read_array("weights", rtype=float, dim=1, shape=(num_components,))
    v, smms = stream.read_indexed_collection(read_smm, (covariance_type, num_components, dimension), name="Gaussians")

    gmm_means = numpy.zeros((num_components, dimension), dtype=float)
    if covariance_type is GaussianMixtureModel.FULL_COVARIANCE:
        var_shape = (num_components, dimension, dimension)
    else:
        assert covariance_type is GaussianMixtureModel.DIAGONAL_COVARIANCE
        var_shape = (num_components, dimension)
    gmm_vars = numpy.zeros(var_shape, dtype=float)

    assert len(smms) == num_components
    for i in xrange(num_components):
        gmm_means[i] = smms[i].means
        gmm_vars[i] = smms[i].vars

    # Construct and return Gmm object
    ret = GaussianMixtureModel(dimension, covariance_type, num_components)
    ret.set_weights(gmm_weights)
    ret.set_means(gmm_means)
    ret.set_vars(gmm_vars)
    ret.set_relevances(relevances)
    return ret
Example #3
0
def read_gmm(stream, *_):
    v, covariance_type = stream.read_singleton("covariance_type")
    v, dimension = stream.read_scalar("dimension", int)
    v, num_components = stream.read_scalar("num_components", int)
    v, relevances = stream.read_list("relevances", float)
    v, gmm_weights = stream.read_array("weights",
                                       rtype=float,
                                       dim=1,
                                       shape=(num_components, ))
    v, smms = stream.read_indexed_collection(
        read_smm, (covariance_type, num_components, dimension),
        name="Gaussians")

    gmm_means = numpy.zeros((num_components, dimension), dtype=float)
    if covariance_type is GaussianMixtureModel.FULL_COVARIANCE:
        var_shape = (num_components, dimension, dimension)
    else:
        assert (covariance_type is GaussianMixtureModel.DIAGONAL_COVARIANCE)
        var_shape = (num_components, dimension)
    gmm_vars = numpy.zeros(var_shape, dtype=float)

    assert (len(smms) == num_components)
    for i in xrange(num_components):
        gmm_means[i] = smms[i].means
        gmm_vars[i] = smms[i].vars

    # Construct and return Gmm object
    ret = GaussianMixtureModel(dimension, covariance_type, num_components)
    ret.set_weights(gmm_weights)
    ret.set_means(gmm_means)
    ret.set_vars(gmm_vars)
    ret.set_relevances(relevances)
    return ret
Example #4
0
def make_gmm_diag(dimension, num_mixtures):
    gmm = GaussianMixtureModel(dimension, GaussianMixtureModel.DIAGONAL_COVARIANCE, num_mixtures)
    w = [1.0 / num_mixtures for n in xrange(num_mixtures)]
    gmm.set_weights(array(w))
    mu = array(((1.5,1.5), (3,3)))
    v = array(((1.0,1.0), (1.0,1.0)))
    gmm.set_model(mu, v)
    return gmm
Example #5
0
def make_target(dimension, num_comps, weights, means, vars):
    ret = GaussianMixtureModel(dimension,
                               GaussianModelBase.DIAGONAL_COVARIANCE,
                               num_comps)
    ret.set_weights(numpy.array(weights))
    ret.set_means(numpy.array(means))
    ret.set_vars(numpy.array(vars))
    return ret
Example #6
0
def make_standard_gmms(dimension, num_models):
    models = []
    for i in xrange(num_models):
        gmm = GaussianMixtureModel(dimension, GaussianMixtureModel.DIAGONAL_COVARIANCE, 1)
        gmm.set_weights(array((1.0,)))
        mu = array(((0.0, 0.0),))
        v = array(((1.0, 1.0),))
        gmm.set_model(mu, v)
        models.append(gmm)
    return models
Example #7
0
def make_standard_gmms(dimension, num_models):
    models = []
    for i in xrange(num_models):
        gmm = GaussianMixtureModel(dimension,
                                   GaussianMixtureModel.DIAGONAL_COVARIANCE, 1)
        gmm.set_weights(array((1.0, )))
        mu = array(((0.0, 0.0), ))
        v = array(((1.0, 1.0), ))
        gmm.set_model(mu, v)
        models.append(gmm)
    return models
Example #8
0
def test9(num_obs):
    """
    Test sequence scoring interface.

    """
    num_states = 3
    dimension = 2
    
    # Data generator setup
    target_means = ((1,1), (2,2), (3,3))
    target_vars = ((0.1,0.1), (0.2,0.2), (0.3,0.3))
    target_durations = (2, 3, 6)
    num_steps = sum(target_durations)
    generators = [SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE) for i in xrange(num_states)]
    [m.set_model(tm, tv) for (m, tm, tv) in izip(generators, target_means, target_vars)]
    SimpleGaussianModel.seed(0)
    obs_gen = obs_generator(generators, target_durations)

    # Gmm setup
    num_states = 3
    models = []
    for i in xrange(num_states):
        gmm = GaussianMixtureModel(dimension, GaussianMixtureModel.DIAGONAL_COVARIANCE, 1)
        gmm.set_weights(array((1.0,)))
        mu = array(((0.0,0.0),))
        v = array(((1.0,1.0),))
        gmm.set_model(mu, v)
        models.append(gmm)
    mm = GmmMgr(models)
    models = range(num_states)

    # Hmm setup
    trans = array(((0.0, 1.0, 0.0, 0.0, 0.0),
                   (0.0, 0.5, 0.5, 0.0, 0.0),
                   (0.0, 0.0, 0.5, 0.5, 0.0),
                   (0.0, 0.0, 0.0, 0.5, 0.5),
                   (0.0, 0.0, 0.0, 0.0, 0.0)))
    hmm0 = Hmm(num_states)
    hmm0.build_model(mm, models, 1, 1, trans)
    print hmm0.to_string(full=True)

    for i in xrange(num_obs):
        obs = obs_gen.next()
        scores = hmm0.forward_score_sequence(obs)
        print scores
Example #9
0
def test3_helper(dataset_idx, num_passes):
    """
    This tests mimics a run ChrisW did with HTK.  The models are 2-D single-mode Gaussians
    embedded in a 1-state Hmm.  Each data point is taken as a sequence of length 1.    
    """
    dimension = 2

    # Gmm setup
    gmm = GaussianMixtureModel(dimension, GaussianMixtureModel.DIAGONAL_COVARIANCE, 1)
    gmm.set_weights(array((1.0,)))
    mu = array(((0.0,0.0),))
    v = array(((1.0,1.0),))
    gmm.set_model(mu, v)
    mm = GmmMgr((gmm,))

    # Hmm setup
    # A transition probability matrix with a p=1 self-loop for the real state.
    # The entry state feeds into the real state with p=1.
    trans = array(((0.0, 1.0, 0.0),
                   (0.0, 0.0, 1.0),
                   (0.0, 0.0, 0.0)))
    hmm0 = Hmm(1, log_domain=True)
    hmm0.build_model(mm, (0,), 1, 1, trans)
    print hmm0.to_string(True)

    # adaptation
    data = datasets[dataset_idx]
    for p in xrange(num_passes):
        mm.set_adaptation_state("INITIALIZING")
        mm.clear_all_accumulators()
        hmm0.begin_adapt("STANDALONE")
        mm.set_adaptation_state("ACCUMULATING")
        for point in data:
            s = array(point)
            # We treat each point as an entire sequence
            hmm0.adapt_one_sequence((s,))
        mm.set_adaptation_state("APPLYING")
        hmm0.end_adapt()
        mm.apply_all_accumulators()
        mm.set_adaptation_state("NOT_ADAPTING")

    print hmm0.to_string(True)
Example #10
0
def test5_helper(num_obs, num_passes):
    """
    This tests mimics a run ChrisW did with HTK.  The models are 2-D single-mode Gaussians
    embedded in a 3-state Hmm.  Each observation is a sequence of length 11, taken by sampling
    2, 3, and 6 times, respectively, from three target distributions.    
    """
    import pprint
    num_states = 3
    dimension = 2
    
    # Data generator setup
    target_means = ((1,1), (2,2), (3,3))
    target_vars = ((0.1,0.1), (0.2,0.2), (0.3,0.3))
    target_durations = (2, 3, 6)
    num_steps = sum(target_durations)
    generators = [SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE) for i in xrange(num_states)]
    [m.set_model(tm, tv) for (m, tm, tv) in izip(generators, target_means, target_vars)]
    SimpleGaussianModel.seed(0)

    # Gmm setup
    num_states = 3
    models = []
    for i in xrange(num_states):
        gmm = GaussianMixtureModel(dimension, GaussianMixtureModel.DIAGONAL_COVARIANCE, 1)
        gmm.set_weights(array((1.0,)))
        mu = array(((0.0,0.0),))
        v = array(((1.0,1.0),))
        gmm.set_model(mu, v)
        models.append(gmm)
    
    mm = GmmMgr(models)
    models = range(num_states)

    # Hmm setup
    trans = array(((0.0, 1.0, 0.0, 0.0, 0.0),
                   (0.0, 0.5, 0.5, 0.0, 0.0),
                   (0.0, 0.0, 0.5, 0.5, 0.0),
                   (0.0, 0.0, 0.0, 0.5, 0.5),
                   (0.0, 0.0, 0.0, 0.0, 0.0)))
    hmm0 = Hmm(num_states, log_domain=True)
    hmm0.build_model(mm, models, 1, 1, trans)
    print hmm0.to_string(True)

    for p in xrange(num_passes):
        # Reseeding here ensures we are repeating the same observations in each pass
        SimpleGaussianModel.seed(0)
        mm.set_adaptation_state("INITIALIZING")
        mm.clear_all_accumulators()
        hmm0.begin_adapt("STANDALONE")
        mm.set_adaptation_state("ACCUMULATING")
        obs_gen = obs_generator(generators, target_durations)
        for i in xrange(num_obs):
            obs = obs_gen.next()
            hmm0.adapt_one_sequence(obs)
        
            obs2 = [tuple(a) for a in obs]
            # Uncomment these lines to show observations as nicely formatted sequences; this
            # is what I gave ChrisW to use with his HTK runs.
            # pprint.pprint(obs2)
            # print
        mm.set_adaptation_state("APPLYING")
        hmm0.end_adapt()
        mm.apply_all_accumulators()
        mm.set_adaptation_state("NOT_ADAPTING")
        print hmm0.to_string(True)
Example #11
0
        dc and dc("m.keys() = \n%s" % (m.keys(),))
        if m.hasattr.decl:
            name = m.decl
        else:
            name = ("UnnamedModel%d" % unnamed_index)
            unnamed_index += 1
        n = m.numstates - 2   # HTK numstates counts virtual entry and exit states
        hmm = Hmm(n, log_domain)
        gmms = []
        for s_label, state in m.states:
            assert s_label == 'state'
            dc and dc("state.keys() = \n%s" % (state.keys(),))
            num_mixtures = 1
            weights = array((1.0,), dtype = float)
            gmm = GaussianMixtureModel(dim, covar_type, num_mixtures)
            gmm.set_weights(weights)
            gmm.set_model(state.mean, state.var)
            dc and dc("gmm = %s" % (gmm,))
            gmms.append(gmm)

        model_indices = gmm_mgr.add_models(gmms)
        hmm.build_model(gmm_mgr, model_indices, 1, 1, m.transp)
        hmms.append(hmm)
        names.append(name)
    indices = hmm_mgr.add_models(hmms)
    return dict(izip(names, indices)), hmm_mgr, gmm_mgr
    
def logreftest():
    module_dir, module_name = os.path.split(__file__)
    files = tuple(os.path.join(module_dir, mmf_file) for mmf_file in ("start.mmf", 'mmf1.mmf', 'mmf4.mmf'))
    for fname in files:
def make_target(dimension, num_comps, weights, means, vars):
    ret = GaussianMixtureModel(dimension, GaussianModelBase.DIAGONAL_COVARIANCE, num_comps)
    ret.set_weights(numpy.array(weights))
    ret.set_means(numpy.array(means))
    ret.set_vars(numpy.array(vars))
    return ret