2 * Copyright (c) 2019 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.Inference
23 /// The TensorsData class sets and gets the buffer data for each Tensor.
25 /// <since_tizen> 6 </since_tizen>
26 public class TensorsData : IDisposable
28 private IntPtr _handle = IntPtr.Zero;
29 private bool _disposed = false;
30 private int _count = Tensor.InvalidCount;
33 /// Creates a TensorsInfo instance with handle which is given by TensorsInfo.
35 /// <param name="handle">The handle of tensors data.</param>
36 /// <since_tizen> 6 </since_tizen>
37 private TensorsData(IntPtr handle)
43 /// Destructor of the TensorsData instance
45 /// <since_tizen> 6 </since_tizen>
51 internal static TensorsData CreateFromNativeHandle(IntPtr handle)
53 TensorsData retTensorsData = new TensorsData(handle);
55 return retTensorsData;
59 /// Gets the number of Tensor in TensorsData class
61 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
62 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
63 /// <since_tizen> 6 </since_tizen>
67 NNStreamer.CheckNNStreamerSupport();
69 if (_count != Tensor.InvalidCount)
72 NNStreamerError ret = NNStreamerError.None;
75 ret = Interop.Util.GetTensorsCount(_handle, out count);
76 NNStreamer.CheckException(ret, "unable to get the count of TensorsData");
84 /// Sets a tensor data to given index.
86 /// <param name="index">The index of the tensor.</param>
87 /// <param name="buffer">Raw tensor data to be set.</param>
88 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
89 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
90 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
91 /// <since_tizen> 6 </since_tizen>
92 public void SetTensorData(int index, byte[] buffer)
94 NNStreamerError ret = NNStreamerError.None;
96 NNStreamer.CheckNNStreamerSupport();
100 string msg = "buffer is null";
101 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, msg);
104 ret = Interop.Util.SetTensorData(_handle, index, buffer, buffer.Length);
105 NNStreamer.CheckException(ret, "unable to set the buffer of TensorsData: " + index.ToString());
109 /// Gets a tensor data to given index.
111 /// <param name="index">The index of the tensor.</param>
112 /// <returns>Raw tensor data</returns>
113 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
114 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
115 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
116 /// <since_tizen> 6 </since_tizen>
117 public byte[] GetTensorData(int index)
119 byte[] retBuffer = null;
122 NNStreamerError ret = NNStreamerError.None;
124 NNStreamer.CheckNNStreamerSupport();
126 ret = Interop.Util.GetTensorData(_handle, index, out raw_data, out size);
127 NNStreamer.CheckException(ret, "unable to get the buffer of TensorsData: " + index.ToString());
129 retBuffer = Interop.Util.IntPtrToByteArray(raw_data, size);
135 /// Releases any unmanaged resources used by this object.
137 /// <since_tizen> 6 </since_tizen>
138 public void Dispose()
141 GC.SuppressFinalize(this);
145 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
147 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
148 protected virtual void Dispose(bool disposing)
155 // release managed object
158 // release unmanaged objects
159 if (_handle != IntPtr.Zero)
161 NNStreamerError ret = Interop.Util.DestroyTensorsData(_handle);
162 if (ret != NNStreamerError.None)
164 Log.Error(NNStreamer.TAG, "failed to destroy TensorsData object");
166 _handle = IntPtr.Zero;
171 internal IntPtr Handle
173 get { return _handle; }