basecls.layers#

class basecls.layers.ELU(alpha=1.0, name=None)[源代码]#

基类:Module

ELU activation function.

\[\begin{split}\text{ELU}(x) = \begin{cases} x, & \text{if } x > 0, \\ \alpha \left( \exp(x) - 1 \right), & \text{if } x \le 0 \end{cases}\end{split}\]
参数

alpha (float) – the \(\alpha\) value for the ELU formulation. Default: 1.0

forward(x)[源代码]#
返回类型

Tensor

class basecls.layers.HSigmoid(name=None)[源代码]#

基类:Module

Hard sigmoid activation function.

\[\begin{split}\text{HSigmoid}(x) = \begin{cases} 0 & \text{if } x \le -3, \\ 1 & \text{if } x \ge 3, \\ x / 6 + 1 / 2 & \text{otherwise} \end{cases}\end{split}\]
forward(x)[源代码]#
返回类型

Tensor

class basecls.layers.HSwish(name=None)[源代码]#

基类:Module

Hard swish activation function.

\[\begin{split}\text{HSwish}(x) = \begin{cases} 0 & \text{if } x \le -3, \\ x & \text{if } x \ge 3, \\ x (x + 3) / 6 & \text{otherwise} \end{cases}\end{split}\]
forward(x)[源代码]#
返回类型

Tensor

class basecls.layers.ReLU6(name=None)[源代码]#

基类:Module

ReLU6 activation function.

\[\text{ReLU6}(x) = \min \left( \max(0, x), 6 \right)\]
forward(x)[源代码]#
返回类型

Tensor

class basecls.layers.Tanh(name=None)[源代码]#

基类:Module

Tanh activation function.

\[\text{Tanh}(x) = \text{tanh}(x) = \frac{\exp(x) - \exp(-x)}{\exp(x) + \exp(-x)}\]
forward(x)[源代码]#
返回类型

Tensor

basecls.layers.activation(name, **kwargs)[源代码]#

Helper for building an activation layer.

参数

name (Union[str, Callable]) – activation name, supports "elu", "gelu", "hsigmoid", "hswish", "leaky_relu", "relu", "relu6", "prelu", "silu" and "tanh".

返回类型

Module

返回

An activation module.

class basecls.layers.ClsHead(w_in, w_out=1000, width=0, dropout_prob=0.0, norm_name='BN', act_name='relu', bias=True)[源代码]#

基类:Module

Cls head: Conv, BN, Act, AvgPool, FC.

参数
  • w_in (int) – input width.

  • w_out (int) – output width, normally the number of classes. Default: 1000

  • width (int) – width for first conv in head, conv will be omitted if set to 0. Default: 0

  • dropout_prob (float) – dropout probability. Default: 0.0

  • norm_name (str) – normalization function. Default: "BN"

  • act_name (str) – activation function. Default: "relu"

  • bias (bool) – whether fc has bias. Default: True

forward(x)[源代码]#
返回类型

Tensor

class basecls.layers.MBV3Head(w_in, w_out=1000, width=960, w_h=1280, dropout_prob=0.0, se_r=0.0, norm_name='BN', act_name='hswish', bias=True)[源代码]#

基类:Module

MobileNet V3 head: Conv, BN, Act, AvgPool, SE, FC, Act, FC.

参数
  • w_in (int) – input width.

  • w_out (int) – output width, normally the number of classes.

  • width (int) – width for first conv in head.

  • w_h (int) – width for first linear in head.

  • dropout_prob (float) – dropout probability. Default: 0.0

  • se_r (float) – Squeeze-and-Excitation (SE) ratio. Default: 0.0

  • norm_name (str) – normalization function. Default: "BN"

  • act_name (str) – activation function. Default: "hswish"

  • bias (bool) – whether fc has bias. Default: True

forward(x)[源代码]#
返回类型

Tensor

class basecls.layers.VGGHead(w_in, w_out=1000, width=4096, dropout_prob=0.5, act_name='relu', **kwargs)[源代码]#

基类:Module

VGG head: AvgPool, [FC, Act, Dropout] x2, FC.

参数
  • w_in (int) – input width.

  • w_out (int) – output width, normally the number of classes. Default: 1000

  • width (int) – width for linear in head. Default: 4096

  • dropout_prob (float) – dropout probability. Default: 0.5

  • act_name (str) – activation function. Default: "relu"

forward(x)[源代码]#
返回类型

Tensor

basecls.layers.build_head(w_in, head_args=None, norm_name='BN', act_name='relu')[源代码]#

The factory function to build head.

备注

if head_args is None or head_args["name"] is None, this function will do nothing and return None.

参数
  • w_in (int) – input width.

  • head_args (Optional[Mapping[str, Any]]) – head args. Default: None

  • norm_name (str) – default normalization function, will be overridden by the same key in head_args. Default: "BN"

  • act_name (str) – default activation function, will be overridden by the same key in head_args. Default: "relu"

