【问题标题】:Sum of neighbors in tensorflow张量流中的邻居总和
【发布时间】:2019-12-11 17:07:37
【问题描述】:

我有一个张量流模型,其中我的真实数据的形状为 (N, 32, 32, 5),即。具有 5 个通道的 32x32 图像。

在损失函数内部,我想为每个像素计算每个通道的相邻像素值的总和,生成一个新的 (N, 32, 32, 5) 张量。

tf.nn.pool 函数做了类似的事情,但不完全符合我的需要。我试图看看 tf.nn.conv2d 是否可以让我到达那里,但我不确定在这种情况下我需要使用什么作为过滤器参数。

是否有特定的功能?或者我可以以某种方式使用 conv2d 吗?

【问题讨论】:

  • initialize tf.nn.conv2d 全部为一 - 这将为您提供所需的卷积
  • 当您说“相邻像素”时,会包括像素本身吗?

标签: python tensorflow


【解决方案1】:

您可以像这样使用tf.nn.separable_conv2d 做到这一点

import tensorflow as tf

input = tf.placeholder(tf.float32, [None, 32, 32, 5])
# Depthwise filter adds the neighborhood of each pixel per channel
depthwise_filter = tf.ones([3, 3, 5, 1], input.dtype)
# Pointwise filter does not do anything
pointwise_filter = tf.eye(5, batch_shape=[1, 1], dtype=input.dtype)
output = tf.nn.separable_conv2d(input, depthwise_filter, pointwise_filter,
                                strides=[1, 1, 1, 1], padding='SAME')
print(output.shape)
# (?, 32, 32, 5)

下面使用tf.nn.conv2d的方法也是等价的:

import tensorflow as tf

input = tf.placeholder(tf.float32, [None, 32, 32, 5])
# Each filter adds the neighborhood for a different channel
filter = tf.eye(5, batch_shape=[3, 3], dtype=input.dtype)
output = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')

【讨论】:

    【解决方案2】:

    过滤器大小为 3x3 且过滤器初始化为 1 的新卷积层将完成这项工作。请注意将此特殊过滤器声明为不可训练的变量,否则您的优化器会更改其内容。此外,将填充设置为“相同”以从该卷积层获得相同大小的输出。在这种情况下,边缘的像素将有零个邻居。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 2021-10-03
      • 2021-08-06
      • 2020-01-29
      • 2016-12-23
      相关资源
      最近更新 更多