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 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.
30 /// <since_tizen> 10 </since_tizen>
31 public class Layer: IDisposable
33 private IntPtr handle = IntPtr.Zero;
34 private bool disposed = false;
37 /// Creates a neural network layer.
39 /// <param name="type">The nntrainer layer type.</param>
40 /// <since_tizen> 10 </since_tizen>
41 public Layer(NNTrainerLayerType type)
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}");
48 /// Frees the neural network layer.
50 /// <since_tizen> 10 </since_tizen>
52 /// Use this function to destroy neural network layer. Fails if layer is owned by a model.
60 /// Releases any unmanaged resources used by this object.
62 /// <since_tizen> 10 </since_tizen>
66 GC.SuppressFinalize(this);
70 /// Releases any unmanaged resources used by this object including opened handle.
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)
80 // release managed object
82 // release unmanaged object
83 if (handle != IntPtr.Zero)
85 // Destroy the neural network layer.
86 NNTrainerError ret = Interop.Layer.Destroy(handle);
87 NNTrainer.CheckException(ret, "Failed to destroy layer instance");
95 /// Sets the neural network layer Property.
98 /// Use this function to set neural network layer Property.
100 /// <param name="property">property for layer.</param>
101 /// <since_tizen> 10 </since_tizen>
102 public void SetProperty(params string[] property)
104 string propertyParams = null;
106 if (property.Length > 0) {
107 propertyParams = string.Join("|", property);
108 Log.Info(NNTrainer.Tag, "Set property:"+ propertyParams);
111 NNTrainerError ret = Interop.Layer.SetProperty(handle, propertyParams);
112 NNTrainer.CheckException(ret, "Failed to set property");