-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.py
89 lines (66 loc) · 3.16 KB
/
test.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
import os
import json
import argparse
import warnings
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from utils import *
from loader import EEGDataLoader
from train_mtcl import OneFoldTrainer
from models.main_model import MainModel
class OneFoldEvaluator(OneFoldTrainer):
def __init__(self, args, fold, config):
self.args = args
self.fold = fold
self.cfg = config
self.ds_cfg = config['dataset']
self.tp_cfg = config['training_params']
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('[INFO] Config name: {}'.format(config['name']))
self.model = self.build_model()
self.loader_dict = self.build_dataloader()
self.criterion = nn.CrossEntropyLoss()
self.ckpt_path = os.path.join('checkpoints', config['name'])
self.ckpt_name = 'ckpt_fold-{0:02d}.pth'.format(self.fold)
def build_model(self):
model = MainModel(self.cfg)
print('[INFO] Number of params of model: ', sum(p.numel() for p in model.parameters() if p.requires_grad))
model = torch.nn.DataParallel(model, device_ids=list(range(len(self.args.gpu.split(",")))))
model.to(self.device)
print('[INFO] Model prepared, Device used: {} GPU:{}'.format(self.device, self.args.gpu))
return model
def build_dataloader(self):
test_dataset = EEGDataLoader(self.cfg, self.fold, set='test')
test_loader = DataLoader(dataset=test_dataset, batch_size=self.tp_cfg['batch_size'], shuffle=False, num_workers=4*len(self.args.gpu.split(",")), pin_memory=True)
print('[INFO] Dataloader prepared')
return {'test': test_loader}
def run(self):
print('\n[INFO] Fold: {}'.format(self.fold))
self.model.load_state_dict(torch.load(os.path.join(self.ckpt_path, self.ckpt_name)))
y_true, y_pred = self.evaluate(mode='test')
print('')
return y_true, y_pred
def main():
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=UserWarning)
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--seed', type=int, default=42, help='random seed')
parser.add_argument('--gpu', type=str, default="0", help='gpu id')
parser.add_argument('--config', type=str, help='config file path')
args = parser.parse_args()
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
with open(args.config) as config_file:
config = json.load(config_file)
config['name'] = os.path.basename(args.config).replace('.json', '')
Y_true = np.zeros(0)
Y_pred = np.zeros((0, config['classifier']['num_classes']))
for fold in range(1, config['dataset']['num_splits'] + 1):
evaluator = OneFoldEvaluator(args, fold, config)
y_true, y_pred = evaluator.run()
Y_true = np.concatenate([Y_true, y_true])
Y_pred = np.concatenate([Y_pred, y_pred])
summarize_result(config, fold, Y_true, Y_pred)
if __name__ == "__main__":
main()