-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
90 lines (72 loc) · 2.72 KB
/
demo.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
from model import EEGNet, DeepConvNet
from dataloader import read_bci_data
from dataset import EEGDataset
from torch.utils.data import DataLoader
import torch.nn as nn
import torch
def demo(checkpoints, X_test, y_test):
"""
In the convenience of demonstrating the best results
:param checkpoints: checkpoint path dict
:param X_test: testing data (signal)
:param y_test: testing label
"""
device = "cuda"
# prepare data
test_set = EEGDataset(X_test, y_test)
test_loader = DataLoader(test_set, batch_size=256, shuffle=True, num_workers=4)
test_size = len(test_set)
for act in checkpoints:
model_path = checkpoints[act]
# model
net = load_model(model_path)
net.to(device)
net.eval()
loss_func = nn.CrossEntropyLoss()
test_loss = 0.0
test_acc = 0.0
for idx, (inputs, targets) in enumerate(test_loader):
inputs = inputs.to(device, dtype=torch.float)
targets = targets.to(device, dtype=torch.long)
outputs = net(inputs)
test_loss += loss_func(outputs, targets).item()
_, predicted = torch.max(outputs.data, 1)
test_acc += (predicted == targets).sum().item()
test_loss /= test_size
test_acc /= test_size
print(f"{act} test_loss: {test_loss:.4}\ttest_acc: {test_acc:.4}")
print("===========================================================\n")
def load_model(model_path):
"""
load the model from checkpoint
:param model_path: checkpoint path
:return model for testing
"""
checkpoint = torch.load(model_path)
model_name = checkpoint['model_name']
act = checkpoint['act']
if model_name == "EEGNet":
net = EEGNet(act)
elif model_name == "DeepConvNet":
net = DeepConvNet(act)
net.load_state_dict(checkpoint['model_state_dict'])
return net
if __name__ == '__main__':
# read data
X_train, y_train, X_test, y_test = read_bci_data()
# EEGNet results
EEGNet_checkpoints = {
'ELU': './checkpoints/EEGNet/EEGNet_elu_5e-3_amsgrad_0.8407.pt',
'ReLU': './checkpoints/EEGNet/EEGNet_relu_1e-3_0.8731.pt',
'LeakyReLU': './checkpoints/EEGNet/EEGNet_leaky_relu_1e-2_init_amsgrad_0.8787.pt'
}
print("EEGNet results")
demo(EEGNet_checkpoints, X_test, y_test)
# DeepConvNet results
DeepConvNet_checkpoints = {
'ELU': './checkpoints/DeepConvNet/DeepConvNet_elu_1e-3_amsgrad_0.7454.pt',
'ReLU': './checkpoints/DeepConvNet/DeepConvNet_relu_1e-2_0.7102.pt',
'LeakyReLU': './checkpoints/DeepConvNet/DeepConvNet_leaky_relu_1e-2_init_amsgrad_0.7352.pt'
}
print("DeepConvNet results")
demo(DeepConvNet_checkpoints, X_test, y_test)