This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Model.py
84 lines (79 loc) · 4.2 KB
/
Model.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
import numpy
numpy.random.seed(1337) # for reproducibility
import tensorflow
tensorflow.random.set_random_seed(1337)
from keras.models import Model
from keras.layers import Dense, Dropout, Flatten, merge, Input, concatenate
from keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.utils import np_utils
from keras.regularizers import l2, l1
from keras.optimizers import SGD, Adadelta
class MultiViewModel:
@classmethod
def getModel(cls, setting, channels, fft_bins, pca_bins, steps):
input1 = Input(shape = (channels * pca_bins, steps, 1), name = "input1")
seq1 = Conv2D(filters = setting.nb_filter, kernel_size = (channels * pca_bins, 1),
kernel_initializer = "lecun_uniform",
kernel_regularizer=l2(l = setting.l2),
activation="relu")(input1)
seq1 = Dropout(setting.dropout)(seq1)
seq1 = Conv2D(filters = setting.nb_filter, kernel_size = (1, 3),
kernel_regularizer=l2(l = setting.l2),
kernel_initializer = "lecun_uniform",
activation="relu")(seq1)
seq1 = Dropout(setting.dropout)(seq1)
seq1 = Conv2D(filters = setting.nb_filter, kernel_size = (1, 3),
kernel_regularizer=l2(l = setting.l2),
kernel_initializer = "lecun_uniform",
activation="relu")(seq1)
seq1 = Dropout(setting.dropout)(seq1)
seq1 = Conv2D(filters = setting.nb_filter, kernel_size = (1, 3),
kernel_regularizer=l2(l = setting.l2),
kernel_initializer = "lecun_uniform",
activation="relu")(seq1)
seq1 = Dropout(setting.dropout)(seq1)
seq1 = Conv2D(filters = setting.nb_filter, kernel_size = (1, 3),
kernel_regularizer=l2(l = setting.l2),
kernel_initializer = "lecun_uniform",
activation="relu")(seq1)
seq1 = Flatten()(seq1)
output1 = Dense(setting.output1, activation="tanh")(seq1)
input2 = Input(shape = (channels * fft_bins, steps, 1), name = "input2")
seq2 = Conv2D(filters = setting.nb_filter, kernel_size = (channels * fft_bins, 1),
kernel_regularizer=l2(l = setting.l2),
kernel_initializer = "lecun_uniform",
activation="relu")(input2)
seq2 = Dropout(setting.dropout)(seq2)
seq2 = Conv2D(filters = setting.nb_filter, kernel_size = (1, 3),
kernel_regularizer=l2(l = setting.l2),
kernel_initializer = "lecun_uniform",
activation="relu")(seq2)
seq2 = Dropout(setting.dropout)(seq2)
seq2 = Conv2D(filters = setting.nb_filter, kernel_size = (1, 3),
kernel_regularizer=l2(l = setting.l2),
kernel_initializer = "lecun_uniform",
activation="relu")(seq2)
seq2 = Dropout(setting.dropout)(seq2)
seq2 = Conv2D(filters = setting.nb_filter, kernel_size = (1, 3),
kernel_regularizer=l2(l = setting.l2),
kernel_initializer = "lecun_uniform",
activation="relu")(seq2)
seq2 = Dropout(setting.dropout)(seq2)
seq2 = Conv2D(filters = setting.nb_filter, kernel_size = (1, 3),
kernel_regularizer=l2(l = setting.l2),
kernel_initializer = "lecun_uniform",
activation="relu")(seq2)
seq2 = Flatten()(seq2)
output2 = Dense(setting.output2, activation="tanh")(seq2)
merged = concatenate([output1, output2])
merged = Dense(512, activation="tanh")(merged)
merged = Dense(256, activation="tanh")(merged)
merged = Dense(128, activation="tanh")(merged)
output = Dense(2, activation="softmax", name="output")(merged)
model = Model(inputs=[input1, input2], outputs=[output])
sgd = SGD(lr = setting.lr)
if setting.name == "Patient_1":
model.compile(loss="binary_crossentropy", optimizer ="adam")
else:
model.compile(loss="binary_crossentropy", optimizer = sgd)
return model