Exemplo n.º 1
0
    def __init__(self, num_phonemes, num_features, var_diag_interval,
                 var_offdiag_interval):
        self.num_features = num_features
        self.num_phonemes = num_phonemes
        self._labels = tuple("label%d" % i for i in range(num_phonemes))
        # Randomly choose num_phonemes vectors
        # KJB - need something more sophisticated here to ensure minimum
        # distance between phonemes  
        self._targets = tuple(tuple(random() for i in range(num_features))
                              for j in range(num_phonemes))

        def buildOneCovarMatrix():
            uni = (uniform(var_offdiag_interval[0], var_offdiag_interval[1])
                   for i in xrange(num_features**2))
            mat = numpy.fromiter(uni, 'float64').reshape(num_features, num_features)
            # Make symetric
            lower_tri = numpy.tril(mat) # lower triangular
            mat = lower_tri + lower_tri.transpose()
            # Now clobber diagonal with other values
            for i in range(num_features):
                mat[i,i] = uniform(var_diag_interval[0], var_diag_interval[1])
            return mat

        # Build covar matrices
        covars = tuple(buildOneCovarMatrix() for i in range(num_phonemes))

        # Create a dictionary from _labels to SimpleGaussianModels
        self._models = dict()
        for i,label in enumerate(self._labels):
            m = SimpleGaussianModel(num_features, SimpleGaussianModel.FULL_COVARIANCE)
            m.set_model(self._targets[i], covars[i])
            self._models[label] = m
Exemplo n.º 2
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)
Exemplo n.º 3
0
def main(args):
    if not args:
        return
    with open(args[0], 'rt') as infile:
        SimpleGaussianModel.seed(0)
        for sample in text_utils.generate_samples(text_utils.make_model_samplers((0, 1), (1, 0.75)), text_utils.deterministic_labels(infile)):
            print sample, 'X' if sample[0][0] == sample[-1][-1] else ''
Exemplo n.º 4
0
def main(args):
    if not args:
        return
    with open(args[0], 'rt') as infile:
        SimpleGaussianModel.seed(0)
        for sample in text_utils.generate_samples(
                text_utils.make_model_samplers((0, 1), (1, 0.75)),
                text_utils.deterministic_labels(infile)):
            print sample, 'X' if sample[0][0] == sample[-1][-1] else ''
Exemplo n.º 5
0
def test6():
    """
    This test builds an Hmm with dummy models which always give a score of 1, but
    with a somewhat unusual topology in which there are 6 actual states chained together
    with 2 virtual inputs and 3 virtual outputs.  The point is to make sure we can handle
    this asymetric case correctly.
    """
    import pprint
    num_states = 6
    dimension = 2
    
    # GmmMgr setup
    models = []
    for i in xrange(num_states):
        dm = DummyModel(dimension, 1.0)
        models.append(dm)
    
    mm = GmmMgr(models)
    models = range(num_states)

    # Hmm setup T0:  i1   i2   1    2    3    4    5    6    o1   o2   o3    FROM: 
    trans = array(((0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # i1
                   (0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # i2

                   (0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # 1
                   (0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # 2
                   (0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0),  # 3
                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.4, 0.0, 0.1, 0.0, 0.0),  # 4
                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.4, 0.0, 0.1, 0.0),  # 5
                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5),  # 6

                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # o1
                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # o2
                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))) # o3
    
    hmm0 = Hmm(num_states, log_domain=True)
    hmm0.build_model(mm, models, 2, 3, trans)
    print hmm0.to_string(True)

    num_passes = 1
    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 = [array((0,0))] * 11  # Dummy sequence of length 11
        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)
Exemplo n.º 6
0
 def __init__(self, primary_classifier, variant_labels):
     self._variant_labels = variant_labels
     self._variant_models = {}
     for label in variant_labels:
         model = SimpleGaussianModel()
         # KJB - this guy shouldn't have to know so much about models -
         # maybe do some kind of cloning from models in the classifier?
         model.set_model(primary_classifier.num_features, 'diagonal')
         self._variant_models[label] = model
         
     self._primary = primary_classifier
     self._variant_classifier = None
Exemplo n.º 7
0
    def __init__(self, primary_classifier, variant_labels):
        self._variant_labels = variant_labels
        self._variant_models = {}
        for label in variant_labels:
            model = SimpleGaussianModel()
            # KJB - this guy shouldn't have to know so much about models -
            # maybe do some kind of cloning from models in the classifier?
            model.set_model(primary_classifier.num_features, 'diagonal')
            self._variant_models[label] = model

        self._primary = primary_classifier
        self._variant_classifier = None
