形状未知的张量流中的空间金字塔池

Spacial pyramid pooling in tensorflow with unknown shape

我想在张量流中执行空间金字塔池化。这已得到解答 there(以及 whosebug.com 中的其他问题),但建议的解决方案不适用于未知输入形状。

是否有在图形定义时处理未知形状的实现?

为了解决这个问题,我想出了一个不同的实现,它使用一个掩码,使用最近的邻居重新缩放:

def avg_spp(self, input, scale, name, padding=DEFAULT_PADDING):
    eye = tf.eye(scale*scale, batch_shape=(tf.shape(input)[0],))
    mask = tf.reshape(eye, (-1, scale, scale, scale*scale))
    mask = tf.image.resize_nearest_neighbor(mask, tf.shape(input)[1:3])
    spp = tf.multiply(tf.expand_dims(input, 4), tf.expand_dims(mask, 3))
    spp = tf.divide(tf.reduce_sum(spp, axis=[1,2]), tf.cast(tf.count_nonzero(spp, axis=[1,2]), tf.float32))
    spp = tf.reshape(spp, (-1, tf.shape(input)[3], scale, scale))
    spp = tf.transpose(spp, [0,2,3,1], name=name)
    return spp