def main(): for img in range(2, 3): print("Denoising for image " + str(img)) data, image = read_data("../a1/" + str(img) + "_noise.txt", True) print(data.shape) print(image.shape) image[image == 0] = -1 image[image == 255] = 1 avg = denoise(image, 0.7, 5, 10) avg[avg >= 0] = 255 avg[avg < 0] = 0 print(avg.shape) width = avg.shape[0] height = avg.shape[1] counter = 0 for i in range(0, width): for j in range(0, height): data[counter][2] = avg[i][j][0] counter = counter + 1 write_data(data, "../output/" + str(img) + "_denoise.txt") read_data("../output/" + str(img) + "_denoise.txt", True, save=True, save_name="../output/" + str(img) + "_denoise.jpg") print("Finished writing data. Please check " + str(img) + "_denoise.jpg \n")
def main(): #Approximate mean values obtained by running K-means using scikit library data, image = read_data("../a2/cow.txt", True) X = np.reshape(image, (image.shape[0] * image.shape[1], image.shape[2])) kmeans = KMeans(n_clusters=2, random_state=0).fit(X) clusters = kmeans.cluster_centers_ fit_EM(clusters[0], clusters[1], "cow", data, image) data, image = read_data("../a2/fox.txt", True) X = np.reshape(image, (image.shape[0] * image.shape[1], image.shape[2])) kmeans = KMeans(n_clusters=2, random_state=0).fit(X) clusters = kmeans.cluster_centers_ fit_EM(clusters[0], clusters[1], "fox", data, image) data, image = read_data("../a2/owl.txt", True) X = np.reshape(image, (image.shape[0] * image.shape[1], image.shape[2])) kmeans = KMeans(n_clusters=2, random_state=0).fit(X) clusters = kmeans.cluster_centers_ fit_EM(clusters[0], clusters[1], "owl", data, image) data, image = read_data("../a2/zebra.txt", True) X = np.reshape(image, (image.shape[0] * image.shape[1], image.shape[2])) kmeans = KMeans(n_clusters=2, random_state=0).fit(X) clusters = kmeans.cluster_centers_ fit_EM(clusters[0], clusters[1], "zebra", data, image)
def main(filename): data, image = read_data(filename, True) image = image.transpose(1, 0, 2) #Converting gray to binary (-1,1) image[image == 0] = -1 image[image == 255] = 1 burn_in_itr = 10 itr = 30 q = 0.73 l1_norm = 0.5 * np.log(q / (1 - q)) denoise_sub = gibbs(image, l1_norm * image, 3) avg = np.zeros_like(image).astype(np.float32) for i in range(burn_in_itr + itr): print("Iteration - " + str(i)) for j in range(image.shape[0]): for k in range(image.shape[1]): if (random.uniform(0, 1) <= 0.73): denoise_sub.gibbs_sample(j, k) if (i > burn_in_itr): avg += denoise_sub.image avg / itr #converting back to gray scale avg[avg >= 0] = 255 avg[avg < 0] = 0 row = avg.shape[0] column = avg.shape[1] cnt = 0 print(avg.shape) for i in range(0, row): for j in range(0, column): data[cnt][2] = avg[i][j][0] cnt = cnt + 1 write_data(data, filename + "_denoise.txt") read_data(filename + "_denoise.txt", True, save=True, save_name=filename + "_denoise.jpg") print("Iterations completed - Please check your folder for output -" + filename + "_denoise.jpg")
probs = np.ndarray((self.__N, self.__K)) for k in range(self.__K): probs[:, k] = self.pi[k] * self.distributions[k].pdf(self.__data) self.likelihood = np.sum(np.log(np.sum(probs, axis=1)), axis=0) if __name__ == "__main__": input_folder = "a2/" file_name = "zebra" output_folder = "results/" max_steps = 100 # Load data data, image = io_data.read_data(input_folder + file_name + ".txt", False) x, y = image.shape[0], image.shape[1] N = x * y image = image.reshape(N, image.shape[2]) # Fit GMM model gmm = GMM(image, 2) gmm.fit(max_steps) # Generate mask mask = np.argmax(gmm.gamma, axis=1) mask = mask.reshape(mask.shape[0], 1) # Separate background and foreground foreground = np.multiply(mask, image) background = image - foreground
for y in range(image.shape[1]): if np.random.uniform() < threshold: ising.gibbs_sampling(x, y) if i > burn_in: average += ising.image denoised = average / iterations return denoised if __name__ == "__main__": for img in range(1, 5): print("Denoising for image " + str(img)) data, image = read_data("../a1/" + str(img) + "_noise.txt", True) print(data.shape) print(image.shape) image[image == 0] = -1 image[image == 255] = 1 burn_in = 5 iterations = 10 q = 0.7 # 0.x or None # for logit - external filed factor threshold = 0.7 d_img = denoising(image, burn_in=burn_in, iterations=iterations,
def run_EM(filename): data, image = read_data("a2/" + filename, False) rows = image.shape[0] cols = image.shape[1] # X = image.reshape((image.shape[0] * image.shape[1], image.shape[2])) channels = data[:, 2:] X = np.copy(data) X = scale(X) EPS = 1E-4 N = X.shape[0] # number of observations K = 2 # number of clusters to segment into D = 3 # dimension of each data point # run K-means to initialize mu_k print("Running initialization...") mu_k, cluster_assignment = do_clustering(X, K) # initialize covariances to std dev of dimension within each cluster sigma_k = initialize_covariance(X, cluster_assignment, mu_k) # initialize pi_k to be uniformly distributed _, counts = np.unique(cluster_assignment, return_counts=True) pi_k = counts / N old_p = 0.0 while True: # calculating responsibilities at expectation step print("Expectation step...") r = expectation_step(X, mu_k, sigma_k, pi_k) # re-estimate parameters using current responsibilities print("Maximization step...") mu_k, sigma_k, pi_k = maximization_step(X, r) # evaluate the log likelihood p = log_likelihood(X, mu_k, sigma_k, pi_k) print("Log likelihood:", p) if check_convergence(old_p, p, EPS): r = expectation_step(X, mu_k, sigma_k, pi_k) break else: old_p = p cluster_assignment = np.argmax(r, axis=1) k_bg = np.argmin(np.unique(cluster_assignment, return_counts=True)[1]) pixel_mask = np.zeros((N, D), dtype=np.float32) pixel_mask[cluster_assignment == k_bg] = [100.0, 0.0, 0.0] mask_image = np.reshape(pixel_mask, (cols, rows, D)).transpose((1, 0, 2)) output_filename = "{}_mask.jpg".format(filename.split('.')[0]) cv2.imwrite(output_filename, (cv2.cvtColor(mask_image, cv2.COLOR_Lab2BGR) * 255).astype(np.uint8)) seg1 = np.copy(channels) seg1[cluster_assignment == k_bg] = [0.0, 0.0, 0.0] seg1_img = np.reshape(seg1, (cols, rows, D)).transpose((1, 0, 2)) output_filename = "{}_seg1.jpg".format(filename.split('.')[0]) cv2.imwrite(output_filename, (cv2.cvtColor(seg1_img, cv2.COLOR_Lab2BGR) * 255).astype(np.uint8)) seg2 = np.copy(channels) seg2[cluster_assignment != k_bg] = [0.0, 0.0, 0.0] seg2_img = np.reshape(seg2, (cols, rows, D)).transpose((1, 0, 2)) output_filename = "{}_seg2.jpg".format(filename.split('.')[0]) cv2.imwrite(output_filename, (cv2.cvtColor(seg2_img, cv2.COLOR_Lab2BGR) * 255).astype(np.uint8))
def fit_EM(m1, m2, filename, data, image): mean1 = m1 #Mean 1 mean2 = m2 #Mean 2 eps = 0.5 #Threshold z = np.asarray([[13, 20, 29], [13, 23, 37], [13, 23, 29]]) cov1 = np.cov(z) #covariance 1 z = np.asarray([[9, -58, 7], [8, -7, 10], [6, -4, 6]]) cov2 = np.cov(z) #covariance 2 mix1 = 0.4 #mixing co-efficient 1 mix2 = 0.6 #mixing co-efficient 2 N = image.shape[0] * image.shape[1] #Total number of samples log_likelihoods = [] print( "Initialization of mean,covariance and mixing co-efficient statement done for " + str(filename)) print("") print("Starting EM algorithm for " + str(filename)) # Expectation Step : iteration_number = 0 while (1): iteration_number += 1 print("Iteration: " + str(iteration_number)) N1 = 0 N2 = 0 resp1_list = [] resp2_list = [] mu_sum1 = [0, 0, 0] mu_sum2 = [0, 0, 0] for y in image: for x in y: prob1 = multivariate_normal.pdf( x, mean=mean1, cov=cov1, allow_singular=True) # gaussian density 1 prob2 = multivariate_normal.pdf( x, mean=mean2, cov=cov2, allow_singular=True) # gaussian density 2 Numerator1 = mix1 * prob1 Numerator2 = mix2 * prob2 denom = Numerator1 + Numerator2 resp1 = Numerator1 / denom #responsibility for 1st cluster resp2 = Numerator2 / denom #responsibility for 2nd cluster resp1_list.append(resp1) resp2_list.append(resp2) mu_sum1 += resp1 * x mu_sum2 += resp2 * x N1 += resp1 N2 += resp2 # Maximization Step : mu_new1 = mu_sum1 / N1 #updated mean 1 mu_new2 = mu_sum2 / N2 #updated mean 2 var_1 = np.zeros((3, 3)) var_2 = np.zeros((3, 3)) i = 0 for y in image: for x in y: var_1 += resp1_list[i] * np.outer((x - mu_new1), (x - mu_new1)) var_2 += resp2_list[i] * np.outer((x - mu_new2), (x - mu_new2)) i = i + 1 var_new1 = var_1 / N1 #updated covariance1 var_new2 = var_2 / N2 #updated covariance2 mix_new1 = N1 / N #updated mixing co-efficient1 mix_new2 = N2 / N #updated mixing co-efficient2 mean1 = mu_new1 mean2 = mu_new2 cov1 = var_new1 cov2 = var_new2 mix1 = mix_new1 mix2 = mix_new2 #Calculate Log Likelihood Z = [0, 0] ll = 0 sumList = [] for y in image: for x in y: prob1 = multivariate_normal.pdf(x, mu_new1, var_new1, allow_singular=True) prob2 = multivariate_normal.pdf(x, mu_new2, var_new2, allow_singular=True) sum = (mix_new1 * prob1) + (mix_new2 * prob2) sumList.append(np.log(sum)) ll = np.sum(np.asarray(sumList)) log_likelihoods.append(ll) print("Log Likelihood: " + str(ll)) if len(log_likelihoods) < 2: continue if np.abs(ll - log_likelihoods[-2]) < eps: break #Break loop if log likelihoods dont change more than threshold over 2 iterations print("") print("End of iterations for: " + str(filename)) print("") #Write to File print("Writing to file for: " + str(filename)) back_data = data.copy() front_data = data.copy() mask_data = data.copy() for i in range(0, len(data) - 1): cell = data[i] point = [cell[2], cell[3], cell[4]] prob1 = multivariate_normal.pdf(point, mean=mean1, cov=cov1, allow_singular=True) resp1 = mix1 * prob1 prob2 = multivariate_normal.pdf(point, mean=mean2, cov=cov2, allow_singular=True) resp2 = mix2 * prob2 resp1 = resp1 / (resp1 + resp2) resp2 = resp2 / (resp1 + resp2) if (resp1 < resp2): back_data[i][2] = back_data[i][3] = back_data[i][4] = 0 mask_data[i][2] = mask_data[i][3] = mask_data[i][4] = 0 else: front_data[i][2] = front_data[i][3] = front_data[i][4] = 0 mask_data[i][2] = 100 mask_data[i][3] = mask_data[i][4] = 0 write_data(back_data, "../output/" + str(filename) + "_back.txt") read_data(str(filename) + "_back.txt", False, save=True, save_name="../output/" + str(filename) + "_background.jpg") write_data(front_data, "../output/" + str(filename) + "_fore.txt") read_data(str(filename) + "_fore.txt", False, save=True, save_name="../output/" + str(filename) + "_foreground.jpg") write_data(mask_data, "../output/" + str(filename) + "_mask.txt") read_data(str(filename) + "_mask.txt", False, save=True, save_name="../output/" + str(filename) + "_masked.jpg") print("Finished writing data. Please check " + str(filename) + "_background.jpg, " + str(filename) + "_foreground.jpg and " + str(filename) + "_masked.jpg ")