DepthToSpace#

  • Domain: ai.onnx

  • Since version: 13

DepthToSpace rearranges (permutes) data from depth into blocks of spatial data. This is the reverse transformation of SpaceToDepth. More specifically, this op outputs a copy of the input tensor where values from the depth dimension are moved in spatial blocks to the height and width dimensions. By default, mode = DCR. In the DCR mode, elements along the depth dimension from the input tensor are rearranged in the following order: depth, column, and then row. The output y is computed from the input x as below:

b, c, h, w = x.shape
tmp = np.reshape(x, [b, blocksize, blocksize, c // (blocksize**2), h, w])
tmp = np.transpose(tmp, [0, 3, 4, 1, 5, 2])
y = np.reshape(tmp, [b, c // (blocksize**2), h * blocksize, w * blocksize])

In the CRD mode, elements along the depth dimension from the input tensor are rearranged in the following order: column, row, and the depth. The output y is computed from the input x as below:

b, c, h, w = x.shape
tmp = np.reshape(x, [b, c // (blocksize ** 2), blocksize, blocksize, h, w])
tmp = np.transpose(tmp, [0, 1, 4, 2, 5, 3])
y = np.reshape(tmp, [b, c // (blocksize ** 2), h * blocksize, w * blocksize])

Inputs

  • input (T): Input tensor of [N,C,H,W], where N is the batch axis, C is the channel or depth, H is the height and W is the width.

Outputs

  • output (T): Output tensor of [N, C/(blocksize * blocksize), H * blocksize, W * blocksize].

Attributes

  • blocksize (int): Blocks of [blocksize, blocksize] are moved.

  • mode (string): DCR (default) for depth-column-row order re-arrangement. Use CRD for column-row-depth order.

Type Constraints

  • T: Constrain input and output types to all tensor types. Allowed types: tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

Examples#

test_cc_depthtospace_crd

Node:
  DepthToSpace(input) -> (output)
  Attributes:
    blocksize = 2
    mode = "CRD"
Inputs:
  input: shape=(1, 8, 2, 3), dtype=float32
    [[[[ 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.]]]]

Outputs:
  output: shape=(1, 2, 4, 6), dtype=float32
    [[[[ 0.,  6.,  1.,  7.,  2.,  8.],
       [12., 18., 13., 19., 14., 20.],
       [ 3.,  9.,  4., 10.,  5., 11.],
       [15., 21., 16., 22., 17., 23.]],

      [[24., 30., 25., 31., 26., 32.],
       [36., 42., 37., 43., 38., 44.],
       [27., 33., 28., 34., 29., 35.],
       [39., 45., 40., 46., 41., 47.]]]]

test_cc_depthtospace_crd_mode_example

Node:
  DepthToSpace(input) -> (output)
  Attributes:
    blocksize = 2
    mode = "CRD"
Inputs:
  input: shape=(1, 8, 2, 3), dtype=float32
    [[[[ 0.,  1.,  2.],
       [ 3.,  4.,  5.]],

      [[ 9., 10., 11.],
       [12., 13., 14.]],

      [[18., 19., 20.],
       [21., 22., 23.]],

      [[27., 28., 29.],
       [30., 31., 32.]],

      [[36., 37., 38.],
       [39., 40., 41.]],

      [[45., 46., 47.],
       [48., 49., 50.]],

      [[54., 55., 56.],
       [57., 58., 59.]],

      [[63., 64., 65.],
       [66., 67., 68.]]]]

Outputs:
  output: shape=(1, 2, 4, 6), dtype=float32
    [[[[ 0.,  9.,  1., 10.,  2., 11.],
       [18., 27., 19., 28., 20., 29.],
       [ 3., 12.,  4., 13.,  5., 14.],
       [21., 30., 22., 31., 23., 32.]],

      [[36., 45., 37., 46., 38., 47.],
       [54., 63., 55., 64., 56., 65.],
       [39., 48., 40., 49., 41., 50.],
       [57., 66., 58., 67., 59., 68.]]]]

test_cc_depthtospace_dcr

Node:
  DepthToSpace(input) -> (output)
  Attributes:
    blocksize = 2
    mode = "DCR"
Inputs:
  input: shape=(1, 8, 2, 3), dtype=float32
    [[[[ 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.]]]]

Outputs:
  output: shape=(1, 2, 4, 6), dtype=float32
    [[[[ 0., 12.,  1., 13.,  2., 14.],
       [24., 36., 25., 37., 26., 38.],
       [ 3., 15.,  4., 16.,  5., 17.],
       [27., 39., 28., 40., 29., 41.]],

      [[ 6., 18.,  7., 19.,  8., 20.],
       [30., 42., 31., 43., 32., 44.],
       [ 9., 21., 10., 22., 11., 23.],
       [33., 45., 34., 46., 35., 47.]]]]

test_cc_depthtospace_default_mode

Node:
  DepthToSpace(input) -> (output)
  Attributes:
    blocksize = 2
Inputs:
  input: shape=(1, 4, 2, 2), dtype=float32
    [[[[ 0.,  1.],
       [ 2.,  3.]],

      [[ 4.,  5.],
       [ 6.,  7.]],

      [[ 8.,  9.],
       [10., 11.]],

      [[12., 13.],
       [14., 15.]]]]

Outputs:
  output: shape=(1, 1, 4, 4), dtype=float32
    [[[[ 0.,  4.,  1.,  5.],
       [ 8., 12.,  9., 13.],
       [ 2.,  6.,  3.,  7.],
       [10., 14., 11., 15.]]]]

test_cc_depthtospace_example

Node:
  DepthToSpace(input) -> (output)
  Attributes:
    blocksize = 2
    mode = "DCR"
Inputs:
  input: shape=(1, 8, 2, 3), dtype=float32
    [[[[ 0.,  1.,  2.],
       [ 3.,  4.,  5.]],

      [[ 9., 10., 11.],
       [12., 13., 14.]],

      [[18., 19., 20.],
       [21., 22., 23.]],

      [[27., 28., 29.],
       [30., 31., 32.]],

      [[36., 37., 38.],
       [39., 40., 41.]],

      [[45., 46., 47.],
       [48., 49., 50.]],

      [[54., 55., 56.],
       [57., 58., 59.]],

      [[63., 64., 65.],
       [66., 67., 68.]]]]

Outputs:
  output: shape=(1, 2, 4, 6), dtype=float32
    [[[[ 0., 18.,  1., 19.,  2., 20.],
       [36., 54., 37., 55., 38., 56.],
       [ 3., 21.,  4., 22.,  5., 23.],
       [39., 57., 40., 58., 41., 59.]],

      [[ 9., 27., 10., 28., 11., 29.],
       [45., 63., 46., 64., 47., 65.],
       [12., 30., 13., 31., 14., 32.],
       [48., 66., 49., 67., 50., 68.]]]]

Differences with previous version (11)#

SchemaDiff: DepthToSpace (domain 'ai.onnx')

  • old version: 11

  • new version: 13

  • breaking: no

Type constraints:

  • changed ‘T’: added types: [‘tensor(bfloat16)’]

Documentation:

  • line similarity: 0.76 (+4/-8 lines)

--- DepthToSpace v11
+++ DepthToSpace v13
@@ -5,23 +5,19 @@
 In the DCR mode, elements along the depth dimension from the input tensor are rearranged in the
 following order: depth, column, and then row. The output y is computed from the input x as below:

+```
 b, c, h, w = x.shape
-
 tmp = np.reshape(x, [b, blocksize, blocksize, c // (blocksize**2), h, w])
-
 tmp = np.transpose(tmp, [0, 3, 4, 1, 5, 2])
-
 y = np.reshape(tmp, [b, c // (blocksize**2), h * blocksize, w * blocksize])
-
+```

 In the CRD mode, elements along the depth dimension from the input tensor are rearranged in the
 following order: column, row, and the depth. The output y is computed from the input x as below:

+```
 b, c, h, w = x.shape
-
 tmp = np.reshape(x, [b, c // (blocksize ** 2), blocksize, blocksize, h, w])
-
 tmp = np.transpose(tmp, [0, 1, 4, 2, 5, 3])
-
 y = np.reshape(tmp, [b, c // (blocksize ** 2), h * blocksize, w * blocksize])
-
+```

Version History#