-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
152 lines (129 loc) · 6.52 KB
/
model.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
import tensorflow as tf
def down_sampling(x, filters, kernel_size=3, dropout_rate=0.1, use_maxpool = True):
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=kernel_size, activation=None, padding='same')(x)
x = tf.keras.layers.Dropout(rate=dropout_rate)(x)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=kernel_size, activation=None, padding='same')(x)
x = tf.keras.layers.Dropout(rate=dropout_rate)(x)
x = tf.keras.layers.ReLU()(x)
if use_maxpool:
return tf.keras.layers.MaxPooling2D(pool_size=2)(x), x
else:
return x
def up_sampling(x, y, filters,dropout_rate=0.2):
x = tf.keras.layers.Conv2DTranspose(filters=filters, kernel_size=2, activation=None, strides=2, padding='same')(x)
x = tf.keras.layers.Dropout(rate=dropout_rate)(x)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.concatenate([x, y])
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=3, activation=None, padding='same')(x)
x = tf.keras.layers.Dropout(rate=dropout_rate)(x)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=3, activation=None, padding='same')(x)
x = tf.keras.layers.Dropout(rate=dropout_rate)(x)
x = tf.keras.layers.ReLU()(x)
return x
def down_sampling_gn(x, filters, halving=True):
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=3, activation=None, strides=1, padding='same')(x)
x = tf.keras.layers.GroupNormalization(groups=32)(x)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=3, activation=None, strides=1, padding='same')(x)
x = tf.keras.layers.GroupNormalization(groups=32)(x)
x = tf.keras.layers.ReLU()(x)
if halving:
half = tf.keras.layers.Conv2D(filters=filters, kernel_size=4, strides=2, padding='same', activation=None)(x)
half = tf.keras.layers.GroupNormalization(groups=32)(half)
return half, x
else:
return x
def up_sampling_gn(x, y, filters):
x = tf.keras.layers.Conv2DTranspose(filters=filters, kernel_size=4, activation=None, strides=2, padding='same')(x)
x = tf.keras.layers.GroupNormalization(groups=32)(x)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.concatenate([x, y])
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=3, activation=None, strides=1, padding='same')(x)
x = tf.keras.layers.GroupNormalization(groups=32)(x)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=3, activation=None, strides=1, padding='same')(x)
x = tf.keras.layers.GroupNormalization(groups=32)(x)
x = tf.keras.layers.ReLU()(x)
return x
def down_sampling_res(x, filters, halving=True):
tmp = tf.keras.layers.Conv2D(filters=filters, kernel_size=3, activation=None, strides=1, padding='same')(x)
x = tf.keras.layers.GroupNormalization(groups=32)(tmp)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=3, activation=None, strides=1, padding='same')(x)
x = tf.keras.layers.GroupNormalization(groups=32)(x)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.concatenate([x, tmp])
if halving:
half = tf.keras.layers.Conv2D(filters=filters, kernel_size=4, strides=2, padding='same', activation=None)(x)
half = tf.keras.layers.GroupNormalization(groups=32)(half)
return half, x
else:
return x
def up_sampling_res(x, y, filters):
x = tf.keras.layers.Conv2DTranspose(filters=filters, kernel_size=4, activation=None, strides=2, padding='same')(x)
x = tf.keras.layers.GroupNormalization(groups=32)(x)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.concatenate([x, y])
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=3, activation=None, strides=1, padding='same')(x)
x = tf.keras.layers.GroupNormalization(groups=32)(x)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=3, activation=None, strides=1, padding='same')(x)
x = tf.keras.layers.GroupNormalization(groups=32)(x)
x = tf.keras.layers.ReLU()(x)
return x
def u_net(input_size = (256,256,1)):
filters = [64, 128, 256, 512, 1024]
# encoder
inputs = tf.keras.layers.Input(input_size)
x, temp1 = down_sampling(inputs, filters[0], dropout_rate=0.1)
x, temp2 = down_sampling(x, filters[1], dropout_rate=0.1)
x, temp3 = down_sampling(x, filters[2], dropout_rate=0.2)
x, temp4 = down_sampling(x, filters[3], dropout_rate=0.2)
x = down_sampling(x, filters[4], use_maxpool=False, dropout_rate=0.3)
# decoder
x = up_sampling(x, temp4, filters[3], dropout_rate=0.2)
x = up_sampling(x, temp3, filters[2], dropout_rate=0.2)
x = up_sampling(x, temp2, filters[1], dropout_rate=0.1)
x = up_sampling(x, temp1, filters[0], dropout_rate=0.1)
outputs = tf.keras.layers.Conv2D(1, (1, 1), activation='sigmoid')(x)
model = tf.keras.Model(inputs=[inputs], outputs=[outputs], name='u_net')
return model
def u_net_gn(input_size = (256,256,1)):
filters = [64, 128, 256, 512, 1024]
# encoder
inputs = tf.keras.layers.Input(input_size)
x, temp1 = down_sampling_gn(inputs, filters[0])
x, temp2 = down_sampling_gn(x, filters[1])
x, temp3 = down_sampling_gn(x, filters[2])
x, temp4 = down_sampling_gn(x, filters[3])
x = down_sampling_gn(x, filters[4], halving=False)
# decoder
x = up_sampling_gn(x, temp4, filters[3])
x = up_sampling_gn(x, temp3, filters[2])
x = up_sampling_gn(x, temp2, filters[1])
x = up_sampling_gn(x, temp1, filters[0])
outputs = tf.keras.layers.Conv2D(1, (1, 1), activation='sigmoid')(x)
model = tf.keras.Model(inputs=[inputs], outputs=[outputs], name='u_net_gn')
return model
def u_net_res(input_size = (256,256,1)):
filters = [64, 128, 256, 512, 1024]
# encoder
inputs = tf.keras.layers.Input(input_size)
x, temp1 = down_sampling_res(inputs, filters[0])
x, temp2 = down_sampling_res(x, filters[1])
x, temp3 = down_sampling_res(x, filters[2])
x, temp4 = down_sampling_res(x, filters[3])
x = down_sampling_res(x, filters[4], halving=False)
# decoder
x = up_sampling_res(x, temp4, filters[3])
x = up_sampling_res(x, temp3, filters[2])
x = up_sampling_res(x, temp2, filters[1])
x = up_sampling_res(x, temp1, filters[0])
outputs = tf.keras.layers.Conv2D(1, (1, 1), activation='sigmoid')(x)
model = tf.keras.Model(inputs=[inputs], outputs=[outputs], name='u_net_res')
return model
if __name__ == '__main__':
model = u_net_res(input_size=(256,256,1))
model.summary()