-
Notifications
You must be signed in to change notification settings - Fork 4
/
models.py
145 lines (114 loc) · 5.22 KB
/
models.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 26 13:57:10 2018
@author: amr
"""
from keras.models import Sequential, Model
from keras.layers import Dense, LSTM, GRU, Input
from keras.layers import Conv1D, Conv2D, MaxPooling2D, AveragePooling2D, AveragePooling1D, GlobalAveragePooling2D
from keras.layers import Activation, Dropout, Flatten, Dense, Reshape, Merge, SeparableConv2D
from keras.layers import PReLU
from keras.layers.merge import average, concatenate, add
from keras.layers.normalization import BatchNormalization
from keras.layers.wrappers import TimeDistributed, Bidirectional
from keras.layers.local import LocallyConnected1D
from keras.optimizers import Adam, RMSprop, SGD
from keras.callbacks import Callback, TensorBoard, EarlyStopping, LearningRateScheduler, ModelCheckpoint, ReduceLROnPlateau
from keras.constraints import maxnorm
from keras.utils import plot_model
from keras import regularizers
from keras.regularizers import l2, l1
import keras.backend as K
def branched2(data_shape, model_config={'bn':True, 'dropout':True, 'branched':True, 'deep':True, 'nonlinear':'tanh'}, f=1):
timepoints = data_shape[1]
channels = data_shape[2]
reg = 0.01
input_data = Input(shape=(timepoints, channels, 1))
spatial_conv = Conv2D(12, (1,channels), padding='valid', kernel_regularizer=l2(reg))(input_data)
if model_config['bn']:
spatial_conv = BatchNormalization()(spatial_conv)
spatial_conv = Activation(model_config['nonlinear'])(spatial_conv)
if model_config['dropout']:
spatial_conv = Dropout(0.5)(spatial_conv)
if model_config['branched']:
branch1 = Conv2D(4, (21*f,1), padding='valid', kernel_regularizer=l2(reg))(spatial_conv)
if model_config['bn']:
branch1 = BatchNormalization()(branch1)
branch1 = Activation(model_config['nonlinear'])(branch1)
branch1 = MaxPooling2D(pool_size=(3,1), strides=(3,1))(branch1)
if model_config['dropout']:
branch1 = Dropout(0.5)(branch1)
branch1 = Flatten()(branch1)
branch2 = Conv2D(4, (5*f,1), padding='valid', dilation_rate=(1,1), kernel_regularizer=l2(reg))(spatial_conv)
if model_config['bn']:
branch2 = BatchNormalization()(branch2)
branch2 = Activation(model_config['nonlinear'])(branch2)
branch2 = MaxPooling2D(pool_size=(3,1), strides=(3,1))(branch2)
if model_config['dropout']:
branch2 = Dropout(0.5)(branch2)
if model_config['deep']:
branch2 = Conv2D(8, (5*f,1), padding='valid', dilation_rate=(1,1), kernel_regularizer=l2(reg))(branch2)
if model_config['bn']:
branch2 = BatchNormalization()(branch2)
branch2 = Activation(model_config['nonlinear'])(branch2)
if model_config['dropout']:
branch2 = Dropout(0.5)(branch2)
#
branch2 = Conv2D(8, (5*f,1), padding='valid', kernel_regularizer=l2(reg))(branch2)
if model_config['bn']:
branch2 = BatchNormalization()(branch2)
branch2 = Activation(model_config['nonlinear'])(branch2)
branch2 = MaxPooling2D(pool_size=(2,1), strides=(2,1))(branch2)
if model_config['dropout']:
branch2 = Dropout(0.5)(branch2)
#
branch2 = Flatten()(branch2)
if model_config['branched']:
merged = concatenate([branch1, branch2])
dense = Dense(1, activation='sigmoid', kernel_regularizer=l2(reg))(merged)
else:
dense = Dense(1, activation='sigmoid', kernel_regularizer=l2(reg))(branch2)
model = Model(inputs = [input_data], outputs=[dense])
return model
def create_cnn(data_shape, f=1):
timepoints = data_shape[1]
channels = data_shape[2]
kernel_size = 3*f
model = Sequential()
model.add(Conv2D(4, (kernel_size,3), activation='tanh', input_shape=(timepoints, channels, 1)))
model.add(BatchNormalization())
model.add(MaxPooling2D((2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(8, (kernel_size,3), strides=(1,1), activation='tanh', padding='valid'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2,2)))
# model.add(Dropout(0.25))
model.add(Conv2D(16, (kernel_size,3), strides=(1,1), activation='tanh', padding='valid'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2,2)))
# model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
return model
def create_eegnet(data_shape, f=1):
timepoints = data_shape[1]
channels = data_shape[2]
spatial_filters = 16
model = Sequential()
model.add(Conv2D(spatial_filters, (1,channels), activation='relu',
kernel_regularizer=regularizers.l1_l2(0.0001), input_shape=(timepoints, channels, 1)))
model.add(BatchNormalization())
model.add(Dropout(0.25))
model.add(Reshape((timepoints,spatial_filters,1)))
model.add(Conv2D(4, (16*f,2), strides=(1,1), activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D((4,2)))
model.add(Dropout(0.25))
model.add(Conv2D(4, (2*f,8), strides=(1,1), activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D((4,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
return model