import knn_trainer_helper from sklearn.neighbors import LSHForest import time import border import matplotlib.pyplot as plt import mahotas import numpy as np from sklearn.decomposition import PCA import metrics import cPickle img = mahotas.imread('results/predict_img_PCA_k20p19s1024n100e20c20m4.tif') label = mahotas.imread('labels/test/0.tif') precisions, recalls = metrics.get_precision_recall_values(img, label, 50, True) thresh = np.linspace(.01,.99,50) f_scores=2.0*(precisions*recalls)/(precisions+recalls) plt.figure(2) plt.plot(thresh, f_scores, 's') max_thresh=thresh[np.argmax(f_scores)] threshPrediction = img>max_thresh plt.figure(3) plt.imshow(threshPrediction, cmap='gray') plt.show()
train_data_transform=pca.transform(train_input[:]) del train_input train_data=train_data_transform#np.vstack((train_data_fit,train_data_transform)) test_data=pca.transform(test_input) for val in param_values: candidates=val lshf = LSHForest(n_estimators=estimators,n_candidates=candidates,min_hash_match=minhash,n_neighbors=k,random_state=1).fit(train_data) approx_neighbors = lshf.kneighbors(test_data, return_distance=False) gray_prediction=[] for query in approx_neighbors: nbrs = [] for j in query: nbrs.append(train_label[j][0]) gray_val=np.mean(nbrs) gray_prediction.append(gray_val) precisions, recalls = metrics.get_precision_recall_values(gray_prediction, test_label.flatten(), 50, False) f_scores=2.0*(precisions*recalls)/(precisions+recalls) f_max = np.nanmax(f_scores) values.append(val) f_maxes.append(f_max) print f_max plt.plot(values,f_maxes,'o') plt.show()
gray_line = [] # looks at the labels of returned nearest neighbors, and puts that into the greater prediction image for query in approx_neighbors: nbrs = [] for j in query: nbrs.append(train_label[j]) gray_val = np.mean(nbrs) gray_line.append(gray_val) full_predicted.append(gray_line) t3 = time.time() print "predict line " + str(i) + " took " + str(t3 - predict_start) output = np.array(full_predicted) # .reshape((side,side)) # get accuracy information precisions, recalls = metrics.get_precision_recall_values(full_predicted, label, 100, True) # formats the prediction image, as well as giving it a title and description based on the params used print "total time: " + str(round(time.time() - start, 2)) + "s" fname = ( "k" + str(k) + "p" + str(pad) + "s" + str(side) + "n" + str(numpoints) + "e" + str(estimators) + "c"