-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
133 lines (97 loc) · 3.88 KB
/
models.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
from keras.models import Model
from keras.optimizers import *
from model_utils import input_tensor, single_conv, double_conv, deconv, \
pooling, merge, callback, dice_coef, iou, dice_coef_loss
class UNet(Model):
def __init__(
self,
input_size,
n_filters,
pretrained_weights=None
):
# input
input = input_tensor(input_size)
# contracting path
conv1 = double_conv(input, n_filters * 1)
pool1 = pooling(conv1)
conv2 = double_conv(pool1, n_filters * 2)
pool2 = pooling(conv2)
conv3 = double_conv(pool2, n_filters * 4)
pool3 = pooling(conv3)
conv4 = double_conv(pool3, n_filters * 8)
pool4 = pooling(conv4)
conv5 = double_conv(pool4, n_filters * 16)
# expansive path
up6 = deconv(conv5, n_filters * 8)
up6 = merge(conv4, up6)
conv6 = double_conv(up6, n_filters * 8)
up7 = deconv(conv6, n_filters * 4)
up7 = merge(conv3, up7)
conv7 = double_conv(up7, n_filters * 4)
up8 = deconv(conv7, n_filters * 2)
up8 = merge(conv2, up8)
conv8 = double_conv(up8, n_filters * 2)
up9 = deconv(conv8, n_filters * 1)
up9 = merge(conv1, up9)
conv9 = double_conv(up9, n_filters * 1)
# output
output = single_conv(conv9, 1, 1)
super(UNet, self).__init__(inputs=input, outputs=output)
if pretrained_weights:
self.load_weights(pretrained_weights)
def build(self, learning_rate, EPOCHS):
decay_rate = learning_rate / EPOCHS
opt = Adam(learning_rate=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=None, decay=decay_rate, amsgrad=False)
self.compile(optimizer=opt, loss=dice_coef_loss, metrics=[iou, dice_coef])
self.summary()
def save_model(self, name):
self.save_weights(name)
@staticmethod
def checkpoint(name):
return callback(name)
class UNet1(Model):
def __init__(
self,
input_size,
n_filters,
pretrained_weights=None
):
# input
input = input_tensor(input_size)
# contracting path
conv1 = double_conv(input, n_filters * 1, batch_norm=True)
pool1 = pooling(conv1, drop=True)
conv2 = double_conv(pool1, n_filters * 2, batch_norm=True)
pool2 = pooling(conv2, drop=True)
conv3 = double_conv(pool2, n_filters * 4, batch_norm=True)
pool3 = pooling(conv3, drop=True)
conv4 = double_conv(pool3, n_filters * 8, batch_norm=True)
pool4 = pooling(conv4, drop=True)
conv5 = double_conv(pool4, n_filters * 16, batch_norm=True)
# expansive path
up6 = deconv(conv5, n_filters * 8)
up6 = merge(conv4, up6)
conv6 = double_conv(up6, n_filters * 8, batch_norm=True)
up7 = deconv(conv6, n_filters * 4)
up7 = merge(conv3, up7)
conv7 = double_conv(up7, n_filters * 4, batch_norm=True)
up8 = deconv(conv7, n_filters * 2)
up8 = merge(conv2, up8)
conv8 = double_conv(up8, n_filters * 2, batch_norm=True)
up9 = deconv(conv8, n_filters * 1)
up9 = merge(conv1, up9)
conv9 = double_conv(up9, n_filters * 1, batch_norm=True)
output = single_conv(conv9, 1, 1)
super(UNet1, self).__init__(inputs=input, outputs=output)
if pretrained_weights:
self.load_weights(pretrained_weights)
def build(self, learning_rate, EPOCHS):
decay_rate = learning_rate / EPOCHS
opt = Adam(learning_rate=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=None, decay=decay_rate, amsgrad=False)
self.compile(optimizer=opt, loss=dice_coef_loss, metrics=[iou, dice_coef])
self.summary()
def save_model(self, name):
self.save_weights(name)
@staticmethod
def checkpoint(name):
return callback(name)