-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.py
149 lines (109 loc) · 4.09 KB
/
utils.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
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
import os
import time
import h5py
import numpy as np
import pprint
import random
from networks import *
from eeg_dataset import *
from torch.utils.data import DataLoader
from sklearn.metrics import confusion_matrix, accuracy_score, f1_score
def set_gpu(x):
torch.set_num_threads(1)
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = x
print('using gpu:', x)
def seed_all(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
np.random.seed(seed)
def ensure_path(path):
if os.path.exists(path):
pass
else:
os.makedirs(path)
class Averager():
def __init__(self):
self.n = 0
self.v = 0
def add(self, x):
self.v = (self.v * self.n + x) / (self.n + 1)
self.n += 1
def item(self):
return self.v
def count_acc(logits, label):
pred = torch.argmax(logits, dim=1)
return (pred == label).type(torch.cuda.FloatTensor).mean().item()
class Timer():
def __init__(self):
self.o = time.time()
def measure(self, p=1):
x = (time.time() - self.o) / p
x = int(x)
if x >= 3600:
return '{:.1f}h'.format(x / 3600)
if x >= 60:
return '{}m'.format(round(x / 60))
return '{}s'.format(x)
_utils_pp = pprint.PrettyPrinter()
def pprint(x):
_utils_pp.pprint(x)
def get_model(args):
if args.model == 'LGGNet':
idx_local_graph = list(np.array(h5py.File('num_chan_local_graph_{}.hdf'.format(args.graph_type), 'r')['data']))
channels = sum(idx_local_graph)
input_size = (args.input_shape[0], channels, args.input_shape[2])
model = LGGNet(
num_classes=args.num_class, input_size=input_size,
sampling_rate=int(args.sampling_rate*args.scale_coefficient),
num_T=args.T, out_graph=args.hidden,
dropout_rate=args.dropout,
pool=args.pool, pool_step_rate=args.pool_step_rate,
idx_graph=idx_local_graph)
return model
def get_dataloader(data, label, batch_size):
# load the data
dataset = eegDataset(data, label)
loader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, pin_memory=True)
return loader
def get_metrics(y_pred, y_true, classes=None):
acc = accuracy_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
if classes is not None:
cm = confusion_matrix(y_true, y_pred, labels=classes)
else:
cm = confusion_matrix(y_true, y_pred)
return acc, f1, cm
def get_trainable_parameter_num(model):
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
return total_params
def L1Loss(model, Lambda):
w = torch.cat([x.view(-1) for x in model.parameters()])
err = Lambda * torch.sum(torch.abs(w))
return err
def L2Loss(model, Lambda):
w = torch.cat([x.view(-1) for x in model.parameters()])
err = Lambda * torch.sum(w.pow(2))
return err
class LabelSmoothing(nn.Module):
"""NLL loss with label smoothing.
refer to: https://github.com/NVIDIA/DeepLearningExamples/blob/8d8b21a933fff3defb692e0527fca15532da5dc6/PyTorch/Classification/ConvNets/image_classification/smoothing.py#L18
"""
def __init__(self, smoothing=0.0):
"""Constructor for the LabelSmoothing module.
:param smoothing: label smoothing factor
"""
super(LabelSmoothing, self).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
def forward(self, x, target):
logprobs = torch.nn.functional.log_softmax(x, dim=-1)
nll_loss = -logprobs.gather(dim=-1, index=target.unsqueeze(1))
nll_loss = nll_loss.squeeze(1)
smooth_loss = -logprobs.mean(dim=-1)
loss = self.confidence * nll_loss + self.smoothing * smooth_loss
return loss.mean()