aboutsummaryrefslogtreecommitdiff
path: root/evaluate.py
blob: 9cb5f78ddacabf18af3ed7228b5dac605a5e3d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/python
# EE4 Selected Topics From Computer Vision Coursework
# Vasil Zlatanov, Nunzio Pucci

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import scikitplot as skplt
import argparse
import logging
from logging import debug
from sklearn.cluster import KMeans
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier 
from sklearn.ensemble import RandomTreesEmbedding
from sklearn.metrics import accuracy_score
import time 

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--data", help="Data path", action='store_true', default='data.npz')
parser.add_argument("-c", "--conf_mat", help="Show visual confusion matrix", action='store_true')
parser.add_argument("-k", "--kmean", help="Perform kmean clustering with KMEAN cluster centers", type=int, default=0)
parser.add_argument("-l", "--leaves", help="Maximum leaf nodes for RF classifier", type=int, default=256)
parser.add_argument("-e", "--estimators", help="number of estimators to be used", type=int, default=100)
parser.add_argument("-D", "--treedepth", help="depth of trees", type=int, default=5)
parser.add_argument("-v", "--verbose", help="Use verbose output", action='store_true')
parser.add_argument("-t", "--timer", help="Display execution time", action='store_true')
parser.add_argument("-T", "--testmode", help="Testmode", action='store_true')
parser.add_argument("-E", "--embest", help="RandomTreesEmbedding estimators", type=int, default=256)
parser.add_argument("-r", "--randomness", help="Randomness parameter", type=int, default=0)
parser.add_argument("-s", "--seed", help="Seed to use for random_state when creating trees", type=int, default=0)

args = parser.parse_args()
if args.verbose:
    logging.basicConfig(level=logging.DEBUG)

def make_histogram(data, model, args):
    if args.kmean:
        hist_size = args.kmean
    else:
        hist_size = args.embest*args.leaves

    histogram = np.zeros((data.shape[0], data.shape[1],hist_size)) 
    for i in range(data.shape[0]):
        for j in range(data.shape[1]):
            if (args.kmean):
                histogram[i][j] = np.bincount(model.predict(data[i][j].T), minlength=args.kmean)
            else:
                leaves = model.apply(data[i][j].T)
                leaves = np.apply_along_axis(np.bincount, axis=0, arr=leaves, minlength=args.leaves)
                histogram[i][j] = leaves.reshape(hist_size)

    print(histogram[0][0].shape)
    plt.bar(np.arange(100), histogram[0][0].flatten())
    plt.show()

    return histogram

def run_model (data, train, test, train_part, args):
    if args.timer:
        start = time.time()
    
    if (args.kmean):
        logging.debug("Computing KMeans with", train_part.shape[0], "keywords")
        kmeans = KMeans(n_clusters=args.kmean, n_init=1, random_state=args.seed).fit(train_part)
        hist_train = make_histogram(train, kmeans, args)
        hist_test = make_histogram(test, kmeans, args)
    else:
        trees = RandomTreesEmbedding(max_leaf_nodes=int(args.leaves/2), n_estimators=args.embest, random_state=args.seed).fit(train_part)
        hist_train = make_histogram(train, trees, args)
        hist_test = make_histogram(test, trees, args)
 
    logging.debug("Generating histograms")
    
    logging.debug("Keywords shape", hist_train.shape, "\n")
    logging.debug("Planting trees...")
    if args.randomness:
        clf = RandomForestClassifier(max_features=args.randomness, n_estimators=args.estimators, max_depth=args.treedepth, random_state=0)
    else:
        clf = RandomForestClassifier(n_estimators=args.estimators, max_depth=args.treedepth, random_state=args.seed)
    clf.fit(
            hist_train.reshape((hist_train.shape[0]*hist_train.shape[1], hist_train.shape[2])),
            np.repeat(np.arange(hist_train.shape[0]), hist_train.shape[1]))

    logging.debug("Random forests created")

    test_pred = clf.predict(hist_test.reshape((hist_test.shape[0]*hist_test.shape[1], hist_test.shape[2])))
    test_label = np.repeat(np.arange(hist_test.shape[0]), hist_test.shape[1])
    train_pred = clf.predict(hist_train.reshape((hist_train.shape[0]*hist_train.shape[1], hist_train.shape[2])))
    train_label = np.repeat(np.arange(hist_train.shape[0]), hist_train.shape[1])

    if args.timer:
        end = time.time()
        print("Execution time: ",end - start)
    if args.conf_mat:
        skplt.metrics.plot_confusion_matrix(test_pred, test_label, normalize=True)
        plt.show()
    
    if args.testmode:
        return accuracy_score(test_pred, test_label), accuracy_score(train_pred, train_label), end-start
    else:
        return accuracy_score(test_pred, test_label) 


def main():
    data = np.load(args.data)
    train = data['desc_tr']
    test = data['desc_te']
    train_part = data['desc_sel'].T
    logging.debug("Verbose is on")

    if args.testmode:
        args.timer = 1
        a = np.zeros(10)
        acc = np.zeros((3,10))
        for i in range(10):
            args.embest = 100+2*i
            a[i] = args.embest*args.leaves
            print("Step: i-",i)
            acc[0][i], acc[1][i], acc[2][i] = run_model (data, train, test, train_part, args) 
            print("Accuracy: ",acc[0][i])

        plt.plot(a,acc[0]+0.03)
        plt.ylabel('Normalized Classification Accuracy')
        plt.xlabel('Vocabulary Size')
        plt.show()
    else:
        acc = run_model (data, train, test, train_part, args)
        print(acc)
    
if __name__ == "__main__":
    main()