# blocks

Python API: `luxonis_train.nodes.necks.reppan_neck.blocks`

The upsampling and downsampling blocks of the RepPAN neck.

## Classes

| Name | Description |
| --- | --- |
| [`CSPDownBlock`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-CSPDownBlock) | Bottom-up fusion step of [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck) with a CSP block. |
| [`CSPUpBlock`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-CSPUpBlock) | Top-down fusion step of [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck) with a CSP block. |
| [`PANDownBlockBase`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-PANDownBlockBase) | Base class of the bottom-up fusion steps of [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck). |
| [`PANUpBlockBase`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-PANUpBlockBase) | Base class of the top-down fusion steps of [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck). |
| [`RepDownBlock`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-RepDownBlock) | Bottom-up fusion step of [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck) with RepVGG-style blocks. |
| [`RepUpBlock`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-RepUpBlock) | Top-down fusion step of [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck) with RepVGG-style blocks. |

### class `CSPDownBlock`

```python
class CSPDownBlock(PANDownBlockBase)
```

Bottom-up fusion step of
[RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck)
with a CSP block.

The encode block is a
[CSPStackRepBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-CSPStackRepBlock)
that maps the concatenation to `out_channels`.
[RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck)
uses this step when `block` is `"CSPStackRepBlock"`, as in the `"m"` and `"l"` variants.

> **Example**
> ```pycon
>>> import torch
>>> block = CSPDownBlock(
...     4, 8, in_channels_next=12, out_channels=16, n_repeats=2, e=0.25
... )
>>> block.encode_block.conv_1.out_channels
4
>>> x0, x1 = torch.zeros(1, 4, 8, 8), torch.zeros(1, 12, 4, 4)
>>> block(x0, x1).shape
torch.Size([1, 16, 4, 4])
```

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-CSPDownBlock-__init__) | Initialize the downsampling layer and the CSP block. |

#### method `__init__`

```python
def __init__(
    in_channels: int,
    downsample_out_channels: int,
    in_channels_next: int,
    out_channels: int,
    n_repeats: int,
    e: float,
)
```

Initialize the downsampling layer and the CSP block.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `in_channels` | `int` | Number of channels of the fine input. | required |
| `downsample_out_channels` | `int` | Number of channels after the downsampling convolution. | required |
| `in_channels_next` | `int` | Number of channels of the lateral input, which the step concatenates. | required |
| `out_channels` | `int` | Number of output channels. | required |
| `n_repeats` | `int` | Controls the number of RepVGG-style blocks in the [CSPStackRepBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-CSPStackRepBlock). Each [BottleRep](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-BottleRep) holds two of them, so the stack has `max(1, n_repeats // 2)` [BottleRep](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-BottleRep) blocks and `2 * max(1, n_repeats // 2)` RepVGG-style blocks. | required |
| `e` | `float` | Fraction of `out_channels` in each of the two paths of the [CSPStackRepBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-CSPStackRepBlock). | required |

### class `CSPUpBlock`

```python
class CSPUpBlock(PANUpBlockBase)
```

Top-down fusion step of [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck) with a CSP block.

The encode block is a [CSPStackRepBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-CSPStackRepBlock) that maps the concatenation to `out_channels`. [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck) uses this step when `block` is `"CSPStackRepBlock"`, as in the `"m"` and `"l"` variants.

> **Example**
> ```pycon
>>> block = CSPUpBlock(
...     32, in_channels_next=16, out_channels=8, n_repeats=4, e=0.5
... )
>>> block.encode_block.conv_1.out_channels
4
>>> len(block.encode_block.rep_stack)
2
```

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-CSPUpBlock-__init__) | Initialize the upsampling layers and the CSP block. |

#### method `__init__`

```python
def __init__(
    in_channels: int,
    in_channels_next: int,
    out_channels: int,
    n_repeats: int,
    e: float,
)
```

