-
Notifications
You must be signed in to change notification settings - Fork 0
/
pooling.py
executable file
·162 lines (121 loc) · 7.89 KB
/
pooling.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
import tensorflow as tf
from .utils import conv_block, convolution, batch_norm
def look_right_fn(input):
input = tf.transpose(input, [0, 2, 3, 1])
output = tf.pad(input, tf.constant([[0, 0], [0, 0], [0, input.get_shape().as_list()[2] - 1], [0, 0]]),
constant_values=tf.reduce_min(input))
output = tf.nn.max_pool(output, ksize=(1, input.get_shape().as_list()[2]), strides=(1, 1), padding='VALID')
return tf.transpose(output, [0, 3, 1, 2])
def look_down_fn(input):
input = tf.transpose(input, [0, 2, 3, 1])
output = tf.pad(input, tf.constant([[0, 0], [0, input.get_shape().as_list()[1] - 1], [0, 0], [0, 0]]),
constant_values=tf.reduce_min(input))
output = tf.nn.max_pool(output, ksize=(input.get_shape().as_list()[1], 1), strides=(1, 1), padding='VALID')
return tf.transpose(output, [0, 3, 1, 2])
def look_up_fn(input):
input = tf.transpose(input, [0, 2, 3, 1])
output = tf.pad(input, tf.constant([[0, 0], [input.get_shape().as_list()[1] - 1, 0], [0, 0], [0, 0]]),
constant_values=tf.reduce_min(input))
output = tf.nn.max_pool(output, ksize=(input.get_shape().as_list()[1], 1), strides=(1, 1), padding='VALID')
return tf.transpose(output, [0, 3, 1, 2])
def look_left_fn(input):
input = tf.transpose(input, [0, 2, 3, 1])
output = tf.pad(input, tf.constant([[0, 0], [0, 0], [input.get_shape().as_list()[2] - 1, 0], [0, 0]]),
constant_values=tf.reduce_min(input))
output = tf.nn.max_pool(output, ksize=(1, input.get_shape().as_list()[2]), strides=(1, 1), padding='VALID')
return tf.transpose(output, [0, 3, 1, 2])
class tl_pool(tf.keras.Model):
def __init__(self, weights_dic=None):
super(tl_pool, self).__init__()
self.tl_pool = Pool(look_down_fn, look_right_fn, weights_dic=None if weights_dic is None else weights_dic)
def call(self, inputs, training=None, mask=None):
return self.tl_pool(inputs, training=training)
class br_pool(tf.keras.Model):
def __init__(self, weights_dic=None):
super(br_pool, self).__init__()
self.br_pool = Pool(look_up_fn, look_left_fn, weights_dic=None if weights_dic is None else weights_dic)
def call(self, inputs, training=None, mask=None):
return self.br_pool(inputs, training=training)
class ct_pool(tf.keras.Model):
def __init__(self, weights_dic=None):
super(ct_pool, self).__init__()
self.ct_pool = Pool_cross(look_down_fn, look_right_fn, look_up_fn, look_left_fn,
weights_dic=None if weights_dic is None else weights_dic)
def call(self, inputs, training=None, mask=None):
return self.ct_pool(inputs, training=training)
class Pool(tf.keras.Model):
def __init__(self, pool1, pool2, weights_dic=None):
super(Pool, self).__init__()
self.p1_conv1 = conv_block(out_dim=128, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['p1_conv1'])
self.p2_conv1 = conv_block(out_dim=128, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['p2_conv1'])
self.p_conv1 = convolution(out_dim=256, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['p_conv1'])
self.p_bn1 = batch_norm(weights_init=None if weights_dic is None else weights_dic['p_bn1'])
self.conv1 = convolution(out_dim=256, kernel=1, stride=1, padding='same',
weights_init=None if weights_dic is None else weights_dic['conv1'])
self.bn1 = batch_norm(weights_init=None if weights_dic is None else weights_dic['bn1'])
self.conv2 = conv_block(out_dim=256, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['conv2'])
self.pool1 = pool1
self.pool2 = pool2
self.look_conv1 = conv_block(out_dim=128, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['look_conv1'])
self.look_conv2 = conv_block(out_dim=128, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['look_conv2'])
self.P1_look_conv = convolution(out_dim=128, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['P1_look_conv'])
self.P2_look_conv = convolution(out_dim=128, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['P2_look_conv'])
def call(self, inputs, training=None, mask=None):
look_conv1 = self.look_conv1(inputs, training=training)
p1_conv1 = self.p1_conv1(inputs, training=training)
look_right = self.pool2(look_conv1)
P1_look_conv = self.P1_look_conv(p1_conv1 + look_right)
pool1 = self.pool1(P1_look_conv)
look_conv2 = self.look_conv2(inputs, training=training)
p2_conv1 = self.p2_conv1(inputs, training=training)
look_down = self.pool1(look_conv2)
P2_look_conv = self.P2_look_conv(p2_conv1 + look_down)
pool2 = self.pool2(P2_look_conv)
p_conv1 = self.p_conv1(pool1 + pool2)
p_bn1 = self.p_bn1(p_conv1, training=training)
conv1 = self.conv1(inputs)
bn1 = self.bn1(conv1, training=training)
relu1 = tf.nn.relu(p_bn1 + bn1)
conv2 = self.conv2(relu1, training=training)
return conv2
class Pool_cross(tf.keras.Model):
def __init__(self, pool1, pool2, pool3, pool4, weights_dic=None):
super(Pool_cross, self).__init__()
self.p1_conv1 = conv_block(out_dim=128, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['p1_conv1'])
self.p2_conv1 = conv_block(out_dim=128, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['p2_conv1'])
self.p_conv1 = convolution(out_dim=256, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['p_conv1'])
self.p_bn1 = batch_norm(weights_init=None if weights_dic is None else weights_dic['p_bn1'])
self.conv1 = convolution(out_dim=256, kernel=1, stride=1, padding='same',
weights_init=None if weights_dic is None else weights_dic['conv1'])
self.bn1 = batch_norm(weights_init=None if weights_dic is None else weights_dic['bn1'])
self.conv2 = conv_block(out_dim=256, kernel=3, stride=1, padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
weights_init=None if weights_dic is None else weights_dic['conv2'])
self.pool1 = pool1
self.pool2 = pool2
self.pool3 = pool3
self.pool4 = pool4
def call(self, inputs, training=None, mask=None):
p1_conv1 = self.p1_conv1(inputs, training=training)
pool1 = self.pool1(p1_conv1)
pool1 = self.pool3(pool1)
p2_conv1 = self.p2_conv1(inputs, training=training)
pool2 = self.pool2(p2_conv1)
pool2 = self.pool4(pool2)
p_conv1 = self.p_conv1(pool1 + pool2)
p_bn1 = self.p_bn1(p_conv1, training=training)
conv1 = self.conv1(inputs)
bn1 = self.bn1(conv1, training=training)
relu1 = tf.nn.relu(p_bn1 + bn1)
conv2 = self.conv2(relu1, training=training)
return conv2