TensorFlow 踩坑 conv2d_transpose
用Tensorlayer 搭建网络,将Deconv2dLayer封装成一个函数,提供一些默认实参,方便调用:
w_init = tf.random_normal_initializer(stddev=0.02)
b_init = None
g_init = tf.random_normal_initializer(1., 0.02)
def deconv2d(layer, out_channels=128, filter_size=3, out_size=(256,256), strides=(1, 2, 2, 1), act=tf.identity, W_init=w_init, b_init=b_init, name='deconv2d'):
    """
    shape - shape of filter : [height, width, out_channels, in_channels]
    output_shape - shape of outputs
    """
    batch, h, w, in_channels = layer.outputs.get_shape().as_list()   
    filter_shape = (filter_size, filter_size, out_channels, in_channels)
    output_shape = (batch, out_size[0], out_size[1], out_channels)
    return tl.layers.DeConv2dLayer(layer, act=act, shape=filter_shape, output_shape=output_shape, strides=strides, padding='SAME', W_init=W_init, b_init=b_init, W_init_args=None, b_init_args=None, name=name)

TensorLayer的Deconv2dLayer是对tf.nn.conv2d_transpose()的封装。

在自定义网络中使用:

batch, w, h, in_channels = lf_extra.shape

n = deconv2d(n, out_channels=channels_interp, filter_size=3, out_size=(h, w), name = 'interp/deconv%d' % i)

运行时出现错误:

TypeError: Failed to convert object of type <type 'tuple'> to Tensor. Contents: (16, Dimension(16), Dimension(16), 128). Consider casting elements to a supported type.
根据错误提示,Deconv2dLayer的output_shape实参从传入的h,w变成了Dimension(16):
(16, Dimension(16), Dimension(16), 128)

在deconv2d中将out_size强制类型转换为int, 则运行成功。

output_shape = (batch, out_size[0], out_size[1], out_channels)
============================================================================

查看TensorLayer的simplified  Layer, 其中有对Deonv2dLayer的封装:DeConv2d,对out_size同样进行了强制类型转换:

output_shape=(batch_size, int(out_size[0]), int(out_size[1]), n_filter)

但是对filter_sizestrides却没有。

原因没有找到。



阅读更多
文章标签: tensorflow
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

TensorFlow 踩坑 conv2d_transpose

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