megengine.module.Conv3d¶
- class Conv3d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, conv_mode='cross_correlation')[源代码]¶
对输入 tensor 进行三维卷积
For instance, given an input of the size \((N, C_{\text{in}}, T, H, W)\), this layer generates an output of the size \((N, C_{\text{out}}, T_{\text{out}}, H_{\text{out}}, W_{\text{out}})\) through the process described as below:
\[\text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + \sum_{k = 0}^{C_{\text{in}} - 1} \text{weight}(C_{\text{out}_j}, k) \star \text{input}(N_i, k)\]where \(\star\) is the valid 3D cross-correlation operator, \(N\) is batch size, \(C\) denotes number of channels.
当 groups == in_channels 且 out_channels == K * in_channels ,其中 K 是正整数,该操作也被称为深度方向卷积(depthwise convolution)。
In other words, for an input of size \((N, C_{in}, T_{int}, H_{in}, W_{in})\), a depthwise convolution with a depthwise multiplier K, can be constructed by arguments \((in\_channels=C_{in}, out\_channels=C_{in} \times K, ..., groups=C_{in})\).
- 参数
in_channels (
int
) – 输入数据中的通道数。out_channels (
int
) – 输出数据中的通道数。kernel_size (
Union
[int
,Tuple
[int
,int
,int
]]) – size of weight on spatial dimensions. If kernel_size is anint
, the actual kernel size would be (kernel_size, kernel_size, kernel_size).stride (
Union
[int
,Tuple
[int
,int
,int
]]) – 三维卷积运算中的步长。 默认: 1padding (
Union
[int
,Tuple
[int
,int
,int
]]) – 输入数据空域维度两侧的填充(padding)大小。仅支持填充0值。默认:0dilation (
Union
[int
,Tuple
[int
,int
,int
]]) – 三维卷积运算的扩张值(dilation)。 默认: 1groups (
int
) – number of groups into which the input and output channels are divided, so as to perform a “grouped convolution”. Whengroups
is not 1,in_channels
andout_channels
must be divisible bygroups
, and there would be an extra dimension at the beginning of the weight’s shape. Default: 1bias (
bool
) – 是否将偏置(bias)加入卷积的结果中。默认:Trueconv_mode (
str
) – Supports cross_correlation. Default: cross_correlation
注解
weight
usually has shape(out_channels, in_channels, depth, height, width)
, if groups is not 1, shape will be(groups, out_channels // groups, in_channels // groups, depth, height, width)
bias
usually has shape(1, out_channels, *1)
实际案例
import numpy as np import megengine as mge import megengine.module as M m = M.Conv3d(in_channels=3, out_channels=1, kernel_size=3) inp = mge.tensor(np.arange(0, 384).astype("float32").reshape(2, 3, 4, 4, 4)) oup = m(inp) print(oup.numpy().shape)
输出:
(2, 1, 2, 2, 2)
方法
apply
(fn)对当前模块中的所有模块应用函数
fn
,包括当前模块本身。buffers
([recursive])返回该模块中对于buffers的一个可迭代对象。
calc_conv
(inp, weight, bias)children
(**kwargs)返回一个可迭代对象,可遍历所有属于当前模块的直接属性的子模块。
disable_quantize
([value])设置
module
的quantize_diabled
属性,并返回module
。eval
()当前模块中所有模块的
training
属性(包括自身)置为False
,并将其切换为推理模式。forward
(inp)load_state_dict
(state_dict[, strict])加载一个参数字典,这个字典通常使用
state_dict
得到。modules
(**kwargs)返回一个可迭代对象,可以遍历当前模块中的所有模块,包括其本身。
named_buffers
([prefix, recursive])返回可遍历模块中 key 与 buffer 的键值对的可迭代对象,其中
key
为从该模块至 buffer 的点路径(dotted path)。named_children
(**kwargs)返回可迭代对象,可以遍历属于当前模块的直接属性的所有子模块(submodule)与键(key)组成的”key-submodule”对,其中'key'是子模块对应的属性名。
named_modules
([prefix])返回可迭代对象,可以遍历当前模块包括自身在内的所有其内部模块所组成的key-module键-模块对,其中'key'是从当前模块到各子模块的点路径(dotted path)。
named_parameters
([prefix, recursive])返回一个可迭代对象,可以遍历当前模块中key与
Parameter
组成的键值对。其中key
是从模块到Parameter
的点路径(dotted path)。named_tensors
([prefix, recursive])Returns an iterable for key tensor pairs of the module, where
key
is the dotted path from this module to the tensor.parameters
([recursive])返回一个可迭代对象,遍历当前模块中的所有
Parameter
register_forward_hook
(hook)给模块输出注册一个回调函数。
给模块输入注册一个回调函数。
replace_param
(params, start_pos[, seen])Replaces module's parameters with
params
, used byParamPack
to- rtype
state_dict
([rst, prefix, keep_var])tensors
([recursive])Returns an iterable for the
Tensor
of the module.train
([mode, recursive])当前模块中所有模块的
training
属性(包括自身)置为mode
。将所有参数的梯度置0。