conv3d¶
- paddle.static.nn. conv3d ( input, num_filters, filter_size, stride=1, padding=0, dilation=1, groups=None, param_attr=None, bias_attr=None, use_cudnn=True, act=None, name=None, data_format='NCDHW' ) [source]
-
The convolution3D layer calculates the output based on the input, filter and strides, paddings, dilations, groups parameters. Input(Input) and Output(Output) are in NCDHW or NDHWC format. Where N is batch size C is the number of channels, D is the depth of the feature, H is the height of the feature, and W is the width of the feature. Convolution3D is similar with Convolution2D but adds one dimension(depth). If bias attribution and activation type are provided, bias is added to the output of the convolution, and the corresponding activation function is applied to the final result.
For each input \(X\), the equation is:
\[Out = \sigma (W \ast X + b)\]In the above equation:
\(X\): Input value, a tensor with NCDHW or NDHWC format.
\(W\): Filter value, a tensor with MCDHW format.
\(\ast\): Convolution operation.
\(b\): Bias value, a 2-D tensor with shape [M, 1].
\(\sigma\): Activation function.
\(Out\): Output value, the shape of \(Out\) and \(X\) may be different.
Example
Input:
Input shape: \((N, C_{in}, D_{in}, H_{in}, W_{in})\)
Filter shape: \((C_{out}, C_{in}, D_f, H_f, W_f)\)
Output: Output shape: \((N, C_{out}, D_{out}, H_{out}, W_{out})\)
Where
\[\begin{split}D_{out}&= \frac{(D_{in} + 2 * paddings[0] - (dilations[0] * (D_f - 1) + 1))}{strides[0]} + 1 \\ H_{out}&= \frac{(H_{in} + 2 * paddings[1] - (dilations[1] * (H_f - 1) + 1))}{strides[1]} + 1 \\ W_{out}&= \frac{(W_{in} + 2 * paddings[2] - (dilations[2] * (W_f - 1) + 1))}{strides[2]} + 1\end{split}\]- Parameters
-
input (Tensor) – The input is 5-D Tensor with shape [N, C, D, H, W], the data type of input is float16 or float32 or float64.
num_filters (int) – The number of filter. It is as same as the output image channel.
filter_size (int|tuple) – The filter size. If filter_size is a tuple, it must contain three integers, (filter_size_depth, filter_size_height, filter_size_width). Otherwise, filter_size_depth = filter_size_height = filter_size_width = filter_size.
stride (int|tuple) – The stride size. It means the stride in convolution. If stride is a tuple, it must contain three integers, (stride_depth, stride_height, stride_width). Otherwise, stride_depth = stride_height = stride_width = stride. Default: stride = 1.
padding (string|int|list|tuple) – The padding size. It means the number of zero-paddings on both sides for each dimension. If padding is a string, either ‘VALID’ or ‘SAME’ which is the padding algorithm. If padding size is a tuple or list, it could be in three forms: [pad_depth, pad_height, pad_width] or [pad_depth_front, pad_depth_back, pad_height_top, pad_height_bottom, pad_width_left, pad_width_right], and when data_format is “NCDHW”, pool_padding can be in the form [[0,0], [0,0], [pad_depth_front, pad_depth_back], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right]]. when data_format is “NDHWC”, pool_padding can be in the form [[0,0], [pad_depth_front, pad_depth_back], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], [0,0]]. Default: padding = 0.
dilation (int|tuple) – The dilation size. It means the spacing between the kernel points. If dilation is a tuple, it must contain three integers, (dilation_depth, dilation_height, dilation_width). Otherwise, dilation_depth = dilation_height = dilation_width = dilation. Default: dilation = 1.
groups (int) – The groups number of the Conv3d Layer. According to grouped convolution in Alex Krizhevsky’s Deep CNN paper: when group=2, the first half of the filters is only connected to the first half of the input channels, while the second half of the filters is only connected to the second half of the input channels. Default: groups=1
param_attr (ParamAttr|None) – The parameter attribute for learnable parameters/weights of conv3d. If it is set to None or one attribute of ParamAttr, conv3d will create ParamAttr as param_attr. If it is set to None, the parameter is initialized with \(Normal(0.0, std)\), and the \(std\) is \((\\frac{2.0 }{filter\_elem\_num})^{0.5}\). Default: None.
bias_attr (ParamAttr|bool|None) – The parameter attribute for the bias of conv3d. If it is set to False, no bias will be added to the output units. If it is set to None or one attribute of ParamAttr, conv3d will create ParamAttr as bias_attr. If the Initializer of the bias_attr is not set, the bias is initialized zero. Default: None.
use_cudnn (bool) – Use cudnn kernel or not, it is valid only when the cudnn library is installed. Default: True
act (str) – Activation type, if it is set to None, activation is not appended. Default: None.
name (str|None) – For detailed information, please refer to Name. Usually name is no need to set and None by default.
data_format (str, optional) – Specify the data format of the input, and the data format of the output will be consistent with that of the input. An optional string from: “NCHW”, “NHWC”. The default is “NCHW”. When it is “NCHW”, the data is stored in the order of: [batch_size, input_channels, input_height, input_width].
- Returns
-
A Tensor representing the conv3d, whose data type is the same with input. If act is None, the tensor variable storing the convolution result, and if act is not None, the tensor variable storing convolution and non-linearity activation result.
Examples
>>> import paddle >>> import numpy as np >>> np.random.seed(1107) >>> paddle.seed(1107) >>> paddle.enable_static() >>> data = paddle.static.data(name='data', shape=[None, 3, 12, 32, 32], dtype='float32') >>> param_attr = paddle.framework.ParamAttr(name='conv3d.weight', initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001) >>> res = paddle.static.nn.conv3d(input=data, num_filters=2, filter_size=3, act="relu", param_attr=param_attr) >>> place = paddle.CPUPlace() >>> exe = paddle.static.Executor(place) >>> exe.run(paddle.static.default_startup_program()) >>> x = np.random.rand(1, 3, 12, 32, 32).astype("float32") >>> output, = exe.run(feed={"data": x}, fetch_list=[res]) >>> print(output.shape) (1, 2, 10, 30, 30)