-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
96 lines (81 loc) · 3.45 KB
/
Copy pathmodel.py
File metadata and controls
96 lines (81 loc) · 3.45 KB
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
import torch
import torch.nn as nn
from torch.utils.data import Dataset
import numpy as np
chunk_size = 1000
# split long signals to smaller chunks, discard no-events chunks
# if chunk's mean is lower than threshold we discard it, because there was no activity during that time
# we keep a record of indices that remained in training dataset
def clean_and_resample_data(data):
index = []
for i in range(len(data)):
for j in range(0, data[i].shape[1], chunk_size):
index.extend([(i, k) for k in range(j, min(data[i].shape[1],j+chunk_size))])
# plt.plot([0, len(mean_val)], [threshold, threshold], color='r')
# plt.scatter(range(len(mean_val)), mean_val, s=1)
# plt.show()
return index
class EEGSignalDataset(Dataset):
def __init__(
self,
data,
events,
soft_label=True,
train=True
):
self.data = data
self.events = events
self.train = train
self.soft_label = soft_label
if train:
self.index = clean_and_resample_data(events)
else:
self.index = [(i, j) for i in range(len(data)) for j in range(data[i].shape[1])]
# for i, gt in enumerate(self.data):
# gt = (gt - np.min(gt)) / (np.max(gt) - np.min(gt))
# self.data[i] = gt
# get chunk of signal, if chunk is smaller than 1024 we add 0 padding
# if soft_label we avoid using exact zeros - we use 0.2 instead
# this is for loss functions with logarithms
def __getitem__(self, i):
i, j = self.index[i]
raw_data, label = self.data[i][:, max(0, j - 1024 + 1):j + 1], self.events[i][:, j]
pad = 1024 - raw_data.shape[1]
if pad:
raw_data = np.pad(raw_data, ((0, 0), (pad, 0)), 'constant', constant_values=0)
raw_data = torch.from_numpy(raw_data.astype(np.float32))
label = torch.from_numpy(label.astype(np.float32))
if self.soft_label:
label[label < .02] = .02
return raw_data, label
def __len__(self):
return len(self.index)
# we return 6 channels because our dataset specify 6 hand gestures
class NNet(nn.Module):
def __init__(self, in_channels=32, out_channels=6):
super(NNet, self).__init__()
self.hidden = 32
self.net = nn.Sequential(
nn.Conv1d(in_channels, in_channels, 5, padding=2),
nn.Conv1d(self.hidden, self.hidden, 16, stride=16),
nn.LeakyReLU(0.1),
nn.Conv1d(self.hidden, self.hidden, 7, padding=3),
)
for i in range(6): # six different hand gestures
self.net.add_module('conv{}'.format(i), \
self.__block(self.hidden, self.hidden))
self.net.add_module('final', nn.Sequential(
nn.Conv1d(self.hidden, out_channels, 1),
nn.Sigmoid() # produces 1 or 0, depending on input
))
def __block(self, inchannels, outchannels):
return nn.Sequential(
nn.MaxPool1d(2, 2),
nn.Dropout(p=0.1, inplace=True),
nn.Conv1d(inchannels, outchannels, 5, padding=2),
nn.LeakyReLU(0.1),
nn.Conv1d(outchannels, outchannels, 5, padding=2),
nn.LeakyReLU(0.1), # TODO maybe we should try PReLU? - The learnable slope adjusts over time, which can yield gradients that are closer to optimal, especially in deeper networks.
)
def forward(self, x):
return self.net(x)