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.Generic;
19 using Log = Tizen.Log;
21 namespace Tizen.MachineLearning.Inference
24 /// The TensorsInfo class manages each Tensor information such as Name, Type and Dimension.
26 /// <since_tizen> 6 </since_tizen>
27 public class TensorsInfo : IDisposable
29 private List<TensorInfo> _infoList;
30 private IntPtr _handle = IntPtr.Zero;
31 private bool _disposed = false;
34 /// Get the number of Tensor information which is added.
36 /// <since_tizen> 6 </since_tizen>
37 public int Count => _infoList.Count;
40 /// Creates a TensorsInfo instance.
42 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
43 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
44 /// <since_tizen> 6 </since_tizen>
47 NNStreamer.CheckNNStreamerSupport();
49 Log.Info(NNStreamer.TAG, "TensorsInfo is created");
50 _infoList = new List<TensorInfo>();
54 /// Destroys the TensorsInfo resource.
56 /// <since_tizen> 6 </since_tizen>
63 /// Add a Tensor information to the TensorsInfo instance. Note that we support up to 16 tensors in TensorsInfo.
65 /// <param name="type">Data element type of Tensor.</param>
66 /// <param name="dimension">Dimension of Tensor. Note that we support up to 4th ranks.</param>
67 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
68 /// <exception cref="IndexOutOfRangeException">Thrown when the number of Tensor already exceeds the size limits (i.e. Tensor.SlzeLimit)</exception>
69 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
70 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
71 /// <since_tizen> 6 </since_tizen>
72 public void AddTensorInfo(TensorType type, int[] dimension)
74 NNStreamer.CheckNNStreamerSupport();
76 AddTensorInfo(null, type, dimension);
80 /// Add a Tensor information to the TensorsInfo instance. Note that we support up to 16 tensors in TensorsInfo.
82 /// <param name="name">Name of Tensor.</param>
83 /// <param name="type">Data element type of Tensor.</param>
84 /// <param name="dimension">Dimension of Tensor. Note that we support up to 4th ranks.</param>
85 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
86 /// <exception cref="IndexOutOfRangeException">Thrown when the number of Tensor already exceeds the size limits (i.e. Tensor.SlzeLimit)</exception>
87 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
88 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
89 /// <since_tizen> 6 </since_tizen>
90 public void AddTensorInfo(string name, TensorType type, int[] dimension)
92 NNStreamer.CheckNNStreamerSupport();
94 int idx = _infoList.Count;
95 if (idx >= Tensor.SizeLimit) {
96 throw new IndexOutOfRangeException("Max size of the tensors is " + Tensor.SizeLimit);
98 _infoList.Add(new TensorInfo(name, type, dimension));
100 if (_handle != IntPtr.Zero)
102 NNStreamerError ret = NNStreamerError.None;
104 ret = Interop.Util.SetTensorsCount(_handle, _infoList.Count);
105 NNStreamer.CheckException(ret, "unable to set the number of tensors");
107 ret = Interop.Util.SetTensorType(_handle, idx, type);
108 NNStreamer.CheckException(ret, "fail to set TensorsInfo type");
110 ret = Interop.Util.SetTensorDimension(_handle, idx, dimension);
111 NNStreamer.CheckException(ret, "fail to set TensorsInfo dimension");
116 /// Sets the tensor name with given index.
118 /// <param name="idx">The index of the tensor to be updated.</param>
119 /// <param name="name">The tensor name to be set.</param>
120 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
121 /// <exception cref="IndexOutOfRangeException">Thrown when the index is greater than the number of Tensor.</exception>
122 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
123 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
124 /// <since_tizen> 6 </since_tizen>
125 public void SetTensorName(int idx, string name)
127 NNStreamer.CheckNNStreamerSupport();
129 CheckIndexBoundary(idx);
130 _infoList[idx].Name = name;
132 if (_handle != IntPtr.Zero)
134 NNStreamerError ret = NNStreamerError.None;
135 ret = Interop.Util.SetTensorName(_handle, idx, name);
136 NNStreamer.CheckException(ret, "unable to set the name of tensor: " + idx.ToString());
141 /// Gets the tensor name with given index.
143 /// <param name="idx">The index of the tensor.</param>
144 /// <returns>The tensor name.</returns>
145 /// <exception cref="IndexOutOfRangeException">Thrown when the index is greater than the number of Tensor.</exception>
146 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
147 /// <since_tizen> 6 </since_tizen>
148 public string GetTensorName(int idx)
150 NNStreamer.CheckNNStreamerSupport();
152 CheckIndexBoundary(idx);
153 return _infoList[idx].Name;
157 /// Sets the tensor type with given index and its type.
159 /// <param name="idx">The index of the tensor to be updated.</param>
160 /// <param name="type">The tensor type to be set.</param>
161 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
162 /// <exception cref="IndexOutOfRangeException">Thrown when the index is greater than the number of Tensor.</exception>
163 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
164 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
165 /// <since_tizen> 6 </since_tizen>
166 public void SetTensorType(int idx, TensorType type)
168 NNStreamer.CheckNNStreamerSupport();
170 CheckIndexBoundary(idx);
171 _infoList[idx].Type = type;
173 if (_handle != IntPtr.Zero)
175 NNStreamerError ret = NNStreamerError.None;
176 ret = Interop.Util.SetTensorType(_handle, idx, type);
177 NNStreamer.CheckException(ret, "unable to set the type of tensor: " + idx.ToString());
182 /// Gets the tensor type with given index.
184 /// <param name="idx">The index of the tensor.</param>
185 /// <returns>The tensor type</returns>
186 /// <exception cref="IndexOutOfRangeException">Thrown when the index is greater than the number of Tensor.</exception>
187 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
188 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
189 /// <since_tizen> 6 </since_tizen>
190 public TensorType GetTensorType(int idx)
192 NNStreamer.CheckNNStreamerSupport();
194 CheckIndexBoundary(idx);
195 return _infoList[idx].Type;
199 /// Sets the tensor dimension with given index and dimension.
201 /// <param name="idx">The index of the tensor to be updated.</param>
202 /// <param name="dimension">The tensor dimension to be set.</param>
203 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
204 /// <exception cref="IndexOutOfRangeException">Thrown when the index is greater than the number of Tensor.</exception>
205 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
206 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
207 /// <since_tizen> 6 </since_tizen>
208 public void SetDimension(int idx, int[] dimension)
210 NNStreamer.CheckNNStreamerSupport();
212 CheckIndexBoundary(idx);
213 _infoList[idx].SetDimension(dimension);
215 if (_handle != IntPtr.Zero)
217 NNStreamerError ret = NNStreamerError.None;
218 ret = Interop.Util.SetTensorDimension(_handle, idx, dimension);
219 NNStreamer.CheckException(ret, "unable to set the dimension of tensor: " + idx.ToString());
224 /// Gets the tensor dimension with given index.
226 /// <param name="idx">The index of the tensor.</param>
227 /// <returns>The tensor dimension.</returns>
228 /// <exception cref="IndexOutOfRangeException">Thrown when the index is greater than the number of Tensor.</exception>
229 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
230 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
231 /// <since_tizen> 6 </since_tizen>
232 public int[] GetDimension(int idx)
234 NNStreamer.CheckNNStreamerSupport();
236 CheckIndexBoundary(idx);
237 return _infoList[idx].Dimension;
241 /// Creates a TensorsData instance based on informations of TensorsInfo
243 /// <returns>TensorsData instance</returns>
244 /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
245 /// <exception cref="ArgumentException">Thrown when the method failed due to TensorsInfo's information is invalid.</exception>
246 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
247 /// <since_tizen> 6 </since_tizen>
248 public TensorsData GetTensorsData()
250 IntPtr tensorsData_h = IntPtr.Zero;
251 TensorsData retTensorData;
252 NNStreamerError ret = NNStreamerError.None;
254 NNStreamer.CheckNNStreamerSupport();
256 if (_handle == IntPtr.Zero)
258 Log.Info(NNStreamer.TAG, "_handle is IntPtr.Zero\n" + " GetTensorsInfoHandle() is called");
259 GetTensorsInfoHandle();
262 ret = Interop.Util.CreateTensorsData(_handle, out tensorsData_h);
263 NNStreamer.CheckException(ret, "unable to create the tensorsData object");
265 retTensorData = TensorsData.CreateFromNativeHandle(tensorsData_h);
267 return retTensorData;
270 internal IntPtr GetTensorsInfoHandle()
272 NNStreamerError ret = NNStreamerError.None;
273 IntPtr ret_handle = IntPtr.Zero;
276 /* Already created */
277 if (_handle != IntPtr.Zero)
280 /* Check required parameters */
281 int num = _infoList.Count;
282 if (num <= 0 || num > Tensor.SizeLimit)
283 ret = NNStreamerError.InvalidParameter;
284 NNStreamer.CheckException(ret, "number of Tensor in TensorsInfo is invalid: " + _infoList.Count);
286 /* Create TensorsInfo object */
287 ret = Interop.Util.CreateTensorsInfo(out ret_handle);
288 NNStreamer.CheckException(ret, "fail to create TensorsInfo object");
290 /* Set the number of tensors */
291 ret = Interop.Util.SetTensorsCount(ret_handle, _infoList.Count);
292 NNStreamer.CheckException(ret, "unable to set the number of tensors");
294 /* Set each Tensor info */
296 foreach (TensorInfo t in _infoList)
298 ret = Interop.Util.SetTensorType(ret_handle, idx, t.Type);
299 NNStreamer.CheckException(ret, "fail to set the type of tensor" + idx.ToString());
301 ret = Interop.Util.SetTensorDimension(ret_handle, idx, t.Dimension);
302 NNStreamer.CheckException(ret, "fail to set the dimension of tensor: " + idx.ToString());
307 _handle = ret_handle;
312 /// Releases any unmanaged resources used by this object.
314 /// <since_tizen> 6 </since_tizen>
315 public void Dispose()
318 GC.SuppressFinalize(this);
322 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
324 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
325 protected virtual void Dispose(bool disposing)
332 // release managed objects
336 // release unmanaged objects
337 if (_handle != IntPtr.Zero)
339 NNStreamerError ret = Interop.Util.DestroyTensorsInfo(_handle);
341 if (ret != NNStreamerError.None)
343 Log.Error(NNStreamer.TAG, "failed to destroy TensorsInfo object");
349 private void CheckIndexBoundary(int idx)
351 if (idx < 0 || idx >= _infoList.Count) {
352 throw new IndexOutOfRangeException("Invalid index [" + idx + "] of the tensors");
356 private class TensorInfo
358 public TensorInfo(TensorType type, int[] dimension)
361 SetDimension(dimension);
364 public TensorInfo(string name, TensorType type, int[] dimension)
368 SetDimension(dimension);
371 public void SetDimension(int[] dimension)
373 if (dimension == null) {
374 throw new ArgumentException("Max size of the tensor rank is" + Tensor.RankLimit);
377 if (dimension.Length > Tensor.RankLimit) {
378 throw new ArgumentException("Max size of the tensor rank is" + Tensor.RankLimit);
380 Dimension = (int[])dimension.Clone();
383 public string Name { get; set; } = null;
385 public TensorType Type { get; set; } = TensorType.Int32;
387 public int[] Dimension { get; private set; } = new int[Tensor.RankLimit];