bc01c125da3738b1452382fab9e21b03f47acfd1
[platform/core/csapi/tizenfx.git] / src / Tizen.MachineLearning.Train / Tizen.MachineLearning.Train / Layer.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     /// Creates a neural network layer.
24     /// </summary>
25     /// <remarks>
26     /// Use this function to create neural network layer.
27     /// If the function succeeds, layer must be released using Dispose(), if not added to a model.
28     /// If added to a model by AddLayer method of Model, layer is available until the model is released. so
29     /// Dispose() must never be used.
30     /// </remarks>
31     /// <since_tizen> 10 </since_tizen>
32     public class Layer: IDisposable
33     {
34         private IntPtr handle = IntPtr.Zero;
35         private bool disposed = false;
36
37         /// if false, model will be destroy layer handle
38         private bool hasOwnership = true;
39
40         /// <summary>
41         /// Creates a neural network layer.
42         /// </summary>
43         /// <param name="type">The nntrainer layer type.</param>
44         /// <since_tizen> 10 </since_tizen>
45         public Layer(NNTrainerLayerType type)
46         {
47             NNTrainerError ret = Interop.Layer.Create(out handle, type);
48             NNTrainer.CheckException(ret, "Failed to create model instance");
49             Log.Info(NNTrainer.Tag, $"Create layer with type:{type}");
50         }
51
52         internal Layer(IntPtr handle, bool hasOwnership)
53         {
54             this.handle = handle;
55             this.hasOwnership = hasOwnership;
56         }
57
58         /// <summary>
59         /// Frees the neural network layer.
60         /// </summary>
61         /// <since_tizen> 10 </since_tizen>
62         /// <remarks>
63         /// Use this function to destroy neural network layer. Fails if layer is owned by a model.
64         /// </remarks>
65         ~Layer()
66         {
67             Dispose(false);
68         }
69
70         /// <summary>
71         /// Releases any unmanaged resources used by this object.
72         /// </summary>
73         /// <since_tizen> 10 </since_tizen>
74         public void Dispose()
75         {
76             Dispose(true);
77             GC.SuppressFinalize(this);
78         }
79
80         /// <summary>
81         /// Releases any unmanaged resources used by this object including opened handle.
82         /// </summary>
83         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
84         /// <since_tizen> 10 </since_tizen>
85         protected virtual void Dispose(bool disposing)
86         {
87             if (disposed)
88                 return;
89             if (disposing)
90             {
91                 // release managed object
92             }
93
94             disposed = true;
95
96             if (!hasOwnership){
97                 Log.Error(NNTrainer.Tag, "Cannot destroy layer already added in a Model. Model will destroy this layer");
98                 return;
99             }
100
101             // release unmanaged object
102             if (handle != IntPtr.Zero)
103             {
104                 // Destroy the neural network layer.
105                 NNTrainerError ret = Interop.Layer.Destroy(handle);
106                 NNTrainer.CheckException(ret, "Failed to destroy layer instance");
107
108                 handle = IntPtr.Zero;
109             }
110         }
111
112         /// <summary>
113         /// Sets the neural network layer Property.
114         /// </summary>
115         /// <remarks>
116         /// Use this function to set neural network layer Property.
117         /// The input format of property must be 'key = value' format.
118         /// </remarks>
119         /// <param name="property">property for layer.</param>
120         /// <since_tizen> 10 </since_tizen>
121         public void SetProperty(params string[] property)
122         {
123             string propertyParams = null;
124
125             if (property.Length > 0) {
126                 propertyParams = string.Join("|", property);
127                 Log.Info(NNTrainer.Tag, "Set property:"+ propertyParams);
128             }
129
130             NNTrainerError ret = Interop.Layer.SetProperty(handle, propertyParams);
131             NNTrainer.CheckException(ret, "Failed to set property");
132         }
133
134         internal IntPtr GetHandle()
135         {
136             return handle;
137         }
138
139         internal void RemoveOwnership()
140         {
141             this.hasOwnership = false;
142         }
143     } 
144 }