# helia_edge.models.unet

## U-Net

### Overview

U-Net is a type of convolutional neural network (CNN) that is commonly used for segmentation tasks. U-Net is a fully convolutional network that consists of a series of convolutional layers and pooling layers. The pooling layers are used to downsample the input while the convolutional layers are used to upsample the input. The skip connections between the pooling layers and convolutional layers allow U-Net to preserve spatial/temporal information while also allowing for faster training and inference times.

For more info, refer to the original paper [U-Net: Convolutional Networks for Biomedical Image Segmentation](https://doi.org/10.1007/978-3-319-24574-4_28).

Parameters are in ``helia_edge.models.unet_params``.

**Functions**

| Name | Description |
| --- | --- |
| `build` | U-Net model from ``UNetParams`` |
| `unet_layer` | Generate functional U-Net model |

### Additions

The U-Net architecture has been modified to allow the following:

* Enable 1D and 2D variants.
* Convolutional pairs can factorized into depthwise separable convolutions.
* Specifiy the number of convolutional layers per block both downstream and upstream.
* Normalization can be set between batch normalization and layer normalization.
* ReLU is replaced with the approximated ReLU6.

### Usage

```python
from helia_edge.models import ModelSpec, UNetBlockParams, UNetParams, build

block = dict(depth=2, ddepth=1, kernel=(1, 5), pool=(1, 3), strides=(1, 2), skip=True, seperable=True)
params = UNetParams(
    blocks=[UNetBlockParams(filters=f, **block) for f in (12, 24, 32, 48)],
    output_kernel_size=(1, 5),
    include_top=True,
    use_logits=True,
    num_classes=5,
)
model = build(ModelSpec(params=params, input_shape=(1, 800, 1)))
```

## helia_edge.models.unet.unet_layer

`function` · `python`

```python
unet_layer(x: keras.KerasTensor, params: UNetParams) -> keras.KerasTensor
```

Create UNet TF functional model

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| x | keras.KerasTensor | Required | Input tensor |
| params | UNetParams | Required | Model parameters. |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.KerasTensor | keras.KerasTensor: Output tensor |

Source: `helia_edge/models/unet.py:51`

## helia_edge.models.unet.build

`function` · `python`

```python
build(
    params: UNetParams,
    input_shape: tuple[int | None, ...],
    *,
    batch_size: int | None = None,
    name: str | None = None,
) -> keras.Model
```

Build a UNet model.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| params | UNetParams | Required | Model parameters. |
| input_shape | tuple[int \| None, ...] | Required | Input shape without the batch axis; None for a variable axis. |
| batch_size | int \| None | None | Static batch size; None for a dynamic batch. |
| name | str \| None | None | Model name; the family when None. |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.Model | keras.Model: The model, named ``unet`` unless ``name`` is given. |

Source: `helia_edge/models/unet.py:264`
