Model Deployment

The last step is to deploy the model, which takes a trained model and generates the source code to run on the PSoC™6 development kit. We will generate the code for the model using Studio, modify the code as per our requirement and then deploy the model on the development kit using ModusToolbox™.

Code Generation

To generate the code for the model, follow the steps:

  1. Open the model file(*.h5) and select the Code Gen tab on the left pane.

  2. In Architecture, select the target architecture as Any(ANSI C99).

  3. In Output Name, enter pdm_model as the name for the generated .c/.h files.

  4. In Output Directory, browse the directory where you want to save the generated code. By, default Gen is selected as the default folder.

  5. In C Prefix, enter IMAI_PDM_ as the prefix to be added to the generated API functions.

  6. In Build, select the build type as Preprocessor.


  7. Click Generate Code to generate the code. The Code generation Report window appears.

  8. The code is generated under the Models/(model name)/Gen folder.


Customize the Code

Let's do the following changes in the code:

Step 1: Setup the Timer
/*System tick variables*/
uint32_t tick = 0;
void systick_isr(void)
{
    tick++; // This increments every time the SysTick counter decrements to 0.
}
/*timer set up*/
    Cy_SysTick_Init(CY_SYSTICK_CLOCK_SOURCE_CLK_IMO , (8000000/1000)-1);
    Cy_SysTick_SetCallback(0, systick_isr);        // point to SysTick ISR to increment the 1ms count
Step 2: Setup the Data
#include "model.h"
/*Model Input variables*/
int16_t  audio_frame[FRAME_SIZE] = {0};
float data_in = 0.0f;
float time_in = 0.0f;
 
/*Model Output variable*/
float data_out[IMAI_DATA_OUT_COUNT] = {0};
float time_out [IMAI_TIME_OUT_COUNT] = {0.0f};
 
/*convert int to float*/
int16_t val_temp = audio_frame[index];
float data_in = ((float) val_temp) / 32767.0f;
 
//scale for multiply the audio value
float scale_factor = 40.0f;
 
data_in = data_in*scale_factor;
 
if (data_in > 1.0)
{
    data_in = 1.0f;
}
if (data_in < -1.0)
{
   data_in = -1.0f;
}
Step 3: Run APIs
/* Initialize Imagimob AI library */
IMAI_init();
 
/*Pass audio data for enqueue*/
IMAI_enqueue(&data_in,&time_in);
if (IMAI_dequeue(data_out,time_out) == IMAI_RET_SUCCESS)
{
     printf("siren:%f\r\n", data_out[1]);
}
Step 4: Postprocessing
/*Post-processing variable*/
float confidence = 0.65f;
if (data_out[1]> confidence)
{
printf("siren:%f\r\n", data_out[1]);
}
Step 5: Print Inference Time
/*time variables*/
uint32_t time_before = 0;
uint32_t infer_time = 0;
 
/*Get time before enqueue*/
time_before = tick;
 
 
/*Get time after dequeue*/
infer_time = tick - time_before;
printf( "infer_time: %lu\r\n", infer_time);
Step 6: Calculate time between Predictions
/* calculate time between predictions*/
static int predic_now =0;
static int predic_before =0;
predic_now = tick;
int between_predic = predic_now-predic_before;
predic_before= tick;
printf("%d\r\n", between_predic);

Deploy the model on Development kit

Before deploying the model, first create the project in ModusToolbox™ following the steps below:

  1. Open ModusToolbox™ > Eclipse IDE for ModusToolbox™ from the Windows Start menu. The Eclipse IDE for ModusToolbox window appears.

  2. Browse and select the workspace directory for your project.

  3. Click Launch to open the ModusToolbox™ workspace.

  4. Select New Application from the Quick Panel or navigate to File> New> Modus Toolbox™ Application to open the Project Creator Tool.

  5. Expand PSoC™6 BSPs, select CY8CKIT-062S2-43012 as the BSP for your board and click Next. The Select Application window appears.

  6. Expand Peripherals and select the checkbox against the PDM PCM Audio and click Create. The PDM_PCM_audio Project is created in the Project explorer.



  7. Create a folder Imagimob in the PDM_PCM_audio Project and paste the model files (model.c and model.h) generated from Studio.



  8. Right-click the PDM_PCM_Audio_1 project and select Build Project or click Build Application in the Quick panel to build the project.



  9. Connect the KitProg3 USB port on the board with the PC using the USB cable.

  10. In Quick Panel> Launches, click PDM_PCM_Audio_1 Program. The model is deployed on the development kit.

Now, play the siren sounds and watch the confidence change for siren sounds as the audio is played. In this tutorial, you learned how to use the Infineon PSoC™ 6 and Imagimob to solve machine learning problems.