[MachineLearning.Train] Add initial Dataset class
authorHyunil <hyunil46.park@samsung.com>
Fri, 17 Jun 2022 00:07:06 +0000 (09:07 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 23 Aug 2022 05:50:26 +0000 (14:50 +0900)
- Create Dataset.cs and Interop.Dataset.cs for Dataset class
- Add Dataset() class to Dataset.cs
- Add Destroy() to Dispose()
- Add ml_train_dataset_create() to interop
- Add ml_train_dataset_destroy() to interop
- Add AddFile(NNTrainerDatasetMode mode, string file)
- Add ml_train_dataset_add_file() to interop

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

diff --git a/src/Tizen.MachineLearning.Train/Interop/Interop.Dataset.cs b/src/Tizen.MachineLearning.Train/Interop/Interop.Dataset.cs
new file mode 100644 (file)
index 0000000..53f6e4d
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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 Dataset
+    {
+        /* int ml_train_dataset_create(ml_train_dataset_h *dataset) */
+        [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_dataset_create")]
+        public static extern NNTrainerError Create(out IntPtr datasetHandle);
+
+        /* int ml_train_dataset_destroy(ml_train_dataset_h dataset) */
+        [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_dataset_destroy")]
+        public static extern NNTrainerError Destroy(IntPtr datasetHandle);
+
+        /* int ml_train_dataset_add_file(ml_train_dataset_h dataset, ml_train_dataset_mode_e mode, const char *file) */
+        [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_dataset_add_file")]
+        internal static extern NNTrainerError AddFile(IntPtr datasetHandle, NNTrainerDatasetMode mode, string file);
+    }
+}
diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Dataset.cs b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Dataset.cs
new file mode 100644 (file)
index 0000000..fe881f1
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+* 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 dataset.
+    /// </summary>
+    /// <remarks>
+    /// Use this function to create a dataset. dataset should be released using Dispose().
+    /// dataset is available until the model is released.
+    /// </remarks>
+    /// <since_tizen> 10 </since_tizen>
+    public class Dataset: IDisposable
+    {
+        private IntPtr handle = IntPtr.Zero;
+        private bool disposed = false;
+
+        /// <summary>
+        ///  Constructs the dataset.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public Dataset()
+        {
+            NNTrainerError ret = Interop.Dataset.Create(out handle);
+            NNTrainer.CheckException(ret, "Failed to create dataset instance");
+            Log.Info(NNTrainer.Tag, "Create Dataset");
+        }
+        /// <summary>
+        /// Frees the neural network dataset.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        /// <remarks>
+        /// Use this function to destroy dataset. Fails if dataset is owned by a model.
+        /// </remarks>
+        ~Dataset()
+        {
+            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 dataset.
+                NNTrainerError ret = Interop.Dataset.Destroy(handle);
+                NNTrainer.CheckException(ret, "Failed to destroy dataset instance");
+                Log.Info(NNTrainer.Tag, "Destroy Dataset");
+
+                handle = IntPtr.Zero;
+            }
+            disposed = true;
+        }
+
+        /// <summary>
+        /// Adds data file to dataset.
+        /// </summary>
+        /// <remarks>
+        /// Use this function to add a data file from where data is retrieved.
+        /// 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 can access both
+        /// storage, you must add all privilege
+        /// </remarks>
+        /// <param name="mode">The phase where this generator should be used.</param>
+        /// <param name="file">file path.</param>
+        /// <since_tizen> 10 </since_tizen>
+        public void AddFile(NNTrainerDatasetMode mode, string file)
+        {
+            if (string.IsNullOrEmpty(file))
+                NNTrainer.CheckException(NNTrainerError.InvalidParameter, "file is null");
+
+            NNTrainerError ret = Interop.Dataset.AddFile(handle, mode, file);
+            NNTrainer.CheckException(ret, "Failed to add file");
+            Log.Info(NNTrainer.Tag, $"Add file{file}");
+        }
+
+        internal IntPtr GetHandle()
+        {
+            return handle;
+        }
+    } 
+}
index dab234d..e90fa23 100644 (file)
@@ -82,7 +82,7 @@ namespace Tizen.MachineLearning.Train
             // release unmanaged object
             if (handle != IntPtr.Zero)
             {
-                // Destroy the neural network layer.
+                // Destroy optimizer.
                 NNTrainerError ret = Interop.Optimizer.Destroy(handle);
                 NNTrainer.CheckException(ret, "Failed to destroy optimizer instance");