aisingapore / PeekingDuck

A modular framework built to simplify Computer Vision inference workloads.
Apache License 2.0
163 stars 39 forks source link

Feat: callbacks #712

Closed liyier90 closed 1 year ago

liyier90 commented 2 years ago

Enables users to attach callback functions to nodes to be run at certain points/events in the pipeline.

Implemented events:

Supported callback types:

UI: Users will specify callbacks for the node under the callbacks config key. Each callback is specified as:

callback definition = module part, "::", callback part
module part         = { subdir name, "." }, script name
callback part       = function
                     | class name, "::", method
                     | instance name, "::", instance method

script name     = ? name of callback Python script ?
subdir name     = ? name of individual subdirectory folders relative to `callbacks` folder ?
class name      = ? name of class which contains the callback methods ?
instance name   = ? name of instantiated object which contains the callback method ?
function        = ? name of callback function ?
method          = ? name of class/static methods ?
instance method = ? name of instance method ?

Incorrect callback definitions are skipped with a warning log, program is not halted.

Implementation detail: In _edit_config() of both AbstractNode and DeclarativeLoader, avoid going deeper into nested dictionary if the key is callbacks. This allows us to define the callbacks config in PeekingDuck as

callbacks: {}

and in pipeline config as

- node.name:
    callbacks:
      run_begin: ["callback::definition"]

This allows us to implement more callback events in the future without have to add the event key to all the config files in PeekingDuck.

Sample usage: Extract callbacks.zip into your project directory. Ensure that the callbacks directory is at the same level as dev_callback.yml. You should have the following directory layout:

project_dir
+-callbacks
| +-cb_dir
| +-my_callbacks.py
| \-sql_callback.py
+-dev_callback.yml
\-...

Run dev_callback.yml and you should see logging outputs such as

Additional changes:

ongtw commented 2 years ago

Can you write up a tutorial Using Callbacks under the Peaking Duck tutorial section on using callbacks and giving some sample codes/pipelines as illustrations?

liyier90 commented 2 years ago

Added a "Using Callbacks" section to Peaking Duck tutorial. Includes: