Skip to content

Python Usage

SleepKit python package allows for more fine-grained control and customization. You can use the package to train, evaluate, and deploy models for both built-in taks and custom tasks. In addition, custom datasets and model architectures can be created and registered with corresponding factories.

Overview

The main components of SleepKit include the following:

Tasks

A Task inherits from the sk.Task class and provides implementations for each of the main modes: download, feature, train, evaluate, export, and demo. Each mode is provided with a set of parameters defined by sk.TaskParams. Additional task-specific parameters can be extended to the TaskParams class. These tasks are then registered and accessed via the sk.TaskFactory using a unique task name as the key and the custom Task class as the value.

1
2
3
import sleepkit as sk

task = sk.TaskFactory.get('stage')

Datasets

A dataset inherits from the sk.Dataset class and provides implementations for downloading, preparing, and loading the dataset. Each dataset is provided with a set of custom parameters for initialization. The datasets are registered and accessed via the DatasetFactory using a unique dataset name as the key and the Dataset class as the value.

1
2
3
import sleepkit as sk

ds = sk.DatasetFactory.get('ecg-synthetic')(num_pts=100)

Features

Since each task will require specific transformations of the data, a feature store is used to generate features from the dataset. The feature store provides a set of feature sets that can be used by the task. Each feature set is provided with a set of custom parameters for initialization. The feature sets are registered and accessed via the sk.FeatureFactory using a unique feature set name as the key and the Feature class as the value.

Models

Lastly, SleepKit leverages neuralspot-edge's customizable model architectures. To enable creating custom network topologies from configuration files, SleepKit provides a sk.ModelFactory that allows you to create models by specifying the model key and the model parameters. Each item in the factory is a callable that takes a keras.Input, model parameters, and number of classes as arguments and returns a keras.Model.

import keras
import sleepkit as sk

inputs = keras.Input((256, 1), dtype="float32")
num_classes = 4
model_params = dict(...)

model = sk.ModelFactory.get('tcn')(
    inputs=inputs,
    params=model_params,
    num_classes=num_classes
)

Usage

Running a built-in task w/ existing datasets

  1. Create a task configuration file defining the model, datasets, class labels, mode parameters, and so on. Have a look at the sk.TaskParams for more details on the available parameters.

  2. Leverage sk.TaskFactory to get the desired built-in task.

  3. Run the task's main modes: download, feature, train, evaluate, export, and/or demo.

import sleepkit as hk

params = sk.TaskParams(...)  # 

task = sk.TaskFactory.get("stage")

task.download(params)  # Download dataset(s)

task.feature(params)  # Generate features

task.train(params)  # Train the model

task.evaluate(params)  # Evaluate the model

task.export(params)  # Export to TFLite

Running a custom task w/ custom datasets

To create a custom task, check out the Bring-Your-Own-Task Guide.

To create a custom dataset, check out the Bring-Your-Own-Dataset Guide.