-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_cfy.py
138 lines (100 loc) · 3.96 KB
/
models_cfy.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
import os, sys, glob
import numpy as np
from tensorflow.keras import layers, models, optimizers
from sklearn.model_selection import train_test_split
def cfy1(xshape, n_class, optimizer):
model = models.Sequential()
model.add(layers.Input(shape=xshape))
for rate in (1,2,4,8):
model.add(layers.Conv1D(filters=20, kernel_size=2,
padding='causal', activation='relu', dilation_rate=rate))
model.add(layers.AveragePooling1D(50, padding='same'))
model.add(layers.Conv1D(20, 100, padding='same', activation='relu'))
model.add(layers.Conv1D(1, 100, padding='same', activation='relu'))
model.add(layers.AveragePooling1D(10, padding='same'))
if n_class==2:
n_dim = n_class-1
act = 'sigmoid'
loss='binary_crossentropy'
else:
n_dim = n_class
act = 'softmax'
loss='sparse_categorical_crossentropy'
# Last layer - for label
model.add(layers.Conv1D(n_dim, 10, padding='same'))
model.add(layers.AveragePooling1D(10, padding='same'))
model.add(layers.Reshape((n_dim, )))
model.add(layers.Activation(act))
model.compile(optimizer, loss, metrics=['accuracy'])
return model
def cfy3(xshape, n_class, optimizer):
model = models.Sequential()
model.add(layers.Input(shape=xshape))
model.add(layers.BatchNormalization())
for rate in (1,2,4,8):
model.add(layers.Conv1D(filters=20, kernel_size=2,
padding='causal', activation='relu', dilation_rate=rate))
model.add(layers.AveragePooling1D(50, padding='same'))
model.add(layers.Conv1D(20, 100, padding='same', activation='relu'))
model.add(layers.Conv1D(1, 100, padding='same', activation='relu'))
model.add(layers.AveragePooling1D(10, padding='same'))
if n_class==2:
n_dim = n_class-1
act = 'sigmoid'
loss='binary_crossentropy'
else:
n_dim = n_class
act = 'softmax'
loss='sparse_categorical_crossentropy'
# Last layer - for label
model.add(layers.Conv1D(n_dim, 10, padding='same'))
model.add(layers.AveragePooling1D(10, padding='same'))
model.add(layers.Reshape((n_dim, )))
model.add(layers.Activation(act))
model.compile(optimizer, loss, metrics=['accuracy'])
return model
def cfy2(xshape, n_class, optimizer):
model = models.Sequential()
model.add(layers.Input(shape=xshape))
for rate in (1,2,4,8):
model.add(layers.Conv1D(filters=20, kernel_size=2,
padding='causal', activation='relu', dilation_rate=rate))
model.add(layers.AveragePooling1D(50, padding='same'))
model.add(layers.Conv1D(20, 100, padding='same', activation='relu'))
model.add(layers.Conv1D(1, 100, padding='same', activation='relu'))
model.add(layers.AveragePooling1D(10, padding='same'))
if n_class==2:
n_dim = n_class-1
act = 'sigmoid'
loss='binary_crossentropy'
else:
n_dim = n_class
act = 'softmax'
loss='sparse_categorical_crossentropy'
model.add(layers.Conv1D(n_dim*4, 10, padding='same'))
model.add(layers.AveragePooling1D(10, padding='same'))
model.add(layers.Reshape((n_dim*4, )))
model.add(layers.Dense(n_dim))
model.add(layers.Activation(act))
model.compile(optimizer, loss, metrics=['accuracy'])
return model
def dense1(xshape, n_class, optimizer):
model = models.Sequential()
model.add(layers.Input(shape=xshape))
model.add(layers.Dense(n_dim))
model.add(layers.Activation(act))
MODELS = {
'cfy1': cfy1,
'cfy2': cfy2,
'cfy3': cfy3
}
def main():
dataDir = sys.argv[1]
model_name = sys.argv[2]
# load data
x_train, x_valid, _, y_train, y_valid, _ = load_data(dataDir)
# load and draw model
model = MODELS[model_name](x_train, 'adam')
model.summary()
if __name__=='__main__':
main()