From 09fd6eed2ddf17d73ae73a9a1f0fe5f2a1d41c72 Mon Sep 17 00:00:00 2001 From: jaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com> Date: Wed, 25 Mar 2020 15:26:09 +0900 Subject: [PATCH] [MachineLearning.Inference] new API to set/get the property (#1414) In SingleShot class, add new API to set/get the property. The property is related with the tensor information, a model may not support the change after open the model. Added: - void SetValue(string name, string value); - string GetValue(string name); Signed-off-by: Jaeyun --- .../Interop/Interop.Nnstreamer.cs | 13 +++++ .../Tizen.MachineLearning.Inference/SingleShot.cs | 66 ++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/src/Tizen.MachineLearning.Inference/Interop/Interop.Nnstreamer.cs b/src/Tizen.MachineLearning.Inference/Interop/Interop.Nnstreamer.cs index c18d98d..d82ab6c 100755 --- a/src/Tizen.MachineLearning.Inference/Interop/Interop.Nnstreamer.cs +++ b/src/Tizen.MachineLearning.Inference/Interop/Interop.Nnstreamer.cs @@ -58,6 +58,14 @@ internal static partial class Interop /* int ml_single_set_timeout (ml_single_h single, unsigned int timeout)*/ [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_set_timeout", CallingConvention = CallingConvention.Cdecl)] internal static extern NNStreamerError SetTimeout(IntPtr single_handle, int time_ms); + + /* int ml_single_set_property (ml_single_h single, const char *name, const char *value) */ + [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_set_property", CallingConvention = CallingConvention.Cdecl)] + internal static extern NNStreamerError SetValue(IntPtr single_handle, string name, string value); + + /* int ml_single_get_property (ml_single_h single, const char *name, char **value) */ + [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_get_property", CallingConvention = CallingConvention.Cdecl)] + internal static extern NNStreamerError GetValue(IntPtr single_handle, string name, out IntPtr value); } internal static partial class Util @@ -128,5 +136,10 @@ internal static partial class Interop Marshal.Copy(unmanagedByteArray, retByte, 0, size); return retByte; } + + internal static string IntPtrToString(IntPtr val) + { + return (val != IntPtr.Zero) ? Marshal.PtrToStringAnsi(val) : string.Empty; + } } } diff --git a/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/SingleShot.cs b/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/SingleShot.cs index 7ddfb17..a90ee95 100755 --- a/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/SingleShot.cs +++ b/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/SingleShot.cs @@ -179,6 +179,72 @@ namespace Tizen.MachineLearning.Inference NNStreamer.CheckException(ret, "fail to set the timeout!"); } + /// Sets the property value for the given model. + /// A model/framework may support changing the model information, such as tensor dimension and data layout, after opening the model. + /// If tries to change unavailable property or the model does not allow changing the information, this will raise an exception. + /// For the details about the properties, see 'tensor_filter' plugin definition in NNStreamer. + /// + /// The property name + /// The property value + /// http://tizen.org/feature/machine_learning.inference + /// Thrown when the feature is not supported, or given property is not available. + /// Thrown when the method failed due to an invalid parameter. + /// 8 + public void SetValue(string name, string value) + { + NNStreamerError ret = NNStreamerError.None; + + NNStreamer.CheckNNStreamerSupport(); + + /* Check the argument */ + if (string.IsNullOrEmpty(name)) + throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, "The property name is invalid"); + + if (string.IsNullOrEmpty(value)) + throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, "The property value is invalid"); + + ret = Interop.SingleShot.SetValue(_handle, name, value); + if (ret != NNStreamerError.None) + { + if (ret == NNStreamerError.NotSupported) + NNStreamer.CheckException(ret, "Failed to to set the property, the property name is not available."); + else + NNStreamer.CheckException(ret, "Failed to to set the property, the property value is invalid."); + } + } + + /// + /// Gets the property value for the given model. + /// + /// The property name + /// The property value + /// http://tizen.org/feature/machine_learning.inference + /// Thrown when the feature is not supported, or given property is not available. + /// Thrown when the method failed due to an invalid parameter. + /// 8 + public string GetValue(string name) + { + NNStreamerError ret = NNStreamerError.None; + IntPtr val = IntPtr.Zero; + + NNStreamer.CheckNNStreamerSupport(); + + /* Check the argument */ + if (string.IsNullOrEmpty(name)) + throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, "The property name is invalid"); + + ret = Interop.SingleShot.GetValue(_handle, name, out val); + if (ret != NNStreamerError.None) + { + if (ret == NNStreamerError.NotSupported) + NNStreamer.CheckException(ret, "Failed to to get the property, the property name is not available."); + else + NNStreamer.CheckException(ret, "Failed to to get the property, the property value is invalid."); + } + + return Interop.Util.IntPtrToString(val); + } + /// /// Destructor of the Single instance. /// -- 2.7.4