-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathops.py
287 lines (231 loc) · 10.1 KB
/
ops.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
def conv2d(inputs, num_outputs, kernel_shape, strides=[1, 1], mask_type=None, scope="conv2d"):
"""
Args:
inputs: nhwc
kernel_shape: [height, width]
mask_type:
(i) None
(ii) sp_horizontal
(iii) sp_vertical
(iv) horizontal
(v) vertical
Returns:
outputs: nhwc
"""
with tf.variable_scope(scope) as scope:
kernel_h, kernel_w = kernel_shape
stride_h, stride_w = strides
batch_size, height, width, in_channel = inputs.get_shape().as_list()
center_h = kernel_h // 2
center_w = kernel_w // 2
assert kernel_h % 2 == 1 and kernel_w % 2 == 1, "kernel height and width must be odd number"
mask = np.zeros((kernel_h, kernel_w, in_channel, num_outputs), dtype=np.float32)
if mask_type == 'sp_horizontal':
mask[center_h,:center_w,:,:] = 1
elif mask_type == 'sp_vertical':
mask[:center_h,:,:,:] = 1
elif mask_type == 'horizontal':
mask[center_h,:center_w+1,:,:] = 1
elif mask_type == 'vertical':
mask[:center_h+1,:,:,:] = 1
else:
mask[:,:,:,:] = 1
weights_shape = [kernel_h, kernel_w, in_channel, num_outputs]
weights = tf.get_variable("weights", weights_shape,
tf.float32, tf.truncated_normal_initializer(stddev=0.1))
weights = weights * mask
biases = tf.get_variable("biases", [num_outputs],
tf.float32, tf.constant_initializer(0.0))
outputs = tf.nn.conv2d(inputs, weights, [1, stride_h, stride_w, 1], padding="SAME")
outputs = tf.nn.bias_add(outputs, biases)
return outputs
def conv2d_rgb(inputs, num_outputs, kernel_shape, strides = [1,1], mask_type = None, scope = 'conv2d_rgb'):
"""
Args:
inputs: nhwc
kernel_shape: [height, width]
mask_type:
(i) None
(ii) sp_horizontal
(iii) sp_vertical
(iv) horizontal
(v) vertical
Returns:
outputs: nhwc
"""
with tf.variable_scope(scope) as scope:
kernel_h, kernel_w = kernel_shape
stride_h, stride_w = strides
batch_size, height, width, in_channel = inputs.get_shape().as_list()
center_h = kernel_h // 2
center_w = kernel_w // 2
assert kernel_h % 2 == 1 and kernel_w % 2 == 1, "kernel height and width must be odd number"
r_mask = np.zeros((kernel_h, kernel_w, in_channel, int(num_outputs/3)), dtype=np.float32)
g_mask = np.zeros((kernel_h, kernel_w, in_channel, int(num_outputs/3)), dtype=np.float32)
b_mask = np.zeros((kernel_h, kernel_w, in_channel, int(num_outputs/3)), dtype=np.float32)
if mask_type == 'sp_horizontal':
# Red mask
r_mask[center_h,:center_w,:,:] = 1
# Green mask
g_mask[center_h,:center_w,:,:] = 1
g_mask[center_h,center_w,:1,:] = 1
# Blue mask
b_mask[center_h,:center_w,:,:] = 1
b_mask[center_h,center_w,:2,:] = 1
elif mask_type == 'horizontal':
# Red mask
r_mask[center_h,:center_w,:,:] = 1
r_mask[center_h,center_w,:int(in_channel/3),:] = 1
# Green mask
g_mask[center_h,:center_w,:,:] = 1
g_mask[center_h,center_w,:2*int(in_channel/3),:] = 1
# Blue mask
b_mask[center_h,:center_w+1,:,:] = 1
else:
r_mask[:,:,:int(in_channel/3),:] = 1
g_mask[:,:,:2*int(in_channel/3),:] = 1
b_mask[:,:,:,:] = 1
# Red convolutions
r_weights_shape = [kernel_h,kernel_w,in_channel,int(num_outputs/3)]
r_weights = tf.get_variable("r_weights", r_weights_shape,
tf.float32, tf.truncated_normal_initializer(stddev=0.1))
r_weights = r_weights * r_mask
r_biases = tf.get_variable("r_biases", [int(num_outputs/3)],
tf.float32, tf.constant_initializer(0.0))
r_outputs = tf.nn.conv2d(inputs,r_weights, [1,stride_h,stride_w,1], padding = "SAME")
r_outputs = tf.nn.bias_add(r_outputs,r_biases)
# Green convolutions
g_weights_shape = [kernel_h,kernel_w,in_channel,int(num_outputs/3)]
g_weights = tf.get_variable("g_weights", g_weights_shape,
tf.float32, tf.truncated_normal_initializer(stddev=0.1))
g_weights = g_weights * g_mask
g_biases = tf.get_variable("g_biases", [int(num_outputs/3)],
tf.float32, tf.constant_initializer(0.0))
g_outputs = tf.nn.conv2d(inputs,g_weights, [1,stride_h,stride_w,1], padding = "SAME")
g_outputs = tf.nn.bias_add(g_outputs,g_biases)
# Blue convolutions
b_weights_shape = [kernel_h,kernel_w,in_channel,int(num_outputs/3)]
b_weights = tf.get_variable("b_weights", b_weights_shape,
tf.float32, tf.truncated_normal_initializer(stddev=0.1))
b_weights = b_weights * b_mask
b_biases = tf.get_variable("b_biases", [int(num_outputs/3)],
tf.float32, tf.constant_initializer(0.0))
b_outputs = tf.nn.conv2d(inputs,b_weights, [1,stride_h,stride_w,1], padding = "SAME")
b_outputs = tf.nn.bias_add(b_outputs,b_biases)
# Concatenate outputs
outputs = tf.concat([r_outputs,g_outputs,b_outputs],axis = 3, name = 'concat_outputs')
return outputs
def gated_conv2d(inputs, state, kernel_shape, num_channels, scope):
"""
Args:
inputs: nhwc
state: nhwc
kernel_shape: [height, width]
Returns:
outputs: nhwc
new_state: nhwc
"""
with tf.variable_scope(scope) as scope:
batch_size, height, width, in_channel = inputs.get_shape().as_list()
kernel_h, kernel_w = kernel_shape
#state route (vertical mask)
left = conv2d(state, 2 * in_channel, kernel_shape, strides=[1, 1], mask_type='vertical', scope="conv_s1")
left1 = left[:, :, :, 0:in_channel]
left2 = left[:, :, :, in_channel:]
left1 = tf.nn.tanh(left1)
left2 = tf.nn.sigmoid(left2)
new_state = left1 * left2
# Vertical ----> Horizontal
left2right = conv2d(left, 2 * in_channel, [1, 1], strides=[1, 1], scope="conv_s2")
# For num_channels == 1
if num_channels == 1:
#input route (horizontal mask)
right = conv2d(inputs, 2 * in_channel, [1, kernel_w], strides=[1, 1], mask_type='horizontal', scope="conv_r1")
right = right + left2right
right1 = right[:, :, :, 0:in_channel]
right2 = right[:, :, :, in_channel:]
right1 = tf.nn.tanh(right1)
right2 = tf.nn.sigmoid(right2)
up_right = right1 * right2
up_right = conv2d(up_right, in_channel, [1, 1], strides=[1, 1], scope="conv_r2")
outputs = inputs + up_right
# For num_channels == 3(rgb)
if num_channels == 3:
#input route (horizontal mask)
right1 = conv2d_rgb(inputs, in_channel, [1,kernel_w], strides = [1,1], mask_type = 'horizontal', scope = "conv_r11")
right2 = conv2d_rgb(inputs, in_channel, [1,kernel_w], strides = [1,1], mask_type = 'horizontal', scope = "conv_r12")
right1 = tf.nn.tanh(right1 + left2right[:,:,:,0:in_channel])
right2 = tf.nn.sigmoid(right2 + left2right[:,:,:,in_channel:])
up_right = right1 * right2
up_right = conv2d_rgb(up_right, in_channel, [1, 1], strides=[1, 1], scope="conv_r2")
outputs = inputs + up_right
return outputs, new_state
def special_gated_conv2d(inputs, num_outputs, kernel_shape, num_channels, scope):
"""
Args:
inputs: nhwc
kernel_shape: [height, width]
Returns:
outputs: nhwc
new_state: nhwc
"""
with tf.variable_scope(scope) as scope:
batch_size, height, width, in_channel = inputs.get_shape().as_list()
in_channel = num_outputs
kernel_h, kernel_w = kernel_shape
# State route (special vertical mask)
left = conv2d(inputs, 2*in_channel, kernel_shape, strides=[1,1], mask_type = 'sp_vertical', scope = "conv_s1")
left1 = left[:, :, :, 0:in_channel]
left2 = left[:, :, :, in_channel:]
left1 = tf.nn.tanh(left1)
left2 = tf.nn.sigmoid(left2)
new_state = left1 * left2
# Vertical ----> Horizontal
left2right = conv2d(left, 2 * in_channel, [1, 1], strides=[1, 1], scope="conv_s2")
# For num_channels == 1
if num_channels == 1:
# Input route (special horizontal mask)
right = conv2d(inputs, 2*in_channel, [1,kernel_w], strides=[1,1], mask_type = 'sp_horizontal', scope = "conv_r1")
right = right + left2right
right1 = right[:, :, :, 0:in_channel]
right2 = right[:, :, :, in_channel:]
right1 = tf.nn.tanh(right1)
right2 = tf.nn.sigmoid(right2)
up_right = right1 * right2
up_right = conv2d(up_right, in_channel, [1, 1], strides=[1, 1], scope="conv_r2")
outputs = up_right
# For num_channels == 3
if num_channels == 3:
# Input route (special horizontal mask)
right1 = conv2d_rgb(inputs, in_channel, [1,kernel_w], strides = [1,1], mask_type = 'sp_horizontal', scope = "conv_r11")
right2 = conv2d_rgb(inputs, in_channel, [1,kernel_w], strides = [1,1], mask_type = 'sp_horizontal', scope = "conv_r12")
right1 = tf.nn.tanh(right1 + left2right[:,:,:,0:in_channel])
right2 = tf.nn.sigmoid(right2 + left2right[:,:,:,in_channel:])
up_right = right1 * right2
up_right = conv2d_rgb(up_right, in_channel, [1, 1], strides=[1, 1], scope="conv_r2")
outputs = up_right
return outputs, new_state
def batch_norm(x, train=True, scope=None):
return tf.contrib.layers.batch_norm(x, center=True, scale=True, updates_collections=None, is_training=train, trainable=True, scope=scope)
def resnet_block(inputs, num_outputs, kernel_shape, strides=[1, 1], scope=None, train=True):
"""
Args:
inputs: nhwc
num_outputs: int
kernel_shape: [kernel_h, kernel_w]
Returns:
outputs: nhw(num_outputs)
"""
with tf.variable_scope(scope) as scope:
conv1 = conv2d(inputs, num_outputs, kernel_shape, strides=[1, 1], mask_type=None, scope="conv1")
bn1 = batch_norm(conv1, train=train, scope='bn1')
relu1 = tf.nn.relu(bn1)
conv2 = conv2d(relu1, num_outputs, kernel_shape, strides=[1, 1], mask_type=None, scope="conv2")
bn2 = batch_norm(conv2, train=train, scope='bn2')
output = inputs + bn2
return output