[ML][Single] Implemented SingleShot.input member - set 85/252085/5
authorPiotr Kosko/Native/Web API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Mon, 25 Jan 2021 13:14:43 +0000 (14:14 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Tue, 26 Jan 2021 12:40:26 +0000 (12:40 +0000)
* added set feature for SingleShot.input
* added single gathering of SingleShot.input and output (wihout checking native layer every time)

[ACR] https://code.sec.samsung.net/jira/browse/TWDAPI-273

[Verification] Code compiles without errors.
Checked in Chrome console:
var model = tizen.ml.single.openModel("documents/mobilenet_v1_1.0_224_quant.tflite")
model.input
> TensorsInfo {_previouslyConstructed: true, _id: 1}

var newTI = new tizen.ml.TensorsInfo()
newTI.addTensorInfo("input", "UINT8", [3,224,224,1])
model.input = newTI;
model.input
> TensorsInfo {_previouslyConstructed: true, _id: 2}

/// calling getter multiple times return always the same TensorsInfo

Change-Id: I16c33a0ba515aba1efe2f5d93d0d3225b410aced

src/ml/js/ml_single.js
src/ml/ml_instance.cc
src/ml/ml_instance.h
src/ml/ml_single_manager.cc
src/ml/ml_single_manager.h
src/ml/ml_singleshot.cc
src/ml/ml_singleshot.h

index 40a00de319ad5eb08b4332d3643bd88bbe6fe1b5..2fdf5712fc29ee7deb2645383769f5644500d5c8 100755 (executable)
@@ -99,13 +99,41 @@ MachineLearningSingle.prototype.openModel = function() {
 // SingleShot interface (input & output)
 var ValidInputExceptions = ['TypeMismatchError', 'AbortError'];
 var SingleShot = function(id) {
+    var _input, _output;
     Object.defineProperties(this, {
         input: {
             get: function() {
-                var result = native_.callSync('MLSingleShotGetTensorsInfo', {
+                if (!_input) {
+                    var result = native_.callSync('MLSingleShotGetTensorsInfo', {
+                        id: this._id,
+                        getInputMode: true // true means gathering input information
+                    });
+                    if (native_.isFailure(result)) {
+                        throw native_.getErrorObjectAndValidate(
+                            result,
+                            ValidInputExceptions,
+                            AbortError
+                        );
+                    }
+                    _input = new TensorsInfo(result.id);
+                }
+                return _input;
+            },
+            set: function() {
+                var args = validator_.validateArgs(arguments, [
+                    {
+                        name: 'inTensorsInfo',
+                        type: types_.PLATFORM_OBJECT,
+                        values: TensorsInfo
+                    }
+                ]);
+
+                var nativeArgs = {
                     id: this._id,
-                    getInputMode: true // true means gathering input information
-                });
+                    inTensorsInfo: args.inTensorsInfo._id
+                };
+
+                var result = native_.callSync('MLSingleShotSetInputInfo', nativeArgs);
                 if (native_.isFailure(result)) {
                     throw native_.getErrorObjectAndValidate(
                         result,
@@ -113,28 +141,26 @@ var SingleShot = function(id) {
                         AbortError
                     );
                 }
-
-                return new TensorsInfo(result.id);
-            },
-            set: function(v) {
-                /* TODO*/
+                _input = args.inTensorsInfo;
             }
         },
         output: {
             get: function() {
-                var result = native_.callSync('MLSingleShotGetTensorsInfo', {
-                    id: this._id,
-                    getInputMode: false // false means gathering output information
-                });
-                if (native_.isFailure(result)) {
-                    throw native_.getErrorObjectAndValidate(
-                        result,
-                        ValidInputExceptions,
-                        AbortError
-                    );
+                if (!_output) {
+                    var result = native_.callSync('MLSingleShotGetTensorsInfo', {
+                        id: this._id,
+                        getInputMode: false // false means gathering output information
+                    });
+                    if (native_.isFailure(result)) {
+                        throw native_.getErrorObjectAndValidate(
+                            result,
+                            ValidInputExceptions,
+                            AbortError
+                        );
+                    }
+                    _output = new TensorsInfo(result.id);
                 }
-
-                return new TensorsInfo(result.id);
+                return _output.clone(); // return new copy to make it 'readonly'
             },
             set: function(v) {
                 /* readonly */
index f9fc8685780db43cec3ac2f21f1d26c158bd3e83..e4c78333f63ae66025aa62c848a3ef3d24bf6515 100644 (file)
@@ -106,6 +106,7 @@ MlInstance::MlInstance()
   // MachineLearningSingle::openModelAsync()
   // OpenModelSuccessCallback
   REGISTER_METHOD(MLSingleShotGetTensorsInfo);
+  REGISTER_METHOD(MLSingleShotSetInputInfo);
   // SingleShot::invoke()
   // SingleShot::getValue()
   // SingleShot::setValue()
@@ -713,6 +714,30 @@ void MlInstance::MLSingleShotGetTensorsInfo(const picojson::value& args, picojso
   ReportSuccess(out);
 }
 
+void MlInstance::MLSingleShotSetInputInfo(const picojson::value& args, picojson::object& out) {
+  ScopeLogger("args: %s", args.serialize().c_str());
+  CHECK_ARGS(args, kId, double, out);
+  CHECK_ARGS(args, kInTensorsInfo, double, out);
+
+  auto id = static_cast<int>(args.get(kId).get<double>());
+  auto inTensorId = static_cast<int>(args.get(kInTensorsInfo).get<double>());
+
+  TensorsInfo* in_tensors_info = GetTensorsInfoManager().GetTensorsInfo(inTensorId);
+  if (nullptr == in_tensors_info) {
+    LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Internal TensorsInfo error"), &out,
+                      ("Could not find TensorsInfo handle with given id: %d", inTensorId));
+    return;
+  }
+
+  auto ret = single_manager_.SetNativeInputInfo(id, in_tensors_info);
+  if (!ret) {
+    ReportError(ret, &out);
+    return;
+  }
+
+  ReportSuccess(out);
+}
+
 // SingleShot::invoke()
 
 // SingleShot::getValue()
index 5e9dd634cfc9b8842ab79434eac6d21fcf9b0ccb..95125fb54f7c7486ee9cbae5dbe9e0da14ddc60b 100644 (file)
@@ -67,6 +67,7 @@ class MlInstance : public common::ParsedInstance {
   // MachineLearningSingle::openModelAsync()
   // OpenModelSuccessCallback
   void MLSingleShotGetTensorsInfo(const picojson::value& args, picojson::object& out);
+  void MLSingleShotSetInputInfo(const picojson::value& args, picojson::object& out);
   // SingleShot::invoke()
   // SingleShot::getValue()
   // SingleShot::setValue()
index 4bc3d1a76743faeae758377ec6a812debf9740ed..5f7438d37bceb1978789e2c7a771fe0d079bc961 100644 (file)
@@ -87,6 +87,25 @@ PlatformResult SingleManager::GetNativeTensorsInfo(int id, bool get_input_mode,
   return PlatformResult{};
 }
 
+PlatformResult SingleManager::SetNativeInputInfo(int id, TensorsInfo* inTensorsInfo) {
+  ScopeLogger();
+
+  SingleShot* single = GetSingleShot(id);
+  if (!single) {
+    LoggerE("Could not find singleShot handle");
+    return PlatformResult(ErrorCode::ABORT_ERR);
+  }
+
+  ml_tensors_info_h in_info = inTensorsInfo ? inTensorsInfo->Handle() : nullptr;
+
+  PlatformResult ret = single->SetInputInfo(in_info);
+  if (!ret) {
+    return ret;
+  }
+
+  return PlatformResult{};
+}
+
 // SingleShot::invoke()
 // SingleShot::getValue()
 // SingleShot::setValue()
index bf9837391b5c5f7ce9f5bed35efa6fdaa79374cc..2edbb8e7967ce5bdab4521ea63aa765f1348de25 100644 (file)
@@ -44,6 +44,7 @@ class SingleManager {
   // MachineLearningSingle::openModelAsync()
   // OpenModelSuccessCallback
   PlatformResult GetNativeTensorsInfo(int id, bool get_input_mode, int* res_id);
+  PlatformResult SetNativeInputInfo(int id, TensorsInfo* inTensorsInfo);
   // SingleShot::invoke()
   // SingleShot::getValue()
   // SingleShot::setValue()
index beff9f1ea5b879406df3b539bf650f6d373f0a91..9a4c96cc1d96cb524973d83bc07faed8178363ed 100644 (file)
@@ -63,6 +63,17 @@ PlatformResult SingleShot::GetTensorsInfo(bool get_input_mode, ml_tensors_info_h
   return PlatformResult{};
 }
 
+PlatformResult SingleShot::SetInputInfo(ml_tensors_info_h in_info) {
+  ScopeLogger();
+  int ret = ml_single_set_input_info(handle_, in_info);
+  if (ML_ERROR_NONE != ret) {
+    LoggerE("ml_single_set_input_info failed: %d (%s)", ret, get_error_message(ret));
+    return util::ToPlatformResult(ret, "Failed to set input info");
+  }
+
+  return PlatformResult{};
+}
+
 // SingleShot::invoke()
 
 // SingleShot::getValue()
index a78d955dbe70fe3bb1f4e054c73a793cf7344aeb..35c18b4f9752323ba6ee803102b7c3f5b9eac180 100644 (file)
@@ -42,7 +42,7 @@ class SingleShot {
   ~SingleShot();
 
   PlatformResult GetTensorsInfo(bool get_input_mode, ml_tensors_info_h* result);
-
+  PlatformResult SetInputInfo(ml_tensors_info_h in_info);
   // SingleShot::invoke()
 
   // SingleShot::getValue()