-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
77 lines (66 loc) · 2.49 KB
/
model.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
"""
Definition of the model used by `train.py`.
"""
from keras.layers import Activation, BatchNormalization, Conv2D, Cropping2D, Dense, Dropout, Flatten, Lambda, \
MaxPooling2D
from keras.models import Sequential
from keras.regularizers import l2
def create_model(dropout_rate=None, l2_weight=None, batch_norm=False):
"""
Returns a Keras sequential model with normalization as specified applied.
:param dropout_rate: Dropout rate to use on every layer. Set to `None` if you don't want to apply.
:param l2_weight: L2 normalization weight to apply all weights. Set to `None` if you don't want to apply.
:param batch_norm: Set `True` to apply batch normalization.
:return: a Keras sequential model.
"""
model = Sequential()
if l2_weight is None:
L2_reg = None
else:
L2_reg = l2(l2_weight)
# Pre-processing
model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3)))
model.add(Cropping2D(cropping=((70, 25), (0, 0))))
# Convolution 1
kernel_size = (5, 5)
model.add(Conv2D(64, kernel_size, padding='same', kernel_regularizer=L2_reg))
if batch_norm:
model.add(BatchNormalization())
model.add(Activation('elu'))
if dropout_rate is not None:
model.add(Dropout(dropout_rate))
model.add(MaxPooling2D(pool_size=(3, 3)))
# Convolution 2
model.add(Conv2D(128, kernel_size, padding='same', kernel_regularizer=L2_reg))
if batch_norm:
model.add(BatchNormalization())
model.add(Activation('elu'))
if dropout_rate is not None:
model.add(Dropout(dropout_rate))
model.add(MaxPooling2D(pool_size=(3, 3)))
# Convolution 3
model.add(Conv2D(256, kernel_size, padding='same', kernel_regularizer=L2_reg))
if batch_norm:
model.add(BatchNormalization())
model.add(Activation('elu'))
if dropout_rate is not None:
model.add(Dropout(dropout_rate))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Flatten())
# Fully Connected 1
model.add(Dense(512, kernel_regularizer=L2_reg))
if batch_norm:
model.add(BatchNormalization())
model.add(Activation('elu'))
if dropout_rate is not None:
model.add(Dropout(dropout_rate))
# Fully Connected 2
model.add(Dense(256, kernel_regularizer=L2_reg))
if batch_norm:
model.add(BatchNormalization())
model.add(Activation('elu'))
if dropout_rate is not None:
model.add(Dropout(dropout_rate))
# Output layer
model.add(Dense(1))
return model