-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_allCNN.py
executable file
·182 lines (146 loc) · 6.96 KB
/
model_allCNN.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# import configuration file
import config
# set random seed
from numpy.random import seed
from tensorflow import set_random_seed
seed(config.fixed_seed)
set_random_seed(config.fixed_seed)
import keras
from keras import Input, Model
from keras.layers import Conv3D, BatchNormalization, Activation, Dropout, GlobalAveragePooling3D
def build_model_allCNN():
"""
Builds a 3D all-CNN model which can be used for AD classification based on MRI.
OUTPUT:
model - the Keras implementation of the all-CNN
"""
# INPUT
input_image = Input(shape=(config.input_shape[0], config.input_shape[1], config.input_shape[2], 1))
# use smaller model for down sampled data
if not config.WB:
# BLOCK 1
x = Conv3D(filters=8, kernel_size=(3, 3, 3), padding='same', kernel_regularizer=config.weight_regularize)(input_image)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=8, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# BLOCK 2
x = Conv3D(filters=16, kernel_size=(3, 3, 3), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=16, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# BLOCK 3
x = Conv3D(filters=24, kernel_size=(3, 3, 3), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=24, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# BLOCK 4
x = Conv3D(filters=32, kernel_size=(3, 3, 3), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# use more blocks and kernels for whole brain data
else:
# BLOCK 1
x = Conv3D(filters=16, kernel_size=(3, 3, 3), padding='same', kernel_regularizer=config.weight_regularize)(input_image)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=16, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# BLOCK 2
x = Conv3D(filters=32, kernel_size=(3, 3, 3), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# BLOCK 3
x = Conv3D(filters=32, kernel_size=(3, 3, 3), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# BLOCK 4
x = Conv3D(filters=64, kernel_size=(3, 3, 3), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=64, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# BLOCK 5
x = Conv3D(filters=64, kernel_size=(3, 3, 3), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=64, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# BLOCK 6
x = Conv3D(filters=32, kernel_size=(3, 3, 3), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# BLOCK 7: 1-by-1 convolutions
x = Conv3D(filters=16, kernel_size=(1, 1, 1), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# pooling
x = Conv3D(filters=16, kernel_size=(1, 1, 1), strides=(2, 2, 2), padding='same', kernel_regularizer=config.weight_regularize)(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# LAST: 1-by-1 convoluation + kernel size of 2
x = Conv3D(filters=2, kernel_size=(1, 1, 1), padding='valid')(x)
x = Dropout(config.dropout)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# gets input of shape (2,2,2,2) and converts this to output with shape (2,)
x = GlobalAveragePooling3D()(x)
# OUTPUT
predictions = Activation('softmax')(x)
model = Model(inputs=input_image, outputs=predictions)
model.compile(loss=keras.losses.binary_crossentropy,
optimizer=keras.optimizers.Adam(lr=config.lr, beta_1=0.9, beta_2=0.999, epsilon=config.epsilon, decay=config.decay, amsgrad=False),
metrics=['accuracy'])
return model