def set_cover(self, uploaded_image): handler = ImageHandler() images = handler.handle_cover_image(uploaded_image) s3 = AmazonS3() s3.upload_file(images['default'], '/u/%d/%s' % (self.musician.id, self.COVER_IMG_NAME)) if not self.media_list.filter(media_type__name = "cover_photo"): self.media_list.add(Media.objects.create(media = "cover", media_type = self.__type("cover_photo")))
def set_avatar(self, uploaded_image): handler = ImageHandler() images = handler.handle_profile_images(uploaded_image) s3 = AmazonS3() s3.upload_file(images['default'], '/u/%d/%s' % (self.musician.id, self.AVATAR_IMG_NAME)) s3.upload_file(images['small'], '/u/%d/%s' % (self.musician.id, self.AVATAR_SMALL_IMG_NAME)) if not self.media_list.filter(media_type__name = "avatar"): self.media_list.add(Media.objects.create(media = "avatar", media_type = self.__type("avatar")))
def train(): THRESHOLD = 0.95 # Proportion of representation when choosing the best eigen vectors args = arguments.get_arguments() # Training set characteristics TRAINING_SET_SIZE = args.training_set_size #Amount of training images for each individual # Testing set characteristics TESTING_SET_SIZE = args.testing_set_size #Amount of testing images for each individual # Load images if (args.verbose): print('Loading images') training_images, training_classes, testing_images, testing_classes, profile, profiles_map = imageHandler.load_images(training_size=TRAINING_SET_SIZE, testing_size=TESTING_SET_SIZE, image_dir=args.images) # Create matrix out of images matrix = (np.matrix(training_images)).T / 255. # Calculate mean different faces mean = matrix.mean(axis=1) imageHandler.save_image(mean, "mean.pgm") # Center matrix centered_matrix = (matrix - mean) # Calculate the covariance matrix # Calculate centered matrix * transposed centered matrix to get a similar matrix to the one of the covariance if(args.verbose): print('Calculating covariance matrix') covariance_matrix = (centered_matrix.T).dot(centered_matrix) # Calculate eigen values and eigen vectors if (args.verbose): print('Calculating eigen values and eigen vectors') if args.type == "pca": eig_values, eig_vectors = calculate_eigen(matrix=covariance_matrix, qr_method=args.qr_method, eig_method=args.eig_method) elif args.type == "kpca": eig_values, eig_vectors = calculate_kernel_eigen(matrix=covariance_matrix, qr_method=args.qr_method, eig_method=args.eig_method) else: raise ValueError("The type is not supported") # Get best eigenvalues if(args.verbose): print('Getting representative eigen values') best_eig_vectors = get_best_eig_vectors(eig_values, eig_vectors, THRESHOLD) # Calculate images # http://blog.manfredas.com/eigenfaces-tutorial/ eigen_faces = centered_matrix.dot(best_eig_vectors) # Normalize eigen faces optimization row_sums = np.linalg.norm(eigen_faces, axis=0) eigen_faces = np.divide(eigen_faces,row(row_sums)) # Project values on eigen vectors if(args.verbose): print('Projecting values on eigen vectors') projected_values = eigen_faces.T.dot(centered_matrix) # Write image files imageHandler.save_images(images=eigen_faces.T) # Generate matrices from loaded images test_matrix = np.matrix(testing_images).T/255. test_matrix = test_matrix - mean testing_set = eigen_faces.T.dot(test_matrix) #Test images clf = svm.LinearSVC() # Training classifier with provided data set+group clf.fit(projected_values.T, training_classes) classifications = clf.score(testing_set.T, testing_classes) return mean, eigen_faces, clf, classifications, profile, profiles_map
def train_kpca(): THRESHOLD = 0.95 # Proportion of representation when choosing the best eigen vectors args = arguments.get_arguments() # Testing set characteristics TESTING_SET_SIZE = args.testing_set_size # Training set characteristics TRAINING_SET_SIZE = args.training_set_size # Amount of training images for each individual # Load images if (args.verbose): print('Loading images') training_images, training_classes, testing_images, testing_classes, profiles, profile_map = imageHandler.load_images( training_size=TRAINING_SET_SIZE, testing_size=TESTING_SET_SIZE, image_dir=args.images) TRAINING_IMAGES = len(training_images) # Create matrix out of images matrix = ((np.matrix(training_images)) - 127.5) / 127.5 # Calculate K matrix and get eigen values and eigen vectors if (args.verbose): print('Calculating eigen values and eigen vectors') eig_values, eig_vectors, K = calculate_kernel_eigen( matrix=matrix, qr_method=args.qr_method, eig_method=args.eig_method, TRAINING_IMAGES=TRAINING_IMAGES) # Get best eigenvalues if (args.verbose): print('Getting representative eigen values') eigen_faces = get_best_eig_vectors(eig_values, eig_vectors, THRESHOLD) if (args.verbose): print('number of eigen faces used: ', eigen_faces.shape[1]) training_projection = np.dot(K.T, eigen_faces) clf = svm.LinearSVC() clf.fit(training_projection, training_classes) testing_projection = get_testing_data(matrix, eigen_faces, testing_images, K) classifications = clf.score(testing_projection, testing_classes) return clf, eigen_faces, matrix, K, classifications, profiles, profile_map