c4a903c72935cd7e4d2c9ce60c71d030e4b38959
[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 Destroy(), if not added to a model.
28     /// If added to a model, layer is available until the model is released.
29     /// </remarks>
30     /// <since_tizen> 10 </since_tizen>
31     public class Layer: IDisposable
32     {
33         private IntPtr handle = IntPtr.Zero;
34         private bool disposed = false;
35
36         /// <summary>
37         /// Creates a neural network layer.
38         /// </summary>
39         /// <param name="type">The nntrainer layer type.</param>
40         /// <since_tizen> 10 </since_tizen>
41         public Layer(NNTrainerLayerType type)
42         {
43             NNTrainerError ret = Interop.Layer.Create(out handle, type);
44             NNTrainer.CheckException(ret, "Failed to create model instance");
45             Log.Info(NNTrainer.Tag, $"Create layer with type:{type}");
46         }
47         /// <summary>
48         /// Frees the neural network layer.
49         /// </summary>
50         /// <since_tizen> 10 </since_tizen>
51         /// <remarks>
52         /// Use this function to destroy neural network layer. Fails if layer is owned by a model.
53         /// </remarks>
54         ~Layer()
55         {
56             Dispose(false);
57         }
58
59         /// <summary>
60         /// Releases any unmanaged resources used by this object.
61         /// </summary>
62         /// <since_tizen> 10 </since_tizen>
63         public void Dispose()
64         {
65             Dispose(true);
66             GC.SuppressFinalize(this);
67         }
68
69         /// <summary>
70         /// Releases any unmanaged resources used by this object including opened handle.
71         /// </summary>
72         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
73         /// <since_tizen> 10 </since_tizen>
74         protected virtual void Dispose(bool disposing)
75         {
76             if (disposed)
77                 return;
78             if (disposing)
79             {
80                 // release managed object
81             }
82             // release unmanaged object
83             if (handle != IntPtr.Zero)
84             {
85                 // Destroy the neural network layer.
86                 NNTrainerError ret = Interop.Layer.Destroy(handle);
87                 NNTrainer.CheckException(ret, "Failed to destroy layer instance");
88
89                 handle = IntPtr.Zero;
90             }
91             disposed = true;
92         }
93     } 
94 }