-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmodel.py
71 lines (43 loc) · 1.62 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
from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, Activation, MaxPool2D, UpSampling2D, Concatenate
from tensorflow.keras.models import Model
def conv_block(inputs, filters, pool=True):
x = Conv2D(filters, 3, padding="same")(inputs)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(filters, 3, padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
if pool == True:
p = MaxPool2D((2, 2))(x)
return x, p
else:
return x
def build_unet(shape, num_classes):
inputs = Input(shape)
""" Encoder """
x1, p1 = conv_block(inputs, 16, pool=True)
x2, p2 = conv_block(p1, 32, pool=True)
x3, p3 = conv_block(p2, 48, pool=True)
x4, p4 = conv_block(p3, 64, pool=True)
""" Bridge """
b1 = conv_block(p4, 128, pool=False)
""" Decoder """
u1 = UpSampling2D((2, 2), interpolation="bilinear")(b1)
c1 = Concatenate()([u1, x4])
x5 = conv_block(c1, 64, pool=False)
u2 = UpSampling2D((2, 2), interpolation="bilinear")(x5)
c2 = Concatenate()([u2, x3])
x6 = conv_block(c2, 48, pool=False)
u3 = UpSampling2D((2, 2), interpolation="bilinear")(x6)
c3 = Concatenate()([u3, x2])
x7 = conv_block(c3, 32, pool=False)
u4 = UpSampling2D((2, 2), interpolation="bilinear")(x7)
c4 = Concatenate()([u4, x1])
x8 = conv_block(c4, 16, pool=False)
""" Output layer """
output = Conv2D(num_classes, 1, padding="same", activation="softmax")(x8)
return Model(inputs, output)
if __name__ == "__main__":
model = build_unet((256, 256, 3), 10)
model.summary()
##