File indexing completed on 2025-01-19 03:57:43
0001 # 0002 # SPDX-FileCopyrightText: 2020, Nghia Duong, <minhnghiaduong997 at gmail dot com> 0003 # 0004 # SPDX-License-Identifier: BSD-3-Clause 0005 0006 import numpy as np 0007 from sklearn.model_selection import train_test_split, cross_val_score 0008 from sklearn.neighbors import KNeighborsClassifier 0009 from sklearn.svm import SVC 0010 0011 import matplotlib.pyplot as plt 0012 import seaborn as sns 0013 import pandas as pd 0014 import umap.umap_ as umap 0015 import json 0016 0017 sns.set(style='white', context='notebook', rc={'figure.figsize':(14,10)}) 0018 0019 with open('Error_images.json') as f: 0020 jsonData = json.load(f) 0021 0022 faceEmbeddings = [] 0023 labels = [] 0024 for json in jsonData: 0025 faceEmbeddings.append(json['faceembedding']) 0026 labels.append(json['id']) 0027 0028 reducer = umap.UMAP(n_neighbors=15, metric='euclidean', min_dist=0.1, n_components=2, transform_seed=42) 0029 reducer.fit(faceEmbeddings) 0030 0031 reducedEmbedding = reducer.transform(faceEmbeddings) 0032 0033 plt.scatter(reducedEmbedding[:, 0], reducedEmbedding[:, 1], c=labels, cmap='Spectral', s=5) 0034 plt.gca().set_aspect('equal', 'datalim') 0035 plt.colorbar(boundaries=np.arange(11)-0.5).set_ticks(np.arange(10)) 0036 plt.title('UMAP projection of the face embedding from Extended Yale B dataset', fontsize=24); 0037 plt.show() 0038 0039 ''' 0040 for i in range(1, 50): 0041 X_train, X_test, y_train, y_test = train_test_split(faceEmbeddings, 0042 labels, 0043 stratify=labels, 0044 random_state=42) 0045 0046 reducer = umap.UMAP(n_neighbors=15, metric='euclidean', min_dist=0.1, n_components=i*2, transform_seed=42) 0047 reducer.fit(X_train, y=y_train) 0048 0049 reduced_X_train = reducer.transform(X_train) 0050 reduced_X_test = reducer.transform(X_test) 0051 0052 svc = SVC().fit(reduced_X_train, y_train) 0053 knn = KNeighborsClassifier().fit(reduced_X_train, y_train) 0054 0055 print(i*2) 0056 print(svc.score(reduced_X_test, y_test), knn.score(reduced_X_test, y_test)) 0057 0058 # split face embedding to train and test sets 0059 X_train, X_test, y_train, y_test = train_test_split(faceEmbeddings, 0060 labels, 0061 stratify=labels, 0062 random_state=42) 0063 0064 reducer = umap.UMAP(n_neighbors=15, metric='euclidean', min_dist=0.1, n_components=28, transform_seed=42) 0065 reducer.fit(X_train, y=y_train) 0066 0067 reduced_X_train = reducer.transform(X_train) 0068 reduced_X_test = reducer.transform(X_test) 0069 0070 svc = SVC().fit(reduced_X_train, y_train) 0071 knn = KNeighborsClassifier().fit(reduced_X_train, y_train) 0072 0073 print(svc.score(reduced_X_test, y_test), knn.score(reduced_X_test, y_test)) 0074 0075 reducedEmbedding = reducer.transform(faceEmbeddings) 0076 0077 plt.scatter(reducedEmbedding[:, 0], reducedEmbedding[:, 1], c=labels, cmap='Spectral', s=5) 0078 plt.gca().set_aspect('equal', 'datalim') 0079 plt.colorbar(boundaries=np.arange(11)-0.5).set_ticks(np.arange(10)) 0080 plt.title('UMAP projection of the face embedding', fontsize=24); 0081 plt.show() 0082 '''