I would like to use tf.nn.conv2d_transpose
to build a deconvolution layer for a GAN network.
I would like to create a function deconv_layer
. It generates a new layer, which outputs filter_num
filters with expand_size
times the resolution of the input.
My code is:
def deconv_layer(x, filter_num, kernel_size=5, expand_size=2):
x_shape = x.get_shape().as_list()
with tf.name_scope('deconv_'+str(filter_num)):
size_in = x_shape[-1]
size_out = filter_num
w = tf.Variable(tf.random_normal([kernel_size, kernel_size, size_in, size_out], mean=0.0, stddev=0.125), name="W")
b = tf.Variable(tf.random_normal([size_out], mean=0.0, stddev=0.125), name="B")
conv = tf.nn.conv2d_transpose(x, w, output_shape=[-1, x_shape[-3]*expand_size, x_shape[-2]*expand_size, filter_num], strides=[1,expand_size,expand_size,1], padding="SAME")
act = tf.nn.relu(tf.nn.bias_add(conv, b))
tf.summary.histogram('weights', w)
tf.summary.histogram('biases', b)
tf.summary.histogram('activations', act)
return act
The error message:
ValueError: input channels does not match filter's input channels
At conv = tf.nn.conv2d_transpose(...)
I am not sure if I use tf.nn.conv2d_transpose
properly. I tried to create it based on a convolutional layer.
The filter dimension is wrong. According to the docs:
filter: A 4-D Tensor with the same type as value and shape [height, width, output_channels, in_channels]. filter's in_channels dimension must match that of value (input).
You need to change your w
size to :
w = tf.Variable(tf.random_normal([kernel_size, kernel_size, size_out, size_in], mean=0.0, stddev=0.125), name="W")