-
Notifications
You must be signed in to change notification settings - Fork 2
/
UNet.py
63 lines (43 loc) · 1.27 KB
/
UNet.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
"""
UNet architecture in Keras TensorFlow
"""
import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
def conv_block(x, num_filters):
x = Conv2D(num_filters, (3, 3), padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(num_filters, (3, 3), padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
return x
def build_model():
size = 256
# num_filters = [16, 32, 48, 64]
num_filters = [8, 16, 32]
inputs = Input((size, size, 3))
skip_x = []
x = inputs
## Encoder
for f in num_filters:
x = conv_block(x, f)
skip_x.append(x)
x = MaxPool2D((2, 2))(x)
## Bridge
x = conv_block(x, num_filters[-1])
num_filters.reverse()
skip_x.reverse()
## Decoder
for i, f in enumerate(num_filters):
x = UpSampling2D((2, 2))(x)
xs = skip_x[i]
x = Concatenate()([x, xs])
x = conv_block(x, f)
## Output
x = Conv2D(1, (1, 1), padding="same")(x)
x = Activation("sigmoid")(x)
return Model(inputs, x)
if __name__ == "__main__":
model = build_model()
model.summary()