-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResNest.py
203 lines (173 loc) · 9.12 KB
/
ResNest.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import tensorflow as tf
class ResNest(tf.Module):
# I'm still having issues figuring out what I can put in the init and what I can't.
# Most of this is self-explanatory. The class_factor and alpha are optional parameters in the loss function
def __init__(self, height, width, channel, ksize, radix=4, kpaths=4, wDecay=None):
# print("\nInitializing Short-ResNeSt...")
super(ResNest, self).__init__()
self.height, self.width, self.channel = height, width, channel
self.ksize = ksize
self.radix, self.kpaths = radix, kpaths
self.wDecay = wDecay
self.conv1 = tf.keras.layers.Conv2D(16, 3, strides=1, padding='SAME', kernel_regularizer=self.wDecay,
kernel_initializer=tf.keras.initializers.HeNormal(), name="initial_conv")
self.conv1_act = tf.keras.layers.LeakyReLU()
self.convtmp_1 = tf.keras.layers.Conv2D(32, 3, strides=1, padding='SAME', kernel_regularizer=self.wDecay,
kernel_initializer=tf.keras.initializers.HeNormal(),)
self.convtmp_1bn = tf.keras.layers.BatchNormalization() # multiple GPU. CHnage to BatchNormalization if not use multiple GPU
self.convtmp_1act = tf.keras.layers.LeakyReLU()
self.convtmp_2 = tf.keras.layers.Conv2D(32, 3, strides=1, padding='SAME', kernel_regularizer=self.wDecay,
kernel_initializer=tf.keras.initializers.HeNormal(),)
self.convtmp_2bn = tf.keras.layers.BatchNormalization() # multiple GPU. CHnage to BatchNormalization if not use multiple GPU
self.convtmp_2act = tf.keras.layers.LeakyReLU()
self.conv1_pool = tf.keras.layers.AveragePooling2D(pool_size=2, strides=2)
self.conv2_pool = tf.keras.layers.AveragePooling2D(pool_size=2, strides=2)
self.conv3_pool = tf.keras.layers.AveragePooling2D(pool_size=2, strides=2)
self.conv4_pool = tf.keras.layers.AveragePooling2D(pool_size=2, strides=2)
self.conv_1 = residual_S(ksize=self.ksize, outchannel=64, radix=self.radix,
kpaths=self.kpaths, wDecay=self.wDecay)
self.conv_2 = residual_S(ksize=self.ksize, outchannel=128, radix=self.radix,
kpaths=self.kpaths, wDecay=self.wDecay)
self.conv_3 = residual_S(ksize=self.ksize, outchannel=256, radix=self.radix,
kpaths=self.kpaths, wDecay=self.wDecay)
self.conv_4 = residual_S(ksize=self.ksize, outchannel=512, radix=self.radix,
kpaths=self.kpaths, wDecay=self.wDecay)
def forward(self, x):
x = self.conv1(x)
x = self.conv1_act(x)
x = self.convtmp_1(x)
x = self.convtmp_1bn(x)
x = self.convtmp_1act(x)
x = self.convtmp_2(x)
x = self.convtmp_2bn(x)
x = self.convtmp_2act(x)
x = self.conv1_pool(x)
x_1 = self.conv_1(x)
x = self.conv2_pool(x_1)
x_2 = self.conv_2(x)
x = self.conv3_pool(x_2)
x_3 = self.conv_3(x)
x = self.conv4_pool(x_3)
x_4 = self.conv_4(x)
return x_4, [x_3, x_2, x_1]
def __call__(self, x, *args, **kwargs):
return self.forward(x)
class residual_S(tf.Module):
def __init__(self, ksize, outchannel, radix, kpaths, atrous=1, wDecay=None):
super(residual_S, self).__init__()
self.kpaths = kpaths
self.atrous = atrous
self.ksize = ksize
self.outchannel = outchannel
self.radix = radix
self.wDecay = wDecay
self.cardinal_blocks = []
for _ in range(self.kpaths):
cardinal_block = cardinal(self.ksize, self.outchannel // 2, self.radix,
self.kpaths, atrous=self.atrous, wDecay=self.wDecay)
self.cardinal_blocks.append(cardinal_block)
self.concats_2 = tf.keras.layers.Conv2D(self.outchannel, self.ksize, strides=1, padding='SAME',
dilation_rate=(self.atrous, self.atrous),
kernel_initializer=tf.keras.initializers.HeNormal(),
kernel_regularizer=self.wDecay)
self.convtmp_sc = tf.keras.layers.Conv2D(self.outchannel, 1, strides=1, padding='SAME',
dilation_rate=(self.atrous, self.atrous),
kernel_initializer=tf.keras.initializers.HeNormal(),
kernel_regularizer=self.wDecay)
self.convtmp_scbn = tf.keras.layers.LayerNormalization()
self.convtmp_scact = tf.keras.layers.LeakyReLU()
def forward(self, x):
concats_1 = None
for layer_block in self.cardinal_blocks:
cardinalI = layer_block(x)
if concats_1 is None:
concats_1 = cardinalI
else:
concats_1 = tf.concat([concats_1, cardinalI], axis=3)
concats_2 = self.concats_2(concats_1)
x = self.convtmp_sc(x)
x = self.convtmp_scbn(x)
x = self.convtmp_scact(x)
output = x + concats_2
return output
def __call__(self, x, *args, **kwargs):
return self.forward(x)
class cardinal(tf.Module):
def __init__(self, ksize, outchannel, radix, kpaths, atrous=1, wDecay=None):
super(cardinal, self).__init__()
self.outchannel = outchannel
self.ksize = ksize
self.radix = radix
self.kpaths = kpaths
self.atrous = atrous
self.wDecay = wDecay
outchannel_cv11 = int(self.outchannel / self.radix / self.kpaths)
outchannel_cvkk = int(self.outchannel / self.kpaths)
self.conv1 = tf.keras.layers.Conv2D(outchannel_cv11, 1, strides=1, padding='SAME',
dilation_rate=(self.atrous, self.atrous), kernel_regularizer=self.wDecay,
kernel_initializer=tf.keras.initializers.HeNormal())
self.conv1_bn = tf.keras.layers.LayerNormalization()
self.conv1_act = tf.keras.layers.LeakyReLU()
self.conv2 = tf.keras.layers.Conv2D(outchannel_cvkk, self.ksize,
strides=1, padding='SAME', dilation_rate=(self.atrous, self.atrous),
kernel_initializer=tf.keras.initializers.HeNormal(),
kernel_regularizer=self.wDecay)
self.conv2_bn = tf.keras.layers.LayerNormalization()
self.conv2_act = tf.keras.layers.LeakyReLU()
self.split = split_attention(outchannel_cvkk, self.radix, self.atrous, self.wDecay)
def forward(self, x):
inputs = []
for idx_r in range(self.radix):
y = self.conv1(x)
y = self.conv1_bn(y)
y = self.conv1_act(y)
y = self.conv2(y)
y = self.conv2_bn(y)
y = self.conv2_act(y)
inputs.append(y)
return self.split(inputs)
def __call__(self, x, *args, **kwargs):
return self.forward(x)
class split_attention(tf.Module):
def __init__(self, inchannel, radix, atrous=1, wDecay=None):
super(split_attention, self).__init__()
self.inchannel = inchannel
self.atrous = atrous
self.radix = radix
self.wDecay = wDecay
self.dense1 = tf.keras.layers.Conv2D(self.inchannel // 2, 1, strides=1, padding='SAME',
dilation_rate=(self.atrous, self.atrous),
kernel_initializer=tf.keras.initializers.HeNormal(),
kernel_regularizer=self.wDecay)
self.dense1_bn = tf.keras.layers.LayerNormalization()
self.dense1_act = tf.keras.layers.LeakyReLU()
self.dense2 = tf.keras.layers.Conv2D(self.inchannel, 1, strides=1, padding='SAME',
dilation_rate=(self.atrous, self.atrous),
kernel_initializer=tf.keras.initializers.HeNormal(),
kernel_regularizer=self.wDecay)
def forward(self, inputs):
input_holder = None
for idx_i, inputi in enumerate(inputs):
if idx_i == 0:
input_holder = inputi
else:
input_holder += inputi
ga_pool = tf.math.reduce_mean(input_holder, axis=(1, 2))
y = tf.expand_dims(tf.expand_dims(ga_pool, axis=1), axis=1)
y = self.dense1(y)
y = self.dense1_bn(y)
y = self.dense1_act(y)
output_holder = None
for idx_r in range(self.radix):
z = self.dense2(y)
if self.radix == 1:
z = tf.keras.activations.sigmoid(z)
elif self.radix > 1:
z = tf.keras.activations.softmax(z)
if idx_r == 0:
output_holder = inputs[idx_r] * z
else:
output_holder += inputs[idx_r] * z
return output_holder
def __call__(self, inputs, *args, **kwargs):
return self.forward(inputs)