Exemple #1
0
 def __init__(self, num_comp, num_process, num_dim):
     MoG.__init__(self, num_comp, num_process, num_dim)
     self.invC_klj_Sk = np.empty((self.num_comp, self.num_comp, self.num_process, self.num_dim))
     self.s = []
     self._fixed_init()
     self._update()
     self.num_free_params = self.parameters.shape[0]
Exemple #2
0
 def _random_init(self):
     MoG._random_init(self)
     # self.m = np.zeros((self.num_comp, self.num_process, self.num_dim))
     for k in range(self.num_comp):
         for j in range(self.num_process):
             self.L_flatten[k, j, :] = np.random.uniform(
                 low=1.1, high=5.0, size=self.get_sjk_size())
Exemple #3
0
 def _fixed_init(self):
     MoG._fixed_init(self)
     self.s = np.random.uniform(low=0.5,
                                high=0.5,
                                size=(self.num_comp, self.num_process,
                                      self.num_dim))
     self.log_s = np.log(self.s)
Exemple #4
0
 def _random_init(self):
     MoG._random_init(self)
     self.s = np.random.uniform(low=1.0,
                                high=3.0,
                                size=(self.num_comp, self.num_process,
                                      self.num_dim))
     self.log_s = np.log(self.s)
Exemple #5
0
 def __init__(self, num_comp, num_process, num_dim):
     MoG.__init__(self, num_comp, num_process, num_dim)
     self.invC_klj_Sk = np.empty(
         (self.num_comp, self.num_comp, self.num_process, self.num_dim))
     self.s = []
     self._fixed_init()
     self._update()
     self.num_free_params = self.parameters.shape[0]
Exemple #6
0
 def __init__(self, num_process, num_dim):
     MoG.__init__(self, 1, num_process, num_dim)
     self.invC_klj = np.empty((self.num_comp, self.num_comp, self.num_process, self.num_dim, self.num_dim))
     self.m = []
     self.pi = []
     self.L_flatten = np.empty((self.num_comp, self.num_process, self.get_sjk_size()))
     self.s = np.empty((self.num_comp, self.num_process, self.num_dim, self.num_dim))
     self.L = np.empty((self.num_comp, self.num_process, self.num_dim, self.num_dim))
     self.log_det = np.empty((self.num_comp, self.num_comp, self.num_process))
     self._fixed_init()
     self._update()
     self.num_free_params = self.parameters.shape[0]
Exemple #7
0
def test_gaussian_kde():

    pdf = MoG(a=[0.3, 0.7], ms=[[-1.0, 0.0], [1.0, 0.0]], Ss=[np.diag([0.1, 1.1]), np.diag([1.1, 0.1])])
    xs = pdf.gen(5000)
    kde = gaussian_kde(xs)

    import util.plot

    lims = [-4.0, 4.0]
    util.plot.plot_pdf_marginals(pdf, lims=lims).suptitle('original')
    util.plot.plot_hist_marginals(xs, lims=lims).suptitle('samples')
    util.plot.plot_pdf_marginals(kde, lims=lims).suptitle('KDE')
    util.plot.plt.show()
Exemple #8
0
    def compute_stat(self, img_array, data_type, file_type, k):
        img_array = np.array(img_array)
        pca = PCA(n_components=self.reduced_dimensions,
                  svd_solver='randomized').fit(img_array)
        pca_array = pca.transform(img_array)
        if data_type == 'positive':
            model = MoG(pca_array, k)
            model.exp_max(tolerance=1e-18)
            for i in range(0, k):
                mu = pca.inverse_transform(model.mu[i])
                if (file_type == "rgb"):
                    mu = np.round(mu.reshape((100, 100, 3))).astype(np.uint8)
                elif file_type == "hsv":
                    mu = np.round(mu.reshape((100, 100, 3))).astype(np.uint8)
                    mu = cv2.cvtColor(mu, cv2.COLOR_HSV2BGR)
                else:
                    mu = np.round(mu.reshape((100, 100, 1))).astype(np.uint8)


