Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
[0.3.5] release.
Browse files Browse the repository at this point in the history
  • Loading branch information
junhuanchen committed Dec 13, 2021
1 parent c66b79e commit a1e8bb3
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 5 deletions.
185 changes: 181 additions & 4 deletions ext_modules/_maix_nn/src/py_maix_nn_Model.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,184 @@ static int Model_init(ModelObject *self, PyObject *args, PyObject *kwds)
Py_DECREF(keys_outputs);
/* load by libmaix API end */
}
else if (strcmp(model_type, "normal") == 0)
{
/*load by libmaix R329 AIP */
PyObject *o_inputs = PyDict_GetItemString(o_opt, "inputs");
if(!o_inputs || !PyDict_Check(o_inputs))
{
PyErr_SetString(PyExc_ValueError, "arg opt need inputs key, value is dict");
return -1;
}
Py_ssize_t inputs_len = PyDict_Size(o_inputs);
self->inputs = o_inputs;
PyObject *o_outputs = PyDict_GetItemString(o_opt, "outputs");
if(!o_outputs || !PyDict_Check(o_outputs))
{
PyErr_SetString(PyExc_ValueError, "arg opt need outputs key, value is dict");
return -1;
}
Py_ssize_t outputs_len = PyDict_Size(o_outputs);
self->outputs = o_outputs;
PyObject *o_mean = PyDict_GetItemString(o_opt, "mean");
if(!o_mean || !(PyFloat_Check(o_mean) || (PyList_Check(o_mean) && (PyList_Size(o_mean)==3 || PyList_Size(o_mean)==1))))
{
PyErr_SetString(PyExc_ValueError, "arg opt need mean key, value is float or list");
return -1;
}
PyObject *o_norm = PyDict_GetItemString(o_opt, "norm");
if(!o_norm || !(PyFloat_Check(o_norm) || (PyList_Check(o_norm) && (PyList_Size(o_norm)==3 || PyList_Size(o_norm)==1))))
{
PyErr_SetString(PyExc_ValueError, "arg opt need norm key, value is float or list");
return -1;
}

// PyObject *o_param_path = PyDict_GetItemString(o_model_path, "param");
// if(!o_param_path || !PyUnicode_Check(o_param_path))
// {
// PyErr_SetString(PyExc_ValueError, "arg model_path need param key, value is str");
// return -1;
// }
PyObject *o_bin_path = PyDict_GetItemString(o_model_path, "bin");
if(!o_bin_path || !PyUnicode_Check(o_bin_path))
{
PyErr_SetString(PyExc_ValueError, "arg model_path need bin key, value is str");
return -1;
}

Py_INCREF(self->inputs);
Py_INCREF(self->outputs);

/* load by libmaix API */
libmaix_nn_module_init();
libmaix_nn_model_path_t model_path = {
.normal.model_path = (char*)PyUnicode_DATA(o_bin_path),
};


// encrypted model?
bool encrypt = false;
PyObject *o_encrypt = PyDict_GetItemString(o_opt, "encrypt");
if(o_encrypt)
{
if(PyLong_AsLong(o_encrypt))
{
encrypt = true;
}
}
int inputs_id[inputs_len];
int outputs_id[outputs_len];
char* inputs_names[inputs_len];
char* outputs_names[outputs_len];
PyObject* keys_inputs = PyDict_Keys(o_inputs);
PyObject* keys_outputs = PyDict_Keys(o_outputs);
PyObject* temp = NULL;
for(Py_ssize_t i=0; i<inputs_len; ++i)
{
temp = PyList_GetItem(keys_inputs, i);
if(PyLong_Check(temp))
{
encrypt = true;
inputs_id[i] = PyLong_AsLong(temp);
}
else
{
inputs_names[i] = (char*)PyUnicode_DATA(temp);
}
}
for(Py_ssize_t i=0; i<outputs_len; ++i)
{
temp = PyList_GetItem(keys_outputs, i);
if(PyLong_Check(temp))
{
encrypt = true;
outputs_id[i] = PyLong_AsLong(temp);
}
else
{
outputs_names[i] = (char*)PyUnicode_DATA(temp);
}
}
libmaix_nn_opt_param_t opt_param = {
.normal.input_names = NULL,
.normal.output_names = NULL,
.normal.input_num = inputs_len, // len(input_names)
.normal.output_num = outputs_len, // len(output_names)
.normal.mean = {127.5, 127.5, 127.5},
.normal.norm = {0.0078125, 0.0078125, 0.0078125},
};
if(!encrypt)
{
opt_param.normal.input_names = inputs_names;
opt_param.normal.output_names = outputs_names;
opt_param.normal.encrypt = false;
}
else
{
opt_param.normal.input_ids = inputs_id;
opt_param.normal.output_ids = outputs_id;
opt_param.normal.encrypt = true;
}
if(PyFloat_Check(o_mean))
{
opt_param.normal.mean[0] = (float)PyFloat_AsDouble(o_mean);
opt_param.normal.mean[1] = opt_param.normal.mean[0];
opt_param.normal.mean[2] = opt_param.normal.mean[0];
}
else
{
Py_ssize_t i=0;
for(; i<PyList_Size(o_mean); ++i)
{
opt_param.normal.mean[i] = (float)PyFloat_AsDouble(PyList_GetItem(o_mean, i));
}
for(Py_ssize_t j=i; j<3; ++j)
{
opt_param.normal.mean[j] = opt_param.normal.mean[i-1];
}
}
if(PyFloat_Check(o_norm))
{
opt_param.normal.norm[0] = (float)PyFloat_AsDouble(o_norm);
opt_param.normal.norm[1] = opt_param.normal.norm[0];
opt_param.normal.norm[2] = opt_param.normal.norm[0];
}
else
{
Py_ssize_t i=0;
for(; i<PyList_Size(o_norm); ++i)
{
opt_param.normal.norm[i] = (float)PyFloat_AsDouble(PyList_GetItem(o_norm, i));
}
for(Py_ssize_t j=i; j<3; ++j)
{
opt_param.normal.norm[j] = opt_param.normal.norm[i-1];
}
}
self->inputs_len = inputs_len;
self->outputs_len = outputs_len;
self->nn = libmaix_nn_create();
if(!self->nn)
{
PyErr_SetString(PyExc_MemoryError, "libmaix_nn object create fail");
goto end;
}
err = self->nn->init(self->nn);
if(err != LIBMAIX_ERR_NONE)
{
PyErr_Format(PyExc_Exception, "libmaix_nn init fail: %s\n", libmaix_get_err_msg(err));
goto end;
}
err = self->nn->load(self->nn, &model_path, &opt_param);
if(err != LIBMAIX_ERR_NONE)
{
PyErr_Format(PyExc_Exception, "libmaix_nn load fail: %s\n", libmaix_get_err_msg(err));
goto end;
}
Py_DECREF(keys_inputs);
Py_DECREF(keys_outputs);
}

else
{
PyErr_SetString(PyExc_ValueError, "now model_type only support awnn");
Expand Down Expand Up @@ -344,7 +522,7 @@ static PyObject* Model_forward(ModelObject *self, PyObject *args, PyObject *kw_a
}
libmaix_err_t err = LIBMAIX_ERR_NONE;
PyObject *o_inputs = NULL;
const char* layout_str = "chw";
const char* layout_str = "hwc";
int quantize = 1;
int debug = 0;
static char *kwlist[] = {"inputs", "quantize", "layout", "debug", NULL};
Expand All @@ -365,8 +543,7 @@ static PyObject* Model_forward(ModelObject *self, PyObject *args, PyObject *kw_a
PyErr_SetString(PyExc_NotImplementedError, "not support multiple input yet");
return NULL;
}
libmaix_nn_layout_t outputs_layout = LIBMAIX_NN_LAYOUT_CHW;

libmaix_nn_layout_t outputs_layout = LIBMAIX_NN_LAYOUT_HWC;
if(strcmp(layout_str, "hwc") == 0)
{
outputs_layout = LIBMAIX_NN_LAYOUT_HWC;
Expand Down Expand Up @@ -425,7 +602,7 @@ static PyObject* Model_forward(ModelObject *self, PyObject *args, PyObject *kw_a
.w = input_w,
.h = input_h,
.c = input_c,
.dtype = (quantize == 0 ? LIBMAIX_NN_DTYPE_INT8 : LIBMAIX_NN_DTYPE_UINT8),
.dtype = (quantize == 1 ? LIBMAIX_NN_DTYPE_INT8 : LIBMAIX_NN_DTYPE_UINT8),
.data = input_bytes,
.need_quantization = (quantize == 0 ? false : true),
.buff_quantization = NULL
Expand Down
2 changes: 1 addition & 1 deletion ext_modules/libmaix
Submodule libmaix updated 34 files
+2 −2 Kconfig
+23 −1 README.md
+21 −2 README_ZH.md
+1 −0 components/libmaix/CMakeLists.txt
+2 −1 components/libmaix/include/libmaix_image.h
+17 −2 components/libmaix/include/libmaix_nn.h
+7 −7 components/libmaix/include/libmaix_nn_classifier.h
+565 −0 components/libmaix/include/standard_api.h
+ components/libmaix/lib/arch/r329/libjpeg.so.9
+ components/libmaix/lib/arch/r329/libmaix_cam.so
+ components/libmaix/lib/arch/r329/libmaix_disp.so
+ components/libmaix/lib/arch/r329/libmaix_image.so
+ components/libmaix/lib/arch/r329/libmaix_nn.so
+ components/libmaix/lib/arch/r329/libmaix_utils.so
+3 −2 components/libmaix/lib/arch/r329/src/CMakeLists.txt
+44 −0 components/libmaix/lib/arch/r329/src/libmaix_nn/CMakeLists.txt
+ components/libmaix/lib/arch/r329/src/libmaix_nn/lib/libaipudrv.so
+350 −0 components/libmaix/lib/arch/r329/src/libmaix_nn/src/libmaix_nn.cpp
+565 −0 components/libmaix/lib/arch/r329/src/libmaix_nn/standard_api.h
+2 −0 components/libmaix/lib/arch/r329/src/libmaix_utils/CMakeLists.txt
+ components/libmaix/lib/arch/v831/libmaix_image.so
+ components/libmaix/lib/arch/v833/libmaix_image.so
+6 −4 components/maix_cv_image/include/libmaix_cv_image.h
+888 −782 components/maix_cv_image/src/libmaix_cv_image.cpp
+37 −16 examples/display/main/src/main.c
+11 −1 examples/hello-world/config_defaults.mk
+1 −1 examples/hello-world/main/CMakeLists.txt
+1 −0 examples/nn_resnet/main/CMakeLists.txt
+105 −96 examples/nn_resnet/main/src/main.c
+1 −0 examples/nn_resnet/project.py
+1 −1 examples/nn_yolo2/main/src/main.c
+13 −10 tools/cmake/compile.cmake
+6 −2 tools/cmake/compile_flags.cmake
+9 −2 tools/cmake/gen_binary.cmake

0 comments on commit a1e8bb3

Please sign in to comment.