From aa3185f2252bdebbfbf48cc3dab3d4c97f54aa7c Mon Sep 17 00:00:00 2001 From: Kwang Son Date: Tue, 3 Aug 2021 21:42:19 -0400 Subject: [PATCH] test: Cleanup perform_facial_landmark_detection Base on commit 0a7ab1f50e641c4937d2ca87ab0950067688860b alloactate mv_source, mv_engine, mv_infer in a function so makes hard to deallocate resource and error handling. Change-Id: Ib242154ec33602a8a9788203cabc34e37e5f878d Signed-off-by: Kwang Son --- .../inference/inference_test_suite.c | 473 ++++----------------- 1 file changed, 90 insertions(+), 383 deletions(-) diff --git a/test/testsuites/machine_learning/inference/inference_test_suite.c b/test/testsuites/machine_learning/inference/inference_test_suite.c index e2df147..5fa02f2 100644 --- a/test/testsuites/machine_learning/inference/inference_test_suite.c +++ b/test/testsuites/machine_learning/inference/inference_test_suite.c @@ -200,6 +200,15 @@ #define TASK_FLD 3 #define TASK_PLD 4 +#define RET_IF_FAIL(exp) \ + do { \ + int err = (exp); \ + if (err != MEDIA_VISION_ERROR_NONE) { \ + printf("[%s] %s failed\n", __func__, #exp); \ + return err; \ + } \ + } while (0) + /****** * Public model: * IC: mobilenet caffe, tf? @@ -2876,413 +2885,111 @@ int perform_face_detection() return MEDIA_VISION_ERROR_NONE; } -int perform_tflite_TweakCNN(mv_engine_config_h *engine_cfg) +int perform_tflite_TweakCNN(mv_engine_config_h handle) { - int err = MEDIA_VISION_ERROR_NONE; - - mv_engine_config_h handle = NULL; - err = mv_create_engine_config(&handle); - 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 = "INPUT_TENSOR_NAME"; const char *outputNodeName[] = { "OUTPUT_TENSOR_NAME" }; - mv_engine_config_set_string_attribute(handle, - MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, - FLD_TFLITE_WEIGHT_PATH); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_DATA_TYPE, - MV_INFERENCE_DATA_FLOAT32); - - 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_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, - 128); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, - 128); - - 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; + RET_IF_FAIL( + engine_config_hosted_tflite_cpu(handle, FLD_TFLITE_WEIGHT_PATH)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_DATA_TYPE, MV_INFERENCE_DATA_FLOAT32)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_MEAN_VALUE, 0.0)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_STD_VALUE, 1.0)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_WIDTH, 128)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, 128)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_CHANNELS, 3)); + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_INPUT_NODE_NAME, inputNodeName)); + RET_IF_FAIL(mv_engine_config_set_array_string_attribute( + handle, MV_INFERENCE_OUTPUT_NODE_NAMES, outputNodeName, 1)); + return MEDIA_VISION_ERROR_NONE; } -int perform_opencv_cnncascade(mv_engine_config_h *engine_cfg) +int perform_opencv_cnncascade(mv_engine_config_h handle) { - int err = MEDIA_VISION_ERROR_NONE; - - mv_engine_config_h handle = NULL; - err = mv_create_engine_config(&handle); - 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 = "data"; const char *outputNodeName[] = { "Sigmoid_fc2" }; - mv_engine_config_set_string_attribute(handle, - MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, - FLD_OPENCV_WEIGHT_CAFFE_PATH); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_DATA_TYPE, - MV_INFERENCE_DATA_FLOAT32); - - mv_engine_config_set_string_attribute( + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, + FLD_OPENCV_WEIGHT_CAFFE_PATH)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_DATA_TYPE, MV_INFERENCE_DATA_FLOAT32)); + RET_IF_FAIL(mv_engine_config_set_string_attribute( handle, MV_INFERENCE_MODEL_CONFIGURATION_FILE_PATH, - FLD_OPENCV_CONFIG_CAFFE_PATH); - - mv_engine_config_set_double_attribute(handle, MV_INFERENCE_MODEL_MEAN_VALUE, - 127.5); - - mv_engine_config_set_double_attribute(handle, MV_INFERENCE_MODEL_STD_VALUE, - 127.5); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_BACKEND_TYPE, - MV_INFERENCE_BACKEND_OPENCV); - - 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, - 128); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, - 128); - - 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_tweakCNN_128_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); - 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, - FLD_TFLITE_WIEGHT_TWEAKCNN_128_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; - return err; -} - -int perform_hosted_tflite_mediapipe_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); - 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, - FLD_TFLITE_WIEGHT_MEDIAPIPE_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; - return err; + FLD_OPENCV_CONFIG_CAFFE_PATH)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_MEAN_VALUE, 127.5)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_STD_VALUE, 127.5)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_BACKEND_TYPE, MV_INFERENCE_BACKEND_OPENCV)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_TARGET_TYPE, MV_INFERENCE_TARGET_CPU)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_WIDTH, 128)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, 128)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_CHANNELS, 3)); + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_INPUT_NODE_NAME, inputNodeName)); + RET_IF_FAIL(mv_engine_config_set_array_string_attribute( + handle, MV_INFERENCE_OUTPUT_NODE_NAMES, outputNodeName, 1)); + return MEDIA_VISION_ERROR_NONE; } int perform_facial_landmark_detection() { int err = MEDIA_VISION_ERROR_NONE; - - int sel_opt = 0; - const char *names[] = { "Configuration", - "Tflite(CPU) + TweakCNN", - "OPENCV(CPU) + TweakCNN", - "Hosted: TFLite(TweakCNN)", - "Hosted: TFLite(MediaPipe)", - "Prepare", - "Run", - "Back" }; - mv_engine_config_h engine_cfg = NULL; - mv_inference_h infer = NULL; - mv_source_h mvSource = NULL; - - 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 SRID 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_tflite_TweakCNN(&engine_cfg); - } break; - case 3: { - //perform CNN cascade - 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); - } - err = perform_opencv_cnncascade(&engine_cfg); - } break; - case 4: { - //perform Hosted 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_hosted_tflite_tweakCNN_128_config(&engine_cfg); - } break; - case 5: { - //perform Hosted MediaPipe 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_mediapipe_192_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. error code:%i\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_facial_landmark_detect( - mvSource, infer, NULL, _facial_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 Facial 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; - } - } + const char *names[] = { + "Tflite(CPU) + TweakCNN", + "OPENCV(CPU) + TweakCNN", + "Hosted: TFLite(TweakCNN)", + "Hosted: TFLite(MediaPipe)", + }; - 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; - } + RET_IF_FAIL(mv_create_engine_config(&engine_cfg)); - 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: { + err = perform_tflite_TweakCNN(engine_cfg); + } break; + case 2: { + err = perform_opencv_cnncascade(engine_cfg); + } break; + case 3: { + err = engine_config_hosted_tflite_cpu( + engine_cfg, FLD_TFLITE_WIEGHT_TWEAKCNN_128_PATH); + } break; + case 4: { + err = engine_config_hosted_tflite_cpu( + engine_cfg, FLD_TFLITE_WIEGHT_MEDIAPIPE_192_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_facial_landmark_engine; } - return MEDIA_VISION_ERROR_NONE; + RET_IF_FAIL(mv_inference_task_helper(engine_cfg, TASK_FLD)); + +clean_facial_landmark_engine: + RET_IF_FAIL(mv_destroy_engine_config(engine_cfg)); + return err; } int engine_config_hosted_tflite_cpu(mv_engine_config_h handle, -- 2.7.4