Reputation: 1208
I have written a code for deconvolution layer,
def deconv2d(x, W,stride):
x_shape = tf.shape(x)
output_shape = tf.stack([x_shape[0], x_shape[1]*2, x_shape[2]*2, x_shape[3]//2])
decon = tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, stride, stride, 1], padding='SAME')
layer_shape = get_layer_shape(decon)
print('DECONV Shape : ', layer_shape)
return decon
I have called the above function like this way,
deconvolution1 = deconv2d(x=cnn_layer10, W=[2,2,512,1024], stride=2)
getting this error,
File "u-net.py", line 84, in obj.computation() File "u-net.py", line 41, in computation deconvolution1 = deconv2d(x=cnn_layer10, W=[2,2,512,1024], stride=2) File "/home/shuvo/u-net/architecture.py", line 35, in deconv2d decon = tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, stride, stride, 1], padding='SAME') File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/nn_ops.py", line 1019, in conv2d_transpose if not value.get_shape()[axis].is_compatible_with(filter.get_shape()[3]):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_shape.py", line 500, in getitem return self._dims[key] IndexError: list index out of range
My intention is to make a deconvolution layer where shape should be, [batch_size, 36,36,1024]=>[batch_size,72,72,512]. Please help me to fix this error,
Upvotes: 0
Views: 762
Reputation: 17191
The input args filter
to tf.nn.conv2d_transpose
is the weights matrix itself and not just the size of the filter.
The modified code that fixes the above problem is shown below:
cnn_layer10 = tf.placeholder(tf.float32, (10, 36, 36, 1024))
def deconv2d(x, W,stride):
x_shape = tf.shape(x)
weights = tf.Variable(tf.random_normal(W))
output_shape = tf.stack([x_shape[0], x_shape[1]*2, x_shape[2]*2, x_shape[3]//2])
decon = tf.nn.conv2d_transpose(x, weights, output_shape, strides=[1, stride, stride, 1], padding='SAME')
return decon
deconvolution1 = deconv2d(x=cnn_layer10, W=[2,2,512,1024], stride=2)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print('Decon Shape:',sess.run(deconvolution1, {cnn_layer10: np.random.random((10, 36,36,1024))}).shape)
#Output
#Decon Shape: (10, 72, 72, 512)
Note: Its better to use tf.layers.conv2d_transpose
API, where the filters
arg is the filter size and the weights initialization happens within.
Upvotes: 1