-
Notifications
You must be signed in to change notification settings - Fork 2
/
DoubleUnet.py
173 lines (134 loc) · 5.23 KB
/
DoubleUnet.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import numpy as np
import cv2
import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Conv2D, Conv2DTranspose
from tensorflow.keras.layers import MaxPooling2D, UpSampling2D, Conv2DTranspose
from tensorflow.keras.layers import concatenate
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras import backend as K
from tensorflow.keras import layers
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Input
from keras.applications.vgg19 import VGG19
from keras.utils.generic_utils import get_custom_objects
# from kaggle_datasets import KaggleDatasets
# from kaggle_secrets import UserSecretsClient
from sklearn.model_selection import train_test_split
from tensorflow.keras.losses import binary_crossentropy
# import plotly
# import plotly.graph_objs as go
import numpy as np # So we can use random numbers in examples
# np.random.seed(13)
# tf.random.set_seed(13)
"""
Double U-Net architecture in Keras TensorFlow
"""
# This Python 3 environment comes with many helpful analytics libraries installed
def squeeze_excite_block(inputs, ratio=8):
init = inputs
channel_axis = -1
filters = init.shape[channel_axis]
se_shape = (1, 1, filters)
se = GlobalAveragePooling2D()(init)
se = Reshape(se_shape)(se)
se = Dense(filters // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se)
se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se)
x = Multiply()([init, se])
return x
def conv_block(inputs, filters):
x = inputs
x = Conv2D(filters, (3, 3), padding="same")(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(filters, (3, 3), padding="same")(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = squeeze_excite_block(x)
return x
def encoder1(inputs):
skip_connections = []
model = VGG19(include_top=False, weights='imagenet', input_tensor=inputs)
names = ["block1_conv2", "block2_conv2", "block3_conv4", "block4_conv4"]
for name in names:
skip_connections.append(model.get_layer(name).output)
output = model.get_layer("block5_conv4").output
return output, skip_connections
def decoder1(inputs, skip_connections):
num_filters = [256, 128, 64, 32]
skip_connections.reverse()
x = inputs
shape = x.shape
for i, f in enumerate(num_filters):
x = Conv2DTranspose(shape[3], (2, 2), activation="relu", strides=(2, 2))(x)
x = Concatenate()([x, skip_connections[i]])
x = conv_block(x, f)
return x
def encoder2(inputs):
num_filters = [32, 64, 128, 256]
skip_connections = []
x = inputs
for i, f in enumerate(num_filters):
x = conv_block(x, f)
skip_connections.append(x)
x = MaxPool2D((2, 2))(x)
return x, skip_connections
def decoder2(inputs, skip_1, skip_2):
num_filters = [256, 128, 64, 32]
skip_2.reverse()
x = inputs
shape = x.shape
for i, f in enumerate(num_filters):
x = Conv2DTranspose(shape[3], (2, 2), activation="relu", strides=(2, 2))(x)
x = Concatenate()([x, skip_1[i], skip_2[i]])
x = conv_block(x, f)
return x
def output_block(inputs):
x = Conv2D(1, (1, 1), padding="same")(inputs)
x = Activation('sigmoid')(x)
return x
def ASPP(x, filter):
shape = x.shape
y1 = AveragePooling2D(pool_size=(shape[1], shape[2]))(x)
y1 = Conv2D(filter, 1, padding="same")(y1)
y1 = BatchNormalization()(y1)
y1 = Activation("relu")(y1)
shape2 = y1.shape
y1 = Conv2DTranspose(shape2[3], (8,8), activation="relu", strides=(shape[1], shape[2]))(y1)
y2 = Conv2D(filter, 1, dilation_rate=1, padding="same", use_bias=False)(x)
y2 = BatchNormalization()(y2)
y2 = Activation("relu")(y2)
y3 = Conv2D(filter, 3, dilation_rate=6, padding="same", use_bias=False)(x)
y3 = BatchNormalization()(y3)
y3 = Activation("relu")(y3)
y4 = Conv2D(filter, 3, dilation_rate=12, padding="same", use_bias=False)(x)
y4 = BatchNormalization()(y4)
y4 = Activation("relu")(y4)
y5 = Conv2D(filter, 3, dilation_rate=18, padding="same", use_bias=False)(x)
y5 = BatchNormalization()(y5)
y5 = Activation("relu")(y5)
y = Concatenate()([y1, y2, y3, y4, y5])
y = Conv2D(filter, 1, dilation_rate=1, padding="same", use_bias=False)(y)
y = BatchNormalization()(y)
y = Activation("relu")(y)
return y
def build_model():
inputs = Input((256,256, 3))
x, skip_1 = encoder1(inputs)
x = ASPP(x, 64)
x = decoder1(x, skip_1)
outputs1 = output_block(x)
x = inputs * outputs1
x, skip_2 = encoder2(x)
x = ASPP(x, 64)
x = decoder2(x, skip_1, skip_2)
outputs2 = output_block(x)
outputs = Concatenate()([outputs1, outputs2])
combine_output = Conv2D(1, (64, 64), activation="sigmoid", padding="same")(outputs)
model = Model(inputs, combine_output)
return model
model = build_model()
model.summary()