mv_inference: consider backward compatibility 86/232686/4
authorInki Dae <inki.dae@samsung.com>
Thu, 7 May 2020 06:50:20 +0000 (15:50 +0900)
committerInki Dae <inki.dae@samsung.com>
Thu, 7 May 2020 23:38:39 +0000 (08:38 +0900)
This patch guarantees backward compatibility of Mediavision C-API.

In new inference engine framework, inference target device types
can be combined together. So to keep backward compatibility with
Tizen 5.5, it adds a new definition, MV_INFERENCE_TARGET_DEVICE_TYPE
and check if Tizen app uses old type of inference target device
or not by checking default values of MV_INFERENCE_TARGET_TYPE
and MV_INFERENCE_TARGET_DEVICE_TYPE.

Change-Id: I31b2fee05ac74265569f3cdac3875879d349dc5d
Signed-off-by: Inki Dae <inki.dae@samsung.com>
include/mv_inference.h
media-vision-config.json
mv_inference/inference/src/Inference.cpp
mv_inference/inference/src/mv_inference_open.cpp

index e7ac0cc2c7017c6a5ed0cb77cd60feaa775cb6d3..97966d321ebe63084431393b56bf4b02467310b5 100644 (file)
@@ -105,24 +105,37 @@ extern "C" {
 #define MV_INFERENCE_BACKEND_TYPE "MV_INFERENCE_BACKEND_TYPE"
 
 /**
+ * @deprecated Deprecated since 6.0. Use #MV_INFERENCE_TARGET_TYPE instead.
  * @brief Defines #MV_INFERENCE_TARGET_TYPE to set the type used
  *        for device running attribute of the engine configuration.
  * @details Switches between CPU, GPU, or Custom:\n
  *          #MV_INFERENCE_TARGET_CPU (Deprecated),\n
  *          #MV_INFERENCE_TARGET_GPU (Deprecated),\n
  *          #MV_INFERENCE_TARGET_CUSTOM (Deprecated).\n
+ *
+ *          The default type is CPU.
+ *
+ * @since_tizen 5.5
+ * @see mv_engine_config_set_int_attribute()
+ * @see mv_engine_config_get_int_attribute()
+ */
+#define MV_INFERENCE_TARGET_TYPE "MV_INFERENCE_TARGET_TYPE"
+
+/**
+ * @brief Defines #MV_INFERENCE_TARGET_DEVICE_TYPE to set the type used
+ *        for device running attribute of the engine configuration.
+ * @details Switches between CPU, GPU, or Custom:\n
  *          #MV_INFERENCE_TARGET_DEVICE_CPU,\n
  *          #MV_INFERENCE_TARGET_DEVICE_GPU,\n
  *          #MV_INFERENCE_TARGET_DEVICE_CUSTOM.\n
  *
- *          The default type is CPU. Please do not use deprecated types since Tizen 6.0.
- *                     Old ones have been deprecated.
+ *          The default type is CPU.
  *
- * @since_tizen 5.5
+ * @since_tizen 6.0
  * @see mv_engine_config_set_int_attribute()
  * @see mv_engine_config_get_int_attribute()
  */
-#define MV_INFERENCE_TARGET_TYPE "MV_INFERENCE_TARGET_TYPE"
+#define MV_INFERENCE_TARGET_DEVICE_TYPE "MV_INFERENCE_TARGET_DEVICE_TYPE"
 
 /**
  * @brief Defines #MV_INFERENCE_INPUT_TENSOR_WIDTH to set the width
index 01f006b0d4a7d88c34c78cc2b1f9896cf3cd9d28..2116fb6b2a6e2594d57fc049bef9e3f2326da222 100644 (file)
             "type"  : "integer",
             "value" : 0
         },
+        {
+            "name"  : "MV_INFERENCE_TARGET_DEVICE_TYPE",
+            "type"  : "integer",
+            "value" : 1
+        },
         {
             "name"  : "MV_INFERENCE_MODEL_STD_VALUE",
             "type"  : "double",
index 0a5c572e037ee28db32473f8439f5eb50ddb8644..12cf8f7b31a4cbdf0053f77abbe25865ee7f8365 100755 (executable)
@@ -431,6 +431,8 @@ int Inference::ConfigureTargetTypes(const int targetType)
                return MEDIA_VISION_ERROR_INVALID_PARAMETER;
        }
 
+       LOGI("Before convering target types : %d", targetType);
+
        unsigned int new_type = MV_INFERENCE_TARGET_DEVICE_NONE;
 
        // Convert old type to new one.
@@ -446,7 +448,7 @@ int Inference::ConfigureTargetTypes(const int targetType)
                break;
        }
 
-       LOGI("target devices : %d", new_type);
+       LOGI("After convering target types : %d", new_type);
 
        mConfig.mTargetTypes = new_type;
 
index 68e891b23c145aef1a097d5e63c4c6c34b8d03e1..17c49be72582e2dec32cf8e192293595d805325f 100755 (executable)
 
 using namespace mediavision::inference;
 
-static bool is_new_mv_inference_engine(mv_engine_config_h engine_config)
+static int check_mv_inference_engine_version(mv_engine_config_h engine_config, bool *is_new_version)
 {
-       int dataType = 0;
+       int oldType = 0, newType = 0;
 
        int ret = mv_engine_config_get_int_attribute(engine_config,
-                                                                                       MV_INFERENCE_INPUT_DATA_TYPE,
-                                                                                       &dataType);
-       if (ret != MEDIA_VISION_ERROR_NONE) {
-               return false;
+                                                                                       MV_INFERENCE_TARGET_TYPE,
+                                                                                       &oldType);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               oldType = -1;
+
+       ret = mv_engine_config_get_int_attribute(engine_config,
+                                                                                       MV_INFERENCE_TARGET_DEVICE_TYPE,
+                                                                                       &newType);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               newType = -1;
+
+       // At least one of two target device types of
+       // media-vision-config.json file should have CPU device.
+       if (oldType == -1 && newType == -1)
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+
+       // If values of both types are changed then return an error.
+       // only one of two types should be used.
+       if (oldType != MV_INFERENCE_TARGET_CPU && newType != MV_INFERENCE_TARGET_DEVICE_CPU) {
+               LOGE("Please use only one of below two device types.");
+               LOGE("MV_INFERENCE_TARGET_TYPE(deprecated) or MV_INFERENCE_TARGET_DEVICE_TYPE(recommended).");
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
        }
 
-       return true;
+       LOGI("oldType = %d, newType = %d", oldType, newType);
+
+       // If default value of only old type is changed then use old type.
+       // Otherwise, use new type in following cases,
+       // - all default values of two types aren't changed.
+       //   (oldType == MV_INFERENCE_TARGET_CPU && newType == MV_INFERENCE_TARGET_DEVICE_CPU)
+       // - default value of only new type is changed.
+       //   (oldType == MV_INFERENCE_TARGET_CPU && (newType != -1 && newType != MV_INFERENCE_TARGET_DEVICE_CPU))
+       if ((oldType != -1 && oldType != MV_INFERENCE_TARGET_CPU) && newType == MV_INFERENCE_TARGET_DEVICE_CPU)
+               *is_new_version = false;
+       else
+               *is_new_version = true;
+
+       return MEDIA_VISION_ERROR_NONE;
 }
 
 mv_engine_config_h mv_inference_get_engine_config(mv_inference_h infer)
@@ -136,7 +167,7 @@ int mv_inference_configure_model_open(mv_inference_h infer, mv_engine_config_h e
        }
 
        ret = mv_engine_config_get_int_attribute(engine_config,
-                                                                                       MV_INFERENCE_TARGET_TYPE,
+                                                                                       MV_INFERENCE_TARGET_DEVICE_TYPE,
                                                                                        &targetTypes);
        if (ret != MEDIA_VISION_ERROR_NONE) {
                LOGE("Fail to get inference target type");
@@ -165,20 +196,27 @@ int mv_inference_configure_model_open(mv_inference_h infer, mv_engine_config_h e
                goto _ERROR_;
        }
 
+       bool is_new_version;
+
        // Check if new inference engine framework or old one.
        // new inference engine framework has different mv_inference_target_type_e enumeration values
        // to support multiple inference target devices. So in case of old version,
        // enumeration value given by user should be converted to new value, which
        // will be done at ConfigureTargetTypes callback internally.
-       if (is_new_mv_inference_engine(engine_config) == false) {
-               ret = pInfer->ConfigureTargetTypes(targetTypes);
-               if (ret != MEDIA_VISION_ERROR_NONE) {
+       // Ps. this function will be dropped with deprecated code version-after-next of Tizen.
+       ret = check_mv_inference_engine_version(engine_config, &is_new_version);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               goto _ERROR_;
+
+       if (is_new_version) {
+               // Use new type.
+               if (pInfer->ConfigureTargetDevices(targetTypes) != MEDIA_VISION_ERROR_NONE) {
                        LOGE("Tried to configure invalid target types.");
                        goto _ERROR_;
                }
        } else {
-               ret = pInfer->ConfigureTargetDevices(targetTypes);
-               if (ret != MEDIA_VISION_ERROR_NONE) {
+               // Convert old type to new one and then use it.
+               if (pInfer->ConfigureTargetTypes(targetTypes) != MEDIA_VISION_ERROR_NONE) {
                        LOGE("Tried to configure invalid target types.");
                        goto _ERROR_;
                }