[MachineLearning.Inference] new API to set/get the property (#1414)
authorjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Wed, 25 Mar 2020 06:26:09 +0000 (15:26 +0900)
committerGitHub <noreply@github.com>
Wed, 25 Mar 2020 06:26:09 +0000 (15:26 +0900)
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 <jy1210.jung@samsung.com>
src/Tizen.MachineLearning.Inference/Interop/Interop.Nnstreamer.cs
src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/SingleShot.cs

index c18d98d..d82ab6c 100755 (executable)
@@ -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;
+        }
     }
 }
index 7ddfb17..a90ee95 100755 (executable)
@@ -179,6 +179,72 @@ namespace Tizen.MachineLearning.Inference
             NNStreamer.CheckException(ret, "fail to set the timeout!");
         }
 
+        /// <summary> Sets the property value for the given model.
+        /// <para>A model/framework may support changing the model information, such as tensor dimension and data layout, after opening the model.</para>
+        /// <para>If tries to change unavailable property or the model does not allow changing the information, this will raise an exception.</para>
+        /// <para>For the details about the properties, see 'tensor_filter' plugin definition in <a href="https://github.com/nnsuite/nnstreamer">NNStreamer</a>.</para>
+        /// </summary>
+        /// <param name="name">The property name</param>
+        /// <param name="value">The property value</param>
+        /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
+        /// <exception cref="NotSupportedException">Thrown when the feature is not supported, or given property is not available.</exception>
+        /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
+        /// <since_tizen> 8 </since_tizen>
+        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.");
+            }
+        }
+
+        /// <summary>
+        /// Gets the property value for the given model.
+        /// </summary>
+        /// <param name="name">The property name</param>
+        /// <returns>The property value</returns>
+        /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
+        /// <exception cref="NotSupportedException">Thrown when the feature is not supported, or given property is not available.</exception>
+        /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
+        /// <since_tizen> 8 </since_tizen>
+        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);
+        }
+
         /// <summary>
         /// Destructor of the Single instance.
         /// </summary>