2 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 namespace Tizen.MachineLearning.Train
23 /// Creates a neural network layer.
26 /// Use this class to create neural network layer.
27 /// 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.
31 /// <since_tizen> 10 </since_tizen>
32 public class Layer: IDisposable
34 private IntPtr handle = IntPtr.Zero;
35 private bool disposed = false;
37 /// if false, model will be destroy layer handle
38 private bool hasOwnership = true;
41 /// Creates a neural network layer.
43 /// <param name="type">The nntrainer layer type.</param>
44 /// <since_tizen> 10 </since_tizen>
45 public Layer(NNTrainerLayerType type)
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}");
52 internal Layer(IntPtr handle, bool hasOwnership)
55 this.hasOwnership = hasOwnership;
59 /// Frees the neural network layer.
61 /// <since_tizen> 10 </since_tizen>
63 /// Use this method to destroy neural network layer. Fails if layer is owned by a model.
71 /// Releases any unmanaged resources used by this object.
73 /// <since_tizen> 10 </since_tizen>
77 GC.SuppressFinalize(this);
81 /// Releases any unmanaged resources used by this object including opened handle.
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)
91 // release managed object
97 Log.Error(NNTrainer.Tag, "Cannot destroy layer already added in a Model. Model will destroy this layer");
101 // release unmanaged object
102 if (handle != IntPtr.Zero)
104 // Destroy the neural network layer.
105 NNTrainerError ret = Interop.Layer.Destroy(handle);
106 NNTrainer.CheckException(ret, "Failed to destroy layer instance");
108 handle = IntPtr.Zero;
113 /// Sets the neural network layer Property.
116 /// Use this method to set neural network layer Property.
117 /// The input format of property must be 'key = value' format.
119 /// <param name="property">property for layer.</param>
120 /// <since_tizen> 10 </since_tizen>
121 public void SetProperty(params string[] property)
123 string propertyParams = null;
125 if (property.Length > 0) {
126 propertyParams = string.Join("|", property);
127 Log.Info(NNTrainer.Tag, "Set property:"+ propertyParams);
130 NNTrainerError ret = Interop.Layer.SetProperty(handle, propertyParams);
131 NNTrainer.CheckException(ret, "Failed to set property");
134 internal IntPtr GetHandle()
139 internal void RemoveOwnership()
141 this.hasOwnership = false;