返回类型

Module

返回

A head.

class basecls.layers.BinaryCrossEntropy(**kwargs)[源代码]#

基类:Module

The module for binary cross entropy.

See binary_cross_entropy() for more details.

forward(x, y)[源代码]#
返回类型

Tensor

class basecls.layers.CrossEntropy(axis=1, label_smooth=0.0)[源代码]#

基类:Module

The module for cross entropy.

It supports both categorical labels and one-hot labels. See cross_entropy() for more details.

参数
  • axis (int) – reduced axis. Default: 1

  • label_smooth (float) – label smooth factor. Default: 0.0

forward(x, y)[源代码]#
返回类型

Tensor

basecls.layers.build_loss(cfg)[源代码]#

The factory function to build loss.

参数

cfg (ConfigDict) – config for building loss function.

返回类型

Module

返回

A loss function.

class basecls.layers.SE(w_in, w_se, act_name, approx_sigmoid=False)[源代码]#

基类:Module

Squeeze-and-Excitation (SE) block: AvgPool, FC, Act, FC, Sigmoid.

参数
  • w_in (int) – input width.

  • w_se (int) – se width.

  • act_name (str) – activation name.

  • approx_sigmoid (bool) – approximated sigmoid function.

avg_pool#

gad2d layer.

f_ex#

sequantial which conbines conv2d -> act -> conv2d -> sigmoid.

forward(x)[源代码]#
返回类型

Tensor

class basecls.layers.DropPath(drop_prob=0.0, **kwargs)[源代码]#

基类:Dropout

DropPath block.

参数

drop_prob – the probability to drop (set to zero) each path.

forward(x)[源代码]#
basecls.layers.conv2d(w_in, w_out, k, *, stride=1, dilation=1, groups=1, bias=False)[源代码]#

Helper for building a conv2d layer.

It will calculate padding automatically.

参数
  • w_in (int) – input width.

  • w_out (int) – output width.

  • k (int) – kernel size.

  • stride (int) – stride. Default: 1

  • dilation (int) – dilation. Default: 1

  • groups (int) – groups. Default: 1

  • bias (bool) – enable bias or not. Default: False

返回类型

Conv2d

返回

A conv2d module.

basecls.layers.gap2d(shape=1)[源代码]#

Helper for building a gap2d layer.

参数

shape – output shape. Default: 1

返回类型

AdaptiveAvgPool2d

返回

A gap2d module.

basecls.layers.linear(w_in, w_out, *, bias=False)[源代码]#

Helper for building a linear layer.

参数
  • w_in (int) – input width.

  • w_out (int) – output width.

  • bias (bool) – enable bias or not. Default: False

返回类型

Linear

返回

A linear module.

basecls.layers.norm2d(name, w_in, **kwargs)[源代码]#

Helper for building a norm2d layer.

参数
  • norm_name – normalization name, supports None, "BN", "GN", "IN", "LN" and "SyncBN".

  • w_in (int) – input width.

返回类型

Module

返回

A norm2d module.

basecls.layers.pool2d(k, *, stride=1, name='max')[源代码]#

Helper for building a pool2d layer.

参数
  • k (int) – kernel size.

  • stride (int) – stride. Default: 1

  • name (str) – pooling name, supports "avg" and "max".

返回类型

Module

返回

A pool2d module.

class basecls.layers.Preprocess(mean, std)[源代码]#

基类:Module

forward(inputs)[源代码]#
返回类型

Tuple[Tensor, Tensor]

basecls.layers.adjust_block_compatibility(ws, bs, gs)[源代码]#

Adjusts the compatibility of widths, bottlenecks and groups.

参数
返回类型

Tuple[List[int], ...]

返回

The adjusted widths, bottlenecks and groups.

basecls.layers.compute_precise_bn_stats(cfg, model, dataloader)[源代码]#

Computes precise BN stats on training data.

References: facebookresearch/pycls

参数
basecls.layers.init_vit_weights(module)[源代码]#

Initialization for Vision Transformer (ViT).

References: rwightman/pytorch-image-models

参数

m – module to be initialized.

basecls.layers.init_weights(m, pytorch_style=False, zero_init_final_gamma=False)[源代码]#

Performs ResNet-style weight initialization.

About zero-initialize: Zero-initialize the last BN in each residual branch, so that the residual branch starts with zeros, and each residual block behaves like an identity. This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677.

References: facebookresearch/pycls

参数
  • m (Module) – module to be initialized.

  • pytorch_style (bool) – utilize pytorch style init for group conv. Default: False

  • zero_init_final_gamma (bool) – enable zero-initialize or not. Default: False

basecls.layers.lecun_normal_(tensor)[源代码]#
basecls.layers.make_divisible(value, divisor=8, min_value=None, round_limit=0.0)[源代码]#
basecls.layers.trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0)[源代码]#