Task-Level Model Demo
Introduction
Section titled “Introduction”Each task in sleepKIT has a corresponding demo mode that allows you to run a task-level demonstration using the specified backend inference engine (e.g. PC or EVB). This is useful to showcase the model’s performance in real-time and to verify its accuracy in a real-world scenario. Similar to other modes, the demo can be invoked either via CLI or within sleepkit python package. At a high level, the demo mode performs the following actions based on the provided configuration parameters:
- Load the configuration data (e.g.
configuration.json) - Load the desired features (e.g.
FS-W-A-5) - Load the trained model (e.g.
model.keras) - Initialize inference engine backend (e.g.
pcorevb) - Generate input data (e.g.
x, y) - Perform inference on backend (e.g.
model.predict) - Generate report (e.g.
report.html)
Inference Backends
Section titled “Inference Backends”sleepKIT includes two built-in backend inference engines: PC and EVB. Additional backends can be easily added to the sleepKIT framework by creating a new backend class and registering it to the backend factory.
PC Backend
Section titled “PC Backend”The PC backend is used to run the task-level demo on the local machine. This is useful for quick testing and debugging of the model.
- Create / modify configuration file (e.g.
stage-class-4.json) - Ensure “pc” is selected as the backend in configuration file.
- Run demo
sleepkit --mode demo --task stage --config ./configs/stage-class-4.json - HTML report will be saved to
${job_dir}/report.html
EVB Backend
Section titled “EVB Backend”The EVB backend is used to run the task-level demo on an Ambiq EVB. This is useful to showcase the model’s performance in real-time and to verify its accuracy in a real-world scenario.
- Create / modify configuration file (e.g.
stage-class-2.json) - Ensure “evb” is selected as the backend in configuration file.
- Plug EVB into PC via two USB-C cables.
- Build and flash firmware to EVB
cd evb && make && make deploy - Run demo
sleepkit --mode demo --task stage --config ./configs/stage-class-4.json - HTML report will be saved to
${job_dir}/report.html
Bring-Your-Own-Backend
Section titled “Bring-Your-Own-Backend”Similar to datasets, tasks, and models, the demo mode can be customized to use your own backend inference engine. sleepKIT includes a backend factory (BackendFactory) that is used to create and run the backend engine.
How it Works
Section titled “How it Works”-
Create a Backend: Define a new backend class that inherits from the
sk.InferenceBackendbase class and implements the required methods. This skeleton must be implemented before use.Python example import sleepkit as skimport numpy.typing as nptclass CustomBackend(sk.InferenceBackend):def __init__(self, params: sk.TaskParams) -> None:self.params = paramsdef open(self):raise NotImplementedError("Implement the backend operation")def close(self):raise NotImplementedError("Implement the backend operation")def set_inputs(self, inputs: npt.NDArray):raise NotImplementedError("Implement the backend operation")def perform_inference(self):raise NotImplementedError("Implement the backend operation")def get_outputs(self) -> npt.NDArray:raise NotImplementedError("Implement the backend operation") -
Register the Backend: Register the new backend with the
sk.BackendFactoryby calling theregistermethod. This method takes the backend name and the backend class as arguments.Python example import sleepkit as sksk.BackendFactory.register("custom", CustomBackend) -
Use the Backend: The new backend can now be used by setting the
backendflag in the demo configuration settings.Python example import sleepkit as skfrom pathlib import Pathparams = sk.TaskParams.model_validate_json(Path("configuration.json").read_text())params.backend = "custom"task = sk.TaskFactory.get("detect")task.demo(params)OR by creating the backend directly:
Python example import sleepkit as skfrom pathlib import Pathparams = sk.TaskParams.model_validate_json(Path("configuration.json").read_text())params.backend = "custom"backend = sk.BackendFactory.get("custom")(params)
The following is an example of a task-level demo report for the sleep staging task.
sleepkit -m demo -t stage -c ./configuration.jsonfrom pathlib import Pathimport sleepkit as sk
task = sk.TaskFactory.get("stage")params = sk.TaskParams.model_validate_json(Path("configuration.json").read_text())task.demo(params)Arguments
Section titled “Arguments”Please refer to TaskParams for the list of arguments that can be used with the demo command.