# blocks

Python API: `luxonis_train.nodes.backbones.ghostfacenet.blocks`

GhostFaceNet 的 Ghost 模块，它通过廉价操作而不是完全卷积来生成部分特征图。

## 类

### AttentionGhostModuleV2

带有解耦注意力分支的门控 Ghost 模块。

注意力分支读取模块的输入。它运行一个卷积，其核大小和步幅与主卷积相同，一个 `1x5` 和一个 `5x1` 深度可分离卷积，一个步幅为 2 的 `2x2` 平均池化，以及一个 sigmoid。分支的卷积具有批量归一化，没有激活。GhostNetV2
将此分支称为 DFC 注意力。最近邻插值将门控调整为输出大小，模块将 Ghost 特征乘以门控。

> **示例**
> ```pycon
>>> import torch
>>> module = AttentionGhostModuleV2(4, 8)
>>> module(torch.zeros(1, 4, 7, 7)).shape
torch.Size([1, 8, 7, 7])
```

#### 方法

##### init

```python
def __init__(in_channels: int, out_channels: int, kernel_size: int = 1, ratio: int = 2, dw_size: int = 3, stride: int = 1,
use_prelu: bool = True):
```

构建 Ghost 卷积和注意力分支。

参数

 * `in_channels` (`int`): 输入通道数。
 * `out_channels` (`int`): 输出通道数。
 * `kernel_size` (`int`): 主核和注意力分支的第一个核的大小。填充为 `kernel_size // 2`。
 * `ratio` (`int`): `out_channels` 与主卷积通道数之比。
 * `dw_size` (`int`): 廉价深度可分离核的大小。填充为 `dw_size // 2`。
 * `stride` (`int`): 主卷积和注意力分支的第一个卷积的步幅。
 * `use_prelu` (`bool`): 主卷积和廉价卷积是否以 `torch.nn.PReLU` 结束。注意力分支在 sigmoid 之前从不激活。

##### forward

```python
def forward(x: Tensor) -> Tensor:
```

将 Ghost 特征乘以注意力门控。

参数

 * `x` (`Tensor`): 形状为 `[B, in_channels, H, W]` 的输入。

返回

 * `Tensor`: 形状为 `[B, out_channels, H', W']` 的输出。主卷积的步幅设置 `H'` 和 `W'`。每个值都是 Ghost 特征乘以 `0` 到 `1` 之间的门控。

#### 属性

##### short_conv

### GhostBottleneckLayer

[GhostBottleneckV2](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/ghostfacenet/blocks.md) 块的阶段，具有一个 Ghost 模块模式。

这五个列表为每个块提供一个值。每个块读取其前一个块的输出。该层将扩展和输出通道乘以 `width_multiplier`。然后将每个计数四舍五入到最接近的 4 的倍数，最小值为 4。如果计数四舍五入后低于其值的 90%，则增加 4。

> **示例**
> ```pycon
>>> import torch
>>> layer = GhostBottleneckLayer(
...     width_multiplier=1,
...     input_channel=16,
...     kernel_sizes=[3, 3],
...     expand_sizes=[48, 72],
...     output_channels=[24, 24],
...     se_ratios=[0.0, 0.25],
...     strides=[2, 1],
...     mode="attention",
... )
>>> len(layer), layer.output_channel
(2, 24)
>>> layer(torch.zeros(1, 16, 8, 8)).shape
torch.Size([1, 24, 4, 4])
```

#### 方法

##### init

```python
def __init__(width_multiplier: int, input_channel: int, kernel_sizes: list[int], expand_sizes: list[int], output_channels: list[int], se_ratios: list[float], strides: list[int], mode: Literal['original', 'attention']):
```

为列表的每个条目构建一个
[GhostBottleneckV2](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/ghostfacenet/blocks.md)。

参数

 * `width_multiplier` (`int`): `expand_sizes` 和 `output_channels` 的比例。
 * `input_channel` (`int`): 第一个块的输入通道数。该层不缩放它。
 * `kernel_sizes` (`list[int]`): 每个块的 `kernel_size`。
 * `expand_sizes` (`list[int]`): 每个块的 `hidden_channels`，在 `width_multiplier` 之前。
 * `output_channels` (`list[int]`): 每个块的 `out_channels`，在 `width_multiplier` 之前。
 * `se_ratios` (`list[float]`): 每个块的 `se_ratio`。
 * `strides` (`list[int]`): 每个块的 `stride`。
 * `mode` (`Literal['original', 'attention']`): 所有块的 `mode`。

引发

 * `ValueError`: 当五个列表长度不同时。

#### 属性

##### output_channel

最后一个块的输出通道数。当列表为空时，它等于 `input_channel`。

### GhostBottleneckV2

GhostFaceNetsV2 的 Ghost 瓶颈，带有一个快捷连接。

