lib/libtf: Add support for 1D/1D regression models.

* Fixes #1751.
* Fixes #1739.
This commit is contained in:
tejalbarnwal 2022-12-28 11:24:52 +05:30 committed by iabdalkader
parent 5eec9d3065
commit ac49aa5285
6 changed files with 57 additions and 2 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -72,6 +72,8 @@ int libtf_generate_micro_features(const int16_t *input, // Audio samples
int8_t *output, // Slice data
size_t *num_samples_read); // Number of samples used
int libtf_regression_1Dinput_1Doutput(const unsigned char *model_data, uint8_t* tensor_arena, libtf_parameters_t* params, float* input_data, float* output_data);
#ifdef __cplusplus
}
#endif

View File

@ -8,10 +8,13 @@
*
* Python Tensorflow library wrapper.
*/
#include <stdio.h>
#include "py/runtime.h"
#include "py/obj.h"
#include "py/objlist.h"
#include "py/objtuple.h"
#include "py/objarray.h"
#include "py/binary.h"
#include "py_helper.h"
#include "imlib_config.h"
@ -254,6 +257,53 @@ STATIC py_tf_model_obj_t *py_tf_load_alloc(mp_obj_t path_obj)
}
}
STATIC mp_obj_t py_tf_regression(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
{
fb_alloc_mark();
py_tf_alloc_putchar_buffer();
// read model
py_tf_model_obj_t *arg_model = py_tf_load_alloc(args[0]);
size_t input_size = (&arg_model->params)->input_width;
size_t output_size = (&arg_model->params)->output_channels;
// read input
mp_obj_array_t *arg_input_array = args[1];
// check for the input size
if (input_size != arg_input_array->len) {
mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Input array size is not same as model input size!"));
}
float input_array[input_size];
for (size_t i=0; i<input_size; i++) {
input_array[i] = (float) mp_obj_float_get(
mp_binary_get_val_array(arg_input_array->typecode, arg_input_array->items, i)
);
}
uint8_t *tensor_arena = fb_alloc(arg_model->params.tensor_arena_size, FB_ALLOC_PREFER_SPEED | FB_ALLOC_CACHE_ALIGN);
float output_data[output_size];
// predict the output using tflite model
if (libtf_regression_1Dinput_1Doutput(arg_model->model_data,
tensor_arena, &arg_model->params, input_array, output_data) != 0){
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Coundnt execute the model to predict the output"));
}
// read output
mp_obj_list_t * out = (mp_obj_list_t *) mp_obj_new_list(output_size, NULL);
for (size_t j=0; j<(output_size); j++) {
out->items[j] = mp_obj_new_float(output_data[j]);
}
fb_alloc_free_till_mark();
return out;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_tf_regression_obj, 2, py_tf_regression);
typedef struct py_tf_input_data_callback_data {
image_t *img;
rectangle_t *roi;
@ -788,7 +838,8 @@ STATIC const mp_rom_map_elem_t locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_output_zero_point), MP_ROM_PTR(&py_tf_output_zero_point_obj) },
{ MP_ROM_QSTR(MP_QSTR_classify), MP_ROM_PTR(&py_tf_classify_obj) },
{ MP_ROM_QSTR(MP_QSTR_segment), MP_ROM_PTR(&py_tf_segment_obj) },
{ MP_ROM_QSTR(MP_QSTR_detect), MP_ROM_PTR(&py_tf_detect_obj) }
{ MP_ROM_QSTR(MP_QSTR_detect), MP_ROM_PTR(&py_tf_detect_obj) },
{ MP_ROM_QSTR(MP_QSTR_regression), MP_ROM_PTR(&py_tf_regression_obj) }
};
STATIC MP_DEFINE_CONST_DICT(locals_dict, locals_dict_table);
@ -811,13 +862,15 @@ STATIC const mp_rom_map_elem_t globals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_classify), MP_ROM_PTR(&py_tf_classify_obj) },
{ MP_ROM_QSTR(MP_QSTR_segment), MP_ROM_PTR(&py_tf_segment_obj) },
{ MP_ROM_QSTR(MP_QSTR_detect), MP_ROM_PTR(&py_tf_detect_obj) },
{ MP_ROM_QSTR(MP_QSTR_regression), MP_ROM_PTR(&py_tf_regression_obj) }
#else
{ MP_ROM_QSTR(MP_QSTR_load), MP_ROM_PTR(&py_func_unavailable_obj) },
{ MP_ROM_QSTR(MP_QSTR_load_builtin_model), MP_ROM_PTR(&py_func_unavailable_obj) },
{ MP_ROM_QSTR(MP_QSTR_free_from_fb), MP_ROM_PTR(&py_func_unavailable_obj) },
{ MP_ROM_QSTR(MP_QSTR_classify), MP_ROM_PTR(&py_func_unavailable_obj) },
{ MP_ROM_QSTR(MP_QSTR_segment), MP_ROM_PTR(&py_func_unavailable_obj) },
{ MP_ROM_QSTR(MP_QSTR_detect), MP_ROM_PTR(&py_func_unavailable_obj) }
{ MP_ROM_QSTR(MP_QSTR_detect), MP_ROM_PTR(&py_func_unavailable_obj) },
{ MP_ROM_QSTR(MP_QSTR_regression), MP_ROM_PTR(&py_func_unavailable_obj) }
#endif // IMLIB_ENABLE_TF
};