Initialize the upsampling layers and the CSP block.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `in_channels` | `int` | Number of channels of the coarse input. | required |
| `in_channels_next` | `int` | Number of channels of the finer input, which the step concatenates. | required |
| `out_channels` | `int` | Number of output channels. | required |
| `n_repeats` | `int` | Controls the number of RepVGG-style blocks in the [CSPStackRepBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-CSPStackRepBlock). Each [BottleRep](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-BottleRep) holds two of them, so the stack has `max(1, n_repeats // 2)` [BottleRep](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-BottleRep) blocks and `2 * max(1, n_repeats // 2)` RepVGG-style blocks. | required |
| `e` | `float` | Fraction of `out_channels` in each of the two paths of the [CSPStackRepBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-CSPStackRepBlock). | required |

### class `PANDownBlockBase`

```python
class PANDownBlockBase(ABC, nn.Module)
```

Base class of the bottom-up fusion steps of
[RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck).

A `3x3`
[ConvBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-ConvBlock)
with stride `2`, padding `1`, batch norm, and ReLU halves the height and the width of the fine input. An odd size rounds up. The
block concatenates the result with the lateral input along the channel axis and runs `encode_block` on it. A subclass selects
`encode_block`.

Attributes

| Name | Description |
| --- | --- |
| `downsample` |  |
| `encode_block` |  |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-PANDownBlockBase-__init__) | Initialize the downsampling convolution. |
| [`forward`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-PANDownBlockBase-forward) | Downsample the fine map and fuse it with the lateral map. |

#### method `__init__`

```python
def __init__(in_channels: int, downsample_out_channels: int, encode_block: nn.Module)
```

Initialize the downsampling convolution.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `in_channels` | `int` | Number of channels of the fine input. | required |
| `downsample_out_channels` | `int` | Number of channels after the downsampling convolution. | required |
| `encode_block` | `nn.Module` | Block that runs on the concatenation. Its input has `downsample_out_channels` plus the channels of the lateral input. | required |

#### method `forward`

```python
def forward(x0: Tensor, x1: Tensor) -> Tensor
```

Downsample the fine map and fuse it with the lateral map.

> **Example**
> ```pycon
>>> import torch
>>> block = RepDownBlock(
...     4, 8, in_channels_next=12, out_channels=16, n_repeats=1
... )
>>> x0, x1 = torch.zeros(1, 4, 8, 8), torch.zeros(1, 12, 4, 4)
>>> block(x0, x1).shape
torch.Size([1, 16, 4, 4])
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `x0` | `Tensor` | Fine map of shape `[B, in_channels, H, W]`. | required |
| `x1` | `Tensor` | Lateral map of shape `[B, C1, H / 2, W / 2]`. An odd `H` or `W` rounds up. In the subclasses, `C1` is `in_channels_next`. | required |

Returns

| Type | Description |
| --- | --- |
| `Tensor` | The output of `encode_block`. In the subclasses, it has the shape `[B, out_channels, H / 2, W / 2]`. |

### class `PANUpBlockBase`

```python
class PANUpBlockBase(ABC, nn.Module)
```

Base class of the top-down fusion steps of [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck).

A `1x1` [ConvBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-ConvBlock) with batch norm and ReLU maps the coarse input to `out_channels`. A `2x2` transposed convolution with stride `2` then doubles the height and the width. The block concatenates the result with the finer input along the channel axis and runs `encode_block` on it. A subclass selects `encode_block`.

Attributes

| Name | Description |
| --- | --- |
| `conv` |  |
| `encode_block` |  |
| `upsample` |  |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-PANUpBlockBase-__init__) | Build the `1x1` convolution and the upsampling layer. |
| [`forward`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-PANUpBlockBase-forward) | Upsample the coarse map and fuse it with the finer map. |

#### method `__init__`

```python
def __init__(in_channels: int, out_channels: int, encode_block: nn.Module)
```

Build the `1x1` convolution and the upsampling layer.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `in_channels` | `int` | Number of channels of the coarse input. | required |
| `out_channels` | `int` | Number of channels after the `1x1` convolution. The upsampling layer keeps this number. | required |
| `encode_block` | `nn.Module` | Block that runs on the concatenation. Its input has `out_channels` plus the channels of the finer input. | required |

#### method `forward`

```python
def forward(x0: Tensor, x1: Tensor) -> tuple[Tensor, Tensor]
```

Upsample the coarse map and fuse it with the finer map.

> **Example**
> ```pycon
>>> import torch
>>> block = RepUpBlock(
...     32, in_channels_next=16, out_channels=8, n_repeats=1
... )
>>> x0, x1 = torch.zeros(1, 32, 4, 4), torch.zeros(1, 16, 8, 8)
>>> conv_out, out = block(x0, x1)
>>> conv_out.shape, out.shape
(torch.Size([1, 8, 4, 4]), torch.Size([1, 8, 8, 8]))
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `x0` | `Tensor` | Coarse map of shape `[B, in_channels, H, W]`. | required |
| `x1` | `Tensor` | Finer map of shape `[B, C1, 2 * H, 2 * W]`. In the subclasses, `C1` is `in_channels_next`. | required |

Returns

| Type | Description |
| --- | --- |
| `tuple[Tensor, Tensor]` | The tuple `(conv_out, out)`. `conv_out` is the output of the `1x1` convolution, of shape `[B, out_channels, H, W]`. [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck) gives it to a bottom-up step as the lateral input. `out` is the output of `encode_block`. In the subclasses, `out` has the shape `[B, out_channels, 2 * H, 2 * W]`. |

### class `RepDownBlock`

```python
class RepDownBlock(PANDownBlockBase)
```

Bottom-up fusion step of
[RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck)
with RepVGG-style blocks.

The encode block is a
[BlockRepeater](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-BlockRepeater)
of `n_repeats`
[GeneralReparameterizableBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-GeneralReparameterizableBlock)
layers. The first layer maps the concatenation to `out_channels`. The other layers keep that number.
[RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck)
uses this step when `block` is `"RepBlock"`, as in the `"n"` and `"s"` variants.

> **Example**
> ```pycon
>>> import torch
>>> block = RepDownBlock(
...     4, 8, in_channels_next=12, out_channels=16, n_repeats=3
... )
>>> block.encode_block(torch.zeros(1, 20, 4, 4)).shape
torch.Size([1, 16, 4, 4])
```

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-RepDownBlock-__init__) | Initialize the downsampling layer and the RepVGG-style stack. |

#### method `__init__`

```python
def __init__(
    in_channels: int,
    downsample_out_channels: int,
    in_channels_next: int,
    out_channels: int,
    n_repeats: int,
)
```

Initialize the downsampling layer and the RepVGG-style stack.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `in_channels` | `int` | Number of channels of the fine input. | required |
| `downsample_out_channels` | `int` | Number of channels after the downsampling convolution. | required |
| `in_channels_next` | `int` | Number of channels of the lateral input, which the step concatenates. | required |
| `out_channels` | `int` | Number of output channels. | required |
| `n_repeats` | `int` | Number of [GeneralReparameterizableBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-GeneralReparameterizableBlock) layers. A value below `1` still builds one layer. | required |

### class `RepUpBlock`

```python
class RepUpBlock(PANUpBlockBase)
```

Top-down fusion step of [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck) with RepVGG-style blocks.

The encode block is a [BlockRepeater](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-BlockRepeater) of `n_repeats` [GeneralReparameterizableBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-GeneralReparameterizableBlock) layers. The first layer maps the concatenation to `out_channels`. The other layers keep that number. [RepPANNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/reppan_neck.md#reppan_neck-Classes-RepPANNeck) uses this step when `block` is `"RepBlock"`, as in the `"n"` and `"s"` variants.

> **Example**
> ```pycon
>>> import torch
>>> block = RepUpBlock(
...     32, in_channels_next=16, out_channels=8, n_repeats=3
... )
>>> block.encode_block(torch.zeros(1, 24, 4, 4)).shape
torch.Size([1, 8, 4, 4])
```

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/reppan_neck/blocks.md#blocks-Classes-RepUpBlock-__init__) | Initialize the upsampling layers and the RepVGG-style stack. |

#### method `__init__`

```python
def __init__(in_channels: int, in_channels_next: int, out_channels: int, n_repeats: int)
```

Initialize the upsampling layers and the RepVGG-style stack.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `in_channels` | `int` | Number of channels of the coarse input. | required |
| `in_channels_next` | `int` | Number of channels of the finer input, which the step concatenates. | required |
| `out_channels` | `int` | Number of output channels. | required |
| `n_repeats` | `int` | Number of [GeneralReparameterizableBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-GeneralReparameterizableBlock) layers. A value below `1` still builds one layer. | required |
