mindspore.nn.Unfold

查看源文件
class mindspore.nn.Unfold(ksizes, strides, rates, padding='valid')[源代码]

从图像中提取滑窗的区域块。

输入为一个四维的Tensor,数据格式为(N, C, H, W)。

参数:
  • ksizes (Union[tuple[int], list[int]]) - 滑窗大小,其格式为[1, ksize_row, ksize_col, 1]的int组成的tuple或list。

  • strides (Union[tuple[int], list[int]]) - 滑窗步长,其格式为[1, stride_row, stride_col, 1]的int组成的tuple或list。

  • rates (Union[tuple[int], list[int]]) - 滑窗元素之间的空洞个数,其格式为[1, rate_row, rate_col, 1] 的int组成的tuple或list。

  • padding (str) - 填充模式,可选值有: "same""valid" 的字符串,不区分大小写。默认值: "valid"

    • "same" - 指所提取的区域块的部分区域可以在原始图像之外,此部分填充为0。

    • "valid" - 表示所取的区域快必须被原始图像所覆盖。

输入:
  • x (Tensor) - 输入四维Tensor,其shape为 [in_batch,in_depth,in_row,in_col],其数据类型为int。

输出:

Tensor,输出为四维Tensor,数据类型与 x 相同,其shape为 (out_batch,out_depth,out_row,out_col),且 out_batchin_batch 相同。

  • out_depth=ksize_rowksize_colin_depth

  • out_row=(in_row(ksize_row+(ksize_row1)(rate_row1)))//stride_row+1

  • out_col=(in_col(ksize_col+(ksize_col1)(rate_col1)))//stride_col+1

异常:
  • TypeError - ksizesstridesrates 既不是tuple,也不是list。

  • ValueError - ksizesstridesrates 的shape不是 (1,x_row,x_col,1)

  • ValueError - ksizesstridesrates 的第二个和第三个元素小于1。

支持平台:

Ascend GPU

样例:

>>> import mindspore
>>> from mindspore import Tensor, nn
>>> import numpy as np
>>> net = nn.Unfold(ksizes=[1, 2, 2, 1], strides=[1, 2, 2, 1], rates=[1, 2, 2, 1])
>>> # As stated in the above code:
>>> # ksize_row = 2, ksize_col = 2, rate_row = 2, rate_col = 2, stride_row = 2, stride_col = 2.
>>> image = Tensor(np.ones([2, 3, 6, 6]), dtype=mindspore.float16)
>>> # in_batch = 2, in_depth = 3, in_row = 6, in_col = 6.
>>> # Substituting the formula to get:
>>> # out_batch = in_batch = 2
>>> # out_depth = 2 * 2 * 3 = 12
>>> # out_row = (6 - (2 + (2 - 1) * (2 - 1))) // 2 + 1 = 2
>>> # out_col = (6 - (2 + (2 - 1) * (2 - 1))) // 2 + 1 = 2
>>> output = net(image)
>>> print(output.shape)
(2, 12, 2, 2)