def main(): datafile = 'data/faces.mat' mat = scipy.io.loadmat(datafile) samples = mat['X'] display_data(samples) # Feature normalize samples_norm = feature_normalize(samples) # Run SVD U, S, Vh = get_usv(samples_norm) # Visualize the top 36 eigenvectors found print('Top 36 principal component is ', U[:, :36]) # Project each image down to 36 dimensions z = project_data(samples_norm, U, 36) # Attempt to recover the original data recovered_samples = recover_data(z, U, 36) # Plot the dimension-reduced data display_data(recovered_samples) # Project each image down to 100 dimensions z_100 = project_data(samples_norm, U, 100) # Attempt to recover the original data recovered_samples_100 = recover_data(z_100, U, 100) # Plot the dimension-reduced data display_data(recovered_samples_100) plt.show()
def compress_images(DATA, k): output = '.\output' if not os.path.exists(output): os.makedirs(output) #data is an array of tuples where item 1: is the file name item 2: is the image array images = [] name = [] for image_tuple in DATA: name.append(image_tuple[0]) images.append(image_tuple[1]) images = np.transpose(np.asarray(images)) Z = pca.compute_Z(images) COV = pca.compute_covariance_matrix(Z) L, PCS = pca.find_pcs(COV) Z_star = pca.project_data(Z, PCS, L, k, 0) PCS = np.delete(PCS, range(k, PCS.shape[1]), axis=1) u_t = np.transpose(PCS) x_compressed = np.dot(Z_star, u_t) minimum = np.min(x_compressed) for value in x_compressed: value += abs(minimum) for column in range(0, images.shape[1]): image = np.reshape(x_compressed[:,column], (60, 48)) plt.imsave( output + '\\' + name[column] + '_compressed.jpg', image, cmap='gray')
def compress_images(DATA, k): # Do PCA Z = pca.compute_Z(DATA, centering=True, scaling=True) COV = pca.compute_covariance_matrix(np.transpose(Z)) L, PCS = pca.find_pcs(COV) Z_star = pca.project_data(np.transpose(Z), PCS, L, k, 0) # Find X compressed U = np.transpose(PCS[:, 0:k]) X_compressed = np.matmul(Z_star, U) # Images have values from 0 to 255, so rescale for row, image in enumerate(X_compressed): min = np.amin(image) max = np.amax(image) for col, pixel in enumerate(image): X_compressed[row][col] = ( X_compressed[row][col] - min) * (255/(max - min)) X_compressed = X_compressed.astype(int) # Output everything into output dir if not os.path.exists('Output'): os.mkdir('Output') imageNumber = 0 for image in X_compressed: filename = 'Output/k' + str(k) + 'image' + str(imageNumber) + '.png' # Love hard coded numbers <3 plt.imsave(filename, image.reshape(60, 48), cmap='gray') imageNumber += 1 return
def compress_images(DATA, k): Z = pca.compute_Z(DATA, True, True) COV = pca.compute_covariance_matrix(Z) L, U = pca.find_pcs(COV) Z_star = pca.project_data(Z, U, L, k, 0) X_compressed = np.dot(Z_star, U[:, :k].T) save_data(X_compressed) return X_compressed
def compress_images(DATA, k): #print("compress") X = DATA # Centering and Scaling Test Z = pca.compute_Z(X, True, True) #print("[RESULT] After scaling and centering Value found for Z = ", Z) COV = pca.compute_covariance_matrix(Z) #print("[RESULT] COV = ", COV) L, PCS = pca.find_pcs(COV) #print("[RESULT] L = ", L) #print("[RESULT] PCS = ", PCS) Z_star = pca.project_data(Z, PCS, L, 1, 0) return Z_star
def main(): datafile = 'data/faces.mat' mat = scipy.io.loadmat(datafile) samples = mat['X'] display_data(samples) # Feature normalize samples_norm = feature_normalize(samples) # Run SVD U, S, v = get_usv(samples_norm) # Visualize the top 36 eigenvectors found print('Top principal component is ', U[:, 36]) # Project each image down to 36 dimensions z = project_data(samples_norm, U, 36) # Attempt to recover the original data recovered_samples = recover_data(z, U, 36) # Plot the dimension-reduced data display_data(recovered_samples) # Project each image down to 100 dimensions z = project_data(samples_norm, U, 100) # Attempt to recover the original data recovered_samples = recover_data(z, U, 100) # Plot the dimension-reduced data fig, axs = plt.subplots(1, 2, figsize=(25, 10)) display_data(samples, figsize=(25, 10), ax=axs[0]) axs[0].set_title("Original Face Images") display_data(recovered_samples, figsize=(25, 10), ax=axs[1]) axs[1].set_title("100-D Recovered Face Images") plt.show()
def compress_images(DATA, k): # transpose command Data_T = DATA.transpose() Z = pca.compute_Z(Data_T, True, False) COV = pca.compute_covariance_matrix(Z) L, PCS = pca.find_pcs(COV) pr_Data = pca.project_data(Z, PCS, L, k, 0) # get the principle component - complex matrix U = np.zeros((len(PCS), k), dtype=complex) for i in range(len(PCS)): for j in range(k): U[i][j] = PCS[i][j] # transpose of the principles components -( eigenvector) U_T = U.transpose() # get the data back the original size X = np.matmul(pr_Data, U_T) X -= X.min() # scaling to range 0-255 X_inter = np.divide(X, 255 / X.max()) # convert to int X_inter2 = X_inter.astype(int) # check folder exists? foldername = './Data/output' # # check the folder output exist? if (not os.path.isdir(foldername)): os.mkdir(foldername) backImage = np.zeros([60, 48], dtype=int) row = 0 column = 0 for j in range(len(X_inter2)): # plt.imsave(foldername,X_compressed, format="'png'") for k in range(len(X_inter2[0])): if (column == 48): row += 1 column = 0 backImage[row][column] = X_inter2[j][k] column += 1 row = 0 column = 0 name = str(j) plt.imsave('./Data/output/' + name, backImage, format="png") return 0
def compress_images(DATA, k): Z = pca.compute_Z(DATA) COV = pca.compute_covariance_matrix(Z) L, PCS = pca.find_pcs(COV) Z_star = pca.project_data(Z, PCS, L, k, 0) PCS = PCS[:, :k] X_compressed = Z_star @ PCS.transpose() X_compressed = X_compressed.transpose() #print(X_compressed) if not os.path.exists('Output'): os.makedirs('Output') for i in range(0, len(X_compressed)): matplotlib.pyplot.imsave("Output/output" + str(i) + ".png", X_compressed[i].reshape(60, 48), cmap='gray')
def compress_images(DATA,k): var = 0 count = 0 Z = pca.compute_Z(DATA, centering=True, scaling=False) COV = pca.compute_covariance_matrix(Z) L, PCS = pca.find_pcs(COV) Z_star = pca.project_data(Z, PCS, L, k, var) PCS = PCS[:,: k] X_compressed = Z_star.dot(PCS.T) if not os.path.exists("./Output"): os.mkdir("./Output") for i in X_compressed.T: count += 1 file_name = "./Output/"+str(count)+"compressed_k"+str(k) n = np.reshape(i,(60,48)) plt.imsave(file_name,n,cmap='Greys_r')
def pac_process(self, variance_threshold=90): #进行pca处理 sigma = pca.covariance_matrix(self.data_s) self.U, self.S, self.V = np.linalg.svd(sigma) self.keep_variance = np.zeros(self.S.shape, dtype=np.float64) for i in range(len(self.keep_variance)): self.keep_variance[i] = np.sum(self.S[0:i])/np.sum(self.S) self.variance_threshold = variance_threshold for i in range(len(self.keep_variance)-1): if (self.keep_variance[i] - variance_threshold/100) * (self.keep_variance[i+1] - variance_threshold/100) <= 0: self.pca_num = i #break #完全单调,可以 self.Z = pca.project_data(self.data_s, self.U, self.pca_num) logging.info('pca process successfully, the data number is %d !!!' %(self.pca_num))
def main(): datafile = 'data/faces.mat' mat = scipy.io.loadmat(datafile) samples = mat['X'] display_data(samples) # Feature normalize samples_norm, mu, std = feature_normalize(samples) # Run SVD U, S, V = get_usv(samples_norm) # Visualize the top 36 eigenvectors found display_data(U[:, :36].T, num_rows=6, num_columns=6) # Project each image down to 36 dimensions z = project_data(samples_norm, U, K=36) # Attempt to recover the original data recovered_samples = recover_data(z, U, K=36) # Plot the dimension-reduced data display_data(recovered_samples) plt.show()
def main(): datafile = 'data/faces.mat' mat = scipy.io.loadmat(datafile) samples = mat['X'] display_data(samples) # Feature normalize samples_norm = feature_normalize(samples) # Run SVD U, S, V = get_usv(samples_norm) # Visualize the top 36 eigenvectors found U_36 = U[:, 0:36].T display_data(U_36, 6, 6) # Project each image down to 36 dimensions Z = project_data(samples_norm, U, 100) # Attempt to recover the original data recovered_samples = recover_data(Z, U, 100) # Plot the dimension-reduced data display_data(recovered_samples) plt.show()
def compress_images(DATA, k): exists = os.path.exists("Output") if not exists: os.mkdir('Output') Z = pca.compute_Z(DATA) COV = pca.compute_covariance_matrix(Z) L, PCS = pca.find_pcs(COV) Zstar = pca.project_data(Z, PCS, L, k, 0) PCS = PCS[:, :k] PCS = PCS.T compress = np.dot(Zstar, PCS) compress = compress.T for c, j in enumerate(compress): j = (j * 255) / (np.max(j) - np.min(j)) py.imsave('Output/out%d.png' % c, j.reshape(60, 48), vmin=0, vmax=255, cmap='gray', format='png')
def pac_process(self, variance_threshold=[80, 90, 95]): #数据是否需要预处理,将相同列的数据全部删除?放在数据预处理里面进行? #进行pca处理 sigma = pca.covariance_matrix(self.data_all) self.U, self.S, self.V = np.linalg.svd(sigma) self.keep_variance = np.zeros(self.S.shape, dtype=np.float64) for i in range(len(self.keep_variance)): self.keep_variance[i] = np.sum(self.S[0:i]) / np.sum(self.S) self.variance_threshold = variance_threshold self.pca_num = {} self.Z = {} for variance_value in self.variance_threshold: for i in range(len(self.keep_variance) - 1): if (self.keep_variance[i] - variance_value / 100) * ( self.keep_variance[i + 1] - variance_value / 100) <= 0: self.pca_num[variance_value] = i self.Z[variance_value] = pca.project_data( self.data_all, self.U, self.pca_num[variance_value]) logging.info('pca process successfully !!!')
def compress_images(DATA, k): Z = pca.compute_Z(DATA) cov = pca.compute_covariance_matrix(Z) PCS = pca.find_pcs(cov) L, PCS_1 = PCS projected_data = pca.project_data(Z, PCS_1, L, k, 0) PCS_1 = PCS_1.T PCS_1 = PCS_1[:k] print(PCS_1.shape) compressed = (projected_data).dot(PCS_1) os.makedirs(os.getcwd() + "/Output", exist_ok=True) output_dir = os.getcwd() + "/Output" number_counter = 0 for image in compressed.T: image = 255 * (image - np.min(image)) / np.ptp(image) image = np.reshape(image, (60, 48)) plt.imsave(output_dir + '/' + str(number_counter) + ".png", image, cmap='gray') number_counter += 1
def compress_images(DATA, k): global fileNames Z = pca.compute_Z(DATA) Z = Z.astype(float) COV = pca.compute_covariance_matrix(Z) L, PCS = pca.find_pcs(COV) Z_star = pca.project_data(Z, PCS, L, k, 0) # Xcompressed = Z * U^T Z_star = Z_star.dot(PCS[:, 0:k].transpose()) Z_star = Z_star.astype(float) Z_star = scale(Z_star) if not os.path.isdir(os.path.join(os.getcwd(), r'Output')): os.mkdir(os.path.join(os.getcwd(), r'Output')) for j in range(0, Z_star.shape[1]): imgData = [] for i in range(0, Z_star.shape[0]): imgData.append(Z_star[i][j]) imgData = np.asarray(imgData) imgData = imgData.reshape(60, 48) filename = os.path.join(os.getcwd(), r'Output/') + str( fileNames[j].strip('.pgm')) + ".png" fileDir = os.path.expanduser(filename) plt.imsave(fileDir, imgData, cmap='gray')
def compress_images(DATA, k): # if it is not exist create output directory if not os.path.exists("Output"): os.makedirs("Output") faces = [] filenames = [] for face in DATA: filenames.append(face[1]) faces.append(face[2]) # we convert each image to a feature (column) faces = np.asarray(faces) faces = np.transpose(faces) # pca processes Z = pca.compute_Z(faces) COV = pca.compute_covariance_matrix(Z) L, PCS = pca.find_pcs(COV) Z_star = pca.project_data(Z, PCS, L, k, 0) # we use components for compressing files, we need to reduce component count component_matrix = np.delete(PCS, range(k, PCS.shape[1]), axis=1) Ut = component_matrix.T X_compressed = np.dot(Z_star, Ut) # write all images for ftr_ind in range(faces.shape[1]): # we need to reshape the feature as image. img = np.reshape(X_compressed[:, ftr_ind], (60, 48)) plt.imsave("Output" + "/" + filenames[ftr_ind] + "_img.jpg", img, cmap="gray") print()
def compress_images(DATA, k): Z = pca.compute_Z(DATA, centering=True, scaling=False) COV = pca.compute_covariance_matrix(Z) L, PCS = pca.find_pcs(COV) Z_Star = pca.project_data(Z, PCS, L, k, 0) PCS = PCS[:, :k] XCompressed = Z_Star.dot(np.transpose(PCS)) path = 'Output' if os.path.exists(path) == False: os.mkdir(path) for col in range(len(XCompressed[0])): XCompressed[:, col] = ( (XCompressed[:, col] - XCompressed[:, col].min()) * 1 / (XCompressed[:, col].max() - XCompressed[:, col].min()) * 255).astype( float) #should this be rescaling cols separately? If so , HOW tempArray = np.zeros((len(XCompressed), 1)) colData = 0 rowData = 0 for items in range(len(XCompressed[1])): for row in range(len(tempArray)): tempArray[row][0] = XCompressed[row][items] #possibly row?? here = str(path) + "/" + str(items) + ".jpg" imArray = tempArray.reshape((r, c)) plt.imsave(here, imArray, format="jpg", cmap='gray')
img = compress.load_data('Data/Train/') X_compressed = compress.compress_images(img, 2000) compress.save_data(X_compressed) exit() #X = np.array([[-1,-1],[-1,1],[1,-1],[1,1]]) # X = np.array([[1,1],[1,0],[2,2],[2,1],[2,4],[3,4],[3,3],[3,2],[4,4],[4,5],[5,5],[5,7],[5,4]]) X = np.array([[90, 60, 90], [90, 90, 30], [60, 60, 60], [60, 60, 90], [30, 30, 30]]) #X = np.array([[2.5,2.4],[.5,.7],[2.2,2.9],[1.9,2.2],[3.1,3],[2.3,2.7],[2,1.6],[1,1.1],[1.5,1.6],[1.1,.9]]) #X = np.array([[0,8],[8,9],[12,11],[20,12]]) Z = pca.compute_Z(X, True, False) print("Z:") print(Z) print() COV = pca.compute_covariance_matrix(Z) COV = np.array([[5, 1], [4, 5]]) print("COV:") print(COV) print() L, U = pca.find_pcs(COV) print("L:") print(L) print("U:") print(U) print() Z_star = pca.project_data(Z, U, L, 1, 0) print("Z_star:") print(Z_star)
import pca import numpy as np X = np.array([[-1, -1], [-1, 1], [1, -1], [1, 1]]) Z = pca.compute_Z(X) COV = pca.compute_covariance_matrix(Z) L, PCS = pca.find_pcs(COV) Z_star = pca.project_data(Z, PCS, L, 1, 0) print(Z_star)
print(PCS_1.shape) compressed = (projected_data).dot(PCS_1) os.makedirs(os.getcwd() + "/Output", exist_ok=True) output_dir = os.getcwd() + "/Output" number_counter = 0 for image in compressed.T: image = 255 * (image - np.min(image)) / np.ptp(image) image = np.reshape(image, (60, 48)) plt.imsave(output_dir + '/' + str(number_counter) + ".png", image, cmap='gray') number_counter += 1 cwd = os.getcwd() + "/Data/Train/" data = load_data(cwd) compress_images(data, 2) Z_2 = pca.compute_Z() cov_2 = pca.compute_covariance_matrix(Z_2) print("cov", cov_2) PCS_2 = pca.find_pcs(cov_2) L2, PCS_2 = PCS_2 print(pca.project_data(Z_2, PCS_2, L2, 0, 0.3))