# bisenet_head

Python API: `luxonis_train.nodes.heads.bisenet_head`

The BiSeNet segmentation head.

## Classes

| Name | Description |
| --- | --- |
| [`BiSeNetHead`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/bisenet_head.md#bisenet_head-Classes-BiSeNetHead) | BiSeNet segmentation head. |

### class `BiSeNetHead`

```python
class BiSeNetHead(BaseHead)
```

BiSeNet segmentation head.

 * `Inputs:`: * `inputs` (`Tensor`): [B, C, H ⁄ s, W ⁄ s]
 * `Outputs:`: * `segmentation` (`Tensor`): [B, nclasses, H, W] logits

> **References**
> * Source: Reimplemented from [BiSeNet: Bilateral Segmentation Network for Real-time Semantic Segmentation](https://arxiv.org/abs/1808.00897).
 * License: Apache-2.0 (this project)

> **Notes**
> H and W are the height and the width of the model input. The scale s is a power of two that the head computes from the input size and the model input size. The head applies a 3x3 [luxonis_train.nodes.blocks.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, and a 1x1 convolution with nclasses⋅s2 output channels. A `torch.nn.PixelShuffle` with the factor s then gives the logits. `forward` does not check the mode, so export mode also gives the logits.

 * `Variants:`: None. Configure the node through `params`.

> **See Also**
> [The BiseNetv1 repository](https://github.com/taveraantonio/BiseNetv1)

> **Example**
> A node entry in the `model.nodes` section of a config:

```yaml
- name: BiSeNetHead
  inputs: [ContextSpatial]
```

 * `Compatible with:`: * Attach index: `-1`, the last output of the input node
    * Required labels: `segmentation`
    * Losses: *
      [BCEWithLogitsLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/bce_with_logits.md#bce_with_logits-Classes-BCEWithLogitsLoss)
       * [CrossEntropyLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/cross_entropy.md#cross_entropy-Classes-CrossEntropyLoss)
       * [OHEMLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/ohem_loss.md#ohem_loss-Classes-OHEMLoss)
       * [SigmoidFocalLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/sigmoid_focal_loss.md#sigmoid_focal_loss-Classes-SigmoidFocalLoss)
       * [SmoothBCEWithLogitsLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/smooth_bce_with_logits.md#smooth_bce_with_logits-Classes-SmoothBCEWithLogitsLoss)
       * [SoftmaxFocalLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/softmax_focal_loss.md#softmax_focal_loss-Classes-SoftmaxFocalLoss)
    * Metrics: *
      [Accuracy](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/torchmetrics.md#torchmetrics-Classes-Accuracy)
       * [ConfusionMatrix](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/confusion_matrix/confusion_matrix.md#confusion_matrix-Classes-ConfusionMatrix)
       * [DiceCoefficient](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/dice_coefficient.md#dice_coefficient-Classes-DiceCoefficient)
       * [F1Score](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/torchmetrics.md#torchmetrics-Classes-F1Score)
       * [JaccardIndex](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/torchmetrics.md#torchmetrics-Classes-JaccardIndex)
       * [MIoU](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/mean_iou.md#mean_iou-Classes-MIoU)
       * [Precision](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/torchmetrics.md#torchmetrics-Classes-Precision)
       * [Recall](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/torchmetrics.md#torchmetrics-Classes-Recall)
    * Visualizers:
      [SegmentationVisualizer](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/segmentation_visualizer.md#segmentation_visualizer-Classes-SegmentationVisualizer)
    * Export parser: `SegmentationParser`

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `conv_1x1` |  |  |
| `conv_3x3` |  |  |
| `in_channels` | `int` |  |
| `in_height` | `int` |  |
| `in_width` | `int` |  |
| `parser` | `str` |  |
| `upscale` |  |  |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/bisenet_head.md#bisenet_head-Classes-BiSeNetHead-__init__) | Build the convolutions and the pixel shuffle upsampling. |
| [`forward`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/bisenet_head.md#bisenet_head-Classes-BiSeNetHead-forward) | Compute the segmentation logits at the model input size. |
| [`get_custom_head_config`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/bisenet_head.md#bisenet_head-Classes-BiSeNetHead-get_custom_head_config) | Return the head-specific metadata for the NN Archive. |

#### method `__init__`

```python
def __init__(intermediate_channels: int = 64, **kwargs)
```

Build the convolutions and the pixel shuffle upsampling.

The constructor computes the scale s = 2n.
[infer_upscale_factor](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/utils/general.md#general-Functions-infer_upscale_factor)
gives n from the input size and the model input size. That function raises `ValueError` when the height ratio or the width ratio
is not a power of two, or when the two ratios differ.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `intermediate_channels` | `int` | The number of output channels of the 3x3 convolution. | `64` |
| `**kwargs` |  | Keyword arguments for [BaseNode](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/base_node.md#base_node-Classes-BaseNode). They must hold `original_in_shape`, `input_shapes` or `in_sizes`, and the class count through `n_classes` or `dataset_metadata`. |  |

#### method `forward`

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

Compute the segmentation logits at the model input size.

> **Example**
> ```pycon
>>> import torch
>>> from torch import Size
>>> from luxonis_train.nodes import BiSeNetHead
>>> head = BiSeNetHead(
...     n_classes=3,
...     input_shapes=[{"features": [Size([1, 16, 8, 8])]}],
...     original_in_shape=Size([3, 32, 32]),
... )
>>> head(torch.zeros(1, 16, 8, 8)).shape
torch.Size([1, 3, 32, 32])
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `inputs` | `Tensor` | The feature map of shape `[B, C, H / s, W / s]`. | required |

Returns

| Type | Description |
| --- | --- |
| `Tensor` | The logits of shape `[B, n_classes, H, W]`. [BaseNode.run](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/base_node.md#base_node-Classes-BaseNode-run) puts them under the `"segmentation"` key. |

#### method `get_custom_head_config`

```python
def get_custom_head_config() -> Params
```

Return the head-specific metadata for the NN Archive.

A subclass overrides the method to give its parser more values. [get_head_config](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/base_head.md#base_head-Classes-BaseHead-get_head_config) merges the result into the `"metadata"` dictionary. The base implementation returns an empty dictionary.

Returns

| Type | Description |
| --- | --- |
| `Params` | The additional metadata keys and their values. |
