From 7e95ffdec0b743ea2466f85323440a448bd7e2fe Mon Sep 17 00:00:00 2001 From: nunzip Date: Tue, 11 Dec 2018 03:09:24 +0000 Subject: Add PCA and fix mahalanobis --- evaluate.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/evaluate.py b/evaluate.py index 3475ef8..208f517 100755 --- a/evaluate.py +++ b/evaluate.py @@ -54,7 +54,7 @@ parser.add_argument("-C", "--comparison", help="Set to 2 to obtain a comparison parser.add_argument("--data", help="You can either put the data in a folder called 'data', or specify the location with --data 'path' -ARGUMENT REQUIRED, default='data'-", default='data') parser.add_argument("-K", "--kmean", help="Perform Kmean clustering of size specified through -K '$size' -ARGUMENT REQUIRED, default=0-", type=int, default=0) parser.add_argument("-P", "--mAP", help="Display Mean Average Precision for ranklist of size -n '$size'", action='store_true') -parser.add_argument("-2", "--PCA", help="Use PCA", action='store_true') +parser.add_argument("-2", "--PCA", help="Use PCA with -2 '$n_components' -ARGUMENT REQUIRED, default=0-", type=int, default=0) args = parser.parse_args() @@ -73,7 +73,7 @@ def draw_results(test_label, pred_label): plt.show() return acc_sc -def test_model(gallery_data, probe_data, gallery_label, probe_label, gallery_cam, probe_cam, showfiles_train, showfiles_test, args): +def test_model(gallery_data, probe_data, gallery_label, probe_label, gallery_cam, probe_cam, showfiles_train, showfiles_test, train_model, args): debug("probe shape: %s", probe_data.shape) debug("gallery shape: %s", gallery_data.shape) @@ -85,7 +85,7 @@ def test_model(gallery_data, probe_data, gallery_label, probe_label, gallery_cam else: if args.mahalanobis: # metric = 'jaccard' is also valid - cov_inv = np.linalg.inv(np.cov(gallery_data.T)) + cov_inv = np.linalg.inv(np.cov(train_model.T)) distances = np.zeros((probe_data.shape[0], gallery_data.shape[0])) for i in range(int(probe_data.shape[0]/10)): print("Comupting from", i*10, "to", (i+1)*10-1) @@ -115,7 +115,7 @@ def test_model(gallery_data, probe_data, gallery_label, probe_label, gallery_cam probe_label[probe_idx] == gallery_label[row[n]]): n += 1 nneighbors[probe_idx][q] = gallery_label[row[n]] - nnshowrank[probe_idx][q] = showfiles_train[row[n]] # + nnshowrank[probe_idx][q] = showfiles_train[row[n]] q += 1 n += 1 @@ -183,7 +183,6 @@ def main(): labs = labels[train_idx].reshape((labels[train_idx].shape[0],1)) tt = np.hstack((train_idx, cam)) train, test, train_label, test_label = train_test_split(tt, labs, test_size=0.3, random_state=0) - #to make it smaller we do a double split del labs del cam train_data = feature_vectors[train[:,0]] @@ -208,10 +207,14 @@ def main(): test_label = labels[query_idx] train_cam = camId[gallery_idx] test_cam = camId[query_idx] + + train_idx = train_idx.reshape(train_idx.shape[0]) + train_model = feature_vectors[train_idx] if(args.PCA): - pca=PCA(n_components=300) #Reconstruction error of 2.3% @300 - train_data=pca.fit_transform(train_data) + pca=PCA(n_components=args.PCA) #Data variance @100 is 94% + train_model=pca.fit_transform(train_model) + train_data=pca.transform(train_data) test_data=pca.transform(test_data) accuracy = np.zeros((2, args.multrank)) @@ -223,7 +226,7 @@ def main(): test_data = np.divide(test_data, LA.norm(test_data,axis=0)) if(args.kmean_alt): debug("Using Kmeans") - train_data, train_label, train_cam = create_kmean_clusters(feature_vectors, labels,gallery_idx,camId) + train_data, train_label, train_cam = create_kmean_clusters(feature_vectors, labels, gallery_idx, camId) if args.kmean: kmeans = KMeans(n_clusters=args.kmean, random_state=0).fit(train_data) @@ -236,12 +239,12 @@ def main(): td = test_data[i].reshape(1,test_data.shape[1]) tc = np.array([test_cam[i]]) tl = np.array([test_label[i]]) - target_pred[i] = (test_model(train_data[np.where(kmeans.labels_==neighbors[i])], td, train_label[np.where(kmeans.labels_==neighbors[i])], tl, train_cam[np.where(kmeans.labels_==neighbors[i])], tc, showfiles_train[np.where(kmeans.labels_==neighbors[i])], showfiles_test[i], args)) + target_pred[i] = (test_model(train_data[np.where(kmeans.labels_==neighbors[i])], td, train_label[np.where(kmeans.labels_==neighbors[i])], tl, train_cam[np.where(kmeans.labels_==neighbors[i])], tc, showfiles_train[np.where(kmeans.labels_==neighbors[i])], showfiles_test[i], train_model, args)) accuracy[0] = draw_results(test_label, target_pred) else: for q in range(args.comparison): - target_pred = test_model(train_data, test_data, train_label, test_label, train_cam, test_cam, showfiles_train, showfiles_test, args) + target_pred = test_model(train_data, test_data, train_label, test_label, train_cam, test_cam, showfiles_train, showfiles_test, train_model, args) for i in range(args.multrank): accuracy[q][i] = draw_results(test_label, target_pred[i]) args.rerank = True -- cgit v1.2.3-54-g00ecf