Exemplo n.º 8
0
def read_smm(stream, params, _):
    covariance_type, num_components, dimension = params
    v, smm_means = stream.read_array("means", rtype=float, dim=1, shape=(dimension,))
    if covariance_type is GaussianMixtureModel.FULL_COVARIANCE:
        var_dim = 2
        var_shape = (dimension, dimension)
    else:
        assert covariance_type is GaussianMixtureModel.DIAGONAL_COVARIANCE
        var_dim = 1
        var_shape = (dimension,)

    v, smm_vars = stream.read_array("vars", rtype=float, dim=var_dim, shape=var_shape)
    ret = SimpleGaussianModel(dimension, covariance_type)
    ret.set_model(smm_means, smm_vars)
    return ret
Exemplo n.º 9
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
Exemplo n.º 10
0
def make_data_generator(dimension):
    # Data generator setup and data generation
    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, 6, 6)
    assert len(target_means) == len(target_vars) == len(target_durations)
    num_generators = len(target_means)
    generators = [
        SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE) for i in xrange(num_generators)
    ]
    for (m, tm, tv) in izip(generators, target_means, target_vars):
        m.set_model(tm, tv)
    SimpleGaussianModel.seed(0)

    def obs_generator():
        while True:
            yield [m.sample() for m, d in izip(generators, target_durations) for i in xrange(d)]

    return obs_generator()
Exemplo n.º 11
0
def read_smm(stream, params, _):
    covariance_type, num_components, dimension = params
    v, smm_means = stream.read_array("means",
                                     rtype=float,
                                     dim=1,
                                     shape=(dimension, ))
    if covariance_type is GaussianMixtureModel.FULL_COVARIANCE:
        var_dim = 2
        var_shape = (dimension, dimension)
    else:
        assert (covariance_type is GaussianMixtureModel.DIAGONAL_COVARIANCE)
        var_dim = 1
        var_shape = (dimension, )

    v, smm_vars = stream.read_array("vars",
                                    rtype=float,
                                    dim=var_dim,
                                    shape=var_shape)
    ret = SimpleGaussianModel(dimension, covariance_type)
    ret.set_model(smm_means, smm_vars)
    return ret
Exemplo n.º 12
0
def make_data_generator(dimension):
    # Data generator setup and data generation
    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, 6, 6)
    assert len(target_means) == len(target_vars) == len(target_durations)
    num_generators = len(target_means)
    generators = [
        SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE)
        for i in xrange(num_generators)
    ]
    for (m, tm, tv) in izip(generators, target_means, target_vars):
        m.set_model(tm, tv)
    SimpleGaussianModel.seed(0)

    def obs_generator():
        while True:
            yield [
                m.sample() for m, d in izip(generators, target_durations)
                for i in xrange(d)
            ]

    return obs_generator()
Exemplo n.º 13
0
 def make_model_samplers(means, vars):
     """
     >>> SimpleGaussianModel.seed(0)
     >>> tuple(int(128 * sampler()) for sampler in text_utils.make_model_samplers((0, 1), (1, 0.5)))
     (-23, 130)
     >>> tuple(int(128 * sampler()) for sampler in text_utils.make_model_samplers((0, 1, 2), (1, 0.5, 2)))
     (89, 119, 511)
     """
     assert len(means) == len(vars)
     num_classes = len(means)
     models = tuple(
         SimpleGaussianModel(1, SimpleGaussianModel.DIAGONAL_COVARIANCE)
         for i in xrange(num_classes))
     for model, mean, var in izip(models, means, vars):
         model.set_model((mean, ), (var, ))
     samplers = tuple(model.sample for model in models)
     assert len(samplers) == num_classes
     return samplers
