[ML][Single] Implemented SingleShot.input member - get 51/252051/2
authorPiotr Kosko/Native/Web API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Thu, 21 Jan 2021 12:09:41 +0000 (13:09 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Mon, 25 Jan 2021 10:11:11 +0000 (10:11 +0000)
[ACR] https://code.sec.samsung.net/jira/browse/TWDAPI-273

[Verification] Code compiles without errors.
Checked in Chrome console:
// open model
var model = tizen.ml.single.openModel("documents/mobilenet_v1_1.0_224_quant.tflite")
// all implemented members of input work
model.input.getDimensions(0)

Change-Id: I132d5e6262f7142c8040c7f40f69f11a1ef63827

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 22cf5ba94bcdc1c2d52a8a46ec5bfab1078e5be5..e79731dd6100efb592d6915f605a5486776f2772 100755 (executable)
@@ -97,8 +97,28 @@ MachineLearningSingle.prototype.openModel = function() {
 // OpenModelSuccessCallback
 
 // SingleShot interface (input & output)
+var ValidInputExceptions = ['TypeMismatch', 'AbortError'];
 var SingleShot = function(id) {
     Object.defineProperties(this, {
+        input: {
+            get: function() {
+                var result = native_.callSync('MLSingleShotGetInputInfo', {
+                    id: this._id
+                });
+                if (native_.isFailure(result)) {
+                    throw native_.getErrorObjectAndValidate(
+                        result,
+                        ValidInputExceptions,
+                        AbortError
+                    );
+                }
+
+                return new TensorsInfo(result.id);
+            },
+            set: function(v) {
+                /* TODO*/
+            }
+        },
         _id: { value: id, writable: false, enumerable: false }
     });
 };
index d2171deef14a6751e33890dd4ef19c211f9bab77..9fe01114386d5b26a190ebbb9dc555f63df5b606 100644 (file)
@@ -68,7 +68,7 @@ using namespace common;
   CHECK_EXIST(args, name, out)            \
   CHECK_TYPE(args, name, type, out)
 
-MlInstance::MlInstance() : pipeline_manager_{this} {
+MlInstance::MlInstance() : single_manager_{&tensors_info_manager_}, pipeline_manager_{this} {
   ScopeLogger();
   using namespace std::placeholders;
 
@@ -94,7 +94,7 @@ MlInstance::MlInstance() : pipeline_manager_{this} {
   REGISTER_METHOD(MLSingleManagerOpenModel);
   // MachineLearningSingle::openModelAsync()
   // OpenModelSuccessCallback
-  // SingleShot input
+  REGISTER_METHOD(MLSingleShotGetInputInfo);
   // SingleShot output
   // SingleShot::invoke()
   // SingleShot::getValue()
@@ -623,6 +623,22 @@ void MlInstance::MLSingleManagerOpenModel(const picojson::value& args, picojson:
 // OpenModelSuccessCallback
 
 // SingleShot input
+void MlInstance::MLSingleShotGetInputInfo(const picojson::value& args, picojson::object& out) {
+  ScopeLogger("args: %s", args.serialize().c_str());
+  CHECK_ARGS(args, kId, double, out);
+
+  auto id = static_cast<int>(args.get(kId).get<double>());
+
+  int res_id = -1;
+  auto ret = single_manager_.GetNativeInputInfo(id, &res_id);
+  if (!ret) {
+    ReportError(ret, &out);
+    return;
+  }
+
+  out["id"] = picojson::value(static_cast<double>(res_id));
+  ReportSuccess(out);
+}
 
 // SingleShot output
 
index ebcb32f6be7ff0fd0c6600b4790f62ace259089b..9619647bee27c719163d8370f0f2c77cf4fdf9d1 100644 (file)
@@ -59,7 +59,7 @@ class MlInstance : public common::ParsedInstance {
   void MLSingleManagerOpenModel(const picojson::value& args, picojson::object& out);
   // MachineLearningSingle::openModelAsync()
   // OpenModelSuccessCallback
-  // SingleShot input
+  void MLSingleShotGetInputInfo(const picojson::value& args, picojson::object& out);
   // SingleShot output
   // SingleShot::invoke()
   // SingleShot::getValue()
index 00f813ffd07064549b00aaa97379aeac2f9071de..0bb6a0a4216abc1ab9083021d5e41a35f31afe29 100644 (file)
@@ -23,7 +23,7 @@ using common::ErrorCode;
 namespace extension {
 namespace ml {
 
-SingleManager::SingleManager() : nextId_{0} {
+SingleManager::SingleManager(TensorsInfoManager* tim) : nextId_{0}, tim_{tim} {
   ScopeLogger();
 }
 
@@ -57,6 +57,35 @@ PlatformResult SingleManager::OpenModel(const std::string& modelPath, TensorsInf
 // MachineLearningSingle::openModelAsync()
 // OpenModelSuccessCallback
 // SingleShot input
+SingleShot* SingleManager::GetSingleShot(int id) {
+  ScopeLogger("id: %d", id);
+
+  if (singles_.end() != singles_.find(id)) {
+    return singles_[id].get();
+  }
+
+  return nullptr;
+}
+
+PlatformResult SingleManager::GetNativeInputInfo(int id, int* res_id) {
+  ScopeLogger();
+
+  SingleShot* single = GetSingleShot(id);
+  if (!single) {
+    LoggerE("Could not find singleShot handle");
+    return PlatformResult(ErrorCode::ABORT_ERR);
+  }
+
+  ml_tensors_info_h in_info = nullptr;
+  PlatformResult ret = single->GetInputInfo(&in_info);
+  if (!ret) {
+    return ret;
+  }
+
+  auto tensor_info = tim_->CreateTensorsInfo(in_info);
+  *res_id = tensor_info->Id();
+  return PlatformResult{};
+}
 // SingleShot output
 // SingleShot::invoke()
 // SingleShot::getValue()
index 034fbff63edd64c6d0fa5baecf9ebd1fe613637e..8dc38046623986c9844c6367830d0cf4d4dc1e18 100644 (file)
@@ -30,9 +30,10 @@ namespace ml {
 
 class SingleManager {
  public:
-  SingleManager();
+  SingleManager(TensorsInfoManager* tim);
   ~SingleManager();
 
+  SingleManager() = delete;
   SingleManager(const SingleManager&) = delete;
   SingleManager& operator=(const SingleManager&) = delete;
 
@@ -42,7 +43,7 @@ class SingleManager {
                            bool isDynamicMode, int* res_id);
   // MachineLearningSingle::openModelAsync()
   // OpenModelSuccessCallback
-  // SingleShot input
+  PlatformResult GetNativeInputInfo(int id, int* res_id);
   // SingleShot output
   // SingleShot::invoke()
   // SingleShot::getValue()
@@ -53,6 +54,8 @@ class SingleManager {
  private:
   int nextId_;
   std::map<int, std::unique_ptr<SingleShot>> singles_;
+  TensorsInfoManager* tim_;
+  SingleShot* GetSingleShot(int id);
 };
 
 }  // namespace ml
index a9fb3a07fd4eecb007af190bd583d8e68e96888a..6fbe25c6f7e684560a97c8ddf0a67a5382ed4590 100644 (file)
@@ -43,7 +43,18 @@ SingleShot::~SingleShot() {
   }
 }
 
-// SingleShot input
+PlatformResult SingleShot::GetInputInfo(ml_tensors_info_h* result) {
+  ScopeLogger();
+  ml_tensors_info_h info = nullptr;
+  int ret = ml_single_get_input_info(handle_, &info);
+  if (ML_ERROR_NONE != ret) {
+    LoggerE("ml_single_get_input_info failed: %d (%s)", ret, get_error_message(ret));
+    return util::ToPlatformResult(ret, "Failed to get tensor dimension");
+  }
+
+  *result = info;
+  return PlatformResult{};
+}
 
 // SingleShot output
 
index 0a02b0ee2dae504ee5eb591ce96815eb8fed0878..eb174f0cbfd3d79db221acba62d8e486d75d8fdc 100644 (file)
@@ -41,7 +41,7 @@ class SingleShot {
   SingleShot& operator=(SingleShot&& o) = delete;
   ~SingleShot();
 
-  // SingleShot input
+  PlatformResult GetInputInfo(ml_tensors_info_h* result);
 
   // SingleShot output