Modify the docs of conv3d functional api

This commit is contained in:
z30020733 2022-11-08 16:30:39 +08:00
parent bd7f370968
commit cf430f82a4
3 changed files with 92 additions and 19 deletions

View File

@ -209,3 +209,4 @@ mindspore/mindspore/ccsrc/pybind_api/ir/dtype_py.cc:mindspore::RegTyping
mindspore/mindspore/ccsrc/pybind_api/ir/tensor_py.cc:mindspore::tensor::RegMetaTensor
mindspore/mindspore/ccsrc/plugin/device/cpu/kernel/eltwise_grad_cpu_kernel.cc:mindspore::kernel::EltWiseGradCpuTypeFunc<T>::InitFunc
mindspore/mindspore/lite/tools/converter/quantizer/weight_quantizer.cc:mindspore::lite::quant::WeightQuantizer::LinearQuant
mindspore/mindspore/python/mindspore/ops/function/nn_func.py:conv3d

View File

@ -8,14 +8,16 @@ mindspore.ops.conv3d
对输入Tensor计算三维卷积该Tensor的常见shape为 :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})` ,其中 :math:`N` 为batch size:math:`C_{in}` 为通道数,:math:`D` 为深度, :math:`H_{in}, W_{in}` 分别为特征层的高度和宽度。 :math:`X_i`:math:`i^{th}` 输入值, :math:`b_i`:math:`i^{th}` 输入值的偏置项。对于每个batch中的Tensor其shape为 :math:`(C_{in}, D_{in}, H_{in}, W_{in})` ,公式定义如下:
.. math::
out_j = \sum_{i=0}^{C_{in} - 1} ccor(W_{ij}, X_i) + b_j,
\operatorname{out}\left(N_{i}, C_{\text {out}_j}\right)=\operatorname{bias}\left(C_{\text {out}_j}\right)+
\sum_{k=0}^{C_{in}-1} ccor(\text {weight}\left(C_{\text {out}_j}, k\right),
\operatorname{input}\left(N_{i}, k\right))
其中,:math:`ccor``cross-correlation <https://en.wikipedia.org/wiki/Cross-correlation>`_ :math:`C_{in}` 为输入通道数, :math:`j` 的范围从 :math:`0`:math:`C_{out} - 1` :math:`W_{ij}` 对应第 :math:`j` 个过滤器的第 :math:`i` 个通道, :math:`out_{j}` 对应输出的第 :math:`j` 个通道。 :math:`W_{ij}` 为卷积核的切片其shape为 :math:`(\text{kernel_size[0]}, \text{kernel_size[1]}, \text{kernel_size[2]})` ,其中 :math:`\text{kernel_size[1]}`:math:`\text{kernel_size[2]}` 是卷积核的高度和宽度,:math:`\text{kernel_size[0]}` 是卷积核的深度。完整卷积核的shape为 :math:`(C_{out}, C_{in} / \text{group}, \text{kernel_size[0]}, \text{kernel_size[1]}, \text{kernel_size[2]})` ,其中 `group` 是在通道上分割输入 `inputs` 的组数。
其中,:math:`k` 为卷积核数,:math:`ccor``cross-correlation <https://en.wikipedia.org/wiki/Cross-correlation>`_
:math:`C_{in}` 为输入通道数, :math:`j` 的范围从 :math:`0`:math:`C_{out} - 1` :math:`W_{ij}` 对应第 :math:`j` 个过滤器的第 :math:`i` 个通道, :math:`out_{j}` 对应输出的第 :math:`j` 个通道。
:math:`W_{ij}` 为卷积核的切片其shape为 :math:`(\text{kernel_size[0]}, \text{kernel_size[1]}, \text{kernel_size[2]})` ,其中 :math:`\text{kernel_size[1]}`:math:`\text{kernel_size[2]}` 是卷积核的高度和宽度,
:math:`\text{kernel_size[0]}` 是卷积核的深度。完整卷积核的shape为 :math:`(C_{out}, C_{in} / \text{group}, \text{kernel_size[0]}, \text{kernel_size[1]}, \text{kernel_size[2]})` ,其中 `group` 是在通道上分割输入 `inputs` 的组数。
如果 `pad_mode` 设置为"valid",则输出高度和宽度将分别为 :math:`\left \lfloor{1 + \frac{H_{in} + \text{padding[0]} + \text{padding[1]} - \text{kernel_size[0]} - (\text{kernel_size[0]} - 1) \times (\text{dilation[0]} - 1) }{\text{stride[0]}}} \right \rfloor`:math:`\left \lfloor{1 + \frac{W_{in} + \text{padding[2]} + \text{padding[3]} - \text{kernel_size[1]} - (\text{kernel_size[1]} - 1) \times (\text{dilation[1]} - 1) }{\text{stride[1]}}} \right \rfloor`
其中, :math:`dialtion` 为卷积核元素之间的间距, :math:`stride` 为移动步长, :math:`padding` 为添加到输入两侧的零填充。
请参考论文 `Gradient Based Learning Applied to Document Recognition <http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf>`_ 。更详细的介绍参见http://cs231n.github.io/convolutional-networks/。
详细内容请参考论文 `Gradient Based Learning Applied to Document Recognition <http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf>`_
参数:
- **inputs** (Tensor) - shape为 :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})` 的Tensor。
@ -29,11 +31,44 @@ mindspore.ops.conv3d
- **padding** (Union[int, tuple[int]],可选) - 输入 `inputs` 的深度、高度和宽度方向上填充的数量。数据类型为int或包含6个int组成的tuple。如果 `padding` 是一个int那么前、后、上、下、左、右的填充都等于 `padding` 。如果 `padding` 是一个有6个int组成的tuple那么前、后、上、下、左、右的填充分别等于 `padding[0]``padding[1]``padding[2]``padding[3]``padding[4]``padding[5]` 。值必须大于等于0默认值0。
- **stride** (Union[int, tuple[int]],可选) - 卷积核移动的步长数据类型为int或两个int组成的tuple。一个int表示在高度和宽度方向的移动步长均为该值。两个int组成的tuple分别表示在高度和宽度方向的移动步长。默认值1。
- **dilation** (Union[int, tuple[int]],可选) - 卷积核膨胀尺寸。数据类型为int或由3个int组成的tuple。若 :math:`k > 1` ,则卷积核间隔 `k` 个元素进行采样。前后、垂直和水平方向上的 `k` ,其取值范围分别为[1, D]、[1, H]和[1, W]。默认值1。
- **group** (int可选) - 将过滤器拆分为组。默认值:1。
- **group** (int可选) - 将筛选器拆分为组默认值1。当前仅支持值为1。
返回:
Tensor卷积后的值。shape为 :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})`
`pad_mode` 为"same"时:
.. math::
\begin{array}{ll} \\
D_{out} \left \lceil{\frac{D_{in}}{\text{stride[0]}}} \right \rceil \\
H_{out} \left \lceil{\frac{H_{in}}{\text{stride[1]}}} \right \rceil \\
W_{out} \left \lceil{\frac{W_{in}}{\text{stride[2]}}} \right \rceil \\
\end{array}
`pad_mode` 为"valid"时:
.. math::
\begin{array}{ll} \\
D_{out} \left \lfloor{\frac{D_{in} - \text{dilation[0]} \times (\text{kernel_size[0]} - 1) }
{\text{stride[0]}} + 1} \right \rfloor \\
H_{out} \left \lfloor{\frac{H_{in} - \text{dilation[1]} \times (\text{kernel_size[1]} - 1) }
{\text{stride[1]}} + 1} \right \rfloor \\
W_{out} \left \lfloor{\frac{W_{in} - \text{dilation[2]} \times (\text{kernel_size[2]} - 1) }
{\text{stride[2]}} + 1} \right \rfloor \\
\end{array}
`pad_mode` 为"pad"时:
.. math::
\begin{array}{ll} \\
D_{out} \left \lfloor{\frac{D_{in} + padding[0] + padding[1] - (\text{dilation[0]} - 1) \times
\text{kernel_size[0]} - 1 }{\text{stride[0]}} + 1} \right \rfloor \\
H_{out} \left \lfloor{\frac{H_{in} + padding[2] + padding[3] - (\text{dilation[1]} - 1) \times
\text{kernel_size[1]} - 1 }{\text{stride[1]}} + 1} \right \rfloor \\
W_{out} \left \lfloor{\frac{W_{in} + padding[4] + padding[5] - (\text{dilation[2]} - 1) \times
\text{kernel_size[2]} - 1 }{\text{stride[2]}} + 1} \right \rfloor \\
\end{array}
异常:
- **TypeError** - `stride``padding``dilation` 既不是int也不是tuple。
- **TypeError** - `out_channel``group` 不是int。