#                print(np.max(mu))
                print(model.theta[i])
                cv2.imshow('mean', mu)
                cv2.waitKey(0)
                cv2.destroyAllWindows()
                #                    print(self.output_image_dir+file_type+"+"_mog_"+repr(i)
                cv2.imwrite(
                    self.output_image_dir + "/" + file_type + "_mog_" +
                    repr(i + 1) + ".jpg", mu)
            np.save(self.output_numpy_dir + "/" + file_type + "_mu", model.mu)
            np.save(self.output_numpy_dir + "/" + file_type + "_cov",
                    model.cov)
            np.save(self.output_numpy_dir + "/" + file_type + "_theta",
                    model.theta)
        else:
            mu = np.mean(img_array, axis=0)
            if (file_type == "rgb"):
                mu = np.round(mu.reshape((100, 100, 3))).astype(np.uint8)
            elif file_type == "hsv":
                mu = np.round(mu.reshape((100, 100, 3))).astype(np.uint8)
                mu = cv2.cvtColor(mu, cv2.COLOR_HSV2BGR)
            else:
                mu = np.round(mu.reshape((100, 100, 1))).astype(np.uint8)
            cv2.imwrite(
                self.output_image_dir + "/" + file_type + "_mog_neg.jpg", mu)
            pca_mu = np.mean(pca_array, axis=0)
            cov = np.cov(pca_array.T)
            np.save(self.output_numpy_dir + "/" + file_type + "_mu_neg",
                    pca_mu)
            np.save(self.output_numpy_dir + "/" + file_type + "_cov_neg", cov)
Exemple #9
0
 def __init__(self, num_process, num_dim):
     MoG.__init__(self, 1, num_process, num_dim)
     self.invC_klj = np.empty(
         (self.num_comp, self.num_comp, self.num_process, self.num_dim,
          self.num_dim))
     self.m = []
     self.pi = []
     self.L_flatten = np.empty(
         (self.num_comp, self.num_process, self.get_sjk_size()))
     self.s = np.empty(
         (self.num_comp, self.num_process, self.num_dim, self.num_dim))
     self.L = np.empty(
         (self.num_comp, self.num_process, self.num_dim, self.num_dim))
     self.log_det = np.empty(
         (self.num_comp, self.num_comp, self.num_process))
     self._fixed_init()
     self._update()
     self.num_free_params = self.parameters.shape[0]
Exemple #10
0
def gaussian_kde(xs, ws=None, std=None):
    """
    Returns a mixture of gaussians representing a kernel density estimate.
    :param xs: rows are datapoints
    :param ws: weights, optional
    :param std: the std of the kernel, if None then a default is used
    :return: a MoG object
    """

    xs = np.array(xs)
    assert xs.ndim == 2, 'wrong shape'

    n_data, n_dims = xs.shape
    ws = np.full(n_data, 1.0 / n_data) if ws is None else np.asarray(ws)
    var = n_data ** (-2.0 / (n_dims + 4)) if std is None else std ** 2

    return MoG(a=ws, ms=xs, Ss=[var * np.eye(n_dims) for _ in xrange(n_data)])
Exemple #11
0
 def _fixed_init(self):
     MoG._fixed_init(self)
     for k in range(self.num_comp):
         for j in range(self.num_process):
             self.L_flatten[k, j, :] = np.random.uniform(
                 low=1.0, high=1.0, size=self.get_sjk_size())
Exemple #12
0
 def _random_init(self):
     MoG._random_init(self)
     self.s = np.random.uniform(low=1.0, high=3.0, size=(self.num_comp, self.num_process, self.num_dim))
     self.log_s = np.log(self.s)
Exemple #13
0
 def _fixed_init(self):
     MoG._fixed_init(self)
     self.s = np.random.uniform(low=0.5, high=0.5, size=(self.num_comp, self.num_process, self.num_dim))
     self.log_s = np.log(self.s)
Exemple #14
0
 def _random_init(self):
     MoG._random_init(self)
     # self.m = np.zeros((self.num_comp, self.num_process, self.num_dim))
     for k in range(self.num_comp):
         for j in range(self.num_process):
             self.L_flatten[k,j,:] = np.random.uniform(low=1.1, high=5.0, size=self.get_sjk_size())
Exemple #15
0
 def _fixed_init(self):
     MoG._fixed_init(self)
     for k in range(self.num_comp):
         for j in range(self.num_process):
             self.L_flatten[k,j,:] = np.random.uniform(low=1.0, high=1.0, size=self.get_sjk_size())