# helia_edge.models.utils

## Model utilities API

This module provides utility functions to work with Keras models.

**Functions**

| Name | Description |
| --- | --- |
| `make_divisible` | Ensure layer has # channels divisble by divisor |
| `load_model` | Loads a Keras model stored either remotely or locally |
| `append_layers` | Appends layers to a model by cloning it and adding the layers |

## helia_edge.models.utils.make_divisible

`function` · `python`

```python
make_divisible(v: int, divisor: int = 4, min_value: int | None = None) -> int
```

Ensure layer has # channels divisble by divisor
   https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| v | int | Required | Number of channels |
| divisor | int | 4 | Divisor. Defaults to 4. |
| min_value | int \| None | None | Min # channels. Defaults to None. |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
| int | int | Number of channels |

Source: `helia_edge/models/utils.py:28`

## helia_edge.models.utils.undot_layer_names

`function` · `python`

```python
undot_layer_names(config: Any) -> tuple[Any, dict[str, str]]
```

Rename layers whose names contain ``.`` in a saved Keras model config.

Torch registers each layer as a module attribute, and attribute names cannot contain ``.``.
Every layer-like entry is renamed, including the model itself and any named loss or metric
objects, so a name in any of them can trigger the collision check.
Each ``.`` becomes ``_``. Layer names, connections (``keras_history``), the model's input
and output lists, and the output-name keys of ``compile_config`` (losses, metrics, loss
weights) are renamed together; weights are stored by structure, not by name, so they need no
change.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| config | Any | Required | The parsed ``config.json`` of a ``.keras`` file. |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
| tuple | tuple[Any, dict[str, str]] | The renamed config (a new object) and the mapping from old to new names. |

**Raises**

| Name | Description |
| --- | --- |
| ValueError | If a new name equals another layer's name. |

Source: `helia_edge/models/utils.py:49`

## helia_edge.models.utils.use_helia_layer_normalization

`function` · `python`

```python
use_helia_layer_normalization(config: Any) -> tuple[Any, int]
```

Swap Keras ``LayerNormalization`` entries for ``helia_edge.layers.LayerNormalization``.

The two classes share configuration and weights. Keras's Torch backend cannot normalize
non-trailing axes; the helia_edge class can, so this lets models saved with the Keras class
load on Torch. Entries that normalize the last axis alone keep the Keras class.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| config | Any | Required | The parsed ``config.json`` of a ``.keras`` file. |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
| tuple | tuple[Any, int] | The new config and the number of entries swapped. |

Source: `helia_edge/models/utils.py:140`

## helia_edge.models.utils.load_model

`function` · `python`

```python
load_model(model_path: os.PathLike) -> keras.Model
```

Loads a Keras model stored either remotely or locally.
NOTE: Currently supports wandb, s3, and https for remote.

On the Torch backend, ``.keras`` files are adapted before loading: names containing ``.``
(common in models saved by earlier helia-edge versions) are renamed to use ``_``, and Keras
``LayerNormalization`` layers over non-trailing axes load as
``helia_edge.layers.LayerNormalization``; see ``undot_layer_names`` and
``use_helia_layer_normalization``. Other formats load unchanged.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| model_path | str | Required | Source path WANDB: wandb:[[entity/]project/]collectionName:[alias] FILE: file:/path/to/model.tf S3: s3:bucket/prefix/model.tf https: https://path/to/model.tf |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.Model | keras.Model: Model |

Source: `helia_edge/models/utils.py:195`

## helia_edge.models.utils.append_layers

`function` · `python`

```python
append_layers(model: keras.Model, layers: list[keras.Layer], copy_weights: bool = True) -> keras.Model
```

Appends layers to a model by cloning it and adding the layers.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| model | keras.Model | Required | Model |
| layers | list[keras.layers.Layer] | Required | Layers to append |
| copy_weights | bool | True | Copy weights. Defaults to True. |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.Model | keras.Model: Model |

Source: `helia_edge/models/utils.py:284`
