-
Notifications
You must be signed in to change notification settings - Fork 2
/
resUnet.py
111 lines (89 loc) · 5.05 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
def batch_Norm_Activation(x, BN=False): ## To Turn off Batch Normalization, Change BN to False >
if BN == True:
x = BatchNormalization()(x)
x = Activation("relu")(x)
else:
x= Activation("relu")(x)
return x
def ResUnet2D(filters):
# encoder
inputs = Input(shape=(image_dims[0], image_dims[1], 3))
conv = Conv2D(filters*1, kernel_size= (3,3), padding= 'same', strides= (1,1))(inputs)
conv = batch_Norm_Activation(conv)
conv = Conv2D(filters*1, kernel_size= (3,3), padding= 'same', strides= (1,1))(conv)
shortcut = Conv2D(filters*1, kernel_size=(1,1), padding='same', strides=(1,1))(inputs)
shortcut = batch_Norm_Activation(shortcut)
output1 = add([conv, shortcut])
res1 = batch_Norm_Activation(output1)
res1 = Conv2D(filters*2, kernel_size= (3,3), padding= 'same', strides= (2,2))(res1)
res1 = batch_Norm_Activation(res1)
res1 = Conv2D(filters*2, kernel_size= (3,3), padding= 'same', strides= (1,1))(res1)
shortcut1 = Conv2D(filters*2, kernel_size= (3,3), padding='same', strides=(2,2))(output1)
shortcut1 = batch_Norm_Activation(shortcut1)
output2 = add([shortcut1, res1])
res2 = batch_Norm_Activation(output2)
res2 = Conv2D(filters*4, kernel_size= (3,3), padding= 'same', strides= (2,2))(res2)
res2 = batch_Norm_Activation(res2)
res2 = Conv2D(filters*4, kernel_size= (3,3), padding= 'same', strides= (1,1))(res2)
shortcut2 = Conv2D(filters*4, kernel_size= (3,3), padding='same', strides=(2,2))(output2)
shortcut2 = batch_Norm_Activation(shortcut2)
output3 = add([shortcut2, res2])
res3 = batch_Norm_Activation(output3)
res3 = Conv2D(filters*8, kernel_size= (3,3), padding= 'same', strides= (2,2))(res3)
res3 = batch_Norm_Activation(res3)
res3 = Conv2D(filters*8, kernel_size= (3,3), padding= 'same', strides= (1,1))(res3)
shortcut3 = Conv2D(filters*8, kernel_size= (3,3), padding='same', strides=(2,2))(output3)
shortcut3 = batch_Norm_Activation(shortcut3)
output4 = add([shortcut3, res3])
res4 = batch_Norm_Activation(output4)
res4 = Conv2D(filters*16, kernel_size= (3,3), padding= 'same', strides= (2,2))(res4)
res4 = batch_Norm_Activation(res4)
res4 = Conv2D(filters*16, kernel_size= (3,3), padding= 'same', strides= (1,1))(res4)
shortcut4 = Conv2D(filters*16, kernel_size= (3,3), padding='same', strides=(2,2))(output4)
shortcut4 = batch_Norm_Activation(shortcut4)
output5 = add([shortcut4, res4])
#bridge
conv = batch_Norm_Activation(output5)
conv = Conv2D(filters*16, kernel_size= (3,3), padding= 'same', strides= (1,1))(conv)
conv = batch_Norm_Activation(conv)
conv = Conv2D(filters*16, kernel_size= (3,3), padding= 'same', strides= (1,1))(conv)
#decoder
uconv1 = UpSampling2D((2,2))(conv)
uconv1 = concatenate([uconv1, output4])
uconv11 = batch_Norm_Activation(uconv1)
uconv11 = Conv2D(filters*16, kernel_size= (3,3), padding= 'same', strides=(1,1))(uconv11)
uconv11 = batch_Norm_Activation(uconv11)
uconv11 = Conv2D(filters*16, kernel_size= (3,3), padding= 'same', strides=(1,1))(uconv11)
shortcut5 = Conv2D(filters*16, kernel_size= (3,3), padding='same', strides=(1,1))(uconv1)
shortcut5 = batch_Norm_Activation(shortcut5)
output6 = add([uconv11,shortcut5])
uconv2 = UpSampling2D((2,2))(output6)
uconv2 = concatenate([uconv2, output3])
uconv22 = batch_Norm_Activation(uconv2)
uconv22 = Conv2D(filters*8, kernel_size= (3,3), padding= 'same', strides=(1,1))(uconv22)
uconv22 = batch_Norm_Activation(uconv22)
uconv22 = Conv2D(filters*8, kernel_size= (3,3), padding= 'same', strides=(1,1))(uconv22)
shortcut6 = Conv2D(filters*8, kernel_size= (3,3), padding='same', strides=(1,1))(uconv2)
shortcut6 = batch_Norm_Activation(shortcut6)
output7 = add([uconv22,shortcut6])
uconv3 = UpSampling2D((2,2))(output7)
uconv3 = concatenate([uconv3, output2])
uconv33 = batch_Norm_Activation(uconv3)
uconv33 = Conv2D(filters*4, kernel_size= (3,3), padding= 'same', strides=(1,1))(uconv33)
uconv33 = batch_Norm_Activation(uconv33)
uconv33 = Conv2D(filters*4, kernel_size= (3,3), padding= 'same', strides=(1,1))(uconv33)
shortcut7 = Conv2D(filters*4, kernel_size= (3,3), padding='same', strides=(1,1))(uconv3)
shortcut7 = batch_Norm_Activation(shortcut7)
output8 = add([uconv33,shortcut7])
uconv4 = UpSampling2D((2,2))(output8)
uconv4 = concatenate([uconv4, output1])
uconv44 = batch_Norm_Activation(uconv4)
uconv44 = Conv2D(filters*2, kernel_size= (3,3), padding= 'same', strides=(1,1))(uconv44)
uconv44 = batch_Norm_Activation(uconv44)
uconv44 = Conv2D(filters*2, kernel_size= (3,3), padding= 'same', strides=(1,1))(uconv44)
shortcut8 = Conv2D(filters*2, kernel_size= (3,3), padding='same', strides=(1,1))(uconv4)
shortcut8 = batch_Norm_Activation(shortcut8)
output9 = add([uconv44,shortcut8])
output_layer = Conv2D(4, (1, 1), padding="same", activation="softmax")(output9)
model = Model(inputs, output_layer)
return model