tfaip.model

ModelBase

Implementation of the ModelBase

class tfaip.model.modelbase.ModelBase(params: tfaip.model.modelbase.TMP, **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[tfaip.model.modelbase.TMP]
classmethod all_custom_objects() Dict[str, Type[keras.engine.base_layer.Layer]]

Custom objects required to instantiate saved keras models in eager mode (reinstantiation)

classmethod base_custom_objects() Dict[str, Type[keras.engine.base_layer.Layer]]

Custom objects required to instantiate saved keras models even in graph mode

__init__(params: tfaip.model.modelbase.TMP, **kwargs)
static root_graph_cls() Type[RootGraph]
property params: tfaip.model.modelbase.TMP
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/mean_epoch” The metric must match the name of the logger :return: str, str

metric(inputs, targets, outputs) List[Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor, keras.engine.keras_tensor.KerasTensor]]

The metrics of the model

Override _metric in a custom implementation.

Instantiate keras metrics in a Models init function and return the called metric here.

sample_weights(inputs: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor, keras.engine.keras_tensor.KerasTensor]], targets: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor, 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

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

Returns a list of losses

The losses will be collected by weighting with the loss_weights that default to 1

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, keras.engine.keras_tensor.KerasTensor]], outputs: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor, keras.engine.keras_tensor.KerasTensor]], targets: Dict[str, Union[tensorflow.python.framework.ops.Tensor, tensorflow.python.keras.engine.keras_tensor.KerasTensor, keras.engine.keras_tensor.KerasTensor]]) Dict[str, keras.engine.training.Model]

Function to export additional (sub)-graphs alongside the default one.

add_all_losses(model, inputs, targets, outputs)

Add the losses, defined by loss and loss_weights.

Losses are summed up to form the final loss. Each loss is added as a standalone metric.

add_all_metrics(model, inputs, targets, outputs)

Add the metrics and their sample_weights to the model.

pre_proc_targets(inputs, targets)

Method to be called on the targets before outputs are computed.

post_proc_targets(inputs, targets, outputs)

Method to be called on the targets after outputs are computed.

wrap_model_with_loss_and_metric(model, inputs, targets, outputs, with_losses=True, with_metrics=True)

Adds all losses and metrics.

This function will apply post-processing on the targets.

class tfaip.model.modelbase.TFAIPKerasModel(*args, **kwargs)

Bases: keras.engine.training.Model

call(inputs, training=None, mask=None)

Calls the model on new inputs.

In this case call just reapplies all ops in the graph to the new inputs (e.g. build a new computational graph from the provided inputs).

Note: This method should not be called directly. It is only meant to be overridden when subclassing tf.keras.Model. To call a model on an input, always use the __call__ method, i.e. model(inputs), which relies on the underlying call method.

Parameters
  • inputs – Input tensor, or dict/list/tuple of input tensors.

  • training – Boolean or boolean scalar tensor, indicating whether to run the Network in training mode or inference mode.

  • mask – A mask or list of masks. A mask can be either a tensor or None (no mask).

Returns

A tensor if there is a single output, or a list of tensors if there are more than one outputs.

get_config()

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).

Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.

Returns

Python dictionary.

__init__(inputs, outputs, **kwargs)

ModelBaseParams

Definition of the ModelBaseParams

class tfaip.model.modelbaseparams.ModelBaseParams

Bases: object

Base-Params for a model and the Graph.

Inherit cls() to return the ModelBase and graph_cls() to return the GenericGraphBase.

abstract static cls() Type[ModelBase]
create(**kwargs) ModelBase
abstract graph_cls() Type[GenericGraphBase]
create_graph(**kwargs) GenericGraphBase
__init__() None
classmethod from_dict(kvs: Optional[Union[dict, list, str, int, float, bool]], *, infer_missing=False) dataclasses_json.api.A
classmethod from_json(s: Union[str, bytes, bytearray], *, parse_float=None, parse_int=None, parse_constant=None, infer_missing=False, **kw) dataclasses_json.api.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[dataclasses_json.mm.A]
to_dict(encode_json=False, include_cls=True) 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