-
Notifications
You must be signed in to change notification settings - Fork 11
/
modules_and_upsample.py
165 lines (119 loc) · 4.92 KB
/
modules_and_upsample.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
from keras import backend as K
from tensorflow.image import resize_bilinear
from keras.engine import Layer, InputSpec
from keras.utils import conv_utils
from keras.layers import Conv2D, SeparableConv2D, BatchNormalization, Activation
from keras.layers import Reshape, multiply, add, GlobalAveragePooling2D, Concatenate
def CA_Block(x,r=8):
''' Creates the Channel Attention Block
Args:
x: input tensor
r: reduction ratio
Returns:
a keras tensor'''
num_ch = x._keras_shape[-1]
y = GlobalAveragePooling2D()(x)
y = Reshape((1,1,int(num_ch)))(y)
y = Conv2D(int(num_ch/r), (1, 1), activation='relu', use_bias=False)(y)
y = Conv2D(int(num_ch), (1, 1), activation='sigmoid', use_bias=False)(y)
y = multiply([x, y])
return y
def LargeKernel(x,num_f,k):
'''Args:
x: input tensor
num_f: number of filters
k: size of the LargeKernel
Returns:
a keras tensor'''
y1 = Conv2D(num_f,(1,k),strides=(1,1),padding='same')(x)
y1 = Conv2D(num_f,(k,1),strides=(1,1),padding='same')(y1)
y2 = SeparableConv2D(num_f, (3,3), strides=(1, 1), padding='same', dilation_rate=int(((k-3)/2)+1))(x)
y = add([y1,y2])
return y
def MAG_Module(x,num_f):
''' Creates the MAG Module"
Args:
x: input tensor
num_f: number of filters
Returns:
a keras tensor'''
y0 = Conv2D(num_f, (1, 1), padding='same')(x)
y0 = BatchNormalization(epsilon=1e-5)(y0)
y0 = Activation('relu')(y0)
y1 = Conv2D(num_f, (3, 3), padding='same')(x)
y1 = BatchNormalization(epsilon=1e-5)(y1)
y1 = Activation('relu')(y1)
y2 = LargeKernel(x,num_f,5)
y2 = BatchNormalization(epsilon=1e-5)(y2)
y2 = Activation('relu')(y2)
y3 = LargeKernel(x,num_f,7)
y3 = BatchNormalization(epsilon=1e-5)(y3)
y3 = Activation('relu')(y3)
y4 = LargeKernel(x,num_f,9)
y4 = BatchNormalization(epsilon=1e-5)(y4)
y4 = Activation('relu')(y4)
y5 = LargeKernel(x,num_f,11)
y5 = BatchNormalization(epsilon=1e-5)(y5)
y5 = Activation('relu')(y5)
y = Concatenate()([y0,y1,y2,y3,y4,y5])
y = CA_Block(y)
return y
def AMI_Module(x1,x2,num_f):
''' Creates the AMI Module"
Args:
x1, x2: input tensors
num_f: number of filters
Returns:
a keras tensor'''
y = Concatenate()([x1,x2])
y = CA_Block(y)
y = Conv2D(num_f, (3, 3), padding='same', strides=(1,1))(y)
y = BatchNormalization(axis=-1)(y)
y = Activation('relu')(y)
return y
class BilinearUpsampling(Layer):
"""Bilinear upsampling layer
Args:
upsampling: the upsampling factors for rows and columns.
output_size: use this arg instead of upsampling arg if
your desired size is not an integer factor of the input size"""
def __init__(self, upsampling=(2, 2), output_size=None, data_format=None, **kwargs):
super(BilinearUpsampling, self).__init__(**kwargs)
self.data_format = K.normalize_data_format(data_format)
self.input_spec = InputSpec(ndim=4)
if output_size:
self.output_size = conv_utils.normalize_tuple(
output_size, 2, 'output_size')
self.upsampling = None
else:
self.output_size = None
self.upsampling = conv_utils.normalize_tuple(
upsampling, 2, 'upsampling')
def compute_output_shape(self, input_shape):
if self.upsampling:
height = self.upsampling[0] * \
input_shape[1] if input_shape[1] is not None else None
width = self.upsampling[1] * \
input_shape[2] if input_shape[2] is not None else None
else:
height = self.output_size[0]
width = self.output_size[1]
return (input_shape[0],
height,
width,
input_shape[3])
def call(self, inputs):
if self.upsampling:
return resize_bilinear(inputs, (inputs.shape[1] * self.upsampling[0],
inputs.shape[2] * self.upsampling[1]),
align_corners=True)
else:
return resize_bilinear(inputs, (self.output_size[0],
self.output_size[1]),
align_corners=True)
def get_config(self):
config = {'upsampling': self.upsampling,
'output_size': self.output_size,
'data_format': self.data_format}
base_config = super(BilinearUpsampling, self).get_config()
return dict(list(base_config.items()) + list(config.items()))