-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtrain_resnet.py
executable file
·73 lines (55 loc) · 2.38 KB
/
train_resnet.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
import os
import argparse
import numpy as np
import torch
from src.utils import read_dataset_from_npy, Logger
from src.resnet.model import ResNet, ResNetTrainer
data_dir = './tmp'
log_dir = './logs'
multivariate_datasets = ['CharacterTrajectories', 'ECG', 'KickvsPunch', 'NetFlow']
def train(X_train, y_train, X_test, y_test, device, logger):
nb_classes = len(np.unique(np.concatenate((y_train, y_test), axis=0)))
input_size = X_train.shape[1]
model = ResNet(input_size, nb_classes)
model = model.to(device)
trainer = ResNetTrainer(device, logger)
model = trainer.fit(model, X_train, y_train)
acc = trainer.test(model, X_test, y_test)
return acc
def argsparser():
parser = argparse.ArgumentParser("Active Timeseries classification")
parser.add_argument('--dataset', help='Dataset name', default='Coffee')
parser.add_argument('--seed', help='Random seed', type=int, default=0)
parser.add_argument('--gpu', type=str, default='0')
parser.add_argument('--shot', help='shot', type=int, default=1)
return parser
if __name__ == "__main__":
# Get the arguments
parser = argsparser()
args = parser.parse_args()
# Setup the gpu
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
if torch.cuda.is_available():
device = torch.device("cuda:0")
print("--> Running on the GPU")
else:
device = torch.device("cpu")
print("--> Running on the CPU")
# Seeding
np.random.seed(args.seed)
torch.manual_seed(args.seed)
log_dir = os.path.join(log_dir, 'resnet_log_'+str(args.shot)+'_shot')
if not os.path.exists(log_dir):
os.makedirs(log_dir)
out_path = os.path.join(log_dir, args.dataset+'_'+str(args.seed)+'.txt')
with open(out_path, 'w') as f:
logger = Logger(f)
# Read data
if args.dataset in multivariate_datasets:
X, y, train_idx, test_idx = read_dataset_from_npy(os.path.join(data_dir, 'multivariate_datasets_'+str(args.shot)+'_shot', args.dataset+'.npy'))
else:
X, y, train_idx, test_idx = read_dataset_from_npy(os.path.join(data_dir, 'ucr_datasets_'+str(args.shot)+'_shot', args.dataset+'.npy'))
# Train the model
acc = train(X[train_idx], y[train_idx], X[test_idx], y[test_idx], device, logger)
logger.log('--> {} Test Accuracy: {:5.4f}'.format(args.dataset, acc))
logger.log(str(acc))