【Tensorflow】极值反池化

1,极大只反池化操作需要记录池化的坐标值,根据坐标值进行映射,如图:

                                         

2,使用tf.nn.max_pool_with_argmax 得到极大值坐标值的 flattened index ((b * height + y) * width + x) * channels + c.,

使用该值解析出坐标的值。

#极值池化

def max_pool_with_argmax(net,stride): 
    net, mask = tf.nn.max_pool_with_argmax( net,ksize=[1, stride, stride, 1], strides=[1, stride, stride, 1],padding='SAME')
    return net,mask

#极值反池化

def unpool(net,mask,stride):
    ksize = [1, stride, stride, 1]
    input_shape = net.get_shape().as_list()
    #  calculation new shape
    output_shape = (input_shape[0], input_shape[1] * ksize[1], input_shape[2] * ksize[2], input_shape[3])
    # calculation indices for batch, height, width and feature maps
    one_like_mask = tf.ones_like(mask)
    batch_range = tf.reshape(tf.range(output_shape[0], dtype=tf.int64), shape=[input_shape[0], 1, 1, 1])
    b = one_like_mask * batch_range
    y = mask // (output_shape[2] * output_shape[3])
    x = mask % (output_shape[2] * output_shape[3]) // output_shape[3]
    feature_range = tf.range(output_shape[3], dtype=tf.int64)
    f = one_like_mask * feature_range
    # transpose indices & reshape update values to one dimension
    updates_size = tf.size(net)
    indices = tf.transpose(tf.reshape(tf.stack([b, y, x, f]), [4, updates_size]))
    values = tf.reshape(net, [updates_size])
    ret = tf.scatter_nd(indices, values, output_shape)
    return ret

参考:https://tensorflow.google.cn/versions/r1.8/api_docs/python/tf/nn/max_pool_with_argmax

https://www.w3cschool.cn/tensorflow_python/tensorflow_python-led42j40.html

https://www.cnblogs.com/zyly/p/8991412.html

猜你喜欢

转载自blog.csdn.net/qq_34106574/article/details/84583359