forked from hsd1503/resnet1d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn1d.py
113 lines (93 loc) · 3.33 KB
/
cnn1d.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
"""
cnn+rnn for 1-d signal data, pytorch version
Shenda Hong, Jan 2020
"""
import numpy as np
from collections import Counter
from tqdm import tqdm
from matplotlib import pyplot as plt
from sklearn.metrics import classification_report
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
class MyDataset(Dataset):
def __init__(self, data, label):
self.data = data
self.label = label
def __getitem__(self, index):
return (torch.tensor(self.data[index], dtype=torch.float), torch.tensor(self.label[index], dtype=torch.long))
def __len__(self):
return len(self.data)
class CNN(nn.Module):
"""
Input:
X: (n_samples, n_channel, n_length)
Y: (n_samples)
Output:
out: (n_samples)
Pararmetes:
n_classes: number of classes
"""
def __init__(self, in_channels, out_channels, n_len_seg, n_classes, device, verbose=False):
super(CNN, self).__init__()
self.n_len_seg = n_len_seg
self.n_classes = n_classes
self.in_channels = in_channels
self.out_channels = out_channels
self.device = device
self.verbose = verbose
# (batch, channels, length)
self.cnn = nn.Conv1d(in_channels=self.in_channels,
out_channels=self.out_channels,
kernel_size=16,
stride=2)
# (batch, channels, length)
self.encoder_layer = nn.TransformerEncoderLayer(
d_model=self.out_channels,
nhead=8,
dim_feedforward=128,
dropout=0.5)
self.transformer_encoder = nn.TransformerEncoder(self.encoder_layer, num_layers=1)
self.dense = nn.Linear(out_channels, n_classes)
def forward(self, x):
self.n_channel, self.n_length = x.shape[-2], x.shape[-1]
assert (self.n_length % self.n_len_seg == 0), "Input n_length should divided by n_len_seg"
self.n_seg = self.n_length // self.n_len_seg
out = x
if self.verbose:
print(out.shape)
# (n_samples, n_channel, n_length) -> (n_samples, n_length, n_channel)
out = out.permute(0,2,1)
if self.verbose:
print(out.shape)
# (n_samples, n_length, n_channel) -> (n_samples*n_seg, n_len_seg, n_channel)
out = out.view(-1, self.n_len_seg, self.n_channel)
if self.verbose:
print(out.shape)
# (n_samples*n_seg, n_len_seg, n_channel) -> (n_samples*n_seg, n_channel, n_len_seg)
out = out.permute(0,2,1)
if self.verbose:
print(out.shape)
# cnn
out = self.cnn(out)
if self.verbose:
print(out.shape)
# global avg, (n_samples*n_seg, out_channels)
out = out.mean(-1)
if self.verbose:
print(out.shape)
out = out.view(-1, self.n_seg, self.out_channels)
if self.verbose:
print(out.shape)
out = self.transformer_encoder(out)
if self.verbose:
print(out.shape)
out = out.mean(-2)
if self.verbose:
print(out.shape)
out = self.dense(out)
if self.verbose:
print(out.shape)
return out