-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
160 lines (107 loc) · 4.37 KB
/
main.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import numpy as np
import pandas as pd
import os
import argparse
from sklearn.metrics import accuracy_score
from utils.utils import load_data, znormalisation, create_directory, encode_labels
from classifiers.CO_FCN import COFCN
from classifiers.H_FCN import HFCN
from classifiers.H_Inception import HINCEPTION
def get_args():
parser = argparse.ArgumentParser(
description="Choose to apply which classifier on which dataset with number of runs.")
parser.add_argument(
'--dataset',
help="which dataset to run the experiment on.",
type=str,
default='Coffee'
)
parser.add_argument(
'--classifier',
help='which classifier to use',
type=str,
choices=['CO-FCN', 'H-FCN', 'H-Inception'],
default='H-Inception'
)
parser.add_argument(
'--runs',
help="number of runs to do",
type=int,
default=5
)
parser.add_argument(
'--output-directory',
help="output directory parent",
type=str,
default='results/'
)
args = parser.parse_args()
return args
if __name__ == '__main__':
use_ensemble = {
'CO-FCN' : False,
'H-FCN' : True,
'H-Inception' : True
}
args = get_args()
output_dir_parent = args.output_directory
create_directory(output_dir_parent)
output_dir_clf = output_dir_parent + args.classifier + '/'
create_directory(output_dir_clf)
if os.path.exists(output_dir_clf + 'results_UCR_128.csv'):
df = pd.read_csv(output_dir_clf + 'results_UCR_128.csv')
dataset_names = list(df['dataset_name'])
if args.dataset in dataset_names:
print("Already done")
exit()
else:
if use_ensemble[args.classifier]:
df = pd.DataFrame(columns=['dataset_name', args.classifier+'-mean', args.classifier+'-std', args.classifier+'Time'])
else:
df = pd.DataFrame(columns=['dataset_name', args.classifier+'-mean', args.classifier+'-std'])
xtrain, ytrain, xtest, ytest = load_data(file_name=args.dataset)
xtrain = znormalisation(xtrain)
xtest = znormalisation(xtest)
xtrain = np.expand_dims(xtrain, axis=2)
xtest = np.expand_dims(xtest, axis=2)
ytrain = encode_labels(ytrain)
ytest = encode_labels(ytest)
length_TS = int(xtrain.shape[1])
n_classes = len(np.unique(ytrain))
scores = []
if use_ensemble[args.classifier]:
ypred_ensemble = np.zeros(shape=(len(ytest), len(np.unique(ytest))))
for _run in range(args.runs):
output_dir = output_dir_clf + 'run_' + str(_run) + '/'
create_directory(output_dir)
output_dir = output_dir + args.dataset + '/'
create_directory(output_dir)
if args.classifier == 'CO-FCN':
clf = COFCN(output_directory=output_dir, length_TS=length_TS, n_classes=n_classes)
elif args.classifier == 'H-FCN':
clf = HFCN(output_directory=output_dir, length_TS=length_TS, n_classes=n_classes)
elif args.classifier == 'H-Inception':
clf = HINCEPTION(output_directory=output_dir, length_TS=length_TS, n_classes=n_classes)
if not os.path.exists(output_dir + 'loss.pdf'):
clf.fit(xtrain=xtrain, ytrain=ytrain, xval=xtest, yval=ytest, plot_test=True)
if use_ensemble[args.classifier]:
ypred, score = clf.predict(xtest=xtest, ytest=ytest)
ypred_ensemble = ypred_ensemble + ypred
else:
score = clf.predict(xtest=xtest, ytest=ytest)
scores.append(score)
if use_ensemble[args.classifier]:
ypred_ensemble = ypred_ensemble / (1.0 * args.runs)
ypred_ensemble = np.argmax(ypred_ensemble, axis=1)
score_ensemble = accuracy_score(y_true=ytest, y_pred=ypred_ensemble, normalize=True)
df = df.append({
'dataset_name' : args.dataset,
args.classifier+'-mean' : np.mean(scores),
args.classifier+'-std' : np.std(scores),
args.classifier+'Time' : score_ensemble}, ignore_index=True)
else:
df = df.append({
'dataset_name' : args.dataset,
args.classifier+'-mean' : np.mean(scores),
args.classifier+'-std' : np.std(scores)}, ignore_index=True)
df.to_csv(output_dir_clf + 'results_UCR_128.csv', index=False)