c132fddd0537ec061cc9ffe4db91884ecec1f8c3
[platform/core/csapi/tizenfx.git] / src / Tizen.MachineLearning.Train / Tizen.MachineLearning.Train / Model.cs
1 /*
2 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 using static Interop;
17 using System;
18 using System.IO;
19
20 namespace Tizen.MachineLearning.Train
21 {
22     /// <summary>
23     /// Constructs the neural network model
24     /// </summary>
25     /// <remarks>
26     /// Use this function to create neural network model.
27     /// The Model class provides interfaces to construct, complle, run, adding layer
28     /// and etc with neural networks.
29     /// If you want to access only internal storage by using this function,
30     /// you should add privilege %http://tizen.org/privilege/mediastorage. Or, if you
31     /// want to access only external storage by using this function, you should add
32     /// privilege %http://tizen.org/privilege/externalstorage. If you want to access
33     /// both storage, you must add all the privileges.
34     /// </remarks>
35     /// <since_tizen> 10 </since_tizen>
36     public class Model: IDisposable
37     {
38         private IntPtr _handle = IntPtr.Zero;
39         private bool _disposed = false;
40
41         /// <summary>
42         /// Constructs the neural network model.
43         /// </summary>
44         /// <since_tizen> 10 </since_tizen>
45         public Model()
46         {
47             NNTrainerError ret = Interop.Model.Construct(out _handle);
48             NNTrainer.CheckException(ret, "Failed to create model instance");
49         }
50
51         /// <summary>
52         /// Constructs the neural network model with the given configuration file.
53         /// </summary>
54         /// <param name="modelConf">The nntrainer model configuration file.</param>
55         /// <since_tizen> 10 </since_tizen>
56         public Model(string modelConf)
57         {
58             if (string.IsNullOrEmpty(modelConf))
59                 NNTrainer.CheckException(NNTrainerError.InvalidParameter, "modelConf is null");
60
61             NNTrainerError ret = Interop.Model.ConstructWithConf(modelConf, out _handle);
62             NNTrainer.CheckException(ret, "Failed to create model instance with modelConf");
63         }
64         /// <summary>
65         /// Destructor of Model
66         /// </summary>
67         /// <since_tizen> 10 </since_tizen>
68         ~Model()
69         {
70             Dispose(false);
71         }
72
73         /// <summary>
74         /// Releases any unmanaged resources used by this object.
75         /// </summary>
76         /// <since_tizen> 10 </since_tizen>
77         public void Dispose()
78         {
79             Dispose(true);
80             GC.SuppressFinalize(this);
81         }
82
83         /// <summary>
84         /// Releases any unmanaged resources used by this object including opened handle.
85         /// </summary>
86         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
87         /// <since_tizen> 10 </since_tizen>
88         protected virtual void Dispose(bool disposing)
89         {
90             if (_disposed)
91                 return;
92             if (disposing)
93             {
94                 // release managed object
95             }
96
97             // release unmanaged object
98             if (_handle != IntPtr.Zero)
99             {
100                 // Destroy the neural network model.
101                 NNTrainerError ret = Interop.Model.Destroy(_handle);
102                 NNTrainer.CheckException(ret, "Failed to destroy model instance");
103
104                 _handle = IntPtr.Zero;
105             }
106             _disposed = true;
107         }
108     } 
109 }