Exemplo n.º 1
0
k=25
pad=4
numpoints=50
input_img = np.float16(mahotas.imread('input/test/0.tif'))
input_bordered = border.createBorder(pad,input_img)
#input = border.outputMatrix(pad,input_img)
train_data=knn_trainer_helper.sortClasses(pad,numpoints,knn_trainer_helper.readImages(209,'input/train/','labels/train/'),seed=42)
t1 = time.time()
print 'init took '+str(t1-start)
lshf = LSHForest(n_estimators=20, n_candidates=200, n_neighbors=k,random_state=1).fit(train_data[0])
t2 = time.time()
print 'fit took '+str(t2-t1)
full_predicted=[]
for i in range(1024):
    predict_start=time.time()
    input = border.matrixRow(pad,input_bordered,i)
    approx_neighbors = lshf.kneighbors(input, return_distance=False)

    y_hat=[]
    for query in approx_neighbors:
        temp = []
        for j in range(len(query)):
            temp.append(train_data[1][query[j]])
        y_hat.append(stats.mode(temp)[0][0])
    full_predicted.append(y_hat)
    t3=time.time()
    print 'predict line ' + str(i) + ' took '+str(t3-predict_start)
    

output = np.array(full_predicted).reshape((side,side))\
precisions, recalls = metrics.get_precision_recall_values(full_predicted, label, 100, True)
Exemplo n.º 2
0
print "init took " + str(t1 - start)

# train the LSHForest classifier
lshf = LSHForest(
    n_estimators=estimators, n_candidates=candidates, min_hash_match=minhash, n_neighbors=k, random_state=1
).fit(train_data)
t2 = time.time()
print "fit took " + str(t2 - t1)

full_predicted = []
# generate the prediction image, one line at a time
for i in range(side):
    predict_start = time.time()
    if usePCA:
        # reduce dimensionality of prediction data
        input = pca.transform(border.matrixRow(pad, input_bordered, i, sideLength=side))
    else:
        input = border.matrixRow(pad, input_bordered, i, sideLength=side)
    # get the nearest neighbors for that line of the image
    approx_neighbors = lshf.kneighbors(input, return_distance=False)

    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()