Skip to content

Instantly share code, notes, and snippets.

@glhfgg1024
Created August 7, 2017 03:23
Show Gist options
  • Save glhfgg1024/cbaad3779e0c70b1644118549982f5f7 to your computer and use it in GitHub Desktop.
Save glhfgg1024/cbaad3779e0c70b1644118549982f5f7 to your computer and use it in GitHub Desktop.
tensorflow convert fully connected layer to convolutional layer
def vgg16_fc_convolution(in_put, out_channel, layer_name, use_relu=True):
with tf.variable_scope(layer_name):
input_shape = in_put.get_shape()
assert len(input_shape) == 4
height, width, in_channel = input_shape[1:]
print(height, width, in_channel)
weights = tf.get_variable(name="weights", shape=[height*width*in_channel, out_channel])
biases = tf.get_variable(name="biases", shape=[out_channel])
reshape_weights = tf.reshape(weights,
shape=[tf.to_int32(height), tf.to_int32(width),
tf.to_int32(in_channel), out_channel])
convolution_output = tf.nn.conv2d(input=in_put, filter=reshape_weights, strides=[1, 1, 1, 1],
padding="VALID")
output = tf.nn.bias_add(convolution_output, biases)
if use_relu:
output = tf.nn.relu(output)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment