Skip to content
heliaEDGE
Reference
HELIA

utils

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

Machine-readable model

  • make_divisiblefunctionEnsure layer has # channels divisble by divisor https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
  • undot_layer_namesfunctionRename layers whose names contain .
  • use_helia_layer_normalizationfunctionSwap Keras LayerNormalization entries for heliaedge.layers.LayerNormalization.
  • load_modelfunctionLoads a Keras model stored either remotely or locally.
  • append_layersfunctionAppends layers to a model by cloning it and adding the layers.
function

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

helia_edge/models/utils.py:28

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 of make_divisible
NameTypeDefaultDescription
vintRequiredNumber of channels
divisorint4Divisor. Defaults to 4.
min_valueint | NoneNoneMin # channels. Defaults to None.
Returns of make_divisible
ValueTypeDescription
intintNumber of channels
function

Rename layers whose names contain .

helia_edge/models/utils.py:49

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 of undot_layer_names
NameTypeDefaultDescription
configAnyRequiredThe parsed ``config.json`` of a ``.keras`` file.
Returns of undot_layer_names
ValueTypeDescription
tupletuple[Any, dict[str, str]]The renamed config (a new object) and the mapping from old to new names.
Errors raised by undot_layer_names
TypeDescription
ValueErrorIf a new name equals another layer's name.
function

Swap Keras LayerNormalization entries for heliaedge.layers.LayerNormalization.

helia_edge/models/utils.py:140

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 of use_helia_layer_normalization
NameTypeDefaultDescription
configAnyRequiredThe parsed ``config.json`` of a ``.keras`` file.
Returns of use_helia_layer_normalization
ValueTypeDescription
tupletuple[Any, int]The new config and the number of entries swapped.
function

Loads a Keras model stored either remotely or locally.

helia_edge/models/utils.py:195

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 of load_model
NameTypeDefaultDescription
model_pathstrRequiredSource 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 of load_model
TypeDescription
keras.Modelkeras.Model: Model
function

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

helia_edge/models/utils.py:284

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 of append_layers
NameTypeDefaultDescription
modelkeras.ModelRequiredModel
layerslist[keras.layers.Layer]RequiredLayers to append
copy_weightsboolTrueCopy weights. Defaults to True.
Returns of append_layers
TypeDescription
keras.Modelkeras.Model: Model