mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
lib/libtf: Update libtf regression to support 2D input.
updated tf_regregression to take ulab array as input updated tf_regregression to take ulab array as input updated libtf_regression function name included ulab ndarray updated libtf header file
This commit is contained in:
parent
cc434bbee0
commit
2d8f3f94a1
@ -1,5 +1,5 @@
|
||||
/* This file is part of the OpenMV project.
|
||||
* Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io> & Kwabena W. Agyeman <kwagyeman@openmv.io>
|
||||
* Copyright (c) 2013-2023 Ibrahim Abdelkader <iabdalkader@openmv.io> & Kwabena W. Agyeman <kwagyeman@openmv.io>
|
||||
* This work is licensed under the MIT license, see the file LICENSE for details.
|
||||
*/
|
||||
|
||||
@ -72,7 +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);
|
||||
// runs regression on 2D/ 1D input(provided as array) and return 1D output
|
||||
int libtf_regression(const unsigned char *model_data, uint8_t* tensor_arena, libtf_parameters_t* params, float* input_data, float* output_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -13,12 +13,14 @@
|
||||
#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"
|
||||
|
||||
#include "ulab/code/ulab.h"
|
||||
#include "ulab/code/ndarray.h"
|
||||
|
||||
#ifdef IMLIB_ENABLE_TF
|
||||
#include "py_image.h"
|
||||
#include "ff_wrapper.h"
|
||||
@ -266,30 +268,27 @@ STATIC mp_obj_t py_tf_regression(uint n_args, const mp_obj_t *args, mp_map_t *kw
|
||||
// read model
|
||||
py_tf_model_obj_t *arg_model = py_tf_load_alloc(args[0]);
|
||||
|
||||
size_t input_size = (&arg_model->params)->input_width;
|
||||
// read input(2D or 1D) and output size(1D)
|
||||
size_t input_size_width = (&arg_model->params)->input_width;
|
||||
size_t input_size_height = (&arg_model->params)->input_height;
|
||||
size_t output_size = (&arg_model->params)->output_channels;
|
||||
|
||||
// read input
|
||||
mp_obj_array_t *arg_input_array = args[1];
|
||||
|
||||
ndarray_obj_t *arg_input_array = args[1];
|
||||
|
||||
// check for the input size
|
||||
if (input_size != arg_input_array->len) {
|
||||
if ((input_size_width * input_size_height) != 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)
|
||||
);
|
||||
}
|
||||
float *input_array = (float *)(arg_input_array->array);
|
||||
|
||||
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,
|
||||
if (libtf_regression(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"));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user