-
Notifications
You must be signed in to change notification settings - Fork 2
/
AOAtrain.py
139 lines (104 loc) · 4.94 KB
/
AOAtrain.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
import keras
from keras.datasets import mnist
from keras.models import Model
from keras.layers import Input, add, LSTM, RepeatVector, TimeDistributed, Bidirectional, BatchNormalization
from keras.layers.core import Layer, Dense, Dropout, Activation, Flatten, Reshape
from keras import regularizers, Sequential
from keras.regularizers import l2
from keras.layers.convolutional import Conv2D, MaxPooling2D, UpSampling2D, ZeroPadding2D
from keras.utils import np_utils, plot_model
def build_fc0(feature_dim=160, out_dim=75):
x = Input(shape=(feature_dim,))
h1 = Dense(512, activation='relu')(x)
h2 = Dense(1024, activation='relu')(h1)
h3 = Dense(512, activation='relu')(h2)
h4 = Dense(256, activation='relu')(h3)
r = Dense(out_dim, activation='softmax')(h4)
model = Model(inputs=x, outputs=r)
optimizer = keras.optimizers.Adam(learning_rate=0.001, decay=1e-5)
loss_obj = keras.losses.CategoricalCrossentropy(from_logits=False)
model.compile(optimizer=optimizer, loss=loss_obj)
model.summary()
return model
def build_fc1(feature_dim=160, output_dim=75):
x = Input(shape=(feature_dim,))
h1 = Dense(512, activation='relu')(x)
h2 = Dense(1024, activation='relu')(h1)
h3 = Dense(512, activation='relu')(h2)
h4 = Dense(256, activation='relu')(h3)
r = Dense(output_dim, activation='sigmoid')(h4) # 'sigmoid' + BinaryCrossentropy
model = Model(inputs=x, outputs=r)
optimizer = keras.optimizers.Adam(learning_rate=0.01, decay=1e-5)
loss_obj = keras.losses.BinaryCrossentropy(from_logits=False)
model.compile(optimizer=optimizer, loss=loss_obj)
model.summary()
return model
def build_CR0(feature_dim=128, num_classes=1, regress1=1, regress2=1):
x = Input(shape=(feature_dim,))
h1 = Dense(1024, activation='relu')(x)
h2 = Dense(2048, activation='relu')(h1)
h3 = Dense(1024, activation='relu')(h2)
h4 = Dense(512, activation='relu')(h3)
# Num-of-signal Classifier
c = Dense(num_classes, activation='sigmoid', name="class_out")(h4)
# Regression of 1-signal
r1 = Dense(regress1, activation='sigmoid', name="regress1_out")(h4)
# Regression of 2-signal
r2 = Dense(regress2, activation='sigmoid', name="regress2_out")(h4)
model = Model(inputs=x, outputs=[c, r1, r2], name="deepaoanet0")
optimizer = keras.optimizers.Adam(learning_rate=0.001, decay=1e-6)
losses = {
"class_out": keras.losses.BinaryCrossentropy(from_logits=False),
"regress1_out": keras.losses.MeanSquaredError(),
"regress2_out": keras.losses.MeanSquaredError(),
}
lossWeights = {"class_out": 0.01, "regress1_out": 1.0, "regress2_out": 1.0}
lossWeights2 = {"class_out": 0.0001, "regress1_out": 1.0, "regress2_out": 1.0}
metrics = {"class_out": 'accuracy', "regress1_out": 'mse', "regress2_out": 'mse'}
model.compile(optimizer=optimizer,
loss=losses,
loss_weights=lossWeights,
metrics=metrics)
model.summary()
return model
def build_CR1(feature_dim=(4, 4, 8), num_classes=1, regress1=1, regress2=1):
x = Input(shape=feature_dim)
c1 = Conv2D(512, kernel_size=(3, 3), padding='valid', strides=(1, 1), activation=None)(x)
b1 = BatchNormalization()(c1)
b2 = Activation("relu")(b1)
c2 = MaxPooling2D(pool_size=(2, 2))(b2)
c2 = Reshape((512,))(c2)
h2 = Dense(1024, activation='relu')(c2)
h3 = Dense(1024, activation='relu')(h2)
h4 = Dense(512, activation='relu')(h3)
# Num-of-signal Classifier
c = Dense(num_classes, activation='sigmoid', name="class_out")(h4)
# Regression of 1-signal
r1 = Dense(regress1, activation='sigmoid', name="regress1_out")(h4)
# Regression of 2-signal
r2 = Dense(regress2, activation='sigmoid', name="regress2_out")(h4)
model = Model(inputs=x, outputs=[c, r1, r2], name="deepaoanet1")
optimizer = keras.optimizers.Adam(learning_rate=0.001, decay=1e-5)
losses = {
"class_out": keras.losses.BinaryCrossentropy(from_logits=False),
"regress1_out": keras.losses.MeanSquaredError(),
"regress2_out": keras.losses.MeanSquaredError(),
}
lossWeights = {"class_out": 0.01, "regress1_out": 1.0, "regress2_out": 1.0}
lossWeights2 = {"class_out": 0.0001, "regress1_out": 1.0, "regress2_out": 1.0}
metrics = {"class_out": 'accuracy', "regress1_out": 'mse', "regress2_out": 'mse'}
model.compile(optimizer=optimizer,
loss=losses,
loss_weights=lossWeights,
metrics=metrics)
model.summary()
return model
def train(X_train_std, yhot_train, X_test_std, yhot_test, epochs=40, batch_size=512):
# Specify Which model?
#model = build_fc0()
#model = build_fc1()
model = build_CR0()
#model = build_CR1()
history = model.fit(X_train_std, yhot_train, batch_size=batch_size, epochs=epochs,
verbose=1, validation_data=(X_test_std, yhot_test))
return model, history