-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_ucr.py
59 lines (50 loc) · 2.03 KB
/
transfer_ucr.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
import os
import json
import torch
import argparse
import ucr
import scikit_wrappers
def parse_arguments():
parser = argparse.ArgumentParser(
description='Uses the learned representations for a dataset to ' +
'learn classifiers for all other UCR datasets'
)
parser.add_argument('--path', type=str, metavar='PATH', required=True,
help='path where the UCR datasets are located')
parser.add_argument('--save_path', type=str, metavar='PATH', required=True,
help='path where the encoder is saved')
parser.add_argument('--dataset', type=str, metavar='D', required=True,
help='dataset name')
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)')
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
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))
print("Classification tasks...")
# List of folders / datasets in the given path
datasets = [x[0][len(args.path) + 1:] for x in os.walk(args.path)][1:]
for dataset in datasets:
train, train_labels, test, test_labels = ucr.load_UCR_dataset(
args.path, dataset
)
classifier.fit_classifier(classifier.encode(train), train_labels)
print(
dataset,
"Test accuracy: " + str(classifier.score(test, test_labels))
)