API

class towhee.runtime.factory._OperatorParser[source]

Bases: object

Class to loading operator with _OperatorWrapper. An operator is usually referred to with its full name: namespace/name.

Examples

>>> from towhee import ops
>>> op = ops.towhee.image_decode()
>>> img = op('./towhee_logo.png')

We can also specify the version of the operator on the hub via the revision method:

>>> op = ops.towhee.image_decode()

And the latest method is used to update the current version of the operator to the latest:

>>> op = ops.towhee.image_decode().latest()
towhee.register(name: Optional[str] = None, input_schema=None, output_schema=None, flag=None)

Register a class, function, or callable as a towhee operator.

Parameters:
  • name (str, optional) – operator name, will use the class/function name if None.

  • input_schema

  • output_schema

  • operators. (flag for legacy) –

Returns:

[description]

Return type:

[type]

class towhee.operator.Operator[source]

Bases: ABC

Operator base class, implements __init__ and __call__,

Examples

class AddOperator(Operator):
def __init__(self, factor: int):

self._factor = factor

def __call__(self, num)

return self._factor + num

flush()[source]

This method is triggered by RuntimePipeline.flush().

After the pipeline runs, some operators may need to trigger some additional operations, such as the to_faiss operator needing to trigger data storage.

class towhee.operator.NNOperator(framework: str = 'pytorch')[source]

Bases: Operator

Neural Network related operators that involve machine learning frameworks.

Parameters:

framework (str) – The framework to apply.

save_model()[source]

Save model to local.

supported_model_names()[source]

Return a list of supported model names.

train(training_config=None, train_dataset=None, eval_dataset=None, resume_checkpoint_path=None, **kwargs)[source]

Start to train an operator.

Parameters:
  • training_config (TrainingConfig) – The config of this trainer.

  • train_dataset (Union[Dataset, TowheeDataSet]) – Training dataset.

  • eval_dataset (Union[Dataset, TowheeDataSet]) – Evaluate dataset.

  • resume_checkpoint_path (str) – If resuming training, pass into the path.

  • **kwargs (Any) – Keyword Args.

setup_trainer(training_config=None, train_dataset=None, eval_dataset=None, train_dataloader=None, eval_dataloader=None, model_card=None)[source]

Set up the trainer instance in operator before training and set trainer parameters. :param training_config: The config of this trainer. :type training_config: TrainingConfig :param train_dataset: Training dataset. :type train_dataset: Union[Dataset, TowheeDataSet] :param eval_dataset: Evaluate dataset. :type eval_dataset: Union[Dataset, TowheeDataSet] :param train_dataloader: If specified, Trainer will use it to load training data.

Otherwise, Trainer will build dataloader from train_dataset.

Parameters:
  • eval_dataloader (Union[DataLoader, Iterable]) – If specified, Trainer will use it to load evaluate data. Otherwise, Trainer will build dataloader from train_dataset.

  • model_card (ModelCard) – Model card contains the training informations.

Returns:

class towhee.operator.PyOperator[source]

Bases: Operator

Python function operator, no machine learning frameworks involved.