-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
126 lines (91 loc) · 3.67 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
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
import keras
from keras import Sequential
from keras.layers import Conv2D, MaxPooling2D, UpSampling2D, Dropout
from keras.initializers import HeNormal
from keras.layers import BatchNormalization
from keras.layers import MaxPooling2D
from keras.layers import BatchNormalization
def build_model():
"""Creates and returns a model described above
Returns
-------
keras.Model
The Sequential model created
"""
model = Sequential()
# -------------------------------------Abstraction path:-------------------------------------
model.add(Conv2D(32, (3,3), activation='relu', padding='same', kernel_initializer=HeNormal(), input_shape=(64,64,1)))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(Conv2D(32, (3,3), activation='relu', padding='same', kernel_initializer=HeNormal()))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(Conv2D(64, (3,3), activation='relu', padding='same', kernel_initializer=HeNormal()))
model.add(BatchNormalization())
model.add(MaxPooling2D((2,2)))
model.add(Dropout(0.3))
# -------------------------------------Bottleneck path:-------------------------------------
model.add(Conv2D(64, (3,3), activation='relu', padding='same', kernel_initializer=HeNormal()))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(Conv2D(64, (3,3), activation='relu', padding='same', kernel_initializer=HeNormal()))
model.add(BatchNormalization())
model.add(Dropout(0.3))
# -------------------------------------Expansion path:-------------------------------------
model.add(UpSampling2D((2,2), interpolation="bilinear"))
model.add(Conv2D(64, (3,3), activation='relu', padding='same', kernel_initializer=HeNormal()))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(Conv2D(32, (3,3), activation='relu', padding='same', kernel_initializer=HeNormal()))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(Conv2D(32, (3,3), activation='relu', padding='same', kernel_initializer=HeNormal()))
model.add(BatchNormalization())
model.add(Dropout(0.3))
# -------------------------------------Final layer (FIXED):-------------------------------------
model.add(Conv2D(filters=1, kernel_size=(1,1), activation='sigmoid'))
model.build((None, 64,64,1))
return model
def compile_model(model, lr):
"""Compile a model according to the description above
Parameters
----------
model : keras.Model
The model to compile
lr : float
The learning ratea
Returns
-------
"""
optimizer = keras.optimizers.Adam(learning_rate=lr)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
def train_model(model, epochs, x_train, y_train, x_val, y_val):
"""Train the model according to the description above.
Parameters
----------
model : keras.Model
The model to train
epochs : int
The number of epochs to train
x_train : np.ndarray
y_train : np.ndarray
x_val : np.ndarray
y_val : np.ndarray
Returns
-------
"""
return model.fit(x_train, y_train, epochs=epochs, batch_size=16, validation_data=(x_val, y_val), validation_batch_size=16)
def predict_model(model, x_val):
"""Predict the segmentation masks of the validation data.
Parameters
----------
model : keras.Model
The model to evaluate
x_val : np.ndarray
Returns
----------
np.ndarray
Predicted segmentation array on validation data
"""
val_preds = model.predict(x_val, batch_size=16).reshape(x_val.shape)
return val_preds