From 63851a52c62aaf416345fad757b2b753f8c3e3da Mon Sep 17 00:00:00 2001 From: Hyunil Date: Tue, 19 Apr 2022 17:58:29 +0900 Subject: [PATCH] [MachineLearning.Train] Add Tizen.MachineLearing.Train project for nntrainer - 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 --- .../Interop/Interop.Libraries.cs | 23 ++++++ .../Interop/Interop.Model.cs | 33 ++++++++ .../Tizen.MachineLearning.Train.csproj | 12 +++ .../Tizen.MachineLearning.Train.sln | 22 +++++ .../Tizen.MachineLearning.Train/Commons.cs | 90 ++++++++++++++++++++ .../Tizen.MachineLearning.Train/Model.cs | 96 ++++++++++++++++++++++ 6 files changed, 276 insertions(+) create mode 100644 src/Tizen.MachineLearning.Train/Interop/Interop.Libraries.cs create mode 100644 src/Tizen.MachineLearning.Train/Interop/Interop.Model.cs create mode 100644 src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.csproj create mode 100644 src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.sln create mode 100644 src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs create mode 100644 src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Model.cs diff --git a/src/Tizen.MachineLearning.Train/Interop/Interop.Libraries.cs b/src/Tizen.MachineLearning.Train/Interop/Interop.Libraries.cs new file mode 100644 index 0000000..4644b29 --- /dev/null +++ b/src/Tizen.MachineLearning.Train/Interop/Interop.Libraries.cs @@ -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 index 0000000..dea35c3 --- /dev/null +++ b/src/Tizen.MachineLearning.Train/Interop/Interop.Model.cs @@ -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 index 0000000..d137199 --- /dev/null +++ b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.csproj @@ -0,0 +1,12 @@ + + + + netstandard2.0 + + + + + + + + diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.sln b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.sln new file mode 100644 index 0000000..d44d602 --- /dev/null +++ b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train.sln @@ -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 index 0000000..f7c7f58 --- /dev/null +++ b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs @@ -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 index 0000000..66ecacc --- /dev/null +++ b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Model.cs @@ -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 +{ + /// + /// Constructs the neural network model + /// + /// + /// 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. + /// + /// 10 + public class Model: IDisposable + { + private IntPtr _handle = IntPtr.Zero; + private bool _disposed = false; + + /// + /// Create a new Model instance. + /// + /// 10 + public Model() + { + NNTrainerError ret = Interop.Model.Construct(out _handle); + NNTrainer.CheckException(ret, "Failed to create model instance"); + } + + /// + /// Destructor of Model + /// + /// 10 + ~Model() + { + Dispose(false); + } + + /// + /// Releases any unmanaged resources used by this object. + /// + /// 10 + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Releases any unmanaged resources used by this object including opened handle. + /// + /// If true, disposes any disposable objects. If false, does not dispose disposable objects. + /// 10 + 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; + } + } +} -- 2.7.4