-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlayer.py
291 lines (253 loc) · 12.3 KB
/
layer.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
288
289
290
291
'''
covlution layer,pool layer,initialization and so on
'''
#from __future__ import division
import tensorflow as tf
import numpy as np
import cv2
# Weight initialization (Xavier's init)
def weight_xavier_init(shape, n_inputs, n_outputs, activefunction='sigomd', uniform=True, variable_name=None):
if activefunction == 'sigomd':
if uniform:
init_range = tf.sqrt(6.0 / (n_inputs + n_outputs))
initial = tf.random_uniform(shape, -init_range, init_range)
return tf.get_variable(name=variable_name, initializer=initial, trainable=True)
else:
stddev = tf.sqrt(2.0 / (n_inputs + n_outputs))
initial = tf.truncated_normal(shape, mean=0.0, stddev=stddev)
return tf.get_variable(name=variable_name, initializer=initial, trainable=True)
elif activefunction == 'relu':
if uniform:
init_range = tf.sqrt(6.0 / (n_inputs + n_outputs)) * np.sqrt(2)
initial = tf.random_uniform(shape, -init_range, init_range)
return tf.get_variable(name=variable_name, initializer=initial, trainable=True)
else:
stddev = tf.sqrt(2.0 / (n_inputs + n_outputs)) * np.sqrt(2)
initial = tf.truncated_normal(shape, mean=0.0, stddev=stddev)
return tf.get_variable(name=variable_name, initializer=initial, trainable=True)
elif activefunction == 'tan':
if uniform:
init_range = tf.sqrt(6.0 / (n_inputs + n_outputs)) * 4
initial = tf.random_uniform(shape, -init_range, init_range)
return tf.get_variable(name=variable_name, initializer=initial, trainable=True)
else:
stddev = tf.sqrt(2.0 / (n_inputs + n_outputs)) * 4
initial = tf.truncated_normal(shape, mean=0.0, stddev=stddev)
return tf.get_variable(name=variable_name, initializer=initial, trainable=True)
# Bias initialization
def bias_variable(shape, variable_name=None):
initial = tf.constant(0.1, shape=shape)
return tf.get_variable(name=variable_name, initializer=initial, trainable=True)
# 3D convolution
def conv3d(x, W, stride=1):
conv_3d = tf.nn.conv3d(x, W, strides=[1, stride, stride, stride, 1], padding='SAME')
return conv_3d
# 2D convolution
def conv2d(x, W, stride=1):
conv_2d = tf.nn.conv2d(x, W, strides=[1, stride, stride, 1], padding='SAME')
return conv_2d
def sepconv2d(x, W, PW,stride=1):
conv_2d = tf.nn.separable_conv2d(x, W, PW,strides=[1, stride, stride, 1], padding='SAME')
return conv_2d
# 3D downsampling
def downsample3d(x):
pool3d = tf.nn.avg_pool3d(x, ksize=[1, 2, 2, 2, 1], strides=[1, 2, 2, 2, 1], padding='SAME')
return pool3d
# 2D downsampling
def downsample2d(x):
pool2d = tf.nn.avg_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
return pool2d
# 3D upsampling
def upsample3d(x, scale_factor, scope=None):
''''
X shape is [nsample,dim,rows, cols, channel]
out shape is[nsample,dim*scale_factor,rows*scale_factor, cols*scale_factor, channel]
'''
x_shape = tf.shape(x)
k = tf.ones([scale_factor, scale_factor, scale_factor, x_shape[-1], x_shape[-1]])
# note k.shape = [dim,rows, cols, depth_in, depth_output]
output_shape = tf.stack(
[x_shape[0], x_shape[1] * scale_factor, x_shape[2] * scale_factor, x_shape[3] * scale_factor, x_shape[4]])
upsample = tf.nn.conv3d_transpose(value=x, filter=k, output_shape=output_shape,
strides=[1, scale_factor, scale_factor, scale_factor, 1],
padding='SAME', name=scope)
return upsample
# 2D upsampling
def upsample2d(x, scale_factor, scope=None):
''''
X shape is [nsample,dim,rows, cols, channel]
out shape is[nsample,dim*scale_factor,rows*scale_factor, cols*scale_factor, channel]
'''
x_shape = tf.shape(x)
k = tf.ones([scale_factor, scale_factor, x_shape[-1], x_shape[-1]])
# note k.shape = [dim,rows, cols, depth_in, depth_output]
output_shape = tf.stack(
[x_shape[0], x_shape[1] * scale_factor, x_shape[2] * scale_factor, x_shape[-1]])
upsample = tf.nn.conv2d_transpose(value=x, filter=k, output_shape=output_shape,
strides=[1, scale_factor, scale_factor, 1], padding='SAME', name=scope)
return upsample
# 3D deconvolution
def deconv3d(x, W, samefeature=False, depth=False):
"""
depth flag:False is z axis is same between input and output,true is z axis is input is twice than output
"""
x_shape = tf.shape(x)
if depth:
if samefeature:
output_shape = tf.stack([x_shape[0], x_shape[1] * 2, x_shape[2] * 2, x_shape[3] * 2, x_shape[4]])
else:
output_shape = tf.stack([x_shape[0], x_shape[1] * 2, x_shape[2] * 2, x_shape[3] * 2, x_shape[4] // 2])
deconv = tf.nn.conv3d_transpose(x, W, output_shape, strides=[1, 2, 2, 2, 1], padding='SAME')
else:
if samefeature:
output_shape = tf.stack([x_shape[0], x_shape[1] * 2, x_shape[2] * 2, x_shape[3], x_shape[4]])
else:
output_shape = tf.stack([x_shape[0], x_shape[1] * 2, x_shape[2] * 2, x_shape[3], x_shape[4] // 2])
deconv = tf.nn.conv3d_transpose(x, W, output_shape, strides=[1, 2, 2, 1, 1], padding='SAME')
return deconv
# 2D deconvolution
def deconv2d(x, W, samefeature=False):
"""
depth flag:False is z axis is same between input and output,true is z axis is input is twice than output
"""
x_shape = tf.shape(x)
if samefeature:
output_shape = tf.stack([x_shape[0], x_shape[1] * 2, x_shape[2] * 2, x_shape[3]])
else:
output_shape = tf.stack([x_shape[0], x_shape[1] * 2, x_shape[2] * 2, x_shape[3] // 2])
deconv = tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, 2, 2, 1], padding='SAME')
return deconv
# Max Pooling
def max_pool3d(x, depth=True):
"""
depth flag:False is z axis is same between input and output,true is z axis is input is twice than output
"""
if depth:
pool3d = tf.nn.max_pool3d(x, ksize=[1, 2, 2, 2, 1], strides=[1, 2, 2, 2, 1], padding='SAME')
else:
pool3d = tf.nn.max_pool3d(x, ksize=[1, 2, 2, 1, 1], strides=[1, 2, 2, 1, 1], padding='SAME')
return pool3d
# Max Pooling
def max_pool2d(x):
"""
depth flag:False is z axis is same between input and output,true is z axis is input is twice than output
"""
pool2d = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
return pool2d
# Unet crop and concat
def crop_and_concat(x1, x2):
x1_shape = tf.shape(x1)
x2_shape = tf.shape(x2)
# offsets for the top left corner of the crop
offsets = [0, (x1_shape[1] - x2_shape[1]) // 2,
(x1_shape[2] - x2_shape[2]) // 2, (x1_shape[3] - x2_shape[3]) // 2, 0]
size = [-1, x2_shape[1], x2_shape[2], x2_shape[3], -1]
x1_crop = tf.slice(x1, offsets, size)
return tf.concat([x1_crop, x2], 4)
# Unet crop and concat
def crop_and_concat2d(x1, x2):
x1_shape = tf.shape(x1)
x2_shape = tf.shape(x2)
aa=x1.get_shape().as_list()
print(aa)
# print(x2_shape)
# offsets for the top left corner of the crop
offsets = [0, (x1_shape[1] - x2_shape[1]) // 2,
(x1_shape[2] - x2_shape[2]) // 2, 0]
size = [-1, x2_shape[1], x2_shape[2], -1]
x1_crop = tf.slice(x1, offsets, size)
return tf.concat([x1_crop, x2], 3)
# Batch Normalization
def normalizationlayer(x, is_train, height=None, width=None, image_z=None, norm_type=None, G=16, esp=1e-5, scope=None):
"""
:param x:input data with shap of[batch,height,width,channel]
:param is_train:flag of normalizationlayer,True is training,False is Testing
:param height:in some condition,the data height is in Runtime determined,such as through deconv layer and conv2d
:param width:in some condition,the data width is in Runtime determined
:param image_z:
:param norm_type:normalization type:support"batch","group","None"
:param G:in group normalization,channel is seperated with group number(G)
:param esp:Prevent divisor from being zero
:param scope:normalizationlayer scope
:return:
"""
with tf.name_scope(scope + norm_type):
if norm_type == None:
output = x
elif norm_type == 'batch':
output = tf.contrib.layers.batch_norm(x, center=True, scale=True, is_train=is_train)
elif norm_type == "group":
# tranpose:[bs,z,h,w,c]to[bs,c,z,h,w]following the paper
x = tf.transpose(x, [0, 4, 1, 2, 3])
N, C, Z, H, W = x.get_shape().as_list()
G = min(G, C)
if H == None and W == None and Z == None:
Z, H, W = image_z, height, width
x = tf.reshape(x, [-1, G, C // G, Z, H, W])
mean, var = tf.nn.moments(x, [2, 3, 4, 5], keep_dims=True)
x = (x - mean) / tf.sqrt(var + esp)
gama = tf.get_variable(scope + norm_type + 'group_gama', [C], initializer=tf.constant_initializer(1.0))
beta = tf.get_variable(scope + norm_type + 'group_beta', [C], initializer=tf.constant_initializer(0.0))
gama = tf.reshape(gama, [1, C, 1, 1, 1])
beta = tf.reshape(beta, [1, C, 1, 1, 1])
output = tf.reshape(x, [-1, C, Z, H, W]) * gama + beta
# tranpose:[bs,c,z,h,w]to[bs,z,h,w,c]following the paper
output = tf.transpose(output, [0, 2, 3, 4, 1])
return output
# Batch Normalization
def normalizationlayer2d(x, is_train, height=None, width=None, norm_type=None, G=16, esp=1e-5,
scope=None):
"""
:param x:input data with shap of[batch,height,width,channel]
:param is_train:flag of normalizationlayer,True is training,False is Testing
:param height:in some condition,the data height is in Runtime determined,such as through deconv layer and conv2d
:param width:in some condition,the data width is in Runtime determined
:param image_z:
:param norm_type:normalization type:support"batch","group","None"
:param G:in group normalization,channel is seperated with group number(G)
:param esp:Prevent divisor from being zero
:param scope:normalizationlayer scope
:return:
"""
with tf.name_scope(scope + norm_type):
if norm_type == None:
output = x
return output
elif norm_type == 'batch':
output = tf.contrib.layers.batch_norm(x, center=True, scale=True, is_train=is_train)
return output
elif norm_type == "group":
# tranpose:[bs,h,w,c]to[bs,c,h,w]following the paper
x = tf.transpose(x, [0, 3, 1, 2])
N, C, H, W = x.get_shape().as_list()
G = min(G, C)
if H == None and W == None:
H, W = height, width
x = tf.reshape(x, [-1, G, C // G, H, W])
mean, var = tf.nn.moments(x, [2, 3, 4], keep_dims=True)
x = (x - mean) / tf.sqrt(var + esp)
gama = tf.get_variable(scope + norm_type + 'group_gama', [C], initializer=tf.constant_initializer(1.0))
beta = tf.get_variable(scope + norm_type + 'group_beta', [C], initializer=tf.constant_initializer(0.0))
gama = tf.reshape(gama, [1, C, 1, 1])
beta = tf.reshape(beta, [1, C, 1, 1])
output = tf.reshape(x, [-1, C, H, W]) * gama + beta
# tranpose:[bs,c,h,w]to[bs,h,w,c]following the paper
output = tf.transpose(output, [0, 2, 3, 1])
return output
# resnet add_connect
def resnet_Add(x1, x2):
residual_connection = tf.add(x1, x2)
return residual_connection
def save_images(images, size, path):
img = (images + 1.0) / 2.0
# h, w, c = img.shape[0], img.shape[1], img.shape[2]
# merge_img = np.zeros((h * size[0], w * size[1]))
# for idx in range(c):
# i = idx % size[1]
# j = idx // size[1]
# merge_img[j * h:j * h + h, i * w:i * w + w] = images[:, :, idx]
# result = merge_img * 255.
img = np.clip(img, 0, 255).astype('uint8')
return cv2.imwrite(path, img)
def cross_entropy(y_,output_map):
return -tf.reduce_mean(y_*tf.log(tf.clip_by_value(output_map,1e-10,1.0)), name="cross_entropy")