在 FCN、segnet 等张量流编码器-解码器模型中,如何给出任意输入大小?

In tensorflow encoder-decoder model like FCN, segnet, how to give arbitrary input size?

对于张量流中的图像语义分割模型,我需要为 FCN 或 segnet 模型提供可变大小的输入。所以我使用占位符:

self.input= tf.placeholder(tf.float32, shape=(batch_size,None,None,3),name='')

在encoder-decoder模型中,我需要在maxpooling之后unpool,代码如下:

#upsample fuction used in decoder
def _upsample_along_axis(volume, axis, stride, mode='COPY'):
    shape = volume.get_shape().as_list()
    assert 0 <= axis < len(shape)
    target_shape = shape[:]
    target_shape[axis] *= stride
    padding = tf.zeros(shape, dtype=volume.dtype) if mode == 'ZEROS' else volume
    parts = [volume] + [padding for _ in range(stride - 1)]
    volume = tf.concat(parts, min(axis+1, len(shape)-1))
    target_shape = np.array(target_shape)
    target_shape[0] = -1
    volume = tf.reshape(volume, target_shape)

    return volume

现在 target_shape[axis] 为 none,因此 target_shape[axis] *= stride 将引发错误: 类型错误:* 不支持的操作数类型:'NoneType' 和 'int'

所以encoder-decoder模型的输入可以是可变的?

Tensor 的get_shape 函数returns 推断形状,对于动态维度推断形状是None。 TensorFlow 中有一个函数 returns 动态形状:tf.shape

此函数 returns 张量将评估为形状的实际值。

还有两个函数可能对您的情况有用:tf.pad and tf.tile