tf.nn.space_to_depth(tf.space_to_depth)

介绍

把[batch, height, width, channels]形状的Tensor,转变成[batch,1,1, height * width * channels]

从图像角度来看,将 height 和 width 的数据 转移到 channels 维度

常用别名

  • tf.space_to_depth

语法

tf.nn.space_to_depth(
    input,
    block_size,
    data_format='NHWC',
    name=None
)
参数:
  • input:tensor
  • block_size:block大小,一般 >= 2
  • data_format:string来自的可选”NHWC”, “NCHW”, “NCHW_VECT_C”。默认为”NHWC”
    • NHWC:[ batch, height, width, channels ]
    • NCHW:[ batch, channels, height, width ]
    • NCHW_VECT_C:未知
返回值
  • tensor

例子

例子1

arr=np.array([[[[1], [2]],
                  [[3], [4]]]])

print(arr.shape)

tf.nn.space_to_depth(arr,2)

执行结果:

(1, 2, 2, 1)
<tf.Tensor: id=659, shape=(1, 1, 1, 4), dtype=int64, numpy=array([[[[1, 2, 3, 4]]]])>

例子2

arr=tf.reshape(tf.range(16),(1,4,4,1))
print(arr)
print("-"*50)
res=tf.nn.space_to_depth(arr,2)
print(res)

执行结果:

tf.Tensor(
[[[[ 0]
   [ 1]
   [ 2]
   [ 3]]

  [[ 4]
   [ 5]
   [ 6]
   [ 7]]

  [[ 8]
   [ 9]
   [10]
   [11]]

  [[12]
   [13]
   [14]
   [15]]]], shape=(1, 4, 4, 1), dtype=int32)
--------------------------------------------------
tf.Tensor(
[[[[ 0  1  4  5]
   [ 2  3  6  7]]

  [[ 8  9 12 13]
   [10 11 14 15]]]], shape=(1, 2, 2, 4), dtype=int32)

例子3

arr = np.arange(150).reshape(2,5,5,3)

print(arr.shape)

tf.nn.space_to_depth(arr,5)

执行结果:

(2, 5, 5, 3)
<tf.Tensor: id=662, shape=(2, 1, 1, 75), dtype=int64, numpy=
array([[[[  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,
           12,  13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,
           24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,
           36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
           48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,
           60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,
           72,  73,  74]]],


       [[[ 75,  76,  77,  78,  79,  80,  81,  82,  83,  84,  85,  86,
           87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,
           99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
          111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
          123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
          135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146,
          147, 148, 149]]]])>

感谢:
https://blog.csdn.net/qq_36556893/article/details/89471553


原文出处:/show_1EF5ECmtZF7F.html