主路径包含以下层：

 * 一个带有 `torch.nn.PReLU` 的 Ghost 模块将 `in_channels` 扩展到 `hidden_channels`。
 * 当 `stride` 大于 `1` 时，一个带有批量归一化且无激活的深度可分离卷积会减小空间大小。
 * 当 `se_ratio` 大于 `0` 时，一个带有硬 sigmoid 和 `torch.nn.PReLU` 的
   [SqueezeExciteBlock](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md)
   会缩放通道。
 * 一个没有激活的
   [OriginalGhostModuleV2](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/ghostfacenet/blocks.md)
   投影到 `out_channels`。

当 `in_channels` 等于 `out_channels` 且 `stride` 为 `1` 时，快捷连接是 `torch.nn.Identity`。否则，它是一个带有 `kernel_size` 和 `stride` 的深度可分离卷积，然后是一个
`1x1` 卷积，每个都带有批量归一化。该块将快捷连接添加到主路径。

> **示例**
> ```pycon
>>> import torch
>>> block = GhostBottleneckV2(
...     8, 16, 8, stride=2, se_ratio=0.25, mode="attention"
... )
>>> block(torch.zeros(1, 8, 8, 8)).shape
torch.Size([1, 8, 4, 4])
>>> GhostBottleneckV2(8, 16, 8, mode="original").shortcut
Identity()
```

#### 方法

##### init

```python
def __init__(in_channels: int, hidden_channels: int, out_channels: int, kernel_size: int = 3, stride: int = 1, se_ratio: float =
0.0, *, mode: Literal['original', 'attention']):
```

构建主路径和快捷连接。

参数

 * `in_channels` (`int`): 输入通道数。
 * `hidden_channels` (`int`): 扩展后的通道数。
 * `out_channels` (`int`): 输出通道数。
 * `kernel_size` (`int`): 主路径和快捷连接的深度可分离核的大小。填充为 `(kernel_size - 1) // 2`。
 * `stride` (`int`): 两个深度可分离卷积的步幅。
 * `se_ratio` (`float`): 挤压-激励通道与 `hidden_channels` 的比率。该块将结果四舍五入到 4 的倍数。`0` 或更小表示不添加 [SqueezeExciteBlock](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md)。
 * `mode` (`Literal['original', 'attention']`): 扩展的 Ghost 模块。`"original"` 选择 [OriginalGhostModuleV2](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/ghostfacenet/blocks.md)，`"attention"` 选择 [AttentionGhostModuleV2](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/ghostfacenet/blocks.md)。

##### forward

```python
def forward(x: Tensor) -> Tensor:
```

应用主路径并添加快捷连接。

参数

 * `x` (`Tensor`): 形状为 `[B, in_channels, H, W]` 的输入。

返回

 * `Tensor`: 形状为 `[B, out_channels, H', W']` 的输出。对于奇数 `kernel_size`，`H'` 和 `W'` 是 `H` 和 `W` 除以 `stride` 并向上取整。

#### 属性

##### bn_dw

##### conv_dw

##### ghost1

##### ghost2

##### se

##### shortcut

### OriginalGhostModuleV2

Ghost 模块，通过廉价操作生成部分输出。

主卷积生成 `ceil(out_channels / ratio)` 个通道。深度可分离卷积（廉价操作）从每个通道生成 `ratio - 1` 个通道。该模块将两个结果连接起来，并保留前 `out_channels` 个通道。两个卷积都带有批量归一化。

> **示例**
> ```pycon
>>> import torch
>>> module = OriginalGhostModuleV2(4, 7, ratio=3)
>>> module.primary_conv.out_channels
3
>>> module.cheap_operation.out_channels
6
>>> module(torch.zeros(1, 4, 8, 8)).shape
torch.Size([1, 7, 8, 8])
```

#### 方法

##### init

```python
def __init__(in_channels: int, out_channels: int, kernel_size: int = 1, ratio: int = 2, dw_size: int = 3, stride: int = 1, use_prelu: bool = True):
```

构建主卷积和廉价卷积。

参数

 * `in_channels` (`int`): 输入通道数。
 * `out_channels` (`int`): 输出通道数。
 * `kernel_size` (`int`): 主核的大小。填充为 `kernel_size // 2`。
 * `ratio` (`int`): `out_channels` 与主卷积通道数之比。
 * `dw_size` (`int`): 深度可分离核的大小。填充为 `dw_size // 2`。
 * `stride` (`int`): 主卷积的步幅。
 * `use_prelu` (`bool`): 两个卷积是否都以 `torch.nn.PReLU` 结束。当为 `False` 时，它们没有激活。

##### forward

```python
def forward(x: Tensor) -> Tensor:
```

连接主特征和廉价特征。

参数

 * `x` (`Tensor`): 形状为 `[B, in_channels, H, W]` 的输入。

返回

 * `Tensor`: 形状为 `[B, out_channels, H', W']` 的输出。主卷积的步幅设置 `H'` 和 `W'`。

#### 属性

##### cheap_operation

##### primary_conv
