-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
executable file
·141 lines (122 loc) · 6.39 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import tensorflow as tf
import time
from metrics import dice_loss, jaccard_distance_loss
def loss_function_is_dice(loss_function):
return loss_function == 'dice'
def get_loss_function(loss_function):
return dice_loss if loss_function_is_dice(loss_function) else jaccard_distance_loss
def evaluate(end_time, fold, model, x_train, x_val, x_test, y_train, y_val, y_test):
loss_train, dice_train, jaccard_train, precision_train, recall_train = \
model.evaluate(x_train, y_train, verbose=False)
loss_val, dice_val, jaccard_val, precision_val, recall_val = \
model.evaluate(x_val, y_val, verbose=False)
loss_test, dice_test, jaccard_test, precision_test, recall_test = \
model.evaluate(x_test, y_test, verbose=False)
return {
'fold': fold,
'time': str(time.strftime('%H:%M:%S', time.gmtime(end_time))),
'loss_train': loss_train,
'dice_train': dice_train,
'jaccard_train': jaccard_train,
'precision_train': precision_train,
'recall_train': recall_train,
'loss_val': loss_val,
'dice_val': dice_val,
'jaccard_val': jaccard_val,
'precision_val': precision_val,
'recall_val': recall_val,
'loss_test': loss_test,
'dice_test': dice_test,
'jaccard_test': jaccard_test,
'precision_test': precision_test,
'recall_test': recall_test
}
def unet_model(cfg):
input_img = tf.keras.layers.Input((cfg['image_size'], cfg['image_size'], cfg['channel']), name='img')
# Contract #1
c1 = tf.keras.layers.Conv2D(16, (3, 3), kernel_initializer='he_uniform', padding='same')(input_img)
c1 = tf.keras.layers.BatchNormalization()(c1)
c1 = tf.keras.layers.Activation('relu')(c1)
c1 = tf.keras.layers.Dropout(0.1)(c1)
c1 = tf.keras.layers.Conv2D(16, (3, 3), kernel_initializer='he_uniform', padding='same')(c1)
c1 = tf.keras.layers.BatchNormalization()(c1)
c1 = tf.keras.layers.Activation('relu')(c1)
p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)
# Contract #2
c2 = tf.keras.layers.Conv2D(32, (3, 3), kernel_initializer='he_uniform', padding='same')(p1)
c2 = tf.keras.layers.BatchNormalization()(c2)
c2 = tf.keras.layers.Activation('relu')(c2)
c2 = tf.keras.layers.Dropout(0.2)(c2)
c2 = tf.keras.layers.Conv2D(32, (3, 3), kernel_initializer='he_uniform', padding='same')(c2)
c2 = tf.keras.layers.BatchNormalization()(c2)
c2 = tf.keras.layers.Activation('relu')(c2)
p2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)
# Contract #3
c3 = tf.keras.layers.Conv2D(64, (3, 3), kernel_initializer='he_uniform', padding='same')(p2)
c3 = tf.keras.layers.BatchNormalization()(c3)
c3 = tf.keras.layers.Activation('relu')(c3)
c3 = tf.keras.layers.Dropout(0.3)(c3)
c3 = tf.keras.layers.Conv2D(64, (3, 3), kernel_initializer='he_uniform', padding='same')(c3)
c3 = tf.keras.layers.BatchNormalization()(c3)
c3 = tf.keras.layers.Activation('relu')(c3)
p3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)
# Contract #4
c4 = tf.keras.layers.Conv2D(128, (3, 3), kernel_initializer='he_uniform', padding='same')(p3)
c4 = tf.keras.layers.BatchNormalization()(c4)
c4 = tf.keras.layers.Activation('relu')(c4)
c4 = tf.keras.layers.Dropout(0.4)(c4)
c4 = tf.keras.layers.Conv2D(128, (3, 3), kernel_initializer='he_uniform', padding='same')(c4)
c4 = tf.keras.layers.BatchNormalization()(c4)
c4 = tf.keras.layers.Activation('relu')(c4)
p4 = tf.keras.layers.MaxPooling2D((2, 2))(c4)
# Middle
c5 = tf.keras.layers.Conv2D(256, (3, 3), kernel_initializer='he_uniform', padding='same')(p4)
c5 = tf.keras.layers.BatchNormalization()(c5)
c5 = tf.keras.layers.Activation('relu')(c5)
c5 = tf.keras.layers.Dropout(0.5)(c5)
c5 = tf.keras.layers.Conv2D(256, (3, 3), kernel_initializer='he_uniform', padding='same')(c5)
c5 = tf.keras.layers.BatchNormalization()(c5)
c5 = tf.keras.layers.Activation('relu')(c5)
# Expand (upscale) #1
u6 = tf.keras.layers.Conv2DTranspose(128, (3, 3), strides=(2, 2), padding='same')(c5)
u6 = tf.keras.layers.concatenate([u6, c4])
c6 = tf.keras.layers.Conv2D(128, (3, 3), kernel_initializer='he_uniform', padding='same')(u6)
c6 = tf.keras.layers.BatchNormalization()(c6)
c6 = tf.keras.layers.Activation('relu')(c6)
c6 = tf.keras.layers.Dropout(0.5)(c6)
c6 = tf.keras.layers.Conv2D(128, (3, 3), kernel_initializer='he_uniform', padding='same')(c6)
c6 = tf.keras.layers.BatchNormalization()(c6)
c6 = tf.keras.layers.Activation('relu')(c6)
# Expand (upscale) #2
u7 = tf.keras.layers.Conv2DTranspose(64, (3, 3), strides=(2, 2), padding='same')(c6)
u7 = tf.keras.layers.concatenate([u7, c3])
c7 = tf.keras.layers.Conv2D(64, (3, 3), kernel_initializer='he_uniform', padding='same')(u7)
c7 = tf.keras.layers.BatchNormalization()(c7)
c7 = tf.keras.layers.Activation('relu')(c7)
c7 = tf.keras.layers.Dropout(0.5)(c7)
c7 = tf.keras.layers.Conv2D(64, (3, 3), kernel_initializer='he_uniform', padding='same')(c7)
c7 = tf.keras.layers.BatchNormalization()(c7)
c7 = tf.keras.layers.Activation('relu')(c7)
# Expand (upscale) #3
u8 = tf.keras.layers.Conv2DTranspose(32, (3, 3), strides=(2, 2), padding='same')(c7)
u8 = tf.keras.layers.concatenate([u8, c2])
c8 = tf.keras.layers.Conv2D(32, (3, 3), kernel_initializer='he_uniform', padding='same')(u8)
c8 = tf.keras.layers.BatchNormalization()(c8)
c8 = tf.keras.layers.Activation('relu')(c8)
c8 = tf.keras.layers.Dropout(0.5)(c8)
c8 = tf.keras.layers.Conv2D(32, (3, 3), kernel_initializer='he_uniform', padding='same')(c8)
c8 = tf.keras.layers.BatchNormalization()(c8)
c8 = tf.keras.layers.Activation('relu')(c8)
# Expand (upscale) #4
u9 = tf.keras.layers.Conv2DTranspose(16, (3, 3), strides=(2, 2), padding='same')(c8)
u9 = tf.keras.layers.concatenate([u9, c1])
c9 = tf.keras.layers.Conv2D(16, (3, 3), kernel_initializer='he_uniform', padding='same')(u9)
c9 = tf.keras.layers.BatchNormalization()(c9)
c9 = tf.keras.layers.Activation('relu')(c9)
c9 = tf.keras.layers.Dropout(0.5)(c9)
c9 = tf.keras.layers.Conv2D(16, (3, 3), kernel_initializer='he_uniform', padding='same')(c9)
c9 = tf.keras.layers.BatchNormalization()(c9)
c9 = tf.keras.layers.Activation('relu')(c9)
output = tf.keras.layers.Conv2D(1, (1, 1), activation='sigmoid')(c9)
model = tf.keras.Model(inputs=[input_img], outputs=[output])
return model