View File

@ -3945,16 +3945,21 @@ def conv3d(inputs, weight, pad_mode="valid", padding=0, stride=1, dilation=1, gr
\sum_{k=0}^{C_{in}-1} ccor(\text {weight}\left(C_{\text {out}_j}, k\right),
\operatorname{input}\left(N_{i}, k\right))
where :math:`k` is kernel, :math:`ccor` is the cross-correlation operator.
If the 'pad_mode' is set to be "valid", the output depth, height and width will be
:math:`\left \lfloor{1 + \frac{D_{in} + 2 \times \text{padding} - \text{ks_d} -
(\text{ks_d} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor` and
:math:`\left \lfloor{1 + \frac{H_{in} + 2 \times \text{padding} - \text{ks_h} -
(\text{ks_h} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor` and
:math:`\left \lfloor{1 + \frac{W_{in} + 2 \times \text{padding} - \text{ks_w} -
(\text{ks_w} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor` respectively. Where
:math:`dilation` is Spacing between kernel elements, :math:`stride` is The step length of each step,
:math:`padding` is zero-padding added to both sides of the input.
where :math:`k` is kernel,
:math:`ccor` is the `cross-correlation <https://en.wikipedia.org/wiki/Cross-correlation>`_ ,
:math:`C_{in}` is the channel number of the input, :math:`out_{j}` corresponds to the jth channel of
the output and :math:`j` is in the range of :math:`[0C_{out}-1]`. :math:`\text{weight}(C_{\text{out}_j}, k)`
is a convolution kernel slice with shape
:math:`(\text{kernel_size[0]}, \text{kernel_size[1]}, \text{kernel_size[2]})`,
where :math:`\text{kernel_size[0]}`, :math:`\text{kernel_size[1]}` and :math:`\text{kernel_size[2]}` are
the depth, height and width of the convolution kernel respectively. :math:`\text{bias}` is the bias parameter
and :math:`\text{X}` is the input tensor.
The shape of full convolution kernel is
:math:`(C_{out}, C_{in} / \text{group}, \text{kernel_size[0]}, \text{kernel_size[1]}, \text{kernel_size[2]})`,
where `group` is the number of groups to split the input `x` in the channel dimension.
For more details, please refers to the paper `Gradient Based Learning Applied to Document
Recognition <http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf>`_ .
Args:
inputs (Tensor): Tensor of shape :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})`.
@ -3989,12 +3994,44 @@ def conv3d(inputs, weight, pad_mode="valid", padding=0, stride=1, dilation=1, gr
Specifies the dilation rate to use for dilated convolution. If set :math:`k > 1`,
there will be :math:`k - 1` pixels skipped for each sampling location.
Its value must be greater than or equal to 1 and bounded by the height and width of the input. Default: 1.
group (int, optional): Splits filter into groups, `in_channels` and `out_channels` must be
divisible by the number of groups. Default: 1. Only 1 is currently supported.
group (int, optional): Splits filter into groups. Default: 1. Only 1 is currently supported.
Returns:
Tensor, the value that applied 3D convolution. The shape is :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})`.
`pad_mode` is 'same':
.. math::
\begin{array}{ll} \\
D_{out} \left \lceil{\frac{D_{in}}{\text{stride[0]}}} \right \rceil \\
H_{out} \left \lceil{\frac{H_{in}}{\text{stride[1]}}} \right \rceil \\
W_{out} \left \lceil{\frac{W_{in}}{\text{stride[2]}}} \right \rceil \\
\end{array}
`pad_mode` is 'valid':
.. math::
\begin{array}{ll} \\
D_{out} \left \lfloor{\frac{D_{in} - \text{dilation[0]} \times (\text{kernel_size[0]} - 1) }
{\text{stride[0]}} + 1} \right \rfloor \\
H_{out} \left \lfloor{\frac{H_{in} - \text{dilation[1]} \times (\text{kernel_size[1]} - 1) }
{\text{stride[1]}} + 1} \right \rfloor \\
W_{out} \left \lfloor{\frac{W_{in} - \text{dilation[2]} \times (\text{kernel_size[2]} - 1) }
{\text{stride[2]}} + 1} \right \rfloor \\
\end{array}
`pad_mode` is 'pad':
.. math::
\begin{array}{ll} \\
D_{out} \left \lfloor{\frac{D_{in} + padding[0] + padding[1] - (\text{dilation[0]} - 1) \times
\text{kernel_size[0]} - 1 }{\text{stride[0]}} + 1} \right \rfloor \\
H_{out} \left \lfloor{\frac{H_{in} + padding[2] + padding[3] - (\text{dilation[1]} - 1) \times
\text{kernel_size[1]} - 1 }{\text{stride[1]}} + 1} \right \rfloor \\
W_{out} \left \lfloor{\frac{W_{in} + padding[4] + padding[5] - (\text{dilation[2]} - 1) \times
\text{kernel_size[2]} - 1 }{\text{stride[2]}} + 1} \right \rfloor \\
\end{array}
Raises:
TypeError: If `out_channel` or `group` is not an int.
TypeError: If `stride`, `padding` or `dilation` is neither an int nor a tuple.