Regression sample

The following sample demonstrates running a generated regression model to predict a continuous air quality value based on gas sensor and environmental data.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

nRF54LM20 DK

PCA10184

nrf54lm20dk

nrf54lm20dk/nrf54lm20b/cpuapp nrf54lm20dk/nrf54lm20a/cpuapp

nRF54L15 DK

PCA10156

nrf54l15dk

nrf54l15dk/nrf54l15/cpuapp

nRF54L15 DK (emulating nRF54L10)

PCA10156

nrf54l15dk

nrf54l15dk/nrf54l10/cpuapp

nRF54L15 DK (emulating nRF54L05)

PCA10156

nrf54l15dk

nrf54l15dk/nrf54l05/cpuapp

nRF54H20 DK

PCA10175

nrf54h20dk

nrf54h20dk/nrf54h20/cpuapp

nRF5340 DK

PCA10095

nrf5340dk

nrf5340dk/nrf5340/cpuapp

nRF52 DK

PCA10040

nrf52dk

nrf52dk/nrf52832

nRF52840 DK

PCA10056

nrf52840dk

nrf52840dk/nrf52840

Overview

The sample validates model predictions over a set of 29 test cases, printing the predicted value, expected value, and absolute error for each sample. The model takes 9 input values for each prediction. These inputs are:

  • Carbon monoxide (CO) concentration

  • 5 readings from different PT08S sensors

  • Temperature

  • Relative humidity (RH)

  • Absolute humidity (AH)

The model makes a prediction every time it receives a single new set of input data (that is, after each individual sample). It does not need multiple samples collected over time to make a prediction. Each prediction from the model is a single floating-point number representing the estimated air quality value.

Configuration

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

The project configuration for this sample is provided in samples/nrf_edgeai/regression/prj.conf.

Model backend (Neuton and Axon)

The sample can use either of two model backends, selected in Kconfig:

  • Neuton (CPU) — Runs on the application core. It is supported on all nRF Edge AI boards.

  • Axon (NPU) — Runs on the Axon neural processing unit. It is available only on SoCs with Axon NPU.

To select the model backend, set the CONFIG_NRF_EDGEAI_REGRESSION_MODEL_NEUTON or CONFIG_NRF_EDGEAI_REGRESSION_MODEL_AXON Kconfig option in your prj.conf file. See board-specific configuration and overlays in the samples/nrf_edgeai/regression/boards/ folder. When using the Axon backend, the generated model saves its buffer requirements in the prj_example.conf file as the CONFIG_NRF_AXON_INTERLAYER_BUFFER_SIZE and CONFIG_NRF_AXON_PSUM_BUFFER_SIZE Kconfig options. You must manually include these values in your prj.conf file before building.

Configuration options

In your prj.conf file, the following settings are applied to ensure the sample builds and runs correctly:

CONFIG_NRF_EDGEAI=y
CONFIG_FPU=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_RTT_CONSOLE=n
CONFIG_PICOLIBC_IO_FLOAT=y

See the following descriptions of the enabled Kconfig options:

  • The CONFIG_NRF_EDGEAI Kconfig option enables the nRF Edge AI library and its components in the build system.

  • The CONFIG_PICOLIBC_IO_FLOAT Kconfig option enables printing of floating-point values (required for model output/logging).

  • The CONFIG_FPU Kconfig option enables the hardware floating-point unit (FPU), if present, to accelerate inference when using floating-point models.

Building and running

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

If the model sources are provided as a generated C library, ensure they are placed under the application source tree (for example, samples/nrf_edgeai/<sample>/src/nrf_edgeai_generated) so CMake discovers and builds them together with the application.

Testing

The application runs 29 validation test cases automatically upon startup. For each case, the sample prints a line similar to the following:

Air quality - Predicted: 12.345678, Expected: 14.300000, absolute error 1.954322
  1. Observe the results printed:

    • Predicted value corresponds to the air quality value predicted by the model for the given input.

    • Expected value corresponds to the reference value the model should ideally predict for this input.

    • Absolute error corresponds to the difference between the predicted and expected values.

  2. Confirm that a total of 29 lines are printed, each corresponding to one validation sample.

  3. Inspect the absolute error value for each line to verify that the model’s predictions are close to the expected values. Acceptable error margins depend on your use case or specified requirements in your project.

Manual inference using the API

You can also run inference manually in your own application code.

The following example demonstrates how to initialize the model, feed your own test data, and print out predicted values:

#include <nrf_edgeai/nrf_edgeai.h>
#include <nrf_edgeai_generated/nrf_edgeai_user_model.h>
#include <assert.h>
#include <stdio.h>
// In this example, our raw features is a window of N elements with 3 accelerometer axis values
// The number of raw features and their order should be the same as in the training dataset file
int16_t raw_features[] =
{
    Accelerometer_X0,
    Accelerometer_Y0,
    Accelerometer_Z0,
    /* ... */
    Accelerometer_Xn,
    Accelerometer_Yn,
    Accelerometer_Zn,
};
// Pointer to user model
static nrf_edgeai_t* p_edgeai = NULL;

void user_init_edegeai_model(void)
{
    // Get user model pointer
    p_edgeai = nrf_edgeai_user_model();
    // Init EdgeAI library based on user solution, should be called once!
    nrf_edgeai_err_t res = nrf_edgeai_init(p_edgeai);
    // Optional check for success, #include <assert.h> required
    assert(res == NRF_EDGEAI_ERR_SUCCESS);
}
//
// ....
//
void user_feed_data_to_model(void)
{
    // Feed and prepare raw inputs for the model inference
    nrf_edgeai_err_t res = nrf_edgeai_feed_inputs(p_edgeai, raw_features,
                                            nrf_edgeai_uniq_inputs_num(p_edgeai) *
                                            nrf_edgeai_input_window_size(p_edgeai));

    // Check if input data is prepared and ready for model inference
    if (res == NRF_EDGEAI_ERR_SUCCESS)
    {
        // Run model inference
        res = nrf_edgeai_run_inference(p_edgeai);
        // Check if model inference is ready and successful
        if (res == NRF_EDGEAI_ERR_SUCCESS)
        {
            const flt32_t* p_predicted_values = p_edgeai->decoded_output.regression.p_outputs;
            size_t values_num = p_edgeai->decoded_output.regression.outputs_num;

            printf("Predicted target values:\r\n");
            for (size_t i = 0; i < values_num; i++)
            {
                printf("%f,", p_predicted_values[i]);
            }
            printf("\r\n");
        }
    }
}

This example prints only the predicted model value(s):

Predicted target values:
12.345678,

If you wish to validate predictions (as done in the automated validation), you add code to compare the prediction to a known expected value, and print the absolute error.

Dependencies

  • Header file: include/zephyr/kernel.h