-
Notifications
You must be signed in to change notification settings - Fork 0
/
unet.py
56 lines (42 loc) · 1.25 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
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 UNet(H, W):
num_filters = [32, 64, 128, 256]
inputs = Input((H, W, 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] * 2)
num_filters.reverse()
skip_x.reverse()
## Decoder
for i, f in enumerate(num_filters):
x = UpSampling2D((2, 2), interpolation='bilinear')(x)
xs = skip_x[i]
try:
x = Concatenate()([x, xs])
except Exception as e:
x = ZeroPadding2D(((1,0),(0,0)))(x)
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 = UNet(360, 640)
model.summary()