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 | def residual_block(x, output_channel):
input_channel = x.get_shape().as_list()[ - 1 ]
if input_channel * 2 = = output_channel:
increase_dim = True
strides = ( 2 , 2 )
elif input_channel = = output_channel:
increase_dim = False
strides = ( 1 , 1 )
else :
raise Exception( "input channel can't match output channel" )
conv1 = tf.layers.conv2d(x,
output_channel,
( 3 , 3 ),
strides = strides,
padding = 'same' ,
activation = tf.nn.relu,
name = 'conv1' )
conv2 = tf.layers.conv2d(conv1,
output_channel,
( 3 , 3 ),
strides = ( 1 , 1 ),
padding = 'same' ,
activation = tf.nn.relu,
name = 'conv2' )
if increase_dim:
pooled_x = tf.layers.average_pooling2d(x,
( 2 , 2 ),
( 2 , 2 ),
padding = 'valid' )
padded_x = tf.pad(pooled_x,
[[ 0 , 0 ],
[ 0 , 0 ],
[ 0 , 0 ],
[input_channel / / 2 , input_channel / / 2 ]])
else :
padded_x = x
output_x = conv2 + padded_x
return output_x
|