如何在 tensorflow 2 中进行空洞卷积 (tf.keras)

How to do atrous convolution in tensorflow 2 (tf.keras)

我正在尝试将一些代码从 tensorflow 1.x 转换为 tensorflow 2.x。 到目前为止一切顺利,但我陷入了空洞卷积。与其他层不同,似乎没有一对一的转换。

到目前为止,我一直在将所有内容统一到 tf.keras。有一个纯 keras 实现 here and a tf.nn.atrous_conv2d implementation here 但我也不确定我是否可以在 tf.keras.Model 函数 api.

中使用它们

代码如下:

with tf.variable_scope('aconv1d_' + name):
        shape = [None, 30, 128]
        kernel = tf.get_variable('kernel', (1, size, shape[-1], n_filters), dtype=tf.float32,
                                 initializer=tf.contrib.layers.xavier_initializer())
        if bias:
            b = tf.get_variable('b', [shape[-1]], dtype=tf.float32, initializer=tf.constant_initializer(0))
        out = tf.nn.atrous_conv2d(tf.expand_dims(input_tensor, dim=1), kernel, rate=rate, padding='SAME') + (
            b if bias else 0)
        out = tf.squeeze(out, [1])

        return out

我只想转换它,将其粘贴到 keras 函数中 api,执行 model.fit,然后 运行。

感谢您帮助像我这样的菜鸟。

A​​trous Convolution 或 Dilated Convolution 在 tensorflow2.x 版本中已经可用,通过参数“dilation_rate”。默认情况下,它设置为 (1,1),如果您查看 https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D。将其修改为其他值,例如 (2,2),您将得到 dilated/atrous 卷积。