# resnet

Python API: `luxonis_train.nodes.blocks.resnet`

ResNet 的残差块和瓶颈块。

## 类

### GenericResidualBlock

残差块，为任何块的输出添加一个快捷连接。

当 `stride` 为 `1` 且 `in_channels` 等于 `expansion * hidden_channels` 时，快捷连接是 `torch.nn.Identity`。否则，它是一个 `1x1`
[ConvBlock](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md)，带有
`stride`、批归一化且无激活。此投影将输入映射到 `expansion * hidden_channels`
通道。[ResNetBlock](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/resnet.md)
和
[ResNetBottleneck](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/resnet.md)
基于此类构建。

#### 方法

##### init

```python
def __init__(in_channels: int, hidden_channels: int, stride: int, expansion: int, final_relu: bool, block: nn.Module):
```

存储分支并构建快捷连接和激活。

参数

 * `in_channels` (`int`): 输入通道数。
 * `hidden_channels` (`int`): 基本宽度。输出有 `expansion * hidden_channels` 个通道。
 * `stride` (`int`): 快捷连接投影的步幅。分支必须以相同的因子减小尺寸。
 * `expansion` (`int`): 从 `hidden_channels` 到输出通道数的因子。
 * `final_relu` (`bool`): 求和后是否跟随 ReLU。
 * `block` (`nn.Module`): 残差分支。其输出必须具有快捷连接输出的形状。

##### forward

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

将快捷连接添加到分支的输出。

加法在 `block` 的输出上就地运行。

参数

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

返回

 * `Tensor`: 求和 `block(x) + shortcut(x)`，当构造函数获得 `final_relu=True` 时，在 ReLU 之后。形状为 `[B, expansion * hidden_channels, H', W']`，其中
   `stride` 设置 `H'` 和 `W'`。

#### 属性

##### block

残差分支。

##### final_relu

`torch.nn.ReLU` 或 `torch.nn.Identity`。

##### shortcut

恒等或 `1x1` 投影。

### ResNetBlock

带有两个 `3x3` 卷积的基本 ResNet 块。

残差分支是一个带有 `stride` 的 `3x3` 卷积、一个批归一化、一个 ReLU、一个 `3x3` 卷积、一个批归一化和
[DropPath](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md)。卷积没有偏置。快捷连接遵循
[GenericResidualBlock](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/resnet.md)
的规则。

> **示例**
> ```pycon
>>> import torch
>>> from torch import nn
>>> from luxonis_train.nodes.blocks import ResNetBlock
>>> isinstance(ResNetBlock(8, 8).shortcut, nn.Identity)
True
>>> block = ResNetBlock(8, 16, stride=2)
>>> block(torch.zeros(1, 8, 8, 8)).shape
torch.Size([1, 16, 4, 4])
```

#### 方法

##### init

```python
def __init__(in_channels: int, hidden_channels: int, stride: int = 1, expansion: int = 1, final_relu: bool = True, droppath_prob:
float = 0.0):
```

构建块的残差分支。

参数

 * `in_channels` (`int`): 输入通道数。
 * `hidden_channels` (`int`): 输出通道数。
 * `stride` (`int`): 第一个卷积和快捷连接的步幅。
 * `expansion` (`int`): 快捷连接通道的因子。分支总是给出 `hidden_channels` 个通道，因此只有 `1` 有效。当值大于 `1` 时，`forward` 会引发 `RuntimeError`。
 * `final_relu` (`bool`): 求和后是否跟随 ReLU。
 * `droppath_prob` (`float`): 分支末尾 [DropPath](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md) 的丢弃概率。

### ResNetBottleneck

ResNet 瓶颈块，包含三个卷积。

残差分支通过 `1x1` 卷积将输入减少到 `hidden_channels`。接着是一个带有 `stride` 的 `3x3` 卷积。最后一个 `1x1` 卷积将结果扩展到 `expansion * hidden_channels` 个通道。每个卷积都有批归一化且无偏置。前两个批归一化后跟随 ReLU，[DropPath](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md) 结束分支。快捷连接遵循 [GenericResidualBlock](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/resnet.md) 的规则。

> **示例**
> ```pycon
>>> import torch
>>> from luxonis_train.nodes.blocks import ResNetBottleneck
>>> block = ResNetBottleneck(16, 8, stride=2)
>>> block(torch.zeros(1, 16, 8, 8)).shape
torch.Size([1, 32, 4, 4])
```

#### 方法

##### init

```python
def __init__(in_channels: int, hidden_channels: int, stride: int = 1, expansion: int = 4, final_relu: bool = True, droppath_prob: float = 0.0):
```

构建块的残差分支。

参数

 * `in_channels` (`int`): 输入通道数。
 * `hidden_channels` (`int`): `1x1` 缩减和 `3x3` 卷积的通道数。
 * `stride` (`int`): `3x3` 卷积和快捷连接的步幅。
 * `expansion` (`int`): 输出有 `expansion * hidden_channels` 个通道。
 * `final_relu` (`bool`): 求和后是否跟随 ReLU。
 * `droppath_prob` (`float`): 分支末尾
   [DropPath](/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md)
   的丢弃概率。
