Model observability sample

This sample demonstrates how to send runtime statistics of a machine learning model to the cloud using Memfault.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA number

Board name

Board target

nRF54L15 TAG

PCA20072

nrf54l15tag

nrf54l15tag/nrf54l15/cpuapp

The sample uses only the blue channel of the on-board RGB LED.

Overview

The sample does not run any actual model; it feeds predefined class-probability vectors to the nRF Edge AI Observability Library in the same format as model inference output. The sample validates the end-to-end observability path from the device through Memfault to the decoding script, using the predefined probability vectors as input. For a conceptual introduction to model observability, see Model lifecycle and observability. For integration details, refer to the nRF Edge AI Observability Library documentation.

The probability vectors cover the following activity classes, in the following order:

  1. Walking

  2. Sitting

  3. Running

  4. Laying

The sample feeds these vectors as three fixed sequences. In the source code and in this documentation page, each sequence is called a scenario.

Predefined class-probability sequences

The sequences are defined in src/scenarios.c. For each vector, the activity with the highest probability is the predicted class. The following table summarizes predicted activities for each scenario:

Predicted activities per scenario

Scenario

Vectors

Walking

Sitting

Running

Laying

Order of the activities

1

20

7 (35%)

3 (15%)

1 (5%)

9 (45%)

Walking → running → walking → sitting → laying

2

20

3 (15%)

3 (15%)

1 (5%)

13 (65%)

Laying → sitting → laying → sitting → walking → running

3

20

5 (25%)

7 (35%)

3 (15%)

5 (25%)

Sitting → walking → running → walking → sitting → laying

The probabilities of the predicted class vary between the vectors, and the vectors at the activity changes have lower probabilities, so the certainty metrics are not constant.

The sample feeds one vector every 500 milliseconds, set by the CONFIG_APP_FEED_INTERVAL_MS Kconfig option. After feeding a scenario, the sample waits until the automatic collection encodes it into a recording, then starts the next scenario. After the third scenario, the sample starts again from the first one.

Metrics collection and Memfault upload

The sample collects the following output metrics from each probability vector:

Metric

Description

Class predictions distribution

How often each class is predicted and with what probability, including how long the same class is predicted in a row.

Transition matrix

How often the predicted class changes from one class to another.

Model certainty descriptor

How confident the model is in its predictions.

The observability code is located in the src/model_obsv.c file. It creates the observability context, registers the metrics and binds the Memfault transport. The src/main.c file defines the model metadata, with a fixed model ID of 777, and passes it to the observability code.

The metrics are collected automatically at an interval set by the CONFIG_NRF_EDGEAI_OBSV_MEMFAULT_AUTO_COLLECT_INTERVAL_SEC Kconfig option. After each collection, the metrics are encoded and staged in RAM as a Memfault Custom Data Recording (CDR). A gateway, for example a phone, reads the staged recording over the Memfault Diagnostic Service (MDS) and uploads it to Memfault. The metrics are reset after each collection, so every recording describes one collection period.

Each recording contains exactly one scenario:

  • The sample starts the next scenario only after the previous one is staged as a recording.

  • A new collection is skipped while the previous recording is still waiting to be read by the gateway. The next scenario then stays in the metrics until the gateway reads the previous recording.

  • A whole scenario is fed within one collection interval, which is checked at build time.

User interface

This section describes the user interface available on development kits in this sample.

LEDs

The on-board RGB LED (status LED) blinks blue and shows the Bluetooth® Low Energy connection state. The firmware drives the blue channel through LED0.

  • Fast blinking (3 Hz) - The device is advertising and waits for a Bluetooth LE connection.

  • Slow blinking (1 Hz) - A peer is connected.

Configuration

See Configuring and building for information about how to permanently or temporarily change the configuration.

Configuration options

The following sample-specific Kconfig options are used in this sample:

CONFIG_APP_FEED_INTERVAL_MS

(int) Interval between probability vectors (ms)

Delay after each predefined probability vector fed to the observability metrics, standing in for the inference period of a real model. A whole scenario must be fed within one CONFIG_NRF_EDGEAI_OBSV_MEMFAULT_AUTO_COLLECT_INTERVAL_SEC interval, which is checked at build time.

