-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathucr.py
More file actions
184 lines (164 loc) · 6.56 KB
/
ucr.py
File metadata and controls
184 lines (164 loc) · 6.56 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import json
import math
import torch
import numpy
import pandas
import argparse
import scikit_wrappers
def load_UCR_dataset(path, dataset):
"""
Loads the UCR dataset given in input in numpy arrays.
@param path Path where the UCR dataset is located.
@param dataset Name of the UCR dataset.
@return Quadruplet containing the training set, the corresponding training
labels, the testing set and the corresponding testing labels.
"""
train_file = os.path.join(path, dataset, dataset + "_TRAIN.tsv")
test_file = os.path.join(path, dataset, dataset + "_TEST.tsv")
train_df = pandas.read_csv(train_file, sep='\t', header=None)
test_df = pandas.read_csv(test_file, sep='\t', header=None)
train_array = numpy.array(train_df)
test_array = numpy.array(test_df)
# Move the labels to {0, ..., L-1}
labels = numpy.unique(train_array[:, 0])
transform = {}
for i, l in enumerate(labels):
transform[l] = i
train = numpy.expand_dims(train_array[:, 1:], 1).astype(numpy.float64)
train_labels = numpy.vectorize(transform.get)(train_array[:, 0])
test = numpy.expand_dims(test_array[:, 1:], 1).astype(numpy.float64)
test_labels = numpy.vectorize(transform.get)(test_array[:, 0])
# Normalization for non-normalized datasets
# To keep the amplitude information, we do not normalize values over
# individual time series, but on the whole dataset
if dataset not in [
'AllGestureWiimoteX',
'AllGestureWiimoteY',
'AllGestureWiimoteZ',
'BME',
'Chinatown',
'Crop',
'EOGHorizontalSignal',
'EOGVerticalSignal',
'Fungi',
'GestureMidAirD1',
'GestureMidAirD2',
'GestureMidAirD3',
'GesturePebbleZ1',
'GesturePebbleZ2',
'GunPointAgeSpan',
'GunPointMaleVersusFemale',
'GunPointOldVersusYoung',
'HouseTwenty',
'InsectEPGRegularTrain',
'InsectEPGSmallTrain',
'MelbournePedestrian',
'PickupGestureWiimoteZ',
'PigAirwayPressure',
'PigArtPressure',
'PigCVP',
'PLAID',
'PowerCons',
'Rock',
'SemgHandGenderCh2',
'SemgHandMovementCh2',
'SemgHandSubjectCh2',
'ShakeGestureWiimoteZ',
'SmoothSubspace',
'UMD'
]:
return train, train_labels, test, test_labels
mean = numpy.nanmean(numpy.concatenate([train, test]))
var = numpy.nanvar(numpy.concatenate([train, test]))
train = (train - mean) / math.sqrt(var)
test = (test - mean) / math.sqrt(var)
return train, train_labels, test, test_labels
def fit_hyperparameters(file, train, train_labels, cuda, gpu,
save_memory=False):
"""
Creates a classifier from the given set of hyperparameters in the input
file, fits it and return it.
@param file Path of a file containing a set of hyperparemeters.
@param train Training set.
@param train_labels Labels for the training set.
@param cuda If True, enables computations on the GPU.
@param gpu GPU to use if CUDA is enabled.
@param save_memory If True, save GPU memory by propagating gradients after
each loss term, instead of doing it after computing the whole loss.
"""
classifier = scikit_wrappers.CausalCNNEncoderClassifier()
# Loads a given set of hyperparameters and fits a model with those
hf = open(os.path.join(file), 'r')
params = json.load(hf)
hf.close()
# Check the number of input channels
params['in_channels'] = numpy.shape(train)[1]
params['cuda'] = cuda
params['gpu'] = gpu
classifier.set_params(**params)
return classifier.fit(
train, train_labels, save_memory=save_memory, verbose=True
)
def parse_arguments():
parser = argparse.ArgumentParser(
description='Classification tests for UCR repository datasets'
)
parser.add_argument('--dataset', type=str, metavar='D', required=True,
help='dataset name')
parser.add_argument('--path', type=str, metavar='PATH', required=True,
help='path where the dataset is located')
parser.add_argument('--save_path', type=str, metavar='PATH', required=True,
help='path where the estimator is/should be saved')
parser.add_argument('--cuda', action='store_true',
help='activate to use CUDA')
parser.add_argument('--gpu', type=int, default=0, metavar='GPU',
help='index of GPU used for computations (default: 0)')
parser.add_argument('--hyper', type=str, metavar='FILE', required=True,
help='path of the file of hyperparameters to use; ' +
'for training; must be a JSON file')
parser.add_argument('--load', action='store_true', default=False,
help='activate to load the estimator instead of ' +
'training it')
parser.add_argument('--fit_classifier', action='store_true', default=False,
help='if not supervised, activate to load the ' +
'model and retrain the classifier')
return parser.parse_args()
if __name__ == '__main__':
args = parse_arguments()
if args.cuda and not torch.cuda.is_available():
print("CUDA is not available, proceeding without it...")
args.cuda = False
train, train_labels, test, test_labels = load_UCR_dataset(
args.path, args.dataset
)
if not args.load and not args.fit_classifier:
classifier = fit_hyperparameters(
args.hyper, train, train_labels, args.cuda, args.gpu
)
else:
classifier = scikit_wrappers.CausalCNNEncoderClassifier()
hf = open(
os.path.join(
args.save_path, args.dataset + '_hyperparameters.json'
), 'r'
)
hp_dict = json.load(hf)
hf.close()
hp_dict['cuda'] = args.cuda
hp_dict['gpu'] = args.gpu
classifier.set_params(**hp_dict)
classifier.load(os.path.join(args.save_path, args.dataset))
if not args.load:
if args.fit_classifier:
classifier.fit_classifier(classifier.encode(train), train_labels)
classifier.save(
os.path.join(args.save_path, args.dataset)
)
with open(
os.path.join(
args.save_path, args.dataset + '_hyperparameters.json'
), 'w'
) as fp:
json.dump(classifier.get_params(), fp)
print("Test accuracy: " + str(classifier.score(test, test_labels)))