[MachineLearning.Train] Add Tizen.MachineLearing.Train project for nntrainer
authorHyunil <hyunil46.park@samsung.com>
Tue, 19 Apr 2022 08:58:29 +0000 (17:58 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 23 Aug 2022 05:50:26 +0000 (14:50 +0900)
- This is a project for C# APIs used for train in machine learning
- Add Interop.Libraries.cs to use libcapi-nntrainer.so
- Add Interop.Model.cs for ml_train_model_construct() and ml_train_model_destroy() and add functions to Model.cs
- Add Commons.cs for NNTrainer that have NNTrainerError

Signed-off-by: Hyunil <hyunil46.park@samsung.com>
src/Tizen.MachineLearning.Train/Interop/Interop.Libraries.cs [new file with mode: 0644]
src/Tizen.MachineLearning.Train/Interop/Interop.Model.cs [new file with mode: 0644]
src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.csproj [new file with mode: 0644]
src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.sln [new file with mode: 0644]
src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs [new file with mode: 0644]
src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Model.cs [new file with mode: 0644]

diff --git a/src/Tizen.MachineLearning.Train/Interop/Interop.Libraries.cs b/src/Tizen.MachineLearning.Train/Interop/Interop.Libraries.cs
new file mode 100644 (file)
index 0000000..4644b29
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+internal static partial class Interop
+{
+    internal static partial class Libraries
+    {
+        public const string Nntrainer = "libcapi-nntrainer.so";
+    }
+}
diff --git a/src/Tizen.MachineLearning.Train/Interop/Interop.Model.cs b/src/Tizen.MachineLearning.Train/Interop/Interop.Model.cs
new file mode 100644 (file)
index 0000000..dea35c3
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Runtime.InteropServices;
+using Tizen.MachineLearning.Train;
+
+internal static partial class Interop
+{
+    internal static partial class Model
+    {
+        /* typedef int ml_train_model_construct(ml_train_model_h *model) */
+        [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_model_construct")]
+        public static extern NNTrainerError Construct(out IntPtr model_handle);
+
+        /* typedef int ml_train_model_destroy(ml_train_model_h model) */
+        [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_model_destroy")]
+        public static extern NNTrainerError Destroy(IntPtr model_handle);
+    }
+}
diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.csproj b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.csproj
new file mode 100644 (file)
index 0000000..d137199
--- /dev/null
@@ -0,0 +1,12 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>netstandard2.0</TargetFramework>
+  </PropertyGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\Tizen.Log\Tizen.Log.csproj"/>
+    <ProjectReference Include="..\Tizen\Tizen.csproj"/>
+    <ProjectReference Include="..\Tizen.System.Information\Tizen.System.Information.csproj"/>
+  </ItemGroup>
+
+</Project>
diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.sln b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.sln
new file mode 100644 (file)
index 0000000..d44d602
--- /dev/null
@@ -0,0 +1,22 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 16
+VisualStudioVersion = 16.0.30114.105
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tizen.MachineLearning.Train", "Tizen.MachineLearning.Train.csproj", "{EEC5226C-00A7-482A-956C-D81DB1CFE06A}"
+EndProject
+Global
+       GlobalSection(SolutionConfigurationPlatforms) = preSolution
+               Debug|Any CPU = Debug|Any CPU
+               Release|Any CPU = Release|Any CPU
+       EndGlobalSection
+       GlobalSection(SolutionProperties) = preSolution
+               HideSolutionNode = FALSE
+       EndGlobalSection
+       GlobalSection(ProjectConfigurationPlatforms) = postSolution
+               {EEC5226C-00A7-482A-956C-D81DB1CFE06A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+               {EEC5226C-00A7-482A-956C-D81DB1CFE06A}.Debug|Any CPU.Build.0 = Debug|Any CPU
+               {EEC5226C-00A7-482A-956C-D81DB1CFE06A}.Release|Any CPU.ActiveCfg = Release|Any CPU
+               {EEC5226C-00A7-482A-956C-D81DB1CFE06A}.Release|Any CPU.Build.0 = Release|Any CPU
+       EndGlobalSection
+EndGlobal
diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs
new file mode 100644 (file)
index 0000000..f7c7f58
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+* Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
+*
+* Licensed under the Apache License, Version 2.0 (the License);
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an AS IS BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using System.IO;
+using Tizen.Internals.Errors;
+
+namespace Tizen.MachineLearning.Train
+{
+    internal enum NNTrainerError
+    {
+        None = ErrorCode.None,
+        InvalidParameter = ErrorCode.InvalidParameter,
+        StreamsPipe = ErrorCode.StreamsPipe,
+        TryAgain = ErrorCode.TryAgain,
+        Unknown = ErrorCode.Unknown,
+        TimedOut = ErrorCode.TimedOut,
+        NotSupported = ErrorCode.NotSupported,
+        PermissionDenied = ErrorCode.PermissionDenied,
+        OutOfMemory = ErrorCode.OutOfMemory,
+        InvalidOperation = ErrorCode.InvalidOperation
+    }
+
+    internal static class NNTrainer
+    {
+        internal const string Tag = "Tizen.MachineLearning.Train";
+
+        internal static void CheckException(NNTrainerError error, string msg)
+        {
+            if (error != NNTrainerError.None)
+            {
+                Log.Error(NNTrainer.Tag, msg + ": " + error.ToString());
+                throw NNTrainerExceptionFactory.CreateException(error, msg);
+            }
+        }
+
+    }
+
+    internal class NNTrainerExceptionFactory
+    {
+        internal static Exception CreateException(NNTrainerError err, string msg)
+        {
+            Exception e;
+
+            switch (err)
+            {
+                case NNTrainerError.InvalidParameter:
+                e = new ArgumentException(msg);
+                break;
+
+                case NNTrainerError.NotSupported:
+                e = new NotSupportedException(msg);
+                break;
+
+                case NNTrainerError.PermissionDenied:
+                e = new UnauthorizedAccessException(msg);
+                break;
+
+                case NNTrainerError.TryAgain:
+                case NNTrainerError.Unknown:
+                case NNTrainerError.OutOfMemory:
+                e = new InvalidOperationException(msg);
+                break;
+
+                case NNTrainerError.TimedOut:
+                e = new TimeoutException(msg);
+                break;
+
+                default:
+                e = new InvalidOperationException(msg);
+                break;
+            }
+            return e;
+        }
+    }
+}
diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Model.cs b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Model.cs
new file mode 100644 (file)
index 0000000..66ecacc
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+* Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
+*
+* Licensed under the Apache License, Version 2.0 (the License);
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an AS IS BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+using static Interop;
+using System;
+using System.IO;
+
+namespace Tizen.MachineLearning.Train
+{
+    /// <summary>
+    /// Constructs the neural network model
+    /// </summary>
+    /// <remarks>
+    /// Use this function to create neural network model.
+    /// The Model class provides interfaces to construct, complle, run, adding layer
+    /// and etc with neural networks.
+    /// If you want to access only internal storage by using this function,
+    /// you should add privilege %http://tizen.org/privilege/mediastorage. Or, if you
+    /// want to access only external storage by using this function, you should add
+    /// privilege %http://tizen.org/privilege/externalstorage. If you want to access
+    /// both storage, you must add all the privileges.
+    /// </remarks>
+    /// <since_tizen> 10 </since_tizen>
+    public class Model: IDisposable
+    {
+        private IntPtr _handle = IntPtr.Zero;
+        private bool _disposed = false;
+
+        /// <summary>
+        /// Create a new Model instance.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public Model()
+        {
+            NNTrainerError ret = Interop.Model.Construct(out _handle);
+            NNTrainer.CheckException(ret, "Failed to create model instance");
+        }
+
+        /// <summary>
+        /// Destructor of Model
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        ~Model()
+        {
+            Dispose(false);
+        }
+
+        /// <summary>
+        /// Releases any unmanaged resources used by this object.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        /// <summary>
+        /// Releases any unmanaged resources used by this object including opened handle.
+        /// </summary>
+        /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
+        /// <since_tizen> 10 </since_tizen>
+        protected virtual void Dispose(bool disposing)
+        {
+            if (_disposed)
+                return;
+            if (disposing)
+            {
+                // release managed object
+            }
+
+            // release unmanaged object
+            if (_handle != IntPtr.Zero)
+            {
+                // Destroy the neural network model.
+                NNTrainerError ret = Interop.Model.Destroy(_handle);
+                NNTrainer.CheckException(ret, "Failed to destroy model instance");
+
+                _handle = IntPtr.Zero;
+            }
+            _disposed = true;
+        }
+    } 
+}