理解tf.nn.depthwise_conv2d

2024-05-16 07:09:43 发布

您现在位置:Python中文网/ 问答频道 /正文

https://www.tensorflow.org/api_docs/python/tf/nn/depthwise_conv2d

Given a 4D input tensor ('NHWC' or 'NCHW' data formats) and a filter tensor of shape [filter_height, filter_width, in_channels, channel_multiplier] containing in_channels convolutional filters of depth 1, depthwise_conv2d applies a different filter to each input channel (expanding from 1 channel to channel_multiplier channels for each), then concatenates the results together. The output has in_channels * channel_multiplier channels

  1. 什么是“从1个通道扩展到每个通道的通道”
  2. 是否可能有外部通道<;在你的频道
  3. 有没有可能把输入张量分成像Pytorchhttps://pytorch.org/docs/stable/nn.html#conv2d这样的组

例如:

import tensorflow as tf
import numpy as np
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

np.random.seed(2020)

print('tf.__version__', tf.__version__)

def get_data_batch():
    bs = 2
    h = 3
    w = 3
    c = 4

    x_np = np.random.rand(bs, h, w, c)
    x_np = x_np.astype(np.float32)
    print('x_np.shape', x_np.shape)

    return x_np


def run_conv_dw():
    print('='*60)
    x_np = get_data_batch()
    in_channels = x_np.shape[-1]
    kernel_size = 3
    channel_multiplier = 1
    with tf.Session() as sess:
        x_tf = tf.convert_to_tensor(x_np)
        filter = tf.get_variable('w1', [kernel_size, kernel_size, in_channels, channel_multiplier],
                                 initializer=tf.contrib.layers.xavier_initializer())
        z_tf = tf.nn.depthwise_conv2d(x_tf, filter=filter, strides=[1, 1, 1, 1], padding='SAME')

        sess.run(tf.global_variables_initializer())
        z_np = sess.run(fetches=[z_tf], feed_dict={x_tf: x_np})[0]
        print('z_np.shape', z_np.shape)


if '__main__' == __name__:
    run_conv_dw()

通道倍增器不能浮动:

如果channel_multiplier = 1

x_np.shape (2, 3, 3, 4)
z_np.shape (2, 3, 3, 4)

如果channel_multiplier = 2

x_np.shape (2, 3, 3, 4)
z_np.shape (2, 3, 3, 8)

Tags: runindatatfnpchannelnnfilter
1条回答
网友
1楼 · 发布于 2024-05-16 07:09:43

用英语来说:

  1. 每组始终有一个输入通道,“通道\倍增器”输出 每组通道数
  2. 不是一步到位
  3. 见1

我看到了一种方法来模拟每个组的几个输入通道。对于两张,执行depthwise_conv2d,然后将结果张量作为一副牌对半分割,然后将获得的两半元素相加(在relu等之前)。注意,输入通道号i将与i+inputs/2一分组


编辑:上面的技巧对于小的组很有用,对于大的组,只需将输入张量拆分为N个部分,其中N是组计数,分别对每个部分进行conv2d,然后连接结果

相关问题 更多 >