def test_python_pca(self): '''Make sure that python implementation matches sklearn's.''' X = lfw_subset()[:10, :, :].reshape((10, -1)).transpose((1, 0)) n_components = 2 Y_py = python_pca(X, n_components) Y_ski = PCA(n_components=n_components, whiten=False).fit_transform(X) # Same within scale factor of -1 self.assertTrue(np.allclose(np.abs(Y_py), np.abs(Y_ski)))
def test_haar_features(): print("\nloading lfw subset\n") images = lfw_subset() feature_types = ['type-2-x', 'type-2-y'] X, feature_coords, feature_types = compute_haar_features( feature_types, images) save_to_disk([X, feature_coords, feature_types], "sav_files/lfw_subset_t2_features.sav")
def test_python_pca(self): dim = 4 # X = np.random.normal(1,.1,(dim,dim)) X = lfw_subset()[:10,:,:].reshape((10,-1)).transpose((1,0)) n_components = 2 Y_py = python_pca(X,n_components) Y_ski = PCA(n_components=n_components,whiten=False).fit_transform(X) # print(Y_py) # print(Y_ski) # Same within scale factor of -1 self.assertTrue(np.allclose(np.abs(Y_py),np.abs(Y_ski)))
###################################################################### # # Stereo images # ============= fig, axes = plt.subplots(1, 2, figsize=(8, 4)) ax = axes.ravel() images = data.stereo_motorcycle() ax[0].imshow(images[0]) ax[1].imshow(images[1]) fig.tight_layout() plt.show() ###################################################################### # # Faces and non-faces dataset # =========================== # # A sample of 20 over 200 images is displayed. fig, axes = plt.subplots(4, 5, figsize=(20, 20)) ax = axes.ravel() images = data.lfw_subset() for i in range(20): ax[i].imshow(images[90 + i], cmap=plt.cm.gray) ax[i].axis('off') fig.tight_layout() plt.show()
0, 0, ii.shape[0], ii.shape[1], feature_type=feature_type, feature_coord=feature_coord) ############################################################################### # We will use a subset of the CBCL which is composed of 100 face images and 100 # non-face images. Each image has been resized to a ROI of 19 by 19 pixels. We # will keep 75 images from each group to train a classifier and check which # extracted features are the most salient, and use the remaining 25 from each # class to check the performance of the classifier. images = lfw_subset() # For speed, only extract the two first types of features feature_types = ['type-2-x', 'type-2-y'] # Build a computation graph using dask. This allows using multiple CPUs for # the computation step X = delayed(extract_feature_image(img, feature_types) for img in images) # Compute the result using the "processes" dask backend t_start = time() X = np.array(X.compute(scheduler='processes')) time_full_feature_comp = time() - t_start y = np.array([1] * 100 + [0] * 100) X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=150, random_state=0,
def test_lfw_subset(): """ Test that "lfw_subset" can be loaded.""" data.lfw_subset()
def extract_feature_image(img, feature_type, feature_coord=None): """Extract the haar feature for the current image""" ii = integral_image(img) return haar_like_feature(ii, 0, 0, ii.shape[0], ii.shape[1], feature_type=feature_type, feature_coord=feature_coord) ############################################################################### # We will use a subset of the CBCL which is composed of 100 face images and 100 # non-face images. Each image has been resized to a ROI of 19 by 19 pixels. We # will keep 75 images from each group to train a classifier and check which # extracted features are the most salient, and use the remaining 25 from each # class to check the performance of the classifier. images = lfw_subset() # For speed, only extract the two first types of features feature_types = ['type-2-x', 'type-2-y'] # Build a computation graph using dask. This allows using multiple CPUs for # the computation step X = delayed(extract_feature_image(img, feature_types) for img in images) # Compute the result using the "processes" dask backend t_start = time() X = np.array(X.compute(scheduler='processes')) time_full_feature_comp = time() - t_start y = np.array([1] * 100 + [0] * 100) X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=150, random_state=0, stratify=y)
##### from example to see how to do it #type(result) #images = lfw_subset() #type(images) #print(np.shape(images)) #images #images = lfw_subset() #print(type(images)) #print(np.shape(images)) print(np.shape(fullset)) print(lfw_subset()[1].max()) #print( lfw_subset()[1]) #print(fullset[1]/255.9) images = fullset/255.9 # rescaling to 1 so we avoid getting Nan in "X" #images = lfw_subset() #### new to avoid error perhaps, doesn't work #images= fullset/(255.9*100000) images[1] plt.imshow(images[200]) images[5]