tfaip.scenario¶
ScenarioBase¶
Definition of ScenarioBase
-
tfaip.scenario.scenariobase.list_scenarios_in_module(module) → List[Tuple[str, Type[tfaip.scenario.scenariobase.ScenarioBase]]]¶
-
class
tfaip.scenario.scenariobase.KerasModelData(extended_loss_names: List[str], extended_metric_names: List[str], tensorboard_output_names: List[str])¶ Bases:
objectUtility structure to hold shared information about the model
-
extended_loss_names: List[str]¶
-
extended_metric_names: List[str]¶
-
tensorboard_output_names: List[str]¶
-
__init__(extended_loss_names: List[str], extended_metric_names: List[str], tensorboard_output_names: List[str]) → 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¶
-
-
class
tfaip.scenario.scenariobase.ScenarioBaseMeta(*args, **kwargs)¶ Bases:
tfaip.util.generic_meta.CollectGenericTypes
-
class
tfaip.scenario.scenariobase.ScenarioBase(params: TScenarioParams)¶ Bases:
Generic[tfaip.scenario.scenariobase.TData,tfaip.scenario.scenariobase.TModel,tfaip.scenario.scenariobase.TScenarioParams,tfaip.scenario.scenariobase.TTrainerPipelineParams],abc.ABCThe scenario base handles the setup of the scenario including training and model exporting.
Several abstract methods must be overridden that define how to create the Model, and Data for this scenario. If your scenario requires different default params in contrast to the values defined in the respective dataclasses, override default_params(). Furthermore it is possible to change the Trainer or LAV class which is usually not required!
A typical Scenario only requires to implement get_meta() and get_param_cls() To set scenario specific (hidden) parameters use the subclasses __init__ or override create_model() or create_data(). Note that create_model() will be called after create_data()!
The metaclass of the Scenario (ScenarioBaseMeta) will track the types of the Generics which is then use to create instances of the actual types automatically (see, e.g., model_cls(), data_cls(), …).
-
classmethod
default_params() → TScenarioParams¶ Override to change the default parameters for this scenario.
Returns: ScenarioParams with adapted defaults of the respective Scenario
-
classmethod
default_trainer_params() → TrainerParams[TScenarioParams, TTrainerPipelineParams]¶ Override to change the default trainer params for this scenario.
Use this to override hyperparameters such as the learning rate.
Returns: TrainerParams with adapted defaults
-
classmethod
params_from_dict(d: dict) → TScenarioParams¶ Instantiate ScenarioParams from a dict. Not that since all dataclasses are a pai_dataclass the actual ScenarioParams will be read from the __cls__ arg within the dict.
See also
ScenarioBaseParams.from_dict
- Parameters
d – trainer or scenario params dict
Returns: ScenarioParams of the respective Scenario
-
classmethod
params_from_path(path: str) → TScenarioParams¶ Read params from a file path and construct the stored ScenarioParams
See also
params_from_dict, read_params_dict_from_path
- Parameters
path – path to the file or to the directory containin a trainer_params.json or scenario_params.json
- Returns
Parsed ScenarioParams
-
classmethod
read_params_dict_from_path(path: str) → dict¶ Read a params dict from a path (or dictionary)
See also
params_from_path
- Parameters
path – path to the file or to the directory containin a trainer_params.json or scenario_params.json
- Returns
dict of ScenarioParams
-
classmethod
from_path(path: str) → Tuple[Type[tfaip.scenario.scenariobase.ScenarioBase], TScenarioParams]¶ Instantiate a scenario from a path.
- Parameters
path – Path to the scenario_params.json or dictionary containing them
- Returns
Tuple of the actual Scenario class and the parsed Scenario params
-
classmethod
from_dict(d: dict) → Tuple[Type[tfaip.scenario.scenariobase.ScenarioBase], TScenarioParams]¶ Instantiate a scenario from a dict.
- Parameters
d – Dict of the scenario_params.json or dictionary containing them
- Returns
Tuple of the actual Scenario class and the parsed Scenario params
-
classmethod
data_cls() → Type[TData]¶
-
classmethod
model_cls() → Type[TModel]¶
-
classmethod
trainer_cls() → Type[Trainer[TrainerParams]]¶
-
classmethod
lav_cls() → Type[LAV]¶
-
classmethod
multi_lav_cls() → Type[tfaip.lav.multilav.MultiLAV]¶
-
classmethod
predictor_cls() → Type[Predictor]¶
-
classmethod
evaluator_cls() → Type[tfaip.evaluator.evaluator.EvaluatorBase]¶
-
classmethod
multi_predictor_cls() → Type[MultiModelPredictor]¶
-
classmethod
create_trainer(trainer_params: TrainerParams, restore=False) → Trainer¶
-
classmethod
create_lav(lav_params: LAVParams, scenario_params: TScenarioParams) → LAV¶
-
classmethod
create_multi_lav(lav_params: LAVParams, scenario_params: TScenarioParams, predictor_params: Optional[PredictorParams] = None)¶
-
classmethod
create_predictor(model: str, params: PredictorParams) → Predictor¶
-
classmethod
create_multi_predictor(paths: List[str], params: PredictorParams) → MultiModelPredictor¶
-
classmethod
create_evaluator(params: tfaip.evaluator.params.EvaluatorParams) → tfaip.evaluator.evaluator.EvaluatorBase¶
-
classmethod
params_cls() → Type[TScenarioParams]¶
-
classmethod
trainer_pipeline_params_cls() → Type[TTrainerPipelineParams]¶
-
classmethod
predict_generator_params_cls() → Type[tfaip.data.databaseparams.DataGeneratorParams]¶
-
static
net_config_cls() → Type[tfaip.scenario.scenariobaseparams.NetConfigParamsBase]¶
-
__init__(params: TScenarioParams)¶ Initialize self. See help(type(self)) for accurate signature.
-
property
params¶
-
property
keras_train_model¶
-
property
keras_predict_model¶
-
setup()¶
-
create_data() → TData¶
-
create_model() → TModel¶
-
print_params() → NoReturn¶ Print the ScenarioParams to logger.info
-
best_logging_settings() → Tuple[str, str]¶ Returns: See ModelBase.best_logging_settings()
-
export(path: str, trainer_params: Optional[TrainerParams] = None, export_resources: bool = True) → NoReturn¶ Export the prediction model to a given path
- Parameters
path – Directory where to export the model to
trainer_params – if given export the full trainer params instead of the scenario params
export_resources – Set to true (default) to also export the resources
-
setup_training(optimizer, skip_model_load_test=False, run_eagerly=False, no_train_scope: Optional[str] = None) → NoReturn¶ Set training by constructing the training and prediction keras Models.
- Parameters
optimizer – The optimizer to use for training
skip_model_load_test – Skip the test checking if the prediction model can be stored and loaded
run_eagerly – Run the model in eager mode
no_train_scope – Regex to match layers to exclude from training
-
fit(epochs, callbacks, steps_per_epoch, initial_epoch=0, **kwargs) → NoReturn¶ Train the scenario.
This function will create the training and optionally validation pipelines, and call fit of the keras model.
- Parameters
epochs – The number of epochs to train
callbacks – Callbacks of training
steps_per_epoch – Number of steps per epoch
initial_epoch – Initial epoch (use if resuming the training)
**kwargs – Additional args passed to keras.Model.fit
-
net_config() → tfaip.scenario.scenariobaseparams.NetConfigParamsBase¶ Create the NetConfigParams for this scenario.
Usually a custom Scenario should override net_config_cls() and _fill_net_config() instead of overwriting this function.
- Returns
NetConfigParamsBase
-
classmethod
-
tfaip.scenario.scenariobase.import_scenario(module_name: str) → Type[tfaip.scenario.scenariobase.ScenarioBase]¶ module_name can either be: - module and class name separated by a ‘:’, e.g.: tfaip.scenarios.tutorial.min.scenario:TutorialScenario - module containing one class inheriting ScenarioBase, e.g.: tfaip.scenarios.tutorial.min.scenario - module containing containing a scenario module that contains one class inheriting ScenarioBase, e.g.: tfaip.scenarios.tutorial.min
ScenarioBaseParams¶
Definition of the ScenarioBaseParams
-
class
tfaip.scenario.scenariobaseparams.ScenarioBaseParamsMeta(*args, **kwargs)¶ Bases:
tfaip.util.generic_meta.ReplaceDefaultDataClassFieldsMeta
-
class
tfaip.scenario.scenariobaseparams.ScenarioBaseParams(debug_graph_construction: bool = False, debug_graph_n_examples: int = 1, print_eval_limit: int = 10, tensorboard_logger_history_size: int = 5, export_serve: bool = True, model: TModelParams = <factory>, data: TDataParams = <factory>, evaluator: tfaip.evaluator.params.EvaluatorParams = <factory>, export_net_config: bool = True, net_config_filename: str = 'net_config.json', default_serve_dir: str = 'serve', additional_serve_dir: str = 'additional', trainer_params_filename: str = 'trainer_params.json', scenario_params_filename: str = 'scenario_params.json', scenario_base_path: Optional[str] = None, scenario_id: Optional[str] = None, id: Optional[str] = None, tfaip_commit_hash: str = <factory>, tfaip_version: str = '1.1.1')¶ Bases:
Generic[tfaip.scenario.scenariobaseparams.TDataParams,tfaip.scenario.scenariobaseparams.TModelParams],abc.ABCDefine the global params of a scenario contains model_params and data_params of the model
NOTE: add @pai_dataclass and @dataclass annotations to inherited class
-
debug_graph_construction: bool = False¶
-
debug_graph_n_examples: int = 1¶
-
print_eval_limit: int = 10¶
-
tensorboard_logger_history_size: int = 5¶
-
export_serve: bool = True¶
-
model: TModelParams¶
-
data: TDataParams¶
-
evaluator: tfaip.evaluator.params.EvaluatorParams¶
-
export_net_config: bool = True¶
-
net_config_filename: str = 'net_config.json'¶
-
default_serve_dir: str = 'serve'¶
-
additional_serve_dir: str = 'additional'¶
-
trainer_params_filename: str = 'trainer_params.json'¶
-
scenario_params_filename: str = 'scenario_params.json'¶
-
scenario_base_path: Optional[str] = None¶
-
scenario_id: Optional[str] = None¶
-
id: Optional[str] = None¶
-
tfaip_commit_hash: str¶
-
tfaip_version: str = '1.1.1'¶
-
cls() → Type[ScenarioBase]¶
-
create() → ScenarioBase¶
-
__init__(debug_graph_construction: bool = False, debug_graph_n_examples: int = 1, print_eval_limit: int = 10, tensorboard_logger_history_size: int = 5, export_serve: bool = True, model: TModelParams = <factory>, data: TDataParams = <factory>, evaluator: tfaip.evaluator.params.EvaluatorParams = <factory>, export_net_config: bool = True, net_config_filename: str = 'net_config.json', default_serve_dir: str = 'serve', additional_serve_dir: str = 'additional', trainer_params_filename: str = 'trainer_params.json', scenario_params_filename: str = 'scenario_params.json', scenario_base_path: Optional[str] = None, scenario_id: Optional[str] = None, id: Optional[str] = None, tfaip_commit_hash: str = <factory>, tfaip_version: str = '1.1.1') → 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¶
-
-
class
tfaip.scenario.scenariobaseparams.NetConfigNodeSpec(shape: List[str], dtype: str, node_serve: str)¶ Bases:
object-
shape: List[str]¶
-
dtype: str¶
-
node_serve: str¶
-
__init__(shape: List[str], dtype: str, node_serve: str) → None¶ Initialize self. See help(type(self)) for accurate signature.
-
-
class
tfaip.scenario.scenariobaseparams.NetConfigParamsBase(id_model: str, id_serve: str, in_nodes: Dict[str, tfaip.scenario.scenariobaseparams.NetConfigNodeSpec], out_nodes: Dict[str, tfaip.scenario.scenariobaseparams.NetConfigNodeSpec], tf_version: str, tfaip_commit_hash: str = <factory>, tfaip_version: str = '1.1.1')¶ Bases:
object-
id_model: str¶
-
id_serve: str¶
-
in_nodes: Dict[str, tfaip.scenario.scenariobaseparams.NetConfigNodeSpec]¶
-
out_nodes: Dict[str, tfaip.scenario.scenariobaseparams.NetConfigNodeSpec]¶
-
tf_version: str¶
-
tfaip_commit_hash: str¶
-
tfaip_version: str = '1.1.1'¶
-
__init__(id_model: str, id_serve: str, in_nodes: Dict[str, tfaip.scenario.scenariobaseparams.NetConfigNodeSpec], out_nodes: Dict[str, tfaip.scenario.scenariobaseparams.NetConfigNodeSpec], tf_version: str, tfaip_commit_hash: str = <factory>, tfaip_version: str = '1.1.1') → 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¶
-
.listfile¶
ListFileScenario¶
Definition of a ListFileScenario
-
class
tfaip.scenario.listfile.listfilescenario.ListFileScenario(params: TScenarioParams)¶ Bases:
tfaip.scenario.scenariobase.ScenarioBase[tfaip.scenario.scenariobase.TData,tfaip.scenario.scenariobase.TModel,tfaip.scenario.scenariobase.TScenarioParams,tfaip.scenario.listfile.params.ListFileTrainerPipelineParams]Base-Class for a Scenario working with list files. A list file is a simple text file where each line is the path to a sample. The ListFileScenario uses ListFileTrainerPipelineParams which create a DataGenerator that will yield each line as a Sample’s input and target. This must then be processed by DataProcessors.
-
classmethod
lav_cls()¶
-
classmethod
Parameters¶
Definition of the parameters for a ListFileScenario
-
class
tfaip.scenario.listfile.params.ListsFileGeneratorParams(lists: Optional[List[str]] = <factory>, list_ratios: Optional[List[float]] = None)¶ Bases:
tfaip.data.databaseparams.DataGeneratorParamsParameters for the ListsFileDataGenerator
-
static
cls()¶
-
lists: Optional[List[str]]¶
-
list_ratios: Optional[List[float]] = None¶
-
__init__(lists: Optional[List[str]] = <factory>, list_ratios: Optional[List[float]] = None) → 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¶
-
static
-
class
tfaip.scenario.listfile.params.ListFileTrainerPipelineParams(setup: tfaip.trainer.params.TrainerPipelines = <factory>, train: TDataGeneratorTrain = <factory>, val: TDataGeneratorVal = <factory>)¶ Bases:
tfaip.trainer.params.TrainerPipelineParams[tfaip.scenario.listfile.params.ListsFileGeneratorParams,tfaip.scenario.listfile.params.ListsFileGeneratorParams]Implemented TrainerPipelineParams that replaces the defaults of train and val to ListFileGeneratorParams.
-
__init__(setup: tfaip.trainer.params.TrainerPipelines = <factory>, train: TDataGeneratorTrain = <factory>, val: TDataGeneratorVal = <factory>) → 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¶
-
train: TDataGeneratorTrain¶
-
val: TDataGeneratorVal¶
-
Data Generator¶
Definition of the ListFileDataGenerator
-
class
tfaip.scenario.listfile.datagenerator.ListsFileDataGenerator(mode: tfaip.data.pipeline.definitions.PipelineMode, params: T)¶ Bases:
tfaip.data.pipeline.datagenerator.DataGenerator[tfaip.scenario.listfile.params.ListsFileGeneratorParams]DataGenerator of ListFiles, a text file where each line is a sample.
On training, random samples (file names) will be chosen respecting the list rations (if multiple lists are passes), see ListMixDefinition.
On any other mode, the samples will be yielded in Order.
-
generate() → Iterable[tfaip.data.pipeline.definitions.Sample]¶
-