tfaip.model

ModelBase

Implementation of the ModelBase

class tfaip.model.modelbase.ModelBase(params: TMP, *args, **kwargs)

Bases: Generic[tfaip.model.modelbase.TMP], abc.ABC

The ModelBase class provides the implementation of the keras Model, its losses and metrics.

classmethod params_cls()Type[TMP]
classmethod all_custom_objects()Dict[str, Any]
classmethod additional_layers()List[Type[tensorflow.python.keras.engine.base_layer.Layer]]

List all custom layers of the model. This is required to enable eager mode in LAV. (See e.g. Tutorial for an example)

Returns

List of all layers

__init__(params: TMP, *args, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

setup()
property params
best_logging_settings()Tuple[str, str]

Which metric/loss shall be logged, and if the minimum or maximum of this value is better. E. G.: “min”, “CER” or “max”, “ACC” or “min”, “loss” The metric must match the name of the logger :return: str, str

build(inputs_targets: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]])Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]]

Override _build for custom implementation. Do this with caution :param inputs_targets: Dictionary of both the inputs and the targets :return: The outputs of the model

abstract create_graph(params: TMP)GraphBase
additional_outputs(inputs: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]], outputs: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]])Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]]
extended_metric(inputs_targets: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]], outputs: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]])Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]]

use lambda layers, you can not use self.<variables> directly, it will result in pickle-error Override _extended_metric for custom implementation. :param inputs_targets: A dictionary containing both the inputs and the targets of the model :param outputs: A dictionary providing the outputs of the graph :return: A dictionary of metric values

metric()Dict[str, tfaip.model.metric.definitions.MetricDefinition]

Override _metric in a custom implementation. Standard metrics allow for one input and one target only, and also have access to the sample weights.

Returns

A Dictionary of MetricDefinition

sample_weights(inputs: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]], targets: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]])Dict[str, Any]

The weights of the samples. The output key must match the respective metric, extended_metric, or loss name. Thus, if you compute the loss “CTC” and the metric “CER” and “CAR” and all three shall be weighted, return a dictionary with three entries but the same values.

Parameters
  • inputs – The inputs of the model

  • targets – The outputs of the model

Returns

Dictionary of the weights

extended_loss(inputs_targets: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]], outputs: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]])Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]]

A dictionary of all losses of the model that will be averaged if there are multiple. Only override _extended_loss for the custom implementation

Parameters
  • inputs_targets – Inputs and targets of the model

  • outputs – Outputs of the model

Returns

Dictionary of the loss

loss()Dict[str, tfaip.model.losses.definitions.LossDefinition]

Losses based on keras.losses. Implement _loss :returns: A dict of the loss name and a LossDefinition

See also

extended_loss

loss_weights()Optional[Dict[str, float]]

An optional dictionary of the weights of the losses. Override _loss_weights for custom implementation. :return: loss name and its weight

print_evaluate(sample: tfaip.data.pipeline.definitions.Sample, data: tfaip.data.data.DataBase, print_fn=<built-in function print>)

Print evaluation output :param sample: an unbatched sample :param data: The data class of the scenario :param print_fn: the print function to use

export_graphs(inputs: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]], outputs: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]], targets: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor]])Dict[str, tensorflow.python.keras.engine.training.Model]
property tensorboard_handler

ModelBaseParams

Definition of the ModelBaseParams

class tfaip.model.modelbaseparams.ModelBaseParams

Bases: object

__init__()None

Initialize self. See help(type(self)) for accurate signature.

classmethod from_dict(kvs: Optional[Union[dict, list, str, int, float, bool]], *, infer_missing=False)A
classmethod from_json(s: Union[str, bytes, bytearray], *, parse_float=None, parse_int=None, parse_constant=None, infer_missing=False, **kw)A
classmethod schema(*, infer_missing: bool = False, only=None, exclude=(), many: bool = False, context=None, load_only=(), dump_only=(), partial: bool = False, unknown=None)dataclasses_json.mm.SchemaF[A]
to_dict(encode_json=False)Dict[str, Optional[Union[dict, list, str, int, float, bool]]]
to_json(*, skipkeys: bool = False, ensure_ascii: bool = True, check_circular: bool = True, allow_nan: bool = True, indent: Optional[Union[int, str]] = None, separators: Optional[Tuple[str, str]] = None, default: Optional[Callable] = None, sort_keys: bool = False, **kw)str