From c8d416a603690821722d0e481dd55feb493b47bc Mon Sep 17 00:00:00 2001 From: Hyunil Date: Fri, 27 May 2022 12:06:45 +0900 Subject: [PATCH] [MachineLearning.Train] Add initial Layer class - Create Layer.cs and Interop.Layer.cs for Layer class - Add Layer(NNTrainerLayerType type) class to Layer.cs - Add Destroy() to Dispose() - Add NNTrainerLayerType to Commons.cs - Add ml_train_layer_create() to interop - Add ml_train_layer_destroy() to interop Signed-off-by: Hyunil --- .../Interop/Interop.Layer.cs | 33 ++++++ .../Tizen.MachineLearning.Train/Commons.cs | 132 +++++++++++++++++++++ .../Tizen.MachineLearning.Train/Layer.cs | 93 +++++++++++++++ 3 files changed, 258 insertions(+) create mode 100644 src/Tizen.MachineLearning.Train/Interop/Interop.Layer.cs create mode 100644 src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Layer.cs diff --git a/src/Tizen.MachineLearning.Train/Interop/Interop.Layer.cs b/src/Tizen.MachineLearning.Train/Interop/Interop.Layer.cs new file mode 100644 index 0000000..20395f8 --- /dev/null +++ b/src/Tizen.MachineLearning.Train/Interop/Interop.Layer.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 Layer + { + /* int ml_train_layer_create(ml_train_layer_h *layer, ml_train_layer_type_e type) */ + [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_layer_create")] + public static extern NNTrainerError Create(out IntPtr layerHandle, NNTrainerLayerType type); + + /* int ml_train_layer_destroy(ml_train_layer_h layer) */ + [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_layer_destroy")] + public static extern NNTrainerError Destroy(IntPtr layerHandle); + } +} \ No newline at end of file diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs index a50a9ef..ad203d2 100644 --- a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs +++ b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs @@ -75,6 +75,138 @@ namespace Tizen.MachineLearning.Train IniWithBin = 2 } + /// + /// Enumeration for the neural network layer type of NNTrainer. + /// + /// 10 + public enum NNTrainerLayerType + { + /// + /// Input Layer + /// + Input = 0, + /// + /// Fully Connected Layer + /// + FC = 1, + /// + /// Batch Normalization Layer + /// + BN = 2, + /// + /// Convolution 2D Layer + /// + Conv2D = 3, + /// + /// Pooling 2D Layer + /// + Pooling2D= 4, + /// + /// Flatten Layer + /// + Flatten = 5, + /// + /// Activation Layer + /// + Activation = 6, + /// + /// Addition Layer + /// + Addition = 7, + /// + /// Concat Layer + /// + Concat = 8, + /// + /// MultiOut Layer + /// + MultiOut = 9, + /// + /// Embedding Layer + /// + Embedding = 10, + /// + /// RNN Layer + /// + RNN = 11, + /// + /// LSTM Layer + /// + LSTM = 12, + /// + /// Split Layer + /// + Split = 13, + /// + /// GRU Layer + /// + GRU = 14, + /// + /// Permute Layer + /// + Permute = 15, + /// + /// Dropout Layer + /// + Dropout = 16, + /// + /// Backbone using NNStreamer + /// + BackboneNNStreamer = 17, + /// + /// Centroid KNN Layer + /// + CentroidKNN = 18, + /// + /// Convolution 1D Layer + /// + Conv1D = 19, + /// + /// LSTM Cell Layer + /// + LSTMCell = 20, + /// + /// GRU Cell Layer + /// + GRUCell = 21, + /// + /// RNN Cell Layer + /// + RNNCell = 22, + /// + /// ZoneoutLSTM Cell Layer + /// + ZoneoutLSTMCell = 23, + /// + /// Preprocess flip Layer + /// + PreprocessFlip = 300, + /// + /// Preprocess translate Layer + /// + PreprocessTranslate = 301, + /// + /// Preprocess L2Normalization Layer + /// + PreprocessL2Norm = 302, + /// + /// Mean Squared Error Loss Layer + /// + LoseMSE = 500, + /// + /// Cross Entropy with Sigmoid Loss Layer + /// + LossCrossEntropySigmoid = 501, + /// + /// Cross Entropy with Softmax Loss Layer + /// + LossCrossEntropySoftmax = 502, + /// + /// Unknown + /// + Unknown = 999 + } + internal static class NNTrainer { diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Layer.cs b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Layer.cs new file mode 100644 index 0000000..7734749 --- /dev/null +++ b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Layer.cs @@ -0,0 +1,93 @@ +/* +* 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 +{ + /// + /// Creates a neural network layer. + /// + /// + /// Use this function to create neural network layer. + /// If the function succeeds, layer must be released using Destroy(), if not added to a model. + /// If added to a model, layer is available until the model is released. + /// + /// 10 + public class Layer: IDisposable + { + private IntPtr handle = IntPtr.Zero; + private bool disposed = false; + + /// + /// Creates a neural network layer. + /// + /// The nntrainer layer type. + /// 10 + public Layer(NNTrainerLayerType type) + { + NNTrainerError ret = Interop.Layer.Create(out handle, type); + NNTrainer.CheckException(ret, "Failed to create model instance"); + } + /// + /// Frees the neural network layer. + /// + /// 10 + /// + /// Use this function to destroy neural network layer. Fails if layer is owned by a model. + /// + ~Layer() + { + 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 layer. + NNTrainerError ret = Interop.Layer.Destroy(handle); + NNTrainer.CheckException(ret, "Failed to destroy layer instance"); + + handle = IntPtr.Zero; + } + disposed = true; + } + } +} -- 2.7.4