Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imai mp integration #180

Merged
merged 5 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions examples/usercimaimodule/imagimob/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# MicroPython ImagiMob Integration for Infineon's PSoC6 port

This directory contains the imagimob generated model files and the interface for it's enablement in micropython. To access your edge ai models developed for PSoC6 based kit from a micropython application, please follow the description below.

## Folder structure
Currently the existing sample model files are generated for [Human Activity Recognition](https://developer.imagimob.com/getting-started/modus-toolbox-solution-and-Imagimob-Studio#human-activity-recognition) code from imagimob.

**model.c** : Imagimob generated source code in C for your developed model in the studio.

**model.h** : Imagimob generated source code in C for your developed model in the studio.

**imai_mp_iface.c** : MicroPython interface for ImagiMob models.

**micropython.mk** : Makefile configurations for firmware generation.

## Installation

Please check for the pre-requisites mentioned [here](../../ports/psoc6/README.md#Pre-requisites).

## Usage

Follow the steps below to generate micropython bindings for your imagimob generated model.

1. The imagimob micropytthon integration is available in imai-mp-integration, so checkout to that branch after cloning this repository.

git clone https://github.com/Infineon/micropython.git

git checkout --track origin/ports-psoc6-main

2. Then initialize the ModusToolbox™ environment:

cd ports/psoc6

make mtb_init BOARD=<board-name>

3. Retrieve submodules:

make submodules

4. In the examples/usercimaimodule/imagimob/ folder, replace the **model.c** and **model.h** files by your imagimob generated model files.

5. Compile it alongwith psoc6 port source code and generate the final .hex to be flashed into your device. From your root:

cd ports/psoc6

make USER_C_MODULES=../../examples/usercimaimodule/

make program

# Run micropython

Use any micropython supported IDE (like Thonny) and establish a session with your device. Now you should be able to import your model as shown below:

import imai as model

model.init() # Initialize the model

model.enqueue(data_in) # Send the data inputs for the model

model.dequeue(data_out) # Get the classifications

**Note**: Above is only an abstract code to show the usage in micropython. Please take care of populating the data-in and data_out with right dimensions and values. The provided api's abide by its operation in imagimob. Please refer to API documentation [here](https://developer.imagimob.com/edge-optimization/edge-api).
67 changes: 67 additions & 0 deletions examples/usercimaimodule/imagimob/imai_mp_iface.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Include MicroPython API.
#include "py/runtime.h"

#include "py/mphal.h"
#include "model.h"

static mp_obj_t init(void){
IMAI_init();
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(init_obj, init);

static mp_obj_t enqueue(const mp_obj_t data_in_obj){
float data_in[IMAI_DATA_IN_COUNT];
mp_obj_t *data_in_items;
size_t len;
mp_obj_get_array(data_in_obj, &len, &data_in_items);
if (len != IMAI_DATA_IN_COUNT) {
mp_raise_ValueError("data_in must be a list of IMAI_DATA_IN_COUNT floats");
}
for (int i = 0; i < IMAI_DATA_IN_COUNT; i++) {
data_in[i] = mp_obj_float_get(data_in_items[i]);
}
int result = IMAI_enqueue(data_in);
return MP_OBJ_NEW_SMALL_INT(result);
}
MP_DEFINE_CONST_FUN_OBJ_1(enqueue_obj, enqueue);

static mp_obj_t dequeue(mp_obj_t data_out_obj) {
mp_buffer_info_t buf_info;
mp_get_buffer(data_out_obj, &buf_info, MP_BUFFER_WRITE);
float *data_out = (float *)buf_info.buf;
int result = IMAI_dequeue(data_out);
if (result == 0) {
return MP_OBJ_NEW_SMALL_INT(result);
} else if (result == -1) {
mp_printf(&mp_plat_print, "No data is currently available\n");
return MP_OBJ_NEW_SMALL_INT(result);
} else if (result == -2) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Internal memory allocation error"), result);
}
return MP_OBJ_NEW_SMALL_INT(result);
}
MP_DEFINE_CONST_FUN_OBJ_1(dequeue_obj, dequeue);

static const mp_rom_map_elem_t example_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_imai) },

// imagimob fns
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&init_obj) },
{ MP_ROM_QSTR(MP_QSTR_enqueue), MP_ROM_PTR(&enqueue_obj) },
{ MP_ROM_QSTR(MP_QSTR_dequeue), MP_ROM_PTR(&dequeue_obj) },

// constants
{ MP_ROM_QSTR(MP_QSTR_DATA_IN), MP_ROM_INT(IMAI_DATA_IN_COUNT) },
{ MP_ROM_QSTR(MP_QSTR_DATA_OUT), MP_ROM_INT(IMAI_DATA_OUT_COUNT) },
};
static MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table);

// Define module object.
const mp_obj_module_t imai_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&example_module_globals,
};

// Register the module to make it available in Python.
MP_REGISTER_MODULE(MP_QSTR_imai, imai_module);
8 changes: 8 additions & 0 deletions examples/usercimaimodule/imagimob/micropython.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
IMAI_MOD_DIR := $(USERMOD_DIR)

# Add all C files to SRC_USERMOD.
SRC_USERMOD += $(IMAI_MOD_DIR)/imai_mp_iface.c $(IMAI_MOD_DIR)/model.c

# We can add our module folder to include paths if needed
# This is not actually needed in this example.
CFLAGS_USERMOD += -I$(IMAI_MOD_DIR)
Loading
Loading