GStreamer Video Analytics (GVA) Plugin
tensor.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2018-2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  ******************************************************************************/
6 
12 #ifndef __TENSOR_H__
13 #define __TENSOR_H__
14 
16 
17 #include <gst/gst.h>
18 #include <gst/video/gstvideometa.h>
19 
20 #include <stdexcept>
21 #include <string>
22 #include <vector>
23 
24 namespace GVA {
25 
38 class Tensor {
39  friend class VideoFrame;
40 #ifdef AUDIO
41  friend class AudioFrame;
42 #endif
43 
44  public:
48  enum class Precision {
52  };
53 
57  enum class Layout {
61  NC = GVA_LAYOUT_NC
62  };
63 
69  template <class T>
70  const std::vector<T> data() const {
71  gsize size = 0;
72  const void *data = gva_get_tensor_data(_structure, &size);
73  if (!data || !size)
74  return std::vector<T>();
75  return std::vector<T>((T *)data, (T *)((char *)data + size));
76  }
77 
82  std::vector<guint> dims() const {
83  GValueArray *arr = NULL;
84  gst_structure_get_array(_structure, "dims", &arr);
85  std::vector<guint> dims;
86  if (arr) {
87  for (guint i = 0; i < arr->n_values; ++i)
88  dims.push_back(g_value_get_uint(g_value_array_get_nth(arr, i)));
89  g_value_array_free(arr);
90  }
91  return dims;
92  }
93 
98  Precision precision() const {
99  if (has_field("precision"))
100  return (Precision)get_int("precision");
101  else
102  return Precision::UNSPECIFIED;
103  }
104 
109  Layout layout() const {
110  if (has_field("layout"))
111  return (Layout)get_int("layout");
112  else
113  return Layout::ANY;
114  }
115 
120  std::string layer_name() const {
121  return get_string("layer_name");
122  }
123 
128  std::string model_name() const {
129  return get_string("model_name");
130  }
131 
136  std::string format() const {
137  return get_string("format");
138  }
139 
144  std::string name() const {
145  const gchar *name = gst_structure_get_name(_structure);
146  if (name)
147  return std::string(name);
148  return std::string{};
149  }
150 
155  double confidence() const {
156  return get_double("confidence");
157  }
158 
164  std::string label() const {
165  if (!this->is_detection())
166  return get_string("label");
167  else
168  throw std::runtime_error("Detection GVA::Tensor can't have label.");
169  }
170 
175  std::vector<std::string> fields() const {
176  std::vector<std::string> fields;
177  int fields_count = gst_structure_n_fields(_structure);
178  if (fields_count <= 0)
179  return fields;
180 
181  fields.reserve(fields_count);
182  for (int i = 0; i < fields_count; ++i)
183  fields.emplace_back(gst_structure_nth_field_name(_structure, i));
184  return fields;
185  }
186 
192  bool has_field(const std::string &field_name) const {
193  return gst_structure_has_field(_structure, field_name.c_str());
194  }
195 
203  std::string get_string(const std::string &field_name, const std::string &default_value = std::string()) const {
204  const gchar *val = gst_structure_get_string(_structure, field_name.c_str());
205  return (val) ? std::string(val) : default_value;
206  }
207 
214  int get_int(const std::string &field_name, int32_t default_value = 0) const {
215  gint val = default_value;
216  gst_structure_get_int(_structure, field_name.c_str(), &val);
217  return val;
218  }
219 
226  double get_double(const std::string &field_name, double default_value = 0) const {
227  double val = default_value;
228  gst_structure_get_double(_structure, field_name.c_str(), &val);
229  return val;
230  }
231 
237  void set_string(const std::string &field_name, const std::string &value) {
238  gst_structure_set(_structure, field_name.c_str(), G_TYPE_STRING, value.c_str(), NULL);
239  }
240 
246  void set_int(const std::string &field_name, int value) {
247  gst_structure_set(_structure, field_name.c_str(), G_TYPE_INT, value, NULL);
248  }
249 
255  void set_double(const std::string &field_name, double value) {
256  gst_structure_set(_structure, field_name.c_str(), G_TYPE_DOUBLE, value, NULL);
257  }
258 
262  void set_name(const std::string &name) {
263  gst_structure_set_name(_structure, name.c_str());
264  }
265 
270  void set_label(const std::string &label) {
271  if (!this->is_detection())
272  set_string("label", label);
273  else
274  throw std::runtime_error("Detection GVA::Tensor can't have label.");
275  }
276 
281  std::string precision_as_string() const {
282  Precision precision_value = precision();
283  switch (precision_value) {
284  case Precision::U8:
285  return "U8";
286  case Precision::FP32:
287  return "FP32";
288  default:
289  return "UNSPECIFIED";
290  }
291  }
292 
297  std::string layout_as_string() const {
298  Layout layout_value = layout();
299  switch (layout_value) {
300  case Layout::NCHW:
301  return "NCHW";
302  case Layout::NHWC:
303  return "NHWC";
304  case Layout::NC:
305  return "NC";
306  default:
307  return "ANY";
308  }
309  }
310 
315  std::string element_id() const {
316  return get_string("element_id");
317  }
318 
324  int label_id() const {
325  return get_int("label_id");
326  }
327 
332  bool is_detection() const {
333  return name() == "detection";
334  }
335 
341  Tensor(GstStructure *structure) : _structure(structure) {
342  if (not _structure)
343  throw std::invalid_argument("GVA::Tensor: structure is nullptr");
344  }
345 
350  GstStructure *gst_structure() const {
351  return _structure;
352  }
353 
354  protected:
358  GstStructure *_structure;
359 };
360 
361 } // namespace GVA
362 
363 #endif // __TENSOR_H__
GVA::Tensor::gst_structure
GstStructure * gst_structure() const
Get ptr to underlying GstStructure.
Definition: tensor.h:350
GVA::Tensor::precision
Precision precision() const
Get inference output blob precision.
Definition: tensor.h:98
GVA::Tensor::label
std::string label() const
Get label. This label is set for Tensor instances produced by gvaclassify element....
Definition: tensor.h:164
GVA_LAYOUT_ANY
@ GVA_LAYOUT_ANY
Definition: gva_tensor_meta.h:38
GVA::Tensor::Tensor
Tensor(GstStructure *structure)
Construct Tensor instance from GstStructure. Tensor does not own structure, so if you use this consrt...
Definition: tensor.h:341
GVA::Tensor::get_double
double get_double(const std::string &field_name, double default_value=0) const
Get double contained in value stored at field_name.
Definition: tensor.h:226
GVA::Tensor::Precision
Precision
Describes tensor precision.
Definition: tensor.h:48
GVA::Tensor::confidence
double confidence() const
Get confidence of detection or classification result extracted from the tensor.
Definition: tensor.h:155
GVA_PRECISION_U8
@ GVA_PRECISION_U8
Definition: gva_tensor_meta.h:31
GVA::Tensor::has_field
bool has_field(const std::string &field_name) const
Check if Tensor instance has field.
Definition: tensor.h:192
GVA::Tensor::layer_name
std::string layer_name() const
Get inference result blob layer name.
Definition: tensor.h:120
GVA::Tensor::set_label
void set_label(const std::string &label)
Set label. It will throw an exception if called for detection Tensor.
Definition: tensor.h:270
GVA::Tensor::Precision::UNSPECIFIED
@ UNSPECIFIED
GVA_LAYOUT_NHWC
@ GVA_LAYOUT_NHWC
Definition: gva_tensor_meta.h:40
GVA::Tensor::Precision::FP32
@ FP32
GVA::Tensor::Layout::NHWC
@ NHWC
GVA::AudioFrame
This class represents audio frame - object for working with AudioEvent and Tensor objects which belon...
Definition: audio_frame.h:42
GVA::Tensor::fields
std::vector< std::string > fields() const
Get vector of fields contained in Tensor instance.
Definition: tensor.h:175
GVA::Tensor::set_int
void set_int(const std::string &field_name, int value)
Set field_name with int value.
Definition: tensor.h:246
GVA::Tensor::layout_as_string
std::string layout_as_string() const
Get inference result blob layout as a string.
Definition: tensor.h:297
GVA::Tensor::format
std::string format() const
Get data format as specified in model pre/post-processing json configuration.
Definition: tensor.h:136
GVA::Tensor::label_id
int label_id() const
Get label id.
Definition: tensor.h:324
GVA::Tensor::get_int
int get_int(const std::string &field_name, int32_t default_value=0) const
Get int contained in value stored at field_name.
Definition: tensor.h:214
GVA::Tensor::element_id
std::string element_id() const
Get inference-id property value of GVA element from which this Tensor came.
Definition: tensor.h:315
GVA_PRECISION_FP32
@ GVA_PRECISION_FP32
Definition: gva_tensor_meta.h:30
GVA_LAYOUT_NC
@ GVA_LAYOUT_NC
Definition: gva_tensor_meta.h:41
gva_tensor_meta.h
This file contains helper functions to control _GstGVATensorMeta instances.
GVA::Tensor::_structure
GstStructure * _structure
ptr to GstStructure that contains all tensor (inference results) data & info.
Definition: tensor.h:358
GVA::Tensor::name
std::string name() const
Get tensor name as a string.
Definition: tensor.h:144
GVA::Tensor::data
const std::vector< T > data() const
Get raw inference output blob data.
Definition: tensor.h:70
GVA::Tensor::Precision::U8
@ U8
GVA::Tensor::is_detection
bool is_detection() const
Check if this Tensor is detection Tensor (contains detection results)
Definition: tensor.h:332
GVA::Tensor::Layout::NC
@ NC
GVA::Tensor
This class represents tensor - map-like storage for inference result information, such as output blob...
Definition: tensor.h:38
GVA::Tensor::get_string
std::string get_string(const std::string &field_name, const std::string &default_value=std::string()) const
Get string contained in value stored at field_name.
Definition: tensor.h:203
GVA::Tensor::dims
std::vector< guint > dims() const
Get inference result blob dimensions info.
Definition: tensor.h:82
gva_get_tensor_data
const void * gva_get_tensor_data(GstStructure *s, gsize *nbytes)
This function returns a pointer to the fixed array of tensor bytes.
Definition: gva_tensor_meta.h:50
GVA::Tensor::set_double
void set_double(const std::string &field_name, double value)
Set field_name with double value.
Definition: tensor.h:255
GVA::Tensor::Layout::ANY
@ ANY
GVA::Tensor::layout
Layout layout() const
Get inference result blob layout.
Definition: tensor.h:109
GVA::Tensor::set_string
void set_string(const std::string &field_name, const std::string &value)
Set field_name with string value.
Definition: tensor.h:237
GVA::VideoFrame
This class represents video frame - object for working with RegionOfInterest and Tensor objects which...
Definition: video_frame.h:43
GVA::Tensor::set_name
void set_name(const std::string &name)
Set Tensor instance's name.
Definition: tensor.h:262
GVA::Tensor::precision_as_string
std::string precision_as_string() const
Get inference result blob precision as a string.
Definition: tensor.h:281
GVA::Tensor::Layout::NCHW
@ NCHW
GVA::Tensor::model_name
std::string model_name() const
Get model name which was used for inference.
Definition: tensor.h:128
GVA_PRECISION_UNSPECIFIED
@ GVA_PRECISION_UNSPECIFIED
Definition: gva_tensor_meta.h:29
GVA::Tensor::Layout
Layout
Describes tensor layout.
Definition: tensor.h:57
GVA_LAYOUT_NCHW
@ GVA_LAYOUT_NCHW
Definition: gva_tensor_meta.h:39