cosense3d.modules.utils package
Submodules
cosense3d.modules.utils.box_coder module
- class cosense3d.modules.utils.box_coder.BoxPredCoder(with_velo=False)[source]
Bases:
object
- class cosense3d.modules.utils.box_coder.CenterBoxCoder(with_velo=False, with_pred=False, reg_radius=1.6, z_offset=1.0)[source]
Bases:
object
cosense3d.modules.utils.common module
- cosense3d.modules.utils.common.bias_init_with_prob(prior_prob: float) float [source]
initialize conv/fc bias value according to a given probability value.
- cosense3d.modules.utils.common.cat_name_str(module_name)[source]
- Parameters:
module_name – str, format in xxx_yyy_zzz
- Returns:
class_name: str, format in XxxYyyZzz
- cosense3d.modules.utils.common.clip_sigmoid(x: Tensor, eps: float = 0.0001) Tensor [source]
Sigmoid function for input feature.
- Parameters:
x – Input feature map with the shape of [B, N, H, W].
eps – Lower bound of the range to be clamped to. Defaults to 1e-4.
- Returns:
Feature map after sigmoid.
- cosense3d.modules.utils.common.draw_sample_prob(centers, reg, samples, res, distr_r, det_r, batch_size, var0)[source]
- cosense3d.modules.utils.common.fuse_batch_indices(coords, num_cav)[source]
Fusing voxels of CAVs from the same frame :param stensor: ME sparse tensor :param num_cav: list of number of CAVs for each frame :return: fused coordinates and features of stensor
- cosense3d.modules.utils.common.get_conv2d_layers(conv_name, in_channels, out_channels, n_layers, kernel_size, stride, padding, relu_last=True, sequential=True, **kwargs)[source]
Build convolutional layers. kernel_size, stride and padding should be a list with the lengths that match n_layers
- cosense3d.modules.utils.common.get_voxel_centers(voxel_coords, downsample_times, voxel_size, point_cloud_range)[source]
Get centers of spconv voxels.
- Parameters:
voxel_coords – (N, 3)
downsample_times –
voxel_size –
point_cloud_range –
- Returns:
- cosense3d.modules.utils.common.instantiate(module_name, cls_name=None, module_cfg=None, **kwargs)[source]
- cosense3d.modules.utils.common.inverse_sigmoid(x, eps=1e-05)[source]
Inverse function of sigmoid.
- Parameters:
x – (Tensor) The tensor to do the inverse.
eps – (float) EPS avoid numerical overflow. Defaults 1e-5.
- Returns:
Tensor: The x has passed the inverse function of sigmoid, has same shape with input.
- cosense3d.modules.utils.common.linear_last(in_channels, mid_channels, out_channels, bias=False, norm='BN')[source]
cosense3d.modules.utils.conv module
- class cosense3d.modules.utils.conv.ConvModule(in_channels: int, out_channels: int, kernel_size: int | Tuple[int, int], stride: int | Tuple[int, int] = 1, padding: int | Tuple[int, int] = 0, dilation: int | Tuple[int, int] = 1, groups: int = 1, bias: bool | str = 'auto', conv_cfg: Dict | None = None, norm_cfg: Dict | None = None, act_cfg: Dict | None = {'type': 'ReLU'}, inplace: bool = True, with_spectral_norm: bool = False, padding_mode: str = 'zeros', order: tuple = ('conv', 'norm', 'act'))[source]
Bases:
Module
A conv block that bundles conv/norm/activation layers.
This block simplifies the usage of convolution layers, which are commonly used with a norm layer (e.g., BatchNorm) and activation layer (e.g., ReLU). It is based upon three build methods: build_conv_layer(), build_norm_layer() and build_activation_layer().
Besides, we add some additional features in this module. 1. Automatically set bias of the conv layer. 2. Spectral norm is supported. 3. More padding modes are supported. Before PyTorch 1.5, nn.Conv2d only supports zero and circular padding, and we add “reflect” padding mode.
- Args:
- in_channels (int): Number of channels in the input feature map.
Same as that in
nn._ConvNd
.- out_channels (int): Number of channels produced by the convolution.
Same as that in
nn._ConvNd
.- kernel_size (int | tuple[int]): Size of the convolving kernel.
Same as that in
nn._ConvNd
.- stride (int | tuple[int]): Stride of the convolution.
Same as that in
nn._ConvNd
.- padding (int | tuple[int]): Zero-padding added to both sides of
the input. Same as that in
nn._ConvNd
.- dilation (int | tuple[int]): Spacing between kernel elements.
Same as that in
nn._ConvNd
.- groups (int): Number of blocked connections from input channels to
output channels. Same as that in
nn._ConvNd
.- bias (bool | str): If specified as auto, it will be decided by the
norm_cfg. Bias will be set as True if norm_cfg is None, otherwise False. Default: “auto”.
- conv_cfg (dict): Config dict for convolution layer. Default: None,
which means using conv2d.
norm_cfg (dict): Config dict for normalization layer. Default: None. act_cfg (dict): Config dict for activation layer.
Default: dict(type=’ReLU’).
- inplace (bool): Whether to use inplace mode for activation.
Default: True.
- with_spectral_norm (bool): Whether use spectral norm in conv module.
Default: False.
- padding_mode (str): If the padding_mode has not been supported by
current Conv2d in PyTorch, we will use our own padding layer instead. Currently, we support [‘zeros’, ‘circular’] with official implementation and [‘reflect’] with our own implementation. Default: ‘zeros’.
- order (tuple[str]): The order of conv/norm/activation layers. It is a
sequence of “conv”, “norm” and “act”. Common examples are (“conv”, “norm”, “act”) and (“act”, “conv”, “norm”). Default: (‘conv’, ‘norm’, ‘act’).
- forward(x: Tensor, activate: bool = True, norm: bool = True) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- property norm
- training: bool
- cosense3d.modules.utils.conv.build_conv_layer(cfg: Dict | None, *args, **kwargs) Module [source]
Build convolution layer. Modified from openmmlab.
- Args:
- cfg (None or dict): The conv layer config, which should contain:
type (str): Layer type.
layer args: Args needed to instantiate an conv layer.
- args (argument list): Arguments passed to the __init__
method of the corresponding conv layer.
- kwargs (keyword arguments): Keyword arguments passed to the __init__
method of the corresponding conv layer.
- Returns:
nn.Module: Created conv layer.
- cosense3d.modules.utils.conv.build_padding_layer(cfg: Dict, *args, **kwargs) Module [source]
Build padding layer.
- Args:
- cfg (dict): The padding layer config, which should contain:
type (str): Layer type.
layer args: Args needed to instantiate a padding layer.
- Returns:
nn.Module: Created padding layer.
cosense3d.modules.utils.edl_utils module
cosense3d.modules.utils.gaussian_utils module
- cosense3d.modules.utils.gaussian_utils.center_to_img_coor(center_in, lidar_range, pixel_sz)[source]
- cosense3d.modules.utils.gaussian_utils.cornernet_gaussian_radius(height, width, min_overlap=0.5)[source]
- cosense3d.modules.utils.gaussian_utils.draw_gaussian_map(boxes, lidar_range, pixel_sz, batch_size, radius=None, sigma=1, min_radius=2)[source]
- cosense3d.modules.utils.gaussian_utils.gaussian_2d(shape: List[int], sigma: float = 1.0) ndarray [source]
Generate gaussian map.
- Parameters:
shape – Shape of the map.
sigma – Sigma to generate gaussian map. Defaults to 1.
- Returns:
Generated gaussian map.
- cosense3d.modules.utils.gaussian_utils.gaussian_radius(box_dims, pixel_sz, overlap, min_radius=2)[source]
- cosense3d.modules.utils.gaussian_utils.mahalanobis_dists_2d(sigmas, dists)[source]
Compute the squared mahalanobis distances.
- Parameters:
sigmas – (N, 2), standard deviation of Gaussian distribution
dists – (N, 2), distances to gaussian center
- Returns:
(N), squared mahalanobis
- cosense3d.modules.utils.gaussian_utils.weighted_mahalanobis_dists(vars, dists, weights=None)[source]
Compute the squared mahalanobis distances.
- Parameters:
vars – (N, 2), variances of Gaussian distribution.
dists – (N, 2), distances to gaussian center at each axis.
weights – weights to be applied to the output probability.
- Returns:
(N), squared mahalanobis
cosense3d.modules.utils.init module
- cosense3d.modules.utils.init.bias_init_with_prob(prior_prob: float) float [source]
initialize conv/fc bias value according to a given probability value.
- cosense3d.modules.utils.init.constant_init(module: Module, val: float, bias: float = 0) None [source]
- cosense3d.modules.utils.init.kaiming_init(module: Module, a: float = 0, mode: str = 'fan_out', nonlinearity: str = 'relu', bias: float = 0, distribution: str = 'normal') None [source]
- cosense3d.modules.utils.init.normal_init(module: Module, mean: float = 0, std: float = 1, bias: float = 0) None [source]
- cosense3d.modules.utils.init.trunc_normal_init(module: Module, mean: float = 0, std: float = 1, a: float = -2, b: float = 2, bias: float = 0) None [source]
cosense3d.modules.utils.me_utils module
- cosense3d.modules.utils.me_utils.devoxelize_with_centroids(out: SparseTensor, x: TensorField, h_embs)[source]
- cosense3d.modules.utils.me_utils.downsample_embeddings(embeddings, inverse_map, size, mode='avg')[source]
- cosense3d.modules.utils.me_utils.get_conv_block(nc, k=3, d=3, tr=False, bn_momentum=0.1, distributed=False)[source]
create sparse convolution block :param nc: number of channels in each layer in [in_layer, mid_layer, out_layer] :param k: kernel size :param tr: transposed convolution :return: conv block
- cosense3d.modules.utils.me_utils.get_kernel_map_and_out_key(stensor, stensor_out=None, kernel_size=3, stride=1, dilation=1, kernel_type='cube', kernel_generator=None)[source]
Generate kernel maps for the input stensor. The hybrid and custom kernel is not implemented in ME v0.5.x, this function uses a kernel mask to select the kernel maps for the customized kernel shapes. :param stensor: ME.SparseTensor, NxC :param kernel_type: ‘cube’(default) | ‘hybrid’ :return: masked kernel maps
- cosense3d.modules.utils.me_utils.indices2metric(indices, voxel_size)[source]
Voxel indices to voxel center in meter
- cosense3d.modules.utils.me_utils.minkconv_conv_block(in_dim, out_dim, kernel, stride, d=3, bn_momentum=0.1, activation='LeakyReLU', tr=False, expand_coordinates=False, norm_before=False, distributed=False)[source]
- cosense3d.modules.utils.me_utils.minkconv_layer(in_dim, out_dim, kernel, stride, d, tr=False)[source]
- cosense3d.modules.utils.me_utils.normalize_centroids(down_points, coordinates, tensor_stride)[source]
- cosense3d.modules.utils.me_utils.prepare_input_data(points_list, voxel_size, QMODE, floor_height, coor_dim=3, feat_dim=3)[source]
- cosense3d.modules.utils.me_utils.update_me_essentials(self: object, data_info: dict, stride: int | None = None)[source]
Update essential variables for ME-based models
- Parameters:
self – instance of a python class
data_info –
det_r: float
lidar_range: [xmin, ymin, zmin, xmax, ymax, zmax]
voxel_size: [vx, vy, vz]
stride –
- Returns:
cosense3d.modules.utils.misc module
- class cosense3d.modules.utils.misc.MLN(c_dim, f_dim=256)[source]
Bases:
Module
- Args:
c_dim (int): dimension of latent code c f_dim (int): feature dimension
- forward(x, c)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class cosense3d.modules.utils.misc.MLN2(c_dim, f_dim=256)[source]
Bases:
Module
- Args:
c_dim (int): dimension of latent code c f_dim (int): feature dimension
- forward(x, c)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class cosense3d.modules.utils.misc.SELayer_Linear(channels, act_layer=<class 'torch.nn.modules.activation.ReLU'>, gate_layer=<class 'torch.nn.modules.activation.Sigmoid'>, norm=False)[source]
Bases:
Module
- forward(x, x_se)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
cosense3d.modules.utils.nbr_attn module
- class cosense3d.modules.utils.nbr_attn.NeighborhoodAttention(emb_dim, n_nbr=16, num_pose_feat=64, **kwargs)[source]
Bases:
Module
Generate reference points and attend neighborhood features.
- forward(memory, mem_coor, q_coor, B)[source]
- Args:
q: (S, D) kv: (L, D) q_coor: (S, 3), [idx, x, y] kv_coor: (L, 3)
Returns:
- training: bool
cosense3d.modules.utils.norm module
- cosense3d.modules.utils.norm.build_norm_layer(cfg: Dict, num_features: int, postfix: int | str = '') Tuple[str, Module] [source]
Build normalization layer. Modified from openmmlab.
- Parameters:
cfg – (dict) The norm layer config, which should contain: - type (str): Layer type. - layer args: Args needed to instantiate a norm layer. - requires_grad (bool, optional): Whether stop gradient updates.
num_features – (int) Number of input channels.
postfix – (int | str) The postfix to be appended into norm abbreviation to create named layer.
- Returns:
tuple[str, nn.Module]: The first element is the layer name consisting of abbreviation and postfix, e.g., bn1, gn. The second element is the created norm layer.
cosense3d.modules.utils.positional_encoding module
- cosense3d.modules.utils.positional_encoding.img_locations(img_size, feat_size=None, stride=None)[source]
- cosense3d.modules.utils.positional_encoding.nerf_positional_encoding(tensor: Tensor, num_encoding_functions: int = 6, include_input: bool = False, log_sampling: bool = True) Tensor [source]
Apply positional encoding to the input.
- Parameters:
tensor – Input tensor to be positionally encoded.
num_encoding_functions – Number of encoding functions used to compute a positional encoding (default: 6).
include_input – Whether or not to include the input in the positional encoding (default: True).
log_sampling –
- Returns:
Positional encoding of the input tensor.
- cosense3d.modules.utils.positional_encoding.pos2posemb1d(pos, num_pos_feats=256, temperature=10000)[source]
- cosense3d.modules.utils.positional_encoding.pos2posemb2d(pos, num_pos_feats=128, temperature=10000)[source]