-
Notifications
You must be signed in to change notification settings - Fork 23
/
resunet.py
95 lines (77 loc) · 2.89 KB
/
resunet.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
"""
ResUNet architecture in Keras TensorFlow
"""
import os
import numpy as np
import cv2
import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
class ResUnet:
def __init__(self, input_size=256):
self.input_size = input_size
def build_model(self):
def conv_block(x, n_filter):
x_init = x
## Conv 1
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(n_filter, (1, 1), padding="same")(x)
## Conv 2
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(n_filter, (3, 3), padding="same")(x)
## Conv 3
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(n_filter, (1, 1), padding="same")(x)
## Shortcut
s = Conv2D(n_filter, (1, 1), padding="same")(x_init)
s = BatchNormalization()(s)
## Add
x = Add()([x, s])
return x
def resnet_block(x, n_filter, pool=True):
x1 = conv_block(x, n_filter)
c = x1
## Pooling
if pool == True:
x = MaxPooling2D((2, 2), (2, 2))(x2)
return c, x
else:
return c
n_filters = [16, 32, 64, 96, 128]
inputs = Input((self.input_size, self.input_size, 3))
c0 = inputs
## Encoder
c1, p1 = resnet_block(c0, n_filters[0])
c2, p2 = resnet_block(p1, n_filters[1])
c3, p3 = resnet_block(p2, n_filters[2])
c4, p4 = resnet_block(p3, n_filters[3])
## Bridge
b1 = resnet_block(p4, n_filters[4], pool=False)
b2 = resnet_block(b1, n_filters[4], pool=False)
## Decoder
d1 = Conv2DTranspose(n_filters[3], (3, 3), padding="same", strides=(2, 2))(b2)
#d1 = UpSampling2D((2, 2))(b2)
d1 = Concatenate()([d1, c4])
d1 = resnet_block(d1, n_filters[3], pool=False)
d2 = Conv2DTranspose(n_filters[3], (3, 3), padding="same", strides=(2, 2))(d1)
#d2 = UpSampling2D((2, 2))(d1)
d2 = Concatenate()([d2, c3])
d2 = resnet_block(d2, n_filters[2], pool=False)
d3 = Conv2DTranspose(n_filters[3], (3, 3), padding="same", strides=(2, 2))(d2)
#d3 = UpSampling2D((2, 2))(d2)
d3 = Concatenate()([d3, c2])
d3 = resnet_block(d3, n_filters[1], pool=False)
d4 = Conv2DTranspose(n_filters[3], (3, 3), padding="same", strides=(2, 2))(d3)
#d4 = UpSampling2D((2, 2))(d3)
d4 = Concatenate()([d4, c1])
d4 = resnet_block(d4, n_filters[0], pool=False)
## output
outputs = Conv2D(1, (1, 1), padding="same")(d4)
outputs = BatchNormalization()(outputs)
outputs = Activation("sigmoid")(outputs)
## Model
model = Model(inputs, outputs)
return model