def init_with_kmeans(self, npimg, mask): print("Creating GMM.....") # print("step8") self._beta = self.Beta(npimg) self.Smoothness(npimg, self._beta, self._gamma) bgd = np.where(mask == self.GT_bgd) prob_fgd = np.where(mask == self.P_fgd) BGDpixels = npimg[bgd] #(_,3) FGDpixels = npimg[prob_fgd] #(_,3) self.KmeansBgd = Kmeans(BGDpixels, dim=3, cluster=5, epoches=2) self.KmeansFgd = Kmeans(FGDpixels, dim=3, cluster=5, epoches=2) bgdlabel = self.KmeansBgd.run() # (BGDpixel.shape[0],1) # print(bgdlabel) fgdlabel = self.KmeansFgd.run() # (FGDpixel.shape[0],1) # print(fgdlabel) self.BGD_GMM = GMM() # The GMM Model for BGD self.FGD_GMM = GMM() # The GMM Model for FGD for idx, label in enumerate(bgdlabel): self.BGD_GMM.add_pixel(BGDpixels[idx], label) for idx, label in enumerate(fgdlabel): self.FGD_GMM.add_pixel(FGDpixels[idx], label) # learning GMM parameters self.BGD_GMM.learning() self.FGD_GMM.learning()
def main(runIndex=None): print("Starting Main.main()") # if the required directory structure doesn't exist, create it makeDirectoryStructure(address) # now start the GMM process Load.main(address, filename_raw_data, runIndex, subsample_uniform,\ subsample_random, subsample_inTime, grid, conc, \ fraction_train, inTime_start, inTime_finish,\ fraction_nan_samples, fraction_nan_depths, cov_type,\ run_bic=False) # loads data, selects train, cleans, centres/standardises, prints PCA.create(address, runIndex, n_dimen, use_fPCA) GMM.create(address, runIndex, n_comp, cov_type) PCA.apply(address, runIndex) GMM.apply(address, runIndex, n_comp) # reconstruction (back into depth space) Reconstruct.gmm_reconstruct(address, runIndex, n_comp) Reconstruct.full_reconstruct(address, runIndex) Reconstruct.train_reconstruct(address, runIndex) # calculate properties mainProperties(address, runIndex, n_comp)
def main(): img = mpimg.imread('mountains.png')[:, :, :3] img_reshape = np.reshape(img, (-1, 3)) for m in [3, 5, 10]: gmm = GMM(m=m) gmm.fit(img_reshape, 10) img_cluster = gmm.clustering(img_reshape) img_cluster = np.reshape(img_cluster, (img.shape[0], img.shape[1], img.shape[2])) plt.imshow(img_cluster) plt.title("m = %d" % m) plt.show()
def main(): gmm = GMM(N=1000) gmm.append(normalDist(array([0.,10.]), array([[5.,0.], [0.,3.]])), 0.4) gmm.append(normalDist(array([-5.,-10.]), array([[5.,0.], [0.,5.]])), 0.3) gmm.append(normalDist(array([15.,15.]), array([[5.,0.], [0.,5.]]))) (t,x) = gmm.sample().mixtures() colors = [['blue', 'red', 'green'][int(label)] for label in t] plt.scatter(x[:,0], x[:,1], color=colors) cls = linear() cls.fit(x,t,target=1) print(cls.wml) xlim = [min(x[:,0]), max(x[:,0])] tics = arange(xlim[0]-3,xlim[1]+3,0.01) plt.xlim(xlim[0]-3,xlim[1]+3) plt.plot(*cls.cPlane(tics), color='black') plt.show()
def For_Iris(features, No_Component=2): data = pd.read_csv("Data/Iris.data", header=0) data = data.reset_index() if (features == 1): col = 'petal_width' x = data[[col]] x = np.array(x) gmm = GMM(x, No_Component) gmm.fit() plot_1D(gmm, x, col) else: replace_map = { 'class': { 'Iris-virginica': 1, 'Iris-versicolor': 2, 'Iris-setosa': 3 } } data.replace(replace_map, inplace=True) label = data[['class']] col = ['petal_width', 'sepal_width'] x = data[col] x = np.array(x) gmm = GMM(x, No_Component) gmm.fit() plot_2D(gmm, x, col, label)
def Training_feature_Covar(output_wavefile): Training_info = GMM.GMM(32, output_wavefile) Covar_training = Training_info.GMM_Model_Covar() return Covar_training
def Training_feature_Weight(output_wavefile): Training_info = GMM.GMM(32, output_wavefile) Weight_training = Training_info.GMM_Model_Weight() return Weight_training
def Training_feature_Mean(output_wavefile): Training_info = GMM.GMM(32, output_wavefile) Mean_training = Training_info.GMM_Model_Mean() return Mean_training
def loading(): time.sleep(5) # read the config file now_config = conf.Config().readConfig() # first run if os.path.exists('/home/pi/Factorynew.conf') is False: tmpfile = open('/home/pi/Factorynew.conf', 'w') tmpfile.close() WIFI(now_config['wifi_account'], now_config['wifi_password']) opencamera.CatchMO().setConfig(now_config) det.Detect().setMinTime(now_config['min_upload_seconds']) connect.Connection().initUrl(getIP(), now_config['server_port']) # init the network checknet = connect.Connection().scanServer() if not checknet: checkagain = connect.Connection().scanServer() if not checkagain: print('Network connection failed') return False # network checked -> start heart beat test t = thr.Thread(target=connect.Connection().heartBeatTest, args=()) t.start() print('Init finished') return True
def train(self, x, sampling=True, independent=True): ''' Parameters ---------- x : a batch of data sampling : whether to sample from the variational posterior distributions(if Ture, the default), or just use the mean of the variational distributions Return ------ log_likehoods : log like hood for each sample kl_sum : Sum of the KL divergences between the variational distributions and their priors ''' # The variational distributions mu = Normal(self.locs, self.scales) sigma = Gamma(self.alpha, self.beta) theta = Dirichlet(self.couts) # Sample from the variational distributions if sampling: # Nb = x.shape[0] Nb = 1 mu_sample = mu.rsample((Nb, )) sigma_sample = torch.pow(sigma.rsample((Nb, )), -0.5) theta_sample = theta.rsample((Nb, )) else: mu_sample = torch.reshape(mu.mean, (1, self.Nc, self.Nd)) sigma_sample = torch.pow( torch.reshape(sigma.mean, (1, self.Nc, self.Nd)), -0.5) theta_sample = torch.reshape(theta.mean, (1, self.Nc)) # 1*Nc # The mixture density log_var = (sigma_sample**2).log() log_likelihoods = GMM.get_likelihoods(x, mu_sample.reshape( (self.Nc, self.Nd)), log_var.reshape( (self.Nc, self.Nd)), log=True) # Nc*Nb log_prob_ = theta_sample @ log_likelihoods log_prob = log_prob_ # Compute the KL divergence sum mu_div = kl_divergence(mu, self.mu_prior) sigma_div = kl_divergence(sigma, self.sigma_prior) theta_div = kl_divergence(theta, self.theta_prior) KL = mu_div + sigma_div + theta_div if 0: print("mu_div: %f \t sigma_div: %f \t theta_div: %f" % (mu_div.sum().detach().numpy(), sigma_div.sum().detach().numpy(), theta_div.sum().detach().numpy())) return KL, log_prob
def main(run=None): print("Starting Main.main()") # Now start the GMM process Load.main(address, dir_raw_data, run, subsample_uniform, subsample_random,\ subsample_inTime, grid, conc, fraction_train, inTime_start,\ inTime_finish, fraction_nan_samples, fraction_nan_depths, dtype) #Load.main(address, filename_raw_data, run, subsample_uniform, subsample_random,\ # Loads data, selects Train, cleans, centres/standardises, prints PCA.create(address, run, n_dimen) # Uses Train to create PCA, prints results, stores object GMM.create(address, run, n_comp) # Uses Train to create GMM, prints results, stores object PCA.apply(address, run) # Applies PCA to test dataset GMM.apply(address, run, n_comp) # Applies GMM to test dataset # Reconstruction Reconstruct.gmm_reconstruct(address, run, n_comp) # Reconstructs the results in original space Reconstruct.full_reconstruct(address, run) Reconstruct.train_reconstruct(address, run) # new stuff DD 27/08/18, after seeing updates on DJ github #mainProperties(address, runIndex, n_comp) # Plotting -- first commented out DD #Plot.plotMapCircular(address, address_fronts, run, n_comp) #Plot.plotPosterior(address, address_fronts, run, n_comp, plotFronts=True) Plot.plotPostZonal(address, run, n_comp, dtype, plotFronts=False) ## zonal frequencies #Plot.plotPosterior(address, run, n_comp, dtype, plotFronts=False) ## works but data overlaps spatially... Plot.plotProfileClass(address, run, n_comp, dtype, 'uncentred') Plot.plotProfileClass(address, run, n_comp, dtype, 'depth') Plot.plotGaussiansIndividual(address, run, n_comp, dtype, 'reduced')#uncentred')#'depth')#reduced') # Plot.plotGaussiansIndividual(address, run, n_comp, 'depth') # ERROR NOT WOKRING PROPERLY # Plot.plotGaussiansIndividual(address, run, n_comp, 'uncentred') # ERROR NOT WOKRING PROPERLY #Plot.plotProfile(address, run, dtype, 'original') # these run just fine but are huge and unhelpful Plot.plotProfile(address, run, dtype, 'uncentred') Plot.plotWeights(address, run, dtype)
def main(): gmm = GMM(N=1000) gmm.append(normalDist(array([0., 10.]), array([[5., 0.], [0., 3.]])), 0.4) gmm.append(normalDist(array([-5., -10.]), array([[5., 0.], [0., 5.]])), 0.3) gmm.append(normalDist(array([15., 15.]), array([[5., 0.], [0., 5.]]))) (t, x) = gmm.sample().mixtures() colors = [['blue', 'red', 'green'][int(label)] for label in t] plt.scatter(x[:, 0], x[:, 1], color=colors) cls = linear() cls.fit(x, t, target=1) print(cls.wml) xlim = [min(x[:, 0]), max(x[:, 0])] tics = arange(xlim[0] - 3, xlim[1] + 3, 0.01) plt.xlim(xlim[0] - 3, xlim[1] + 3) plt.plot(*cls.cPlane(tics), color='black') plt.show()
def estGMM_Mean(self): w = self.alpha w /= w.sum() mu = np.zeros((self.K, self.D)) Sigma = np.zeros((self.K, self.D, self.D)) for k in xrange(self.K): m, S = self.qObs[k].getMean() mu[k] = m Sigma[k] = S mygmm = GMM.GMM(self.K, covariance_type='full') mygmm.w = w mygmm.mu = mu mygmm.Sigma = Sigma return mygmm
def For_BalanceScale(features): data = pd.read_csv("Data/balance-scale.data", header=0) if (features == 1): col = 'right_weight' x = data[[col]] x = np.array(x) gmm = GMM(x, 2) gmm.fit() plot_1D(gmm, x, col) else: replace_map = {'class': {'L': 1, 'B': 2, 'R': 3}} data.replace(replace_map, inplace=True) label = data[['class']] col = ['left_distance', 'right_distance'] x = data[col] x = np.array(x) gmm = GMM(x, 2) gmm.fit() plot_2D(gmm, x, col, label)
import numpy as np import data import Kmeans import GMM (D,L)=data.data(100,4) GMM.GMMrun(D,8) Kmeans.Kmeansrun(D,8)
import GMM import glob import cv2 as cv if __name__ == '__main__': train_num = 790 category_path = 'cameraJitter' video_path = 'boulevard' dataset_path = r'./dataset/' + category_path + '/' + video_path + '/input' results_path = r'./results/' + category_path + '/' + video_path gmm = GMM.GMM(data_dir=dataset_path, train_num=train_num) gmm.train() print('train finished') file_list = glob.glob(dataset_path + '/*') for index, file in enumerate(file_list): # only the frames after train_num will be used to evaulate scores. if index + 1 < train_num: continue print('infering:{}'.format(file)) img = cv.imread(file) img = gmm.infer(img) cv.imwrite(results_path +'/in%06d'%(index+1)+'.jpg', img)
from matplotlib import pyplot as plt import k_means as km import GMM as gmm def preprocess_data(file_name): """ Pre-process data, split by space :param file_name: string :return: ndarray """ output = [] with open(file_name, 'r') as read_file: for input_line in read_file: input_line = input_line.strip('\n').split(" ") vector = [float(x) for x in input_line] output.append(vector) return np.array(output, float) if __name__ == "__main__": data = preprocess_data("GMM_dataset.txt") # plt.scatter(data[:,0], data[:,1], c='black', s=7) # plt.savefig('raw_data.png') # k = 10 # k_cluster = km.KMeans(k, data) # k_cluster.best_run(data) # k_cluster.iteration_run(data, iterations=10) g_model = gmm.GMM(n_clusters=5, data=data) g_model.run(data)
import kMeansSimple as km import kMeansKernel as kmk import GMM as gm if __name__ == '__main__': km.kMMain() kmk.kMKernelMain() gm.GMMMain()
# # PCAT magic: Lifting the following from GMM.py in PCAT # import PCA, GMM score_matrix, principal_components, means, stds, eigenvalues = \ PCA.PCA(catalogue, components_number=10) principal_components_number=10 reduced_score_matrix = score_matrix[:,:principal_components_number] mat, tmp, tmp1 = PCA.matrix_whiten(reduced_score_matrix, std=True) #labels = GMM.gaussian_mixture(mat,upper_bound=5) labels = GMM.gaussian_mixture(reduced_score_matrix,upper_bound=5) colored_clusters = GMM.color_clusters( score_matrix, labels ) GMM.print_cluster_info(colored_clusters) #sys.exit() # # PCA # #H = np.matrix(waveform_catalogue) H = np.matrix(catalogue) # --- 1) Compute catalogue covariance C = H.T * H / np.shape(H)[0]
pi1 = 0.7 pi2 = 0.3 N = 500 X = np.zeros([N,2]) for i in range(N): u = np.random.rand() if u < pi1: X[i] = mvn(mu1,C1) else: X[i] = mvn(mu2,C2) ### Initial estimates for Gaussian Mixture Model ### mu = [ np.array([1,1]), np.array([-1,-1]) ] C = [np.eye(2),np.eye(2)] pi = np.array([0.5,0.5]) ### Create and train Gaussian Mixture Model ### gmm = GMM(X=X, mu_init=mu, C_init=C, pi_init=pi, N_mixtures=2) gmm.train(Ni=5) plt.plot(X[:,0],X[:,1],'o') gmm.plot() ### Print results ### for k in range(2): print('\nMean', k+1, ' = ', gmm.mu[k], '\n') print('Covariance matrix', k+1, ' = ', gmm.C[k], '\n') print('Mixture proportion', k+1, ' = ', gmm.pi[k], '\n')
import GMM import numpy as np # change the following parameters to user inputs path1 = "datasets/bat_flu.fa" path2 = "datasets/penguin_flu.fa" k_min = 2 k_max = 3 num_class = 2 cov_type = 'full' predictions = GMM.get_predictions(path1, path2, k_min, k_max, num_class, cov_type) df = GMM.get_kmer_table(["datasets/bat_flu.fa", "datasets/penguin_flu.fa"], k_min, k_max) df2 = df.drop_duplicates(keep="last") kept_viruses = df2.index.values bat_len = len(GMM.get_gene_sequences("datasets/bat_flu.fa")) penguin_len = len(GMM.get_gene_sequences("datasets/penguin_flu.fa")) zeros = [0] * bat_len labels1 = np.append(zeros, [1] * penguin_len, axis=None) plot_labels = labels1[kept_viruses] X = df2.as_matrix(columns=df2.columns) [y, E] = GMM.sammon(X, 2) GMM.sammon_plot(y, plot_labels) GMM.PCA_plot(df, labels1, num_class) GMM.tsne_plot(df, labels1) GMM.model_selection(["datasets/bat_flu.fa", "datasets/penguin_flu.fa"], labels1, num_class)
data = [] img = image.open(f) m, n = img.size for i in range(m): for j in range(n): x= img.getpixel( (i, j) ) # print(x[0]) data.append( [x[0] / 256.0] ) #只有一维的灰度值 f.close() return np.mat(data), m, n matY, row, col = loadData('1.jpg') #1.jpg是彩色图片 #matY, row, col = greyData('3.jpg')#3.jpg是灰度图片 K=3 mu,cov,alpha=GMM.GMM_EM(matY,K,20) #20代表EM算法迭代的次数 N=matY.shape[0] gamma=GMM.expectation(matY,mu,cov,alpha) category=gamma.argmax(axis=1).flatten().tolist()[0] category=np.array(category) category = category.reshape( [row, col] ) print(category) pic_new = image.new( 'L', (row, col) ) print(pic_new) for i in range(row): for j in range(col):
import GMM if __name__ == '__main__': GMM.gmm('C:/Users/UsedToBe/Desktop/PyLab/GMM/data/train.txt', 2)
ylabel = [dictionary[elt] for elt in labels] plt.plot(x, ypred, label='Predicted') plt.plot(x, ylabel, label='True Gesture') plt.legend(loc='best') plt.title('HMM Gesture Estimate') plt.yticks(np.arange(8), ('None','G1','G11','G12','G13a','G13b','G14','G15')) plt.show() if __name__ == "__main__": num_Gaussians = 12 GMM_G1 = GMM(gesture_name="G1", num_files=19, num_Gaussians=num_Gaussians) GMM_G11 = GMM(gesture_name="G11", num_files=36, num_Gaussians=num_Gaussians) GMM_G12 = GMM(gesture_name="G12", num_files=70, num_Gaussians=num_Gaussians) GMM_G13 = GMM(gesture_name="G13", num_files=75, num_Gaussians=num_Gaussians) GMM_G14 = GMM(gesture_name="G14", num_files=98, num_Gaussians=num_Gaussians) GMM_G15 = GMM(gesture_name="G15", num_files=73, num_Gaussians=num_Gaussians) state_G1 = State(GMM_G1.model, name="g1") state_G11 = State(GMM_G11.model, name="g11") state_G12 = State(GMM_G12.model, name="g12") state_G13 = State(GMM_G13.model, name="g13") state_G14 = State(GMM_G14.model, name="g14") state_G15 = State(GMM_G15.model, name="g15")
def composeGMM(type=1): if type == 1: gmm = GMM(N=1000) gmm.append(normalDist(array([-10., 5.]), array([[5., 0.], [0., 3.]])), 0.4) gmm.append(normalDist(array([-5., -10.]), array([[5., 0.], [0., 5.]])), 0.3) gmm.append(normalDist(array([15., 15.]), array([[5., 0.], [0., 5.]]))) elif type == 2: gmm = GMM(N=1000) gmm.append(normalDist(array([0., 5.]), array([[5., 0.], [0., 3.]])), 0.3) gmm.append(normalDist(array([-2., -10.]), array([[5., 0.], [0., 5.]])), 0.3) gmm.append(normalDist(array([-5., 15.]), array([[5., 0.], [0., 5.]]))) elif type == 3: gmm = GMM(N=1000) gmm.append(normalDist(array([5., 5.]), array([[20., 0.], [0., 3.]])), 0.5) gmm.append(normalDist(array([-5., -5.]), array([[20., 0.], [0., 3.]]))) return gmm
import matplotlib.pyplot as plt import GMM import numpy as np DEBUG = True #Y=np.loadtxt("GMMData.txt") Y = np.loadtxt("kmeanstestset.txt") matY = np.matrix(Y, copy=True) K = 4 #k为分类的数量 mu, cov, alpha = GMM.GMM_EM(matY, K, 100) N = Y.shape[0] gamma = GMM.expectation(matY, mu, cov, alpha) category = gamma.argmax(axis=1).flatten().tolist()[0] class0 = np.array([Y[i] for i in range(N) if category[i] == 0]) class1 = np.array([Y[i] for i in range(N) if category[i] == 1]) class2 = np.array([Y[i] for i in range(N) if category[i] == 2]) class3 = np.array([Y[i] for i in range(N) if category[i] == 3]) plt.plot(class0[:, 0], class0[:, 1], 'rs', label="class0") plt.plot(class1[:, 0], class1[:, 1], 'bo', label="class1") plt.plot(class2[:, 0], class2[:, 1], 'gs', label="class2") plt.plot(class3[:, 0], class3[:, 1], 'ko', label="class3") plt.legend(loc="best") plt.title("GMM") plt.savefig("666", dpi=600) plt.show()
import GMM import glob import cv2 as cv if __name__ == '__main__': data_dir = r'./WavingTrees' train_num = 200 gmm = GMM.GMM(data_dir=data_dir, train_num=train_num) gmm.train() print('train finished') file_list = glob.glob(r'./WavingTrees/b*.bmp') file_index = 0 for index, file in enumerate(file_list): print('infering:{}'.format(file)) img = cv.imread(file) img = gmm.infer(img) cv.imwrite(r'./output/' + '%05d' % index + '.bmp', img) index += 1
N = np.array([10,20,30]) mu = np.array([[0.5, 1.2], [-1.2, 0.3], [3, -3]]) cov = np.zeros((3, 2, 2)) cov[0,:] = 0.5 * np.eye(2) cov[1,:] = 0.3 * np.eye(2) cov[2,:] = np.eye(2) print('pi') print(N/np.sum(N)) print('mu') print(mu) print('s') print(cov) Y = np.zeros((np.sum(N), 2)) for i in range(N[0]): Y[i,:] = np.random.multivariate_normal(mu[0,:], cov[0,:]) for i in range(N[0], (N[0] + N[1])): Y[i,:] = np.random.multivariate_normal(mu[1,:], cov[1,:]) for i in range((N[0] + N[1]), np.sum(N)): Y[i,:] = np.random.multivariate_normal(mu[2,:], cov[2,:]) GMM.GMM(Y, 3, initializer = GMM.kmean_initialization)
def newton_raphson(df, p0, W, X_dep=None, X_cons=None, X_comm=None, X_ins=None, X_inv=None, Z_dep=None, Z_cons=None, Z_comm=None, Z_ins=None, Z_inv=None, ftol=1e-8): # p_idx = list(range(0,5)) # p_idx.extend(range(9,len(p0))) # capped_params_idx = list(range(0,5)) p_idx = list(range(len(p0))) capped_params_idx = list(range(0, 9)) print(p0[capped_params_idx]) # print(capped_params_idx) ### Print Format np.set_printoptions(precision=8) ### Initial Evaluation fval, G, H = gmm.compute_gmm_hessian(df, p0, W, X_dep=X_dep, X_cons=X_cons, X_comm=X_comm, X_ins=X_ins, X_inv=X_inv, Z_dep=Z_dep, Z_cons=Z_cons, Z_comm=Z_comm, Z_ins=Z_ins, Z_inv=Z_inv) G = G[p_idx] H = H[np.ix_(p_idx, p_idx)] print('Function value at starting parameter:', "{:.8g}".format(fval)) print('Gradient value at starting parameter:', G) print('hessian value at starting parameter:', H) grad_size = np.sqrt(np.dot(G, G)) param_vec = p0.copy() param_new = p0.copy() itr = 0 while grad_size > ftol: ## Find NR step and new parameter vector if len(p_idx) > 1: step = -np.matmul(G, np.linalg.inv(H)) else: step = -G / H # Cap step change at half of parameters estimate. # This should effectively stop the affected parameters from changing sign check_cap = [(abs(step[x]) / param_vec[x]) < 0.5 or abs(step[x]) < 0.25 for x in capped_params_idx] if False in check_cap: cap = max( abs(step[capped_params_idx]) / param_vec[capped_params_idx]) step = step / cap * 0.5 print( 'Hit step cap of 50% parameter value on non_linear_parameters') param_new[p_idx] = param_vec[p_idx] + step print('Now trying parameter vector', param_vec[0:9]) # print('Newton Raphson Step: ',step) ## Make an attempt to be descending new_fval = gmm.compute_gmm(df, param_new, W, X_dep=X_dep, X_cons=X_cons, X_comm=X_comm, X_ins=X_ins, X_inv=X_inv, Z_dep=Z_dep, Z_cons=Z_cons, Z_comm=Z_comm, Z_ins=Z_ins, Z_inv=Z_inv) alpha = abs(1 / np.diag(H)) while new_fval > fval * (1 + 1e-5): step = -G * alpha cap = max( abs(step[capped_params_idx]) / param_vec[capped_params_idx]) if cap > 0.5: step = step / cap * 0.5 print( 'Hit step cap of 50% parameter value on non_linear_parameters' ) # print("New value","{:.3g}".format(new_fval),"exceeds old value","{:.3g}".format(fval),"by too much") # print("Step along the gradient:",step) print("Gradient step") param_new[p_idx] = param_vec[p_idx] + step print('Now trying parameter vector', param_vec[0:9]) new_fval = gmm.compute_gmm(df, param_new, W, X_dep=X_dep, X_cons=X_cons, X_comm=X_comm, X_ins=X_ins, X_inv=X_inv, Z_dep=Z_dep, Z_cons=Z_cons, Z_comm=Z_comm, Z_ins=Z_ins, Z_inv=Z_inv) alpha = alpha / 10 param_vec[p_idx] = param_vec[p_idx] + step # Evaluation for next iteration fval, G, H = gmm.compute_gmm_hessian(df, param_vec, W, X_dep=X_dep, X_cons=X_cons, X_comm=X_comm, X_ins=X_ins, X_inv=X_inv, Z_dep=Z_dep, Z_cons=Z_cons, Z_comm=Z_comm, Z_ins=Z_ins, Z_inv=Z_inv) ## Allow for estiamtion to finish even if it's not well identified check_unidentified = [ x for x in capped_params_idx if (param_vec[x] > 1e7) or (param_vec[x] < -1e7) ] print(check_unidentified) G[check_unidentified] = 0 G = G[p_idx] H = H[np.ix_(p_idx, p_idx)] grad_size = np.sqrt(np.dot(G, G)) itr += 1 # Print Status Report print('Function value is', "{:.8g}".format(fval), 'and gradient is', "{:.3g}".format(grad_size), 'on iteration number', itr) print('Solution!', param_vec) print('Function value is ', "{:.8g}".format(fval), 'and gradient is', "{:.3g}".format(grad_size), 'after', itr, 'iterations') return param_vec
class grabcut(object): # print("step3") def __init__(self): # print("step5") self.cluster = 5 self.iter = 2 self.BGD_GMM = None self.FGD_GMM = None self.KmeansBgd = None self.KmeansFgd = None self._gamma = 50 self._lambda = 9 * self._gamma self.GT_bgd = 0 #ground truth background self.P_fgd = 1 #ground truth foreground self.P_bgd = 2 #may be background self.GT_fgd = 3 #may be foreground #calculating Beta for smootheness def Beta(self, npimg): # print("step6") rows, cols = npimg.shape[:2] ldiff = np.linalg.norm(npimg[:, 1:] - npimg[:, :-1]) uldiff = np.linalg.norm(npimg[1:, 1:] - npimg[:-1, :-1]) udiff = np.linalg.norm(npimg[1:, :] - npimg[:-1, :]) urdiff = np.linalg.norm(npimg[1:, :-1] - npimg[:-1, 1:]) beta = np.square(ldiff) + np.square(uldiff) + np.square( udiff) + np.square(urdiff) beta = 1 / (2 * beta / (4 * cols * rows - 3 * cols - 3 * rows + 2)) # print(beta) return beta #estimating smoothness term def Smoothness(self, npimg, beta, gamma): # print("step7") rows, cols = npimg.shape[:2] self.lweight = np.zeros([rows, cols]) self.ulweight = np.zeros([rows, cols]) self.uweight = np.zeros([rows, cols]) self.urweight = np.zeros([rows, cols]) for y in range(rows): # print("stop1") for x in range(cols): color = npimg[y, x] if x >= 1: diff = color - npimg[y, x - 1] # print(np.exp(-self.beta*(diff*diff).sum())) self.lweight[y, x] = gamma * np.exp(-beta * (diff * diff).sum()) if x >= 1 and y >= 1: diff = color - npimg[y - 1, x - 1] self.ulweight[y, x] = gamma / np.sqrt(2) * np.exp( -beta * (diff * diff).sum()) if y >= 1: diff = color - npimg[y - 1, x] self.uweight[y, x] = gamma * np.exp(-beta * (diff * diff).sum()) if x + 1 < cols and y >= 1: diff = color - npimg[y - 1, x + 1] self.urweight[y, x] = gamma / np.sqrt(2) * np.exp( -beta * (diff * diff).sum()) #creating GMM for foreground and background def init_with_kmeans(self, npimg, mask): print("Creating GMM.....") # print("step8") self._beta = self.Beta(npimg) self.Smoothness(npimg, self._beta, self._gamma) bgd = np.where(mask == self.GT_bgd) prob_fgd = np.where(mask == self.P_fgd) BGDpixels = npimg[bgd] #(_,3) FGDpixels = npimg[prob_fgd] #(_,3) self.KmeansBgd = Kmeans(BGDpixels, dim=3, cluster=5, epoches=2) self.KmeansFgd = Kmeans(FGDpixels, dim=3, cluster=5, epoches=2) bgdlabel = self.KmeansBgd.run() # (BGDpixel.shape[0],1) # print(bgdlabel) fgdlabel = self.KmeansFgd.run() # (FGDpixel.shape[0],1) # print(fgdlabel) self.BGD_GMM = GMM() # The GMM Model for BGD self.FGD_GMM = GMM() # The GMM Model for FGD for idx, label in enumerate(bgdlabel): self.BGD_GMM.add_pixel(BGDpixels[idx], label) for idx, label in enumerate(fgdlabel): self.FGD_GMM.add_pixel(FGDpixels[idx], label) # learning GMM parameters self.BGD_GMM.learning() self.FGD_GMM.learning() # initial call def __call__(self, epoches, npimg, mask): print("Starting.....") # print("step9") self.init_with_kmeans(npimg, mask) for epoch in range(epoches): self.assign_step(npimg, mask) self.learn_step(npimg, mask) self.construct_gcgraph(npimg, mask) mask = self.estimate_segmentation(mask) img = copy.deepcopy(npimg) img[np.logical_or(mask == self.P_bgd, mask == self.GT_bgd)] = 0 return Image.fromarray(img.astype(np.uint8)) # assigning GMMs parameters def assign_step(self, npimg, mask): print("Assinging GMM parameter.....") # print("step10") rows, cols = npimg.shape[:2] clusterid = np.zeros((rows, cols)) for row in range(rows): for col in range(cols): pixel = npimg[row, col] if mask[row, col] == self.GT_bgd or mask[row, col] == self.P_bgd: #bgd clusterid[row, col] = self.BGD_GMM.pixel_from_cluster(pixel) else: clusterid[row, col] = self.FGD_GMM.pixel_from_cluster(pixel) self.clusterid = clusterid.astype(np.int) #Learning GMM parameter def learn_step(self, npimg, mask): print("Learning parameter......") # print("step11") for cluster in range(self.cluster): bgd_cluster = np.where( np.logical_and( self.clusterid == cluster, np.logical_or(mask == self.GT_bgd, mask == self.P_bgd))) fgd_cluster = np.where( np.logical_and( self.clusterid == cluster, np.logical_or(mask == self.GT_fgd, mask == self.P_fgd))) for pixel in npimg[bgd_cluster]: self.BGD_GMM.add_pixel(pixel, cluster) for pixel in npimg[fgd_cluster]: self.FGD_GMM.add_pixel(pixel, cluster) self.BGD_GMM.learning() self.FGD_GMM.learning() # constructing graph def construct_gcgraph(self, npimg, mask): print("Graph construction...may take a while.....") # print("step12") rows, cols = npimg.shape[:2] vertex_count = rows * cols edge_count = 2 * (4 * vertex_count - 3 * (rows + cols) + 2) self.graph = GCGraph(vertex_count, edge_count) for row in range(rows): for col in range(cols): #source background sink foreground vertex_index = self.graph.add_vertex() color = npimg[row, col] if mask[row, col] == self.P_bgd or mask[ row, col] == self.P_fgd: #pred fgd fromSource = -log(self.BGD_GMM.pred_GMM(color)) toSink = -log(self.FGD_GMM.pred_GMM(color)) elif mask[row, col] == self.GT_bgd: fromSource = 0 toSink = self._lambda else: fromSource = self._lambda toSink = 0 self.graph.add_term_weights(vertex_index, fromSource, toSink) if col - 1 >= 0: w = self.lweight[row, col] self.graph.add_edges(vertex_index, vertex_index - 1, w, w) if row - 1 >= 0 and col - 1 >= 0: w = self.ulweight[row, col] self.graph.add_edges(vertex_index, vertex_index - cols - 1, w, w) if row - 1 >= 0: w = self.uweight[row, col] self.graph.add_edges(vertex_index, vertex_index - cols, w, w) if col + 1 < cols and row - 1 >= 0: w = self.urweight[row, col] self.graph.add_edges(vertex_index, vertex_index - cols + 1, w, w) # segmentation estimation E( α , k, θ , z) - min cut def estimate_segmentation(self, mask): print("Estimation.......") # print("step13") rows, cols = mask.shape self.graph.max_flow() for row in range(rows): for col in range(cols): if mask[row, col] == self.P_fgd or mask[row, col] == self.P_bgd: if self.graph.insource_segment(row * cols + col): # Vertex Index mask[row, col] = self.P_fgd else: mask[row, col] = self.P_bgd # print("working") # self.KmeansBgd.plot() # self.KmeansFgd.plot() return mask
sum_nij += var * (var - 1) / 2.0 sum_a = np.sum(conting_matrix, axis=0) sum_b = np.sum(conting_matrix, axis=1) for i in range(K): sum_ai += sum_a[i] * (sum_a[i] - 1) / 2.0 sum_bj += sum_b[i] * (sum_b[i] - 1) / 2.0 var = N * (N - 1) / 2.0 ARI = (sum_nij - (sum_ai * sum_bj) / var) / (0.5 * (sum_ai + sum_bj) - sum_ai * sum_bj / var) print('ARI: ' + str(ARI)[:6]) return ARI if __name__ == "__main__": K = 3 '''生成多维高斯分布数据并保存''' #X, Y = gaussian_data(30, K) # np.savetxt('GaussianData1', X) '''加载数据''' X = np.loadtxt('dataset/GaussianData') '''UCI数据集''' #X, Y = load_iris_dataset() '''K-means聚类 & GMM''' print('k-means process...') Y_ = kmeans_process(X, K) print('GMM process...') Y_ = GMM(X, K) '''对聚类结果进行评价和展示''' #ARI(Y, Y_) show(X, Y_, 'GMM')
def composeGMM(type=1): if type == 1: gmm = GMM(N=1000) gmm.append(normalDist(array([-10.,5.]), array([[5.,0.], [0.,3.]])), 0.4) gmm.append(normalDist(array([-5.,-10.]), array([[5.,0.], [0.,5.]])), 0.3) gmm.append(normalDist(array([15.,15.]), array([[5.,0.], [0.,5.]]))) elif type == 2: gmm = GMM(N=1000) gmm.append(normalDist(array([0.,5.]), array([[5.,0.], [0.,3.]])), 0.3) gmm.append(normalDist(array([-2.,-10.]), array([[5.,0.], [0.,5.]])), 0.3) gmm.append(normalDist(array([-5.,15.]), array([[5.,0.], [0.,5.]]))) elif type == 3: gmm = GMM(N=1000) gmm.append(normalDist(array([5.,5.]), array([[20.,0.], [0.,3.]])), 0.5) gmm.append(normalDist(array([-5.,-5.]), array([[20.,0.], [0.,3.]]))) return gmm
(2) [email protected] """ # Load 2D data with 3 clusters file = open('3_cluster_data_unsupervised.dat', 'rb') X, N = pickle.load(file) file.close() # Create and train GMM object (our code) mu = [] C = [] for i in range(3): mu.append(np.random.randn(2)) C.append(np.eye(2)) pi = np.array([1/3, 1/3, 1/3]) gmm = GMM(X=X, mu_init=mu, C_init=C, pi_init=pi, N_mixtures=3) gmm.train(Ni=10) # Print results print('\n### Our code ###') for k in range(3): print('Mean', k+1, ' = ', gmm.mu[k]) print('Covariance matrix', k+1, ' = ', gmm.C[k]) print('Mixture proportion', k+1, ' = ', gmm.pi[k], '\n') # Plot results r1 = np.linspace(np.min(gmm.X[:, 0]), np.max(gmm.X[:, 1]), 100) r2 = np.linspace(np.min(gmm.X[:, 1]), np.max(gmm.X[:, 1]), 100) x_r1, x_r2 = np.meshgrid(r1, r2) pos = np.empty(x_r1.shape + (2, )) pos[:, :, 0] = x_r1