来自https://www.tensorflow.org/api_docs/python/tf/nn/depthwise_conv2d
给出了一个4D输入张量('NHWC‘或'NCHW’数据格式)和一个包含深度为1的in_channels卷积滤波器的形状filter_height、filter_width、in_channels、channel_multiplier的滤波器张量,depthwise_conv2d对每个输入通道应用不同的滤波器(每个通道扩展为channel_multiplier通道),然后将结果连在一起。输出有in_channels * channel_multiplier通道
什么是“每个通道从一个通道扩展到in_channels?
中那样的组?
示例:
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)
发布于 2020-03-17 15:15:17
用比目鱼术语来说:
我看到了一种方法来模拟每个组的几个输入通道。第二步,做depthwise_conv2d
,然后将结果张量分割成一副牌,然后将获得的和按元素减半(在relu等之前)。注意,输入通道号i
将与i+inputs/2
1一起分组。
编辑:上面的技巧对于小的组很有用,对于大的组只对N个部分分裂输入张量,其中N是组计数,使每个独立的conv2d
,然后连接结果。
https://stackoverflow.com/questions/60724571
复制相似问题