-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcnn.py
154 lines (121 loc) · 4.66 KB
/
lcnn.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
150
151
152
153
154
import torch
import math
from torch import nn
import torch.nn.init as init
import torch.nn.functional as F
from torch.autograd import Variable
class LCNN(nn.Module):
def __init__(self, num_classes=2):
super(LCNN, self).__init__()
self.features = nn.Sequential(
mfm(1, 8, 5, 1, 2),
nn.BatchNorm2d(8), #
nn.Dropout2d(p=0.35), #
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
group(8, 16, 3, 1, 1),
nn.BatchNorm2d(16), #
nn.Dropout2d(p=0.35), #
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
group(16, 32, 3, 1, 1),
nn.BatchNorm2d(32), #
nn.Dropout2d(p=0.35), #
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
group(32, 24, 3, 1, 1),
group(24, 24, 3, 1, 1),
nn.BatchNorm2d(24), #
nn.Dropout2d(p=0.35), #
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
)
# self.block = nn.Sequential(
# mfm(960, 256, type=0), # MFCC -> 384, MFCC_delta -> [960], spect -> 12480 IMFCC -> 1872
# nn.Dropout()
# )
# self.logits = nn.Linear(256, 2)
self.block = nn.Sequential(
nn.Conv1d(144, 60, 3, padding=1),
nn.BatchNorm1d(60),
nn.Conv1d(60, 30, 3, padding=1),
nn.BatchNorm1d(30),
nn.Conv1d(30, 2, 3, padding=1)
)
# self.b1 = nn.Linear(144, 60)
# self.b2 = nn.Conv1d(60, 60, 3, padding=1)
# self.b3 = nn.BatchNorm1d(60)
# self.b4 = nn.Linear(60, 30)
# self.b5 = nn.Conv1d(30, 30, 3, padding=1)
# self.b6 = nn.BatchNorm1d(30)
# self.b7 = nn.Linear(30, 2)
#self.sigmoid = nn.Sigmoid()
self.logsoftmax = nn.LogSoftmax(dim=1)
self.softmax = nn.Softmax(dim=1)
self.init_weight()
def forward(self, x):
x = x.view(x.size(0), 1, x.size(1), x.size(2))
x = self.features(x)
x = x.view(x.size(0), x.size(1)*x.size(3), -1)
# out = self.block(x)
# logits = self.logits(out)
x = self.block(x)
# x = x.transpose(1,2)
# x = self.b1(x)
# x = x.transpose(1,2)
# x = self.b2(x)
# x = self.b3(x)
# x = x.transpose(1,2)
# x = self.b4(x)
# x = x.transpose(1,2)
# x = self.b5(x)
# x = self.b6(x)
# x = x.transpose(1,2)
# x = self.b7(x)
# x = x.transpose(1,2)
logits = torch.mean(x, 2)
logsoftmax = self.logsoftmax(logits)
softmax = self.softmax(logits)
uncertanity, prob, alpha, evidence = self.compute_uncertanity(
relu_evidence=self.exp_evidence, logits=logits)
return logits, logsoftmax, softmax, uncertanity, prob, alpha
def compute_uncertanity(self, relu_evidence, logits):
K = 2
evidence = relu_evidence(logits)
alpha = evidence + 1
uncrtnty = K / torch.sum(alpha, dim=1)
prob = alpha / torch.sum(alpha, dim=1).reshape(-1,1)
return uncrtnty, prob, alpha, evidence
def relu_evidence(self):
return F.relu
def exp_evidence_2(self):
return torch.exp
def exp_evidence(self, logits):
return torch.exp(torch.clamp(logits,-10,10))
def softplus_evidence(self):
return F.softplus
def init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.xavier_normal(m.weight.data)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
nn.init.xavier_normal(m.weight.data)
m.bias.data.zero_()
class mfm(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, type=1):
super(mfm, self).__init__()
self.out_channels = out_channels
if type == 1:
self.filter = nn.Conv2d(in_channels, 2*out_channels, kernel_size=kernel_size, stride=stride, padding=padding)
else:
self.filter = nn.Linear(in_channels, 2*out_channels)
def forward(self, x):
x = self.filter(x)
out = torch.split(x, self.out_channels, 1)
return torch.max(out[0], out[1])
class group(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding):
super(group, self).__init__()
self.conv_a = mfm(in_channels, in_channels, 1, 1, 0)
self.conv = mfm(in_channels, out_channels, kernel_size, stride, padding)
def forward(self, x):
x = self.conv_a(x)
x = self.conv(x)
return x