Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
0
    def __init__(self, *args):
        """
        Initialization can take four forms.  First, one int argument constructs
        an empty GmmMgr for models with the given dimension (number of
        features).  Second, another GmmMgr can be passed in, in which case the
        new GmmMgr is a deep copy of the argument.  Third, an iterable of models
        can be passed in, in which case the new GmmMgr will have those models,
        in the order iterated.  In this case, the iterable should return
        instances of either GaussianMixtureModel or DummyModel instances, and
        models must all have the same covariance type.  Finally, arguments may
        be provided in the form (num_comps, dimension, covar_type priming=None),
        that is, with 3 or 4 arguments, where num_comps is an iterable of the
        number of components for each model, dimension is a positive integer,
        and covar_type is either SimpleGaussianModel.DIAGONAL_COVARIANCE or
        SimpleGaussianModel.FULL_COVARIANCE .  New GaussianMixtureModels will be
        created for each element returned by num_comps.  Priming, if it is
        provided, is an iterable of SimpleGaussianModels which will be used to
        initialize all the components of each model, so the priming argument
        must be as long as the num_comps argument, and the priming models should
        have the same covariance type as covar_type.
        """

        if not len(args) in set((1, 3, 4)):
            raise ValueError("expected 1, 3, or 4 arguments, but got %d" %
                             (len(args), ))

        self._models = list()
        self._covariance_type = None
        self._accums = dict()

        if len(args) == 1:
            if isinstance(args[0], GmmMgr):
                other = args[0]
                super(GmmMgr, self).__init__(other.dimension)
                for model in other:
                    self._models.append(model.copy())
                self._covariance_type = other._covariance_type
            elif isinstance(args[0], int):
                super(GmmMgr, self).__init__(args[0])
            else:
                models = tuple(args[0])
                if len(models) == 0:
                    raise ValueError("can't construct from an empty iterable")
                super(GmmMgr, self).__init__(models[0].dimension)
                self.add_models(models)

        else:
            assert (3 <= len(args) <= 4)
            num_comps, dimension, covar_type = args[0], args[1], args[2]
            super(GmmMgr, self).__init__(dimension)
            num_comps = tuple(num_comps)
            priming = tuple(args[3]) if len(args) == 4 else None
            if priming is not None:
                if len(priming) < len(num_comps):
                    raise ValueError(
                        "not enough priming models were provided - expected %d but got %d"
                        % (len(num_comps), len(priming)))
            else:
                priming = repeat(None)
            for nc, primer in izip(num_comps, priming):
                self._models.append(
                    GaussianMixtureModel(dimension, covar_type, nc, primer))
            self._covariance_type = covar_type