-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtrain_cnn.py
173 lines (136 loc) · 5.52 KB
/
train_cnn.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
from os.path import join
import keras.backend as K
from keras.callbacks import ModelCheckpoint
from keras.layers import Conv2D, Dense, Activation, MaxPooling2D
from keras.layers import Flatten, BatchNormalization, Dropout
from keras.layers.advanced_activations import PReLU
from keras.models import Sequential, load_model
from keras.optimizers import Adam
from keras.regularizers import l2
from data_provider import MODELS_DIR
from data_provider import load_organized_data_info
from data_provider import train_val_dirs_generators
IMGS_DIM_3D = (3, 256, 256)
CNN_MODEL_FILE = join(MODELS_DIR, 'cnn.h5')
MAX_EPOCHS = 500
BATCH_SIZE = 96
L2_REG = 0.003
W_INIT = 'he_normal'
LAST_FEATURE_MAPS_LAYER = 46
LAST_FEATURE_MAPS_SIZE = (128, 8, 8)
PENULTIMATE_LAYER = 51
PENULTIMATE_SIZE = 2048
SOFTMAX_LAYER = 55
SOFTMAX_SIZE = 1584
def _train_model():
data_info = load_organized_data_info(IMGS_DIM_3D[1])
dir_tr = data_info['dir_tr']
dir_val = data_info['dir_val']
gen_tr, gen_val = train_val_dirs_generators(BATCH_SIZE, dir_tr, dir_val)
model = _cnn(IMGS_DIM_3D)
model.fit_generator(
generator=gen_tr,
nb_epoch=MAX_EPOCHS,
samples_per_epoch=data_info['num_tr'],
validation_data=gen_val,
nb_val_samples=data_info['num_val'],
callbacks=[ModelCheckpoint(CNN_MODEL_FILE, save_best_only=True)],
verbose=2)
def _cnn(imgs_dim, compile_=True):
model = Sequential()
model.add(_convolutional_layer(nb_filter=16, input_shape=imgs_dim))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(_convolutional_layer(nb_filter=16))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(_convolutional_layer(nb_filter=32))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(_convolutional_layer(nb_filter=32))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(_convolutional_layer(nb_filter=32))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(_convolutional_layer(nb_filter=64))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(_convolutional_layer(nb_filter=64))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(_convolutional_layer(nb_filter=64))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(_convolutional_layer(nb_filter=128))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(_convolutional_layer(nb_filter=128))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(_convolutional_layer(nb_filter=128))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(_convolutional_layer(nb_filter=256))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(_convolutional_layer(nb_filter=256))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(_convolutional_layer(nb_filter=256))
model.add(BatchNormalization(axis=1, mode=2))
model.add(PReLU(init=W_INIT))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(p=0.5))
model.add(Flatten())
model.add(_dense_layer(output_dim=PENULTIMATE_SIZE))
model.add(BatchNormalization(mode=2))
model.add(PReLU(init=W_INIT))
if compile_:
model.add(Dropout(p=0.5))
model.add(_dense_layer(output_dim=SOFTMAX_SIZE))
model.add(BatchNormalization(mode=2))
model.add(Activation(activation='softmax'))
return compile_model(model)
return model
def _convolutional_layer(nb_filter, input_shape=None):
if input_shape:
return _first_convolutional_layer(nb_filter, input_shape)
else:
return _intermediate_convolutional_layer(nb_filter)
def _first_convolutional_layer(nb_filter, input_shape):
return Conv2D(
nb_filter=nb_filter, nb_row=3, nb_col=3, input_shape=input_shape,
border_mode='same', init=W_INIT, W_regularizer=l2(l=L2_REG))
def _intermediate_convolutional_layer(nb_filter):
return Conv2D(
nb_filter=nb_filter, nb_row=3, nb_col=3, border_mode='same',
init=W_INIT, W_regularizer=l2(l=L2_REG))
def _dense_layer(output_dim):
return Dense(output_dim=output_dim, W_regularizer=l2(l=L2_REG), init=W_INIT)
def compile_model(model):
adam = Adam(lr=0.000074)
model.compile(
loss='categorical_crossentropy',
optimizer=adam,
metrics=['accuracy'])
return model
def load_trained_cnn_feature_maps_layer(model_path):
return _load_trained_cnn_layer(model_path, LAST_FEATURE_MAPS_LAYER)
def load_trained_cnn_penultimate_layer(model_path):
return _load_trained_cnn_layer(model_path, PENULTIMATE_LAYER)
def load_trained_cnn_softmax_layer(model_path):
return _load_trained_cnn_layer(model_path, SOFTMAX_LAYER)
def _load_trained_cnn_layer(model_path, layer_index):
model = load_model(model_path)
dense_output = K.function(
[model.layers[0].input, K.learning_phase()],
[model.layers[layer_index].output])
# output in test mode = 0
return lambda X: dense_output([X, 0])[0]
if __name__ == '__main__':
_train_model()