Exemplo n.º 14
0
def test2a(num_obs, num_passes):
    dimension = 2
    
    # Data generator setup
    target_means = (1,1)
    target_vars = (0.1,0.1)
    generator = SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE)
    generator.set_model(target_means, target_vars)
    SimpleGaussianModel.seed(0)
    data = [generator.sample() for i in xrange(num_obs)]


    # Gmm setup
    num_mixtures = 2
    gmm0 = make_gmm_diag(dimension, num_mixtures)
    gmm1 = make_gmm_diag(dimension, num_mixtures)
    mm = GmmMgr((gmm1,))

    # 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.  We use p ~= 1 for the
    # second self loop since we need *some* probability of finishing.
    trans = array(((0.0, 1.0, 0.0),
                   (0.0, 0.999999999999, 0.000000000001),
                   (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) + '\n'
    print gmm0
    print '\n\n'

    # Try some adaptation
    for p in xrange(num_passes):
        mm.set_adaptation_state("INITIALIZING")
        mm.clear_all_accumulators()
        hmm0.begin_adapt("STANDALONE")
        mm.set_adaptation_state("ACCUMULATING")
        hmm0.adapt_one_sequence(data)
        mm.set_adaptation_state("APPLYING")
        hmm0.end_adapt()
        mm.apply_all_accumulators()
        mm.set_adaptation_state("NOT_ADAPTING")

    really_print = False
    with DebugPrint("gaussian", "gaussian_pt", "gaussian_gmm_score") if really_print else DebugPrint():
        gmm0.adapt(data, max_iters = num_passes)

    print hmm0.to_string(True) + '\n'
    print gmm0
Exemplo n.º 15
0
def test1(num_obs, num_passes):
    dimension = 2
    
    # Data generator setup
    target_means = (1,1)
    target_vars = (0.1,0.1)
    generator = SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE)
    generator.set_model(target_means, target_vars)

    SimpleGaussianModel.seed(0)
    GaussianMixtureModel.seed(0)

    # Gmm setup
    num_mixtures = 2
    gmm0 = make_gmm(dimension, num_mixtures)
    gmm1 = make_gmm(dimension, num_mixtures)
    mm = GmmMgr((gmm1,))

    # Hmm setup
    hmm0 = Hmm(1, log_domain=True)

    # A transition probability matrix with a p=1/2 exit 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.5, 0.5),
                   (0.0, 0.0, 0.0)))

    
    hmm0.build_model(mm, (0,), 1, 1, trans)
    print hmm0.to_string(True)
    print gmm0

    # Try some adaptation.  Note that we are feeding the entire data set as one stream
    # to the Hmm adaption call.
    data = [generator.sample() for i in xrange(num_obs)]
    for p in xrange(num_passes):
        mm.set_adaptation_state("INITIALIZING")
        mm.clear_all_accumulators()
        hmm0.begin_adapt("STANDALONE")
        mm.set_adaptation_state("ACCUMULATING")
        hmm0.adapt_one_sequence(data)
        mm.set_adaptation_state("APPLYING")
        hmm0.end_adapt()
        mm.apply_all_accumulators()
        mm.set_adaptation_state("NOT_ADAPTING")
    gmm0.adapt(data, max_iters = num_passes)

    print hmm0.to_string(True)
    print gmm0
Exemplo n.º 16
0
def test0(num_obs, num_passes):
    dimension = 2
    
    # Data generator setup
    target_means = (1,1)
    target_vars = (0.1,0.1)
    generator = SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE)
    generator.set_model(target_means, target_vars)

    SimpleGaussianModel.seed(0)
    GaussianMixtureModel.seed(0)

    mm = GmmMgr(dimension)

    # Hmm setup
    hmm0 = Hmm(0, log_domain=True)

    # A transition probability matrix with no real state.
    # The entry state feeds into the exit state with p=1.
    trans = array(((0.0, 1.0),
                   (0.0, 0.0)))
    
    hmm0.build_model(mm, (), 1, 1, trans)
    print hmm0.to_string(True)

    # Try some adaptation.  Note that we are feeding the entire data set as one stream
    # to the Hmm adaption call.
    data = [generator.sample() for i in xrange(num_obs)]
    for p in xrange(num_passes):
        mm.set_adaptation_state("INITIALIZING")
        mm.clear_all_accumulators()
        hmm0.begin_adapt("STANDALONE")
        mm.set_adaptation_state("ACCUMULATING")
        with DebugPrint("hmm_gxfs", "hmm_aos") if False else DebugPrint():
            hmm0.adapt_one_sequence(data)
        mm.set_adaptation_state("APPLYING")
        hmm0.end_adapt()
        mm.apply_all_accumulators()
        mm.set_adaptation_state("NOT_ADAPTING")

    print hmm0.to_string(True)
Exemplo n.º 17
0
 def __init__(self, labels, dimension):
     super(SimpleClassifier, self).__init__(labels, dimension)
     for label in labels:
         m = SimpleGaussianModel(dimension,
                                 SimpleGaussianModel.DIAGONAL_COVARIANCE)
         self._models[label] = m
Exemplo n.º 18
0
def test7():
    """
    This test builds an Hmm with dummy models which always give a score of 1, but
    with a somewhat unusual topology in which there are 6 actual states chained together
    with 2 virtual inputs and 3 virtual outputs.  The point is to make sure we can handle
    this asymetric case correctly.  This is the same as test6 except that now we'll use
    the network adaptation interface instead.
    """
    import pprint
    num_states = 6
    dimension = 2
    
    # GmmMgr setup
    models = []
    for i in xrange(num_states):
        dm = DummyModel(dimension, 1.0)
        models.append(dm)
    
    mm = GmmMgr(models)

    # Hmm setup T0:  i1   i2   1    2    3    4    5    6    o1   o2   o3    FROM: 
    trans = array(((0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # i1
                   (0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # i2

                   (0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # 1
                   (0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # 2
                   (0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0),  # 3
                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.4, 0.0, 0.1, 0.0, 0.0),  # 4
                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.4, 0.0, 0.1, 0.0),  # 5
                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5),  # 6

                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # o1
                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),  # o2
                   (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))) # o3
    
    hmm0 = Hmm(num_states, log_domain=True)
    models = range(num_states)
    hmm0.build_model(mm, models, 2, 3, trans)
    print hmm0.to_string(True)

    num_passes = 1
    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")
        hmm0.begin_adapt("NETWORK")
        mm.set_adaptation_state("ACCUMULATING")

        num_obs = 11
        obs = [array((0,0))] * num_obs  # Dummy sequence

        context = hmm0.init_for_forward_pass(obs, terminal = True)

        # Add some mass into the system for the forward pass.  To match the behavior of
        # standalone adaptation, we divide an initial mass of 1 evenly across the inputs
        hmm0.accum_input_alphas(context, array([1.0/hmm0.num_inputs] * hmm0.num_inputs))

        # Actually do the forward pass.  Note that we must process one more frame than the number of
        # observations - this is because an extra frame is automatically added which scores 1 on the exit
        # states of the Hmm (and 0 on all real states).  XXX we might want clients to do this for
        # themselves at some point rather than this automatic behavior:

        for frame in xrange(num_obs + 1):
            output_alphas = hmm0.process_one_frame_forward(context)
            print output_alphas

        # Likewise, we initialize and then make the backward pass:
        hmm0.init_for_backward_pass(context)
        hmm0.accum_input_betas(context, array([1.0] * hmm0.num_outputs))
        for frame in xrange(num_obs + 1):
            output_betas = hmm0.process_one_frame_backward(context)
            print output_betas

        # Now collect all the gamma sums; here there's only one:
        norm = hmm0.get_initial_gamma_sum()
        hmm0.add_to_gamma_sum(norm, context)

        # Here's where the actual accumulation happens:
        hmm0.do_accumulation(context, norm)
        
        mm.set_adaptation_state("APPLYING")
        hmm0.end_adapt()
        mm.apply_all_accumulators()
        mm.set_adaptation_state("NOT_ADAPTING")
        print hmm0.to_string(True)
Exemplo n.º 19
0
labels = True, False
ncomps = 7, 3
nfeatures = numceps

# mfcc, with c0: gathered from about 60,000 frames of 'train' data from Ken and Hugh skyping
true_means_prime = N.array((-5.5522, -1.0517, 1.0240, 3.1609, 0.5628, -122.5789), dtype=N.float32)
true_vars_prime = N.array((26.2238, 26.4064, 59.7242, 35.1180, 47.2471, 3967.2240), dtype=N.float32)
false_means_prime = N.array((-9.4634, -5.3991, -4.2773, 1.7494, -0.0822, -228.6211), dtype=N.float32)
false_vars_prime = N.array((3.0097, 6.0277, 8.3711, 10.7198, 13.4285, 456.7074), dtype=N.float32)

# mfcc, no c0: 20,000 frames of Hugh talking
true_means_prime = N.array((-4.8087, 3.9863, -0.5217, 1.3076, 0.7514, -4.6497), dtype=N.float32)
true_vars_prime = N.array((26.8496, 32.6631, 32.3662, 24.2963, 36.2244, 34.1555), dtype=N.float32)
false_means_prime = N.array((-6.8806, -1.3424, -3.8147, 0.4520, 0.7129, -3.1560), dtype=N.float32)
false_vars_prime = N.array((2.7468, 6.2286, 7.4355, 10.1530, 13.3865, 15.9309), dtype=N.float32)
true_prime = SimpleGaussianModel(nfeatures, GaussianModelBase.DIAGONAL_COVARIANCE)
true_prime.set_model(true_means_prime, true_vars_prime)
false_prime = SimpleGaussianModel(nfeatures, GaussianModelBase.DIAGONAL_COVARIANCE)
false_prime.set_model(false_means_prime, false_vars_prime)

primer = (true_prime, false_prime)

GaussianMixtureModel.seed(0)
gmm_mgr0 = GmmMgr(ncomps, nfeatures, GaussianModelBase.DIAGONAL_COVARIANCE, primer)
gmm_mgr1 = GmmMgr(ncomps, nfeatures, GaussianModelBase.DIAGONAL_COVARIANCE, primer)
classify0 = AdaptingGmmClassifier(gmm_mgr0, izip(labels, count()))
classify1 = AdaptingGmmClassifier(gmm_mgr1, izip(labels, count()))
classify0.set_relevance(333)
classify1.set_relevance(333)
classify0.set_num_em_iterations(2)
classify1.set_num_em_iterations(2)
Exemplo n.º 20
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)
Exemplo n.º 21
0
 def __init__(self, labels, nfeatures):
     self.labels = list(labels)
     self.nfeatures = nfeatures
     self.models = tuple(SimpleGaussianModel(nfeatures, GaussianModelBase.DIAGONAL_COVARIANCE) for _ in self.labels)
     self.samples_seen = [0] * len(self.models)