26

是否有 TensorFlow 本机函数可以对反卷积网络进行反池化?

我已经用普通的 python 编写了这个,但是当想要将它翻译到 TensorFlow 时它变得越来越复杂,因为它的对象目前甚至不支持项目分配,我认为这对 TF 来说是一个很大的不便。

4

6 回答 6

16

我不认为有一个官方的解池层令人沮丧,因为你必须使用图像调整大小(双线性插值或最近邻),这就像一个平均解池操作,它真的很慢。查看“图像”部分中的 tf api,您会发现它。

Tensorflow 有一个 maxpooling_with_argmax 东西,您可以在其中获得最大池输出以及激活图,这很好,因为您可以在解池层中使用它来保留“丢失”的空间信息,但似乎没有这样的解池操作可以。我猜他们正计划添加它......很快。

编辑:一周前我在谷歌讨论中发现了一些人,他似乎已经实现了这样的东西,但我个人还没有尝试过。 https://github.com/ppwwyyxx/tensorpack/blob/master/tensorpack/models/pool.py#L66

于 2016-04-11T19:52:06.597 回答
11

pooling.py 这里有几个 tensorflow实现

即:

1 )利用tf.nn.max_pool_with_argmax. 虽然请注意,从 tensorflow 1.0 开始tf.nn.max_pool_with_argmax,只有 GPU

2)通过用零或最大元素的副本填充未池化区域的位置来模拟最大池化的逆的上采样操作。与tensorpack相比,它允许元素的副本而不是零,并支持除[2, 2].

无需重新编译,支持支持。

插图: 上采样

取消池化

于 2017-03-02T07:31:13.320 回答
5

我正在寻找最大取消池操作并尝试实现它。当我在 CUDA 上苦苦挣扎时,我想出了一些用于渐变的 hacky 实现。

代码在这里,您需要从源代码构建它并支持 GPU。下面是一个演示应用程序。但是没有任何保证!

此操作还存在一个未解决的问题。

import tensorflow as tf
import numpy as np

def max_pool(inp, k=2):
    return tf.nn.max_pool_with_argmax_and_mask(inp, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding="SAME")

def max_unpool(inp, argmax, argmax_mask, k=2):
    return tf.nn.max_unpool(inp, argmax, argmax_mask, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding="SAME")

def conv2d(inp, name):
    w = weights[name]
    b = biases[name]
    var = tf.nn.conv2d(inp, w, [1, 1, 1, 1], padding='SAME')
    var = tf.nn.bias_add(var, b)
    var = tf.nn.relu(var)
    return var

def conv2d_transpose(inp, name, dropout_prob):
    w = weights[name]
    b = biases[name]

    dims = inp.get_shape().dims[:3]
    dims.append(w.get_shape()[-2]) # adpot channels from weights (weight definition for deconv has switched input and output channel!)
    out_shape = tf.TensorShape(dims)

    var = tf.nn.conv2d_transpose(inp, w, out_shape, strides=[1, 1, 1, 1], padding="SAME")
    var = tf.nn.bias_add(var, b)
    if not dropout_prob is None:
        var = tf.nn.relu(var)
        var = tf.nn.dropout(var, dropout_prob)
    return var


weights = {
    "conv1":    tf.Variable(tf.random_normal([3, 3,  3, 16])),
    "conv2":    tf.Variable(tf.random_normal([3, 3, 16, 32])),
    "conv3":    tf.Variable(tf.random_normal([3, 3, 32, 32])),
    "deconv2":  tf.Variable(tf.random_normal([3, 3, 16, 32])),
    "deconv1":  tf.Variable(tf.random_normal([3, 3,  1, 16])) }

biases = {
    "conv1":    tf.Variable(tf.random_normal([16])),
    "conv2":    tf.Variable(tf.random_normal([32])),
    "conv3":    tf.Variable(tf.random_normal([32])),
    "deconv2":  tf.Variable(tf.random_normal([16])),
    "deconv1":  tf.Variable(tf.random_normal([ 1])) }


## Build Miniature CEDN
x = tf.placeholder(tf.float32, [12, 20, 20, 3])
y = tf.placeholder(tf.float32, [12, 20, 20, 1])
p = tf.placeholder(tf.float32)

conv1                                   = conv2d(x, "conv1")
maxp1, maxp1_argmax, maxp1_argmax_mask  = max_pool(conv1)

conv2                                   = conv2d(maxp1, "conv2")
maxp2, maxp2_argmax, maxp2_argmax_mask  = max_pool(conv2)

conv3                                   = conv2d(maxp2, "conv3")

maxup2                                  = max_unpool(conv3, maxp2_argmax, maxp2_argmax_mask)
deconv2                                 = conv2d_transpose(maxup2, "deconv2", p)

maxup1                                  = max_unpool(deconv2, maxp1_argmax, maxp1_argmax_mask)
deconv1                                 = conv2d_transpose(maxup1, "deconv1", None)


## Optimizing Stuff
loss        = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(deconv1, y))
optimizer   = tf.train.AdamOptimizer(learning_rate=1).minimize(loss)


## Test Data
np.random.seed(123)
batch_x = np.where(np.random.rand(12, 20, 20, 3) > 0.5, 1.0, -1.0)
batch_y = np.where(np.random.rand(12, 20, 20, 1) > 0.5, 1.0,  0.0)
prob    = 0.5


with tf.Session() as session:
    tf.set_random_seed(123)
    session.run(tf.initialize_all_variables())

    print "\n\n"
    for i in range(10):
        session.run(optimizer, feed_dict={x: batch_x, y: batch_y, p: prob})
        print "step", i + 1
        print "loss",  session.run(loss, feed_dict={x: batch_x, y: batch_y, p: 1.0}), "\n\n"

编辑 29.11.17

前段时间,我针对 TensorFlow 1.0 以干净的方式重新实现了它,前向操作也可作为 CPU 版本使用。你可以在这个分支中找到它,如果你想使用它,我建议你查看最后几个提交。

于 2016-08-26T13:06:37.230 回答
1

现在有一个 TensorFlow 插件MaxUnpooling2D

Unpool 最大池化操作的输出。

tfa.layers.MaxUnpooling2D(
    pool_size: Union[int, Iterable[int]] = (2, 2),
    strides: Union[int, Iterable[int]] = (2, 2),
    padding: str = 'SAME',
    **kwargs
)

此类可以例如用作

import tensorflow as tf
import tensorflow_addons as tfa

pooling, max_index = tf.nn.max_pool_with_argmax(input, 2, 2, padding='SAME')
unpooling = tfa.layers.MaxUnpooling2D()(pooling, max_index)
于 2022-02-17T19:31:46.857 回答
0

我检查了这里提到的shagas 它正在工作。

x = [[[[1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3]],
  [[1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3]],
[[1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3],
  [1, 1, 2,2, 3, 3]]]]

x = np.array(x)

inp = tf.convert_to_tensor(x)

out = UnPooling2x2ZeroFilled(inp)

out
Out[19]: 
<tf.Tensor: id=36, shape=(1, 6, 12, 6), dtype=int64, numpy=
array([[[[1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0]],

        [[0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0]],

        [[1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0]],

        [[0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0]],

        [[1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0],
         [1, 1, 2, 2, 3, 3],
         [0, 0, 0, 0, 0, 0]],

        [[0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0]]]])>


out1 = tf.keras.layers.MaxPool2D()(out)

out1
Out[37]: 
<tf.Tensor: id=118, shape=(1, 3, 6, 6), dtype=int64, numpy=
array([[[[1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3]],

        [[1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3]],

        [[1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3],
         [1, 1, 2, 2, 3, 3]]]])>

如果您需要最大取消池,那么您可以使用(尽管我没有检查)这个

于 2019-11-04T05:30:22.250 回答
0

这是我的实现。您应该使用tf.nn.max_pool_with_argmax应用最大池,然后传递argmax结果tf.nn.max_pool_with_argmax

def unpooling(inputs, output_shape, argmax):
        """
        Performs unpooling, as explained in:
        https://www.oreilly.com/library/view/hands-on-convolutional-neural/9781789130331/6476c4d5-19f2-455f-8590-c6f99504b7a5.xhtml
        :param inputs: Input Tensor.
        :param output_shape: Desired output shape. For example, on 2D unpooling, this should be 4D (because of number of samples and channels).
        :param argmax: Result argmax from tf.nn.max_pool_with_argmax
            https://www.tensorflow.org/api_docs/python/tf/nn/max_pool_with_argmax
        """
        flat_output_shape = tf.cast(tf.reduce_prod(output_shape), tf.int64)

        updates = tf.reshape(inputs, [-1])
        indices = tf.expand_dims(tf.reshape(argmax, [-1]), axis=-1)

        ret = tf.scatter_nd(indices, updates, shape=[flat_output_shape])
        ret = tf.reshape(ret, output_shape)
        return ret

这有一个小错误/功能,即如果 argmax 具有重复值,它将执行加法,而不是仅将值放入一次。如果步幅为 1,请注意这一点。但是,我不知道是否需要这样做。

于 2021-03-18T16:01:38.307 回答