diff options
author | Kwang Son <k.son@samsung.com> | 2021-08-02 03:37:46 -0400 |
---|---|---|
committer | Kwang Son <k.son@samsung.com> | 2021-08-03 02:02:55 -0400 |
commit | 0a7ab1f50e641c4937d2ca87ab0950067688860b (patch) | |
tree | a9d4fc169fe2bbbb59372046d19f912e8775eca5 | |
parent | ae8c51996a086998145617eba1f3ac6deebfb165 (diff) | |
download | mediavision-0a7ab1f50e641c4937d2ca87ab0950067688860b.tar.gz mediavision-0a7ab1f50e641c4937d2ca87ab0950067688860b.tar.bz2 mediavision-0a7ab1f50e641c4937d2ca87ab0950067688860b.zip |
test: Seperate resource allocation
perform_pose_landmark_detection alloactate mv_source, mv_engine,
mv_infer in a function so makes hard to deallocate resource and
error handling.
Change-Id: I80df947730b734e9cb4f95bc238d66cccbe0ca7d
Signed-off-by: Kwang Son <k.son@samsung.com>
-rw-r--r-- | test/testsuites/machine_learning/inference/inference_test_suite.c | 559 |
1 files changed, 212 insertions, 347 deletions
diff --git a/test/testsuites/machine_learning/inference/inference_test_suite.c b/test/testsuites/machine_learning/inference/inference_test_suite.c index 5053ceac..e2df1475 100644 --- a/test/testsuites/machine_learning/inference/inference_test_suite.c +++ b/test/testsuites/machine_learning/inference/inference_test_suite.c @@ -194,6 +194,12 @@ #define PLD_TFLITE_WEIGHT_INT8_MOVENET_PATH \ "/usr/share/capi-media-vision/models/PLD/tflite/pld_int8_movenet.tflite" +#define TASK_IC 0 +#define TASK_OD 1 +#define TASK_FD 2 +#define TASK_FLD 3 +#define TASK_PLD 4 + /****** * Public model: * IC: mobilenet caffe, tf? @@ -332,6 +338,113 @@ void _image_classified_cb(mv_source_h source, const int number_of_classes, } } +int infer_task_with_img(char *img_file_name, mv_inference_h infer, int task_id) +{ + mv_source_h mvSource = NULL; + struct timespec s_tspec; + struct timespec e_tspec; + + int err = mv_create_source(&mvSource); + if (err != MEDIA_VISION_ERROR_NONE) { + printf("Fail to create mvSource.\n"); + return err; + } + + err = load_mv_source_from_file(img_file_name, mvSource); + if (err != MEDIA_VISION_ERROR_NONE) { + printf("Fail to load mvSource err: %d.\n", err); + return err; + } + + clock_gettime(CLOCK_MONOTONIC, &s_tspec); + + switch (task_id) { + case TASK_IC: + err = mv_inference_image_classify(mvSource, infer, NULL, + _image_classified_cb, NULL); + break; + case TASK_OD: + err = mv_inference_object_detect(mvSource, infer, _object_detected_cb, + NULL); + break; + case TASK_FD: + err = mv_inference_face_detect(mvSource, infer, _face_detected_cb, + NULL); + break; + case TASK_FLD: + err = mv_inference_facial_landmark_detect( + mvSource, infer, NULL, _facial_landmark_detected_cb, NULL); + break; + case TASK_PLD: + err = mv_inference_pose_landmark_detect( + mvSource, infer, NULL, _pose_landmark_detected_cb, NULL); + break; + default: + err = MEDIA_VISION_ERROR_INVALID_PARAMETER; + break; + } + if (err != MEDIA_VISION_ERROR_NONE) + printf("Fail to infer task [err:%i]\n", err); + + clock_gettime(CLOCK_MONOTONIC, &e_tspec); + struct timespec diffspec = diff(s_tspec, e_tspec); + unsigned long timeDiff = gettotalmillisec(diffspec); + printf("elapsed time : %lu(ms)\n", timeDiff); + + err = mv_destroy_source(mvSource); + if (err != MEDIA_VISION_ERROR_NONE) + printf("Fail to destroy mvSource [err:%i]\n", err); + + return err; +} + +int infer_task(mv_inference_h infer, int task_id) +{ + char *in_file_name = NULL; + /* Load media source */ + while (input_string("Input file name to be inferred:", 1024, + &(in_file_name)) == -1) + printf("Incorrect input! Try again.\n"); + + int err = infer_task_with_img(in_file_name, infer, task_id); + free(in_file_name); + return err; +} + +int mv_inference_task_helper(mv_engine_config_h engine_cfg, int task_id) +{ + mv_inference_h infer = NULL; + + int err = mv_inference_create(&infer); + if (err != MEDIA_VISION_ERROR_NONE) { + printf("Fail to create inference handle [err:%i]\n", err); + return err; + } + + err = mv_inference_configure(infer, engine_cfg); + if (err != MEDIA_VISION_ERROR_NONE) { + printf("Fail to configure inference handle [err:%i]\n", err); + goto clean_mv_inference; + } + + err = mv_inference_prepare(infer); + if (err != MEDIA_VISION_ERROR_NONE) { + printf("Fail to prepare inference handle"); + goto clean_mv_inference; + } + + err = infer_task(infer, task_id); + if (err != MEDIA_VISION_ERROR_NONE) { + printf("Fail to infer task"); + } + +clean_mv_inference: + err = mv_inference_destroy(infer); + if (err != MEDIA_VISION_ERROR_NONE) + printf("Fail to destroy inference handle [err:%i]\n", err); + return err; +} + int perform_configure_set_model_config_path(mv_engine_config_h engine_cfg) { int err = MEDIA_VISION_ERROR_NONE; @@ -3172,398 +3285,150 @@ int perform_facial_landmark_detection() return MEDIA_VISION_ERROR_NONE; } -int perform_armnn_cpm_config(mv_engine_config_h *engine_cfg) +int engine_config_hosted_tflite_cpu(mv_engine_config_h handle, + const char *tf_weight) { - int err = MEDIA_VISION_ERROR_NONE; - - mv_engine_config_h handle = NULL; - err = mv_create_engine_config(&handle); + int err = mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, tf_weight); if (err != MEDIA_VISION_ERROR_NONE) { - printf("Fail to create engine configuration handle.\n"); - if (handle) { - int err2 = mv_destroy_engine_config(handle); - if (err2 != MEDIA_VISION_ERROR_NONE) { - printf("Fail to destroy engine configuration.\n"); - } - } return err; } - const char *inputNodeName = "image"; - const char *outputNodeName[] = { "Convolutional_Pose_Machine/stage_5_out" }; - - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, PLD_TFLITE_WEIGHT_PATH); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_DATA_TYPE, - MV_INFERENCE_DATA_FLOAT32); - - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_USER_FILE_PATH, PLD_POSE_LABEL_PATH); - - mv_engine_config_set_double_attribute(handle, MV_INFERENCE_MODEL_MEAN_VALUE, - 0.0); - - mv_engine_config_set_double_attribute(handle, MV_INFERENCE_MODEL_STD_VALUE, - 1.0); - - mv_engine_config_set_double_attribute( - handle, MV_INFERENCE_CONFIDENCE_THRESHOLD, 0.3); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_BACKEND_TYPE, - MV_INFERENCE_BACKEND_TFLITE); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_TARGET_TYPE, - MV_INFERENCE_TARGET_CPU); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_TENSOR_WIDTH, - 192); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, - 192); - - mv_engine_config_set_int_attribute(handle, - MV_INFERENCE_INPUT_TENSOR_CHANNELS, 3); - - mv_engine_config_set_string_attribute(handle, MV_INFERENCE_INPUT_NODE_NAME, - inputNodeName); - - mv_engine_config_set_array_string_attribute( - handle, MV_INFERENCE_OUTPUT_NODE_NAMES, outputNodeName, 1); - - *engine_cfg = handle; - return err; -} - -int perform_hosted_tflite_cpm_192_config(mv_engine_config_h *engine_cfg) -{ - int err = MEDIA_VISION_ERROR_NONE; - - mv_engine_config_h handle = NULL; - err = mv_create_engine_config(&handle); + err = mv_engine_config_set_int_attribute(handle, MV_INFERENCE_BACKEND_TYPE, + MV_INFERENCE_BACKEND_TFLITE); if (err != MEDIA_VISION_ERROR_NONE) { - printf("Fail to create engine configuration handle.\n"); - if (handle) { - int err2 = mv_destroy_engine_config(handle); - if (err2 != MEDIA_VISION_ERROR_NONE) { - printf("Fail to destroy engine configuration.\n"); - } - } return err; } - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, - PLD_TFLITE_WEIGHT_CPM_192_PATH); - - mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_BACKEND_TYPE, - MV_INFERENCE_BACKEND_TFLITE); - - mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_TARGET_TYPE, - MV_INFERENCE_TARGET_CPU); - - *engine_cfg = handle; + err = mv_engine_config_set_int_attribute(handle, MV_INFERENCE_TARGET_TYPE, + MV_INFERENCE_TARGET_CPU); return err; } -int perform_hosted_tflite_mobilenet_v1_posenet_257_config(mv_engine_config_h *engine_cfg) +int perform_armnn_cpm_config(mv_engine_config_h handle) { - int err = MEDIA_VISION_ERROR_NONE; + const char *inputNodeName = "image"; + const char *outputNodeName[] = { "Convolutional_Pose_Machine/stage_5_out" }; - mv_engine_config_h handle = NULL; - err = mv_create_engine_config(&handle); + int err = engine_config_hosted_tflite_cpu(handle, PLD_TFLITE_WEIGHT_PATH); if (err != MEDIA_VISION_ERROR_NONE) { - printf("Fail to create engine configuration handle.\n"); - if (handle) { - int err2 = mv_destroy_engine_config(handle); - if (err2 != MEDIA_VISION_ERROR_NONE) { - printf("Fail to destroy engine configuration.\n"); - } - } return err; } - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, - PLD_TFLITE_WEIGHT_MOBILENET_V1_POSENET_257_PATH); + err = mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_DATA_TYPE, MV_INFERENCE_DATA_FLOAT32); + if (err != MEDIA_VISION_ERROR_NONE) { + return err; + } -/* - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_USER_FILE_PATH, - PLD_LABEL_MOBILENET_V1_POSENET_257_PATH); -*/ - mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_BACKEND_TYPE, - MV_INFERENCE_BACKEND_TFLITE); + err = mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_MODEL_USER_FILE_PATH, PLD_POSE_LABEL_PATH); + if (err != MEDIA_VISION_ERROR_NONE) { + return err; + } - mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_TARGET_TYPE, - MV_INFERENCE_TARGET_CPU); + err = mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_MEAN_VALUE, 0.0); + if (err != MEDIA_VISION_ERROR_NONE) { + return err; + } - *engine_cfg = handle; - return err; -} + err = mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_STD_VALUE, 1.0); + if (err != MEDIA_VISION_ERROR_NONE) { + return err; + } -int perform_hosted_tflite_int8_movenet(mv_engine_config_h *engine_cfg) -{ - int err = MEDIA_VISION_ERROR_NONE; + err = mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_CONFIDENCE_THRESHOLD, 0.3); + if (err != MEDIA_VISION_ERROR_NONE) { + return err; + } - mv_engine_config_h handle = NULL; - err = mv_create_engine_config(&handle); + err = mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_WIDTH, 192); if (err != MEDIA_VISION_ERROR_NONE) { - printf("Fail to create engine configuration handle.\n"); - if (handle) { - int err2 = mv_destroy_engine_config(handle); - if (err2 != MEDIA_VISION_ERROR_NONE) { - printf("Fail to destroy engine configuration.\n"); - } - } return err; } - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, - PLD_TFLITE_WEIGHT_INT8_MOVENET_PATH); + err = mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, 192); + if (err != MEDIA_VISION_ERROR_NONE) { + return err; + } - mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_BACKEND_TYPE, - MV_INFERENCE_BACKEND_TFLITE); + err = mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_CHANNELS, 3); + if (err != MEDIA_VISION_ERROR_NONE) { + return err; + } - mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_TARGET_TYPE, - MV_INFERENCE_TARGET_CPU); + err = mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_INPUT_NODE_NAME, inputNodeName); + if (err != MEDIA_VISION_ERROR_NONE) { + return err; + } - *engine_cfg = handle; + err = mv_engine_config_set_array_string_attribute( + handle, MV_INFERENCE_OUTPUT_NODE_NAMES, outputNodeName, 1); return err; } int perform_pose_landmark_detection() { int err = MEDIA_VISION_ERROR_NONE; - - int sel_opt = 0; - const char *names[] = { "Configuration", - "TFLITE(CPU) + CPM", - "Hosted: TFLITE(CPU) + CPM", - "Hosted: TFLITE(CPU) + MOBILENET_V1_POSENET", - "Hosted: TFLITE(CPU) + INT8_MOVENET", - "Prepare", - "Run", - "Back" }; - mv_engine_config_h engine_cfg = NULL; - mv_inference_h infer = NULL; - mv_source_h mvSource = NULL; + const char *names[] = { + "TFLITE(CPU) + CPM", + "Hosted: TFLITE(CPU) + CPM", + "Hosted: TFLITE(CPU) + MOBILENET_V1_POSENET", + "Hosted: TFLITE(CPU) + INT8_MOVENET", + }; - while (sel_opt == 0) { - sel_opt = show_menu_linear("Select Action:", names, ARRAY_SIZE(names)); - switch (sel_opt) { - case 1: { - //perform configuration - if (engine_cfg) { - int err2 = mv_destroy_engine_config(engine_cfg); - if (err2 != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy engine_cfg [err:%i]\n", err2); - engine_cfg = NULL; - } - - err = perform_configuration(&engine_cfg); - } break; - case 2: { - //perform TweakCNN config - if (engine_cfg) { - int err2 = mv_destroy_engine_config(engine_cfg); - if (err2 != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy engine_cfg [err:%i]\n", err2); - engine_cfg = NULL; - } - err = perform_armnn_cpm_config(&engine_cfg); - } break; - case 3: { - //perform cpm config - if (engine_cfg) { - int err2 = mv_destroy_engine_config(engine_cfg); - if (err2 != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy engine_cfg [err:%i]\n", err2); - engine_cfg = NULL; - } - err = perform_hosted_tflite_cpm_192_config(&engine_cfg); - } break; - case 4: { - //perform mobilenet-v1 posenet config - if (engine_cfg) { - int err2 = mv_destroy_engine_config(engine_cfg); - if (err2 != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy engine_cfg [err:%i]\n", err2); - engine_cfg = NULL; - } - err = perform_hosted_tflite_mobilenet_v1_posenet_257_config(&engine_cfg); - } break; - case 5: { - //perform int8 movenet - if (engine_cfg) { - int err2 = mv_destroy_engine_config(engine_cfg); - if (err2 != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy engine_cfg [err:%i]\n", err2); - engine_cfg = NULL; - } - err = perform_hosted_tflite_mobilenet_v1_posenet_257_config(&engine_cfg); - } break; - case 6: { - // create - configure - prepare - if (infer) { - int err2 = mv_inference_destroy(infer); - if (err2 != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy inference handle [err:%i]\n", err2); - infer = NULL; - } - - // inference - // create handle - err = mv_inference_create(&infer); - if (err != MEDIA_VISION_ERROR_NONE) { - printf("Fail to create inference handle [err:%i]\n", err); - break; - } - - //configure - err = mv_inference_configure(infer, engine_cfg); - if (err != MEDIA_VISION_ERROR_NONE) { - printf("Fail to configure inference handle [err:%i]\n", err); - break; - } - - //prepare - err = mv_inference_prepare(infer); - if (err != MEDIA_VISION_ERROR_NONE) { - printf("Fail to prepare inference handle"); - break; - } - } break; - case 7: { - if (mvSource) { - int err2 = mv_destroy_source(mvSource); - if (err2 != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy mvSource\n"); - mvSource = NULL; - } - - char *in_file_name = NULL; - /* Load media source */ - while (input_string("Input file name to be inferred:", 1024, - &(in_file_name)) == -1) - printf("Incorrect input! Try again.\n"); - - err = mv_create_source(&mvSource); - if (err != MEDIA_VISION_ERROR_NONE) { - printf("Fail to create mvSource.\n"); - free(in_file_name); - break; - } - - err = load_mv_source_from_file(in_file_name, mvSource); - if (err != MEDIA_VISION_ERROR_NONE) { - int err2 = mv_destroy_source(mvSource); - if (err2 != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy mvSource err: %d.\n", err2); - mvSource = NULL; - free(in_file_name); - break; - } - free(in_file_name); - - struct timespec s_tspec; - struct timespec e_tspec; - - clock_gettime(CLOCK_MONOTONIC, &s_tspec); - - // Object Detect - err = mv_inference_pose_landmark_detect( - mvSource, infer, NULL, _pose_landmark_detected_cb, NULL); - - clock_gettime(CLOCK_MONOTONIC, &e_tspec); - - struct timespec diffspec = diff(s_tspec, e_tspec); - unsigned long timeDiff = gettotalmillisec(diffspec); - printf("elapsed time : %lu(ms)\n", timeDiff); - } break; - case 8: { - //perform destroy - if (engine_cfg) { - err = mv_destroy_engine_config(engine_cfg); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy engine_cfg [err:%i]\n", err); - engine_cfg = NULL; - } - - if (infer) { - err = mv_inference_destroy(infer); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy inference handle [err:%i]\n", err); - infer = NULL; - } - - if (mvSource) { - err = mv_destroy_source(mvSource); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy mvSource [err:%i]\n", err); - mvSource = NULL; - } - } break; - default: - printf("Invalid option.\n"); - sel_opt = 0; - continue; - } - - int do_another = 0; - if (err != MEDIA_VISION_ERROR_NONE) { - printf("ERROR: Action is finished with error code: %i\n", err); - } - - sel_opt = 0; - while (sel_opt == 0) { - sel_opt = show_menu_yes_or_no("Run Pose Landmark Detection again?:"); - switch (sel_opt) { - case 1: - do_another = 1; - break; - case 2: - do_another = 0; - break; - default: - printf("Invalid option.\n"); - sel_opt = 0; - } - } - - sel_opt = (do_another == 1) ? 0 : 1; + int sel_opt = show_menu_linear("Select Action:", names, ARRAY_SIZE(names)); + if (sel_opt <= 0 || sel_opt > ARRAY_SIZE(names)) { + printf("Invalid option"); + return -1; } - if (engine_cfg) { - err = mv_destroy_engine_config(engine_cfg); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy engine_cfg [err:%i]\n", err); - engine_cfg = NULL; + err = mv_create_engine_config(&engine_cfg); + if (err != MEDIA_VISION_ERROR_NONE) { + printf("Fail to create engine configuration handle.\n"); + return err; } - if (infer) { - err = mv_inference_destroy(infer); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy inference handle [err:%i]\n", err); - infer = NULL; + switch (sel_opt) { + case 1: { //perform TweakCNN config + err = perform_armnn_cpm_config(engine_cfg); + } break; + case 2: { //perform cpm config + err = engine_config_hosted_tflite_cpu(engine_cfg, + PLD_TFLITE_WEIGHT_CPM_192_PATH); + } break; + case 3: { //perform mobilenet-v1 posenet config + err = engine_config_hosted_tflite_cpu( + engine_cfg, PLD_TFLITE_WEIGHT_MOBILENET_V1_POSENET_257_PATH); + } break; + case 4: { //perform int8 movenet + err = engine_config_hosted_tflite_cpu( + engine_cfg, PLD_TFLITE_WEIGHT_INT8_MOVENET_PATH); + } break; } - - if (mvSource) { - err = mv_destroy_source(mvSource); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy mvSource [err:%i]\n", err); - mvSource = NULL; + if (err != MEDIA_VISION_ERROR_NONE) { + printf("Fail to perform config [err:%i]\n", err); + goto clean_pose_engine; } - return MEDIA_VISION_ERROR_NONE; + err = mv_inference_task_helper(engine_cfg, TASK_PLD); + if (err != MEDIA_VISION_ERROR_NONE) + printf("Fail to detect with engine [err:%i]\n", err); + +clean_pose_engine: + err = mv_destroy_engine_config(engine_cfg); + if (err != MEDIA_VISION_ERROR_NONE) + printf("Fail to destroy engine_cfg [err:%i]\n", err); + + return err; } int main() |