Additional configuration

For the observability library, Memfault transport, built-in metrics, and buffer sizing, see nRF Edge AI Observability Library. For the Bluetooth® LE transport (MDS) options, see Bluetooth: Peripheral Memfault Diagnostic Service (MDS).

The prj.conf file of the sample enables the library, the Memfault transport with automatic collection, and MDS. The following settings are specific to the sample:

  • CONFIG_NRF_EDGEAI_OBSV_MAX_CLASSES=4 - The number of the activity classes. The sample returns an error at start if the model has more classes.

  • CONFIG_NRF_EDGEAI_OBSV_MEMFAULT_AUTO_COLLECT_INTERVAL_SEC=120 - A short collection interval, so that the three scenarios are sent within a few minutes. The sample checks at build time that a whole scenario is fed within this interval. Memfault accepts a limited number of recordings per device per day, so enable server debug mode for the device in the Memfault dashboard.

  • CONFIG_MEMFAULT_PROJECT_KEY - Your Memfault project key. The key in the prj.conf file is a placeholder, and your device data is not available in your own Memfault project until you replace it.

  • CONFIG_MEMFAULT_NCS_DEVICE_ID - A fixed device ID, so that you can find the device in the Memfault dashboard.

Building and running

This sample can be found under samples/model_observability in the Edge AI Add-on folder structure.

To build the sample, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Testing

After programming the sample to your development kit, complete the following steps to test it:

  1. Connect the kit to the computer using a USB cable. The kit is assigned a serial port. Serial ports are referred to as COM ports on Windows, /dev/ttyACM devices on Linux, and /dev/tty devices on macOS. To list Nordic Semiconductor devices connected to your computer together with their serial ports, open a terminal and run the nrfutil device list command. Alternatively, check your operating system’s device manager or its equivalent.

  2. Connect to the kit with a terminal emulator (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps. Connect to the serial device printing console output.

  3. Observe that the RGB LED blinks blue at 3 Hz and that the terminal displays messages similar to the following:

    Observability initialized: model id 777, 4 classes
    Scenario 1: feeding 20 probability vectors
    Scenario 1 fed: walking 7, sitting 3, running 1, laying 9
    Scenario 1: waiting for auto collect
    
  4. Connect a gateway to your device. Ready-to-use gateways are nRF Connect Device Manager or Memfault WebBluetooth Client. Check the Testing section of Bluetooth: Peripheral Memfault Diagnostic Service (MDS) for details. After the connection, the RGB LED blinks at 1 Hz.

  5. Wait until the terminal shows that the three scenarios are staged:

    Scenario 1 staged as CDR
    Scenario 2: feeding 20 probability vectors
    Scenario 2 fed: walking 3, sitting 3, running 1, laying 13
    Scenario 2: waiting for auto collect
    Scenario 2 staged as CDR
    Scenario 3: feeding 20 probability vectors
    Scenario 3 fed: walking 5, sitting 7, running 3, laying 5
    Scenario 3: waiting for auto collect
    Scenario 3 staged as CDR
    
  6. In a web browser, open Memfault.

  7. In the left-hand menu, go to CDR Payloads.

  8. Select a CDR payload from your test device with “edgeai_observability” as reason. Filter the list of CDR payloads by device name and time.

  9. Download the payload for metrics analysis.

  10. Decode it on a host PC using the scripts/decode_edgeai_obsv_cdr/decode_edgeai_obsv_cdr.py script in binary mode:

    ./scripts/decode_edgeai_obsv_cdr/decode_edgeai_obsv_cdr.py --binary --file <downloaded>.bin
    

    See Decoding CDR payloads for installation and other input modes. The payload format is defined in the lib/nrf_edgeai_obsv/obsv.cddl file.

  11. Compare the decoded metrics with the scenario. For example, num_inferences is 20, the class predictions distribution matches the predicted activities in the scenarios table above, and the transition matrix shows the order of the activities.

Dependencies

This sample uses the following Edge AI Add-on library:

This sample uses the following nRF Connect SDK service:

This sample uses the following Zephyr library: