-
Notifications
You must be signed in to change notification settings - Fork 15
/
SANet.py
174 lines (135 loc) · 6.48 KB
/
SANet.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
174
from keras.layers import Conv2D, MaxPooling2D, concatenate, Input, Dense, Conv2DTranspose, ReLU, UpSampling2D, Activation
from keras.models import Model
from keras_contrib.layers import InstanceNormalization
from keras.initializers import RandomNormal
def SANet(input_shape=(None, None, 3), IN=True):
input_flow = Input(input_shape)
# Module 1
channel_out_1 = 16
x_1 = Conv2D(channel_out_1, 1, padding='same', use_bias=(not IN))(input_flow)
x_1 = Activation('relu')(x_1)
x_1 = InstanceNormalization()(x_1) if IN else x_1
x_2 = Conv2D(channel_out_1*2, 1, padding='same', use_bias=(not IN))(input_flow)
x_2 = Activation('relu')(x_2)
x_2 = InstanceNormalization()(x_2) if IN else x_2
x_2 = Conv2D(channel_out_1, 3, padding='same', use_bias=(not IN))(x_2)
x_2 = Activation('relu')(x_2)
x_2 = InstanceNormalization()(x_2) if IN else x_2
x_3 = Conv2D(channel_out_1*2, 1, padding='same', use_bias=(not IN))(input_flow)
x_3 = Activation('relu')(x_3)
x_3 = InstanceNormalization()(x_3) if IN else x_3
x_3 = Conv2D(channel_out_1, 5, padding='same', use_bias=(not IN))(x_3)
x_3 = Activation('relu')(x_3)
x_3 = InstanceNormalization()(x_3) if IN else x_3
x_4 = Conv2D(channel_out_1*2, 1, padding='same', use_bias=(not IN))(input_flow)
x_4 = Activation('relu')(x_4)
x_4 = InstanceNormalization()(x_4) if IN else x_4
x_4 = Conv2D(channel_out_1, 7, padding='same', use_bias=(not IN))(x_4)
x_4 = Activation('relu')(x_4)
x_4 = InstanceNormalization()(x_4) if IN else x_4
x = concatenate([x_1, x_2, x_3, x_4])
x = MaxPooling2D()(x)
# Module 2
channel_out_2 = 32
x_1 = Conv2D(channel_out_2, 1, padding='same', use_bias=(not IN))(x)
x_1 = Activation('relu')(x_1)
x_1 = InstanceNormalization()(x_1) if IN else x_1
x_2 = Conv2D(channel_out_2*2, 1, padding='same', use_bias=(not IN))(x)
x_2 = Activation('relu')(x_2)
x_2 = InstanceNormalization()(x_2) if IN else x_2
x_2 = Conv2D(channel_out_2, 3, padding='same', use_bias=(not IN))(x_2)
x_2 = Activation('relu')(x_2)
x_2 = InstanceNormalization()(x_2) if IN else x_2
x_3 = Conv2D(channel_out_2*2, 1, padding='same', use_bias=(not IN))(x)
x_3 = Activation('relu')(x_3)
x_3 = InstanceNormalization()(x_3) if IN else x_3
x_3 = Conv2D(channel_out_2, 5, padding='same', use_bias=(not IN))(x_3)
x_3 = Activation('relu')(x_3)
x_3 = InstanceNormalization()(x_3) if IN else x_3
x_4 = Conv2D(channel_out_2*2, 1, padding='same', use_bias=(not IN))(x)
x_4 = Activation('relu')(x_4)
x_4 = InstanceNormalization()(x_4) if IN else x_4
x_4 = Conv2D(channel_out_2, 7, padding='same', use_bias=(not IN))(x_4)
x_4 = Activation('relu')(x_4)
x_4 = InstanceNormalization()(x_4) if IN else x_4
x = concatenate([x_1, x_2, x_3, x_4])
x = MaxPooling2D()(x)
# Module 3
channel_out_3 = 32
x_1 = Conv2D(channel_out_3, 1, padding='same', use_bias=(not IN))(x)
x_1 = Activation('relu')(x_1)
x_1 = InstanceNormalization()(x_1) if IN else x_1
x_2 = Conv2D(channel_out_3*2, 1, padding='same', use_bias=(not IN))(x)
x_2 = Activation('relu')(x_2)
x_2 = InstanceNormalization()(x_2) if IN else x_2
x_2 = Conv2D(channel_out_3, 3, padding='same', use_bias=(not IN))(x_2)
x_2 = Activation('relu')(x_2)
x_2 = InstanceNormalization()(x_2) if IN else x_2
x_3 = Conv2D(channel_out_3*2, 1, padding='same', use_bias=(not IN))(x)
x_3 = Activation('relu')(x_3)
x_3 = InstanceNormalization()(x_3) if IN else x_3
x_3 = Conv2D(channel_out_3, 5, padding='same', use_bias=(not IN))(x_3)
x_3 = Activation('relu')(x_3)
x_3 = InstanceNormalization()(x_3) if IN else x_3
x_4 = Conv2D(channel_out_3*2, 1, padding='same', use_bias=(not IN))(x)
x_4 = Activation('relu')(x_4)
x_4 = InstanceNormalization()(x_4) if IN else x_4
x_4 = Conv2D(channel_out_3, 7, padding='same', use_bias=(not IN))(x_4)
x_4 = Activation('relu')(x_4)
x_4 = InstanceNormalization()(x_4) if IN else x_4
x = concatenate([x_1, x_2, x_3, x_4])
x = MaxPooling2D()(x)
# Module 4
channel_out_4 = 32
x_1 = Conv2D(channel_out_4, 1, padding='same', use_bias=(not IN))(x)
x_1 = Activation('relu')(x_1)
x_1 = InstanceNormalization()(x_1) if IN else x_1
x_2 = Conv2D(channel_out_4*2, 1, padding='same', use_bias=(not IN))(x)
x_2 = Activation('relu')(x_2)
x_2 = InstanceNormalization()(x_2) if IN else x_2
x_2 = Conv2D(channel_out_4, 3, padding='same', use_bias=(not IN))(x_2)
x_2 = Activation('relu')(x_2)
x_2 = InstanceNormalization()(x_2) if IN else x_2
x_3 = Conv2D(channel_out_4*2, 1, padding='same', use_bias=(not IN))(x)
x_3 = Activation('relu')(x_3)
x_3 = InstanceNormalization()(x_3) if IN else x_3
x_3 = Conv2D(channel_out_4, 5, padding='same', use_bias=(not IN))(x_3)
x_3 = Activation('relu')(x_3)
x_3 = InstanceNormalization()(x_3) if IN else x_3
x_4 = Conv2D(channel_out_4*2, 1, padding='same', use_bias=(not IN))(x)
x_4 = Activation('relu')(x_4)
x_4 = InstanceNormalization()(x_4) if IN else x_4
x_4 = Conv2D(channel_out_4, 7, padding='same', use_bias=(not IN))(x_4)
x_4 = Activation('relu')(x_4)
x_4 = InstanceNormalization()(x_4) if IN else x_4
x = concatenate([x_1, x_2, x_3, x_4])
# Decoder
x = Conv2D(64, 9, padding='same', use_bias=(not IN))(x)
x = Activation('relu')(x)
x = InstanceNormalization()(x) if IN else x
x = Conv2DTranspose(64, kernel_size=(2, 2), strides=(2, 2))(x)
x = Activation('relu')(x)
x = InstanceNormalization()(x) if IN else x
x = Conv2D(32, 7, padding='same', use_bias=(not IN))(x)
x = Activation('relu')(x)
x = InstanceNormalization()(x) if IN else x
x = Conv2DTranspose(32, kernel_size=(2, 2), strides=(2, 2))(x)
x = Activation('relu')(x)
x = InstanceNormalization()(x) if IN else x
x = Conv2D(16, 5, padding='same', use_bias=(not IN))(x)
x = Activation('relu')(x)
x = InstanceNormalization()(x) if IN else x
x = Conv2DTranspose(16, kernel_size=(2, 2), strides=(2, 2))(x)
x = Activation('relu')(x)
x = InstanceNormalization()(x) if IN else x
# Output
x = Conv2D(16, 3, padding='same', use_bias=(not IN))(x)
x = Activation('relu')(x)
x = InstanceNormalization()(x) if IN else x
x = Conv2D(16, 5, padding='same', use_bias=(not IN))(x)
x = Activation('relu')(x)
x = InstanceNormalization()(x) if IN else x
x = Conv2D(1, 1)(x)
x = Activation('relu')(x)
model = Model(inputs=input_flow, outputs=x)
return model