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.
18 using System.Collections;
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 TensorsInfo _tensorsInfo = null;
31 private ArrayList _dataList = null;
34 /// Creates a TensorsData instance with handle which is given by TensorsInfo.
36 /// <param name="handle">The handle of tensors data.</param>
37 /// <param name="info">The handle of tensors info. (Default: null)</param>
38 /// <param name="isFetch">The boolean value for fetching the data (Default: false)</param>
39 /// <since_tizen> 6 </since_tizen>
40 private TensorsData(IntPtr handle, TensorsInfo info, bool isFetch)
42 NNStreamer.CheckNNStreamerSupport();
43 NNStreamerError ret = NNStreamerError.None;
45 /* Set internal object */
51 ret = Interop.Util.GetTensorsCount(_handle, out count);
52 NNStreamer.CheckException(ret, "unable to get the count of TensorsData");
54 _dataList = new ArrayList(count);
58 for (int i = 0; i < count; ++i)
61 byte[] bufData = null;
64 ret = Interop.Util.GetTensorData(_handle, i, out raw_data, out size);
65 NNStreamer.CheckException(ret, "unable to get the buffer of TensorsData: " + i.ToString());
67 bufData = Interop.Util.IntPtrToByteArray(raw_data, size);
68 _dataList.Add(bufData);
73 for (int i = 0; i < count; ++i)
75 int size = info.GetTensorSize(i);
76 byte[] bufData = new byte[size];
78 _dataList.Add(bufData);
84 /// Destructor of the TensorsData instance
86 /// <since_tizen> 6 </since_tizen>
93 /// Gets the number of Tensor in TensorsData class
95 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
96 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
97 /// <since_tizen> 6 </since_tizen>
101 NNStreamer.CheckNNStreamerSupport();
103 return _dataList.Count;
108 /// Gets the tensors information.
110 /// <returns>The TensorsInfo instance</returns>
111 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
112 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
113 /// <since_tizen> 8 </since_tizen>
114 public TensorsInfo TensorsInfo
117 NNStreamer.CheckNNStreamerSupport();
124 /// Allocates a new TensorsData instance with the given tensors information.
126 /// <param name="info">TensorsInfo object which has Tensor information</param>
127 /// <returns>The TensorsInfo instance</returns>
128 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
129 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
130 /// <since_tizen> 8 </since_tizen>
131 public static TensorsData Allocate(TensorsInfo info)
133 NNStreamer.CheckNNStreamerSupport();
136 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, "TensorsInfo is null");
138 TensorsData retData = info.GetTensorsData();
143 /// Sets a tensor data to given index.
145 /// <param name="index">The index of the tensor.</param>
146 /// <param name="buffer">Raw tensor data to be set.</param>
147 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
148 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
149 /// <exception cref="ArgumentException">Thrown when the data is not valid.</exception>
150 /// <since_tizen> 6 </since_tizen>
151 public void SetTensorData(int index, byte[] buffer)
153 NNStreamer.CheckNNStreamerSupport();
156 CheckDataBuffer(index, buffer);
158 _dataList[index] = buffer;
162 /// Gets a tensor data to given index.
164 /// <param name="index">The index of the tensor.</param>
165 /// <returns>Raw tensor data</returns>
166 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
167 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
168 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
169 /// <since_tizen> 6 </since_tizen>
170 public byte[] GetTensorData(int index)
172 NNStreamer.CheckNNStreamerSupport();
176 return (byte[])_dataList[index];
180 /// Releases any unmanaged resources used by this object.
182 /// <since_tizen> 6 </since_tizen>
183 public void Dispose()
186 GC.SuppressFinalize(this);
190 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
192 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
193 protected virtual void Dispose(bool disposing)
200 // release managed object
203 // release unmanaged objects
204 if (_handle != IntPtr.Zero)
206 NNStreamerError ret = Interop.Util.DestroyTensorsData(_handle);
207 if (ret != NNStreamerError.None)
209 Log.Error(NNStreamer.TAG, "failed to destroy TensorsData object");
211 _handle = IntPtr.Zero;
216 internal IntPtr GetHandle()
221 internal void PrepareInvoke()
223 NNStreamerError ret = NNStreamerError.None;
224 int count = _dataList.Count;
226 for (int i = 0; i < count; ++i)
228 byte[] data = (byte[])_dataList[i];
229 ret = Interop.Util.SetTensorData(_handle, i, data, data.Length);
230 NNStreamer.CheckException(ret, "unable to set the buffer of TensorsData: " + i.ToString());
234 internal static TensorsData CreateFromNativeHandle(IntPtr dataHandle, IntPtr infoHandle, bool isFetch)
236 TensorsData retTensorsData = null;
238 if (infoHandle == IntPtr.Zero)
240 retTensorsData = new TensorsData(dataHandle, null, isFetch);
244 TensorsInfo info = TensorsInfo.ConvertTensorsInfoFromHandle(infoHandle);
245 retTensorsData = new TensorsData(dataHandle, info, isFetch);
248 return retTensorsData;
251 private void CheckIndex(int index)
253 if (index < 0 || index >= _dataList.Count)
255 string msg = "Invalid index [" + index + "] of the tensors";
256 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, msg);
260 private void CheckDataBuffer(int index, byte[] data)
264 string msg = "data is not valid";
265 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, msg);
268 if (index >= Tensor.SizeLimit)
270 string msg = "Max size of the tensors is " + Tensor.SizeLimit;
271 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.QuotaExceeded, msg);
274 if (_tensorsInfo != null)
276 if (index >= _tensorsInfo.Count)
278 string msg = "Current information has " + _tensorsInfo.Count + " tensors";
279 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.QuotaExceeded, msg);
282 int size = _tensorsInfo.GetTensorSize(index);
283 if (data.Length != size)
285 string msg = "Invalid buffer size, required size is " + size.ToString();
286 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, msg);