class GVA::Tensor#

Overview#

This class represents tensor - map-like storage for inference result information, such as output blob description (output layer dims, layout, rank, precision, etc.), inference result in a raw and interpreted forms. Tensor is based on GstStructure and, in general, can contain arbitrary (user-defined) fields of simplest data types, like integers, floats & strings. Tensor can contain raw inference result (such Tensor is produced by gvainference in Gstreamer pipeline), detection result (such Tensor is produced by gvadetect in Gstreamer pipeline and it’s called detection Tensor), or both raw & interpreted inference results (such Tensor is produced by gvaclassify in Gstreamer pipeline). Tensors can be created and used on their own, or they can be created within RegionOfInterest or VideoFrame instances. Usually, in Gstreamer pipeline with GVA elements (gvadetect, gvainference, gvaclassify) Tensor objects will be available for access and modification from RegionOfInterest and VideoFrame instances. More…

#include <tensor.h>

class Tensor {
public:
    // enums

    enum Layout;
    enum Precision;

    // construction

    Tensor(GstStructure* structure);

    // methods

    template <class T>
    const std::vector<T> data() const;

    void set_data(const void* buffer, size_t size);
    std::vector<guint> dims() const;
    void set_dims(const std::vector<guint>& dims);
    Precision precision() const;
    void set_precision(const Precision precision);
    Layout layout() const;
    void set_layout(const Layout layout);
    std::string layer_name() const;
    void set_layer_name(const std::string& name);
    std::string model_name() const;
    void set_model_name(const std::string& name);
    std::string format() const;
    void set_format(const std::string& format);
    std::string type() const;
    void set_type(const std::string& type);
    std::string name() const;
    void set_name(const std::string& name);
    double confidence() const;
    void set_confidence(const double confidence);
    std::string label() const;
    void set_label(const std::string& label);
    std::vector<std::string> fields() const;
    bool has_field(const std::string& field_name) const;

    std::string get_string(
        const std::string& field_name,
        const std::string& default_value = std::string()
    ) const;

    int get_int(const std::string& field_name, int32_t default_value = 0) const;
    double get_double(const std::string& field_name, double default_value = 0) const;

    template <typename T>
    std::vector<T> get_vector(const char* field_name) const;

    template <typename T>
    void set_vector(
        const char* field_name,
        const std::vector<T>& data
    );

    void set_string(const std::string& field_name, const std::string& value);
    void set_int(const std::string& field_name, int value);
    void set_uint64(const std::string& field_name, uint64_t value);
    void set_double(const std::string& field_name, double value);
    void set_bool(const std::string& field_name, bool value);
    std::string precision_as_string() const;
    std::string layout_as_string() const;
    std::string element_id() const;
    int label_id() const;
    bool is_detection() const;
    GstStructure* gst_structure() const;
    std::string to_string() const;

    bool convert_to_meta(
        GstAnalyticsMtd* mtd,
        GstAnalyticsODMtd* od_mtd,
        GstAnalyticsRelationMeta* meta
    );

    static GstStructure* convert_to_tensor(GstAnalyticsMtd mtd);
};

Detailed Documentation#

This class represents tensor - map-like storage for inference result information, such as output blob description (output layer dims, layout, rank, precision, etc.), inference result in a raw and interpreted forms. Tensor is based on GstStructure and, in general, can contain arbitrary (user-defined) fields of simplest data types, like integers, floats & strings. Tensor can contain raw inference result (such Tensor is produced by gvainference in Gstreamer pipeline), detection result (such Tensor is produced by gvadetect in Gstreamer pipeline and it’s called detection Tensor), or both raw & interpreted inference results (such Tensor is produced by gvaclassify in Gstreamer pipeline). Tensors can be created and used on their own, or they can be created within RegionOfInterest or VideoFrame instances. Usually, in Gstreamer pipeline with GVA elements (gvadetect, gvainference, gvaclassify) Tensor objects will be available for access and modification from RegionOfInterest and VideoFrame instances.

Construction#

Tensor(GstStructure* structure)

Construct Tensor instance from GstStructure. Tensor does not own structure, so if you use this constructor, free structure after Tensor ‘s lifetime, if needed.

Parameters:

structure

GstStructure to create Tensor instance from.

Methods#

template <class T>
const std::vector<T> data() const

Get raw inference output blob data.

Parameters:

T

type to interpret blob data

Returns:

vector of values of type T representing raw inference data, empty vector if data can’t be read

void set_data(const void* buffer, size_t size)

Set raw data buffer as inference output data.

Parameters:

buffer

with data element

size

of data buffer in bytes

std::vector<guint> dims() const

Get inference result blob dimensions info.

Returns:

vector of dimensions. Empty vector if dims are not set

void set_dims(const std::vector<guint>& dims)

Set inference result blob dimensions info.

Parameters:

dims

vector of dimensions

Precision precision() const

Get inference output blob precision.

Returns:

Enum Precision, Precision::UNSPECIFIED if can’t be read

void set_precision(const Precision precision)

Set inference output blob precision.

Parameters:

precision

of inference data buffer

Layout layout() const

Get inference result blob layout.

Returns:

Enum Layout, Layout::ANY if can’t be read

void set_layout(const Layout layout)

Set layout of output blob precision.

Parameters:

layout

of inference data buffer

std::string layer_name() const

Get inference result blob layer name.

Returns:

layer name as a string, empty string if failed to get

void set_layer_name(const std::string& name)

Set name of output blob layer.

Parameters:

name

of output blob layer

std::string model_name() const

Get model name which was used for inference.

Returns:

model name as a string, empty string if failed to get

void set_model_name(const std::string& name)

Set model name of output blob.

Parameters:

name

of output blob model

std::string format() const

Get data format as specified in model pre/post-processing json configuration.

Returns:

format as a string, empty string if failed to get

void set_format(const std::string& format)

Set inference output blob precision.

Parameters:

format

of inference data buffer

std::string type() const

Get tensor type as a string.

Returns:

Tensor instance’s type

void set_type(const std::string& type)

Set tensor type as a string.

Parameters:

type

of tensor data buffer

std::string name() const

Get tensor name as a string.

Returns:

Tensor instance’s name

void set_name(const std::string& name)

Set Tensor instance’s name.

Parameters:

name

of tensor instance

double confidence() const

Get confidence of detection or classification result extracted from the tensor.

Returns:

confidence of inference result as a double, 0 if failed to get

void set_confidence(const double confidence)

Set confidence of detection or classification result.

Parameters:

confidence

of inference result

std::string label() const

Get label. This label is set for Tensor instances produced by gvaclassify element. It will throw an exception if called for detection Tensor. To get detection class label, use RegionOfInterest::label.

Returns:

label as a string, empty string if failed to get

void set_label(const std::string& label)

Set label. It will throw an exception if called for detection Tensor.

Parameters:

label

label name as a string

std::vector<std::string> fields() const

Get vector of fields contained in Tensor instance.

Returns:

vector of fields contained in Tensor instance

bool has_field(const std::string& field_name) const

Check if Tensor instance has field.

Parameters:

field_name

field name

Returns:

True if field with this name is found, False otherwise

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.

Parameters:

field_name

field name

default_value

default value

Returns:

string value stored at field_name if field_name is found and contains a string, default_value string otherwise

int get_int(const std::string& field_name, int32_t default_value = 0) const

Get int contained in value stored at field_name.

Parameters:

field_name

field name

default_value

default value

Returns:

int value stored at field_name if field_name is found and contains an int, default_value otherwise

double get_double(const std::string& field_name, double default_value = 0) const

Get double contained in value stored at field_name.

Parameters:

field_name

field name

default_value

default value

Returns:

double value stored at field_name if field_name is found and contains an double, default_value otherwise

template <typename T>
std::vector<T> get_vector(const char* field_name) const

Get vector stored as GST_TYPE_ARRAY field.

Parameters:

field_name

name of GST_TYPE_ARRAY field to get

Returns:

vector with data of type T

template <typename T>
void set_vector(
    const char* field_name,
    const std::vector<T>& data
)

Set vector as GST_TYPE_ARRAY field.

Parameters:

field_name

name of GST_TYPE_ARRAY field to set

data

vector to set

void set_string(const std::string& field_name, const std::string& value)

Set field_name with string value.

Parameters:

field_name

field name

value

value to set

void set_int(const std::string& field_name, int value)

Set field_name with int value.

Parameters:

field_name

field name

value

value to set

void set_uint64(const std::string& field_name, uint64_t value)

Set field_name with uint64 value.

Parameters:

field_name

field name

value

value to set

void set_double(const std::string& field_name, double value)

Set field_name with double value.

Parameters:

field_name

field name

value

value to set

void set_bool(const std::string& field_name, bool value)

Set field_name with bool value.

Parameters:

field_name

field name

value

value to set

std::string precision_as_string() const

Get inference result blob precision as a string.

Returns:

precision as a string, “ANY” if can’t be read

std::string layout_as_string() const

Get inference result blob layout as a string.

Returns:

layout as a string, “ANY” if can’t be read

std::string element_id() const

Get inference-id property value of GVA element from which this Tensor came.

Returns:

inference-id property value of GVA element from which this Tensor came, empty string if failed to get

int label_id() const

Get label id.

Returns:

label id as an int, 0 if failed to get

bool is_detection() const

Check if this Tensor is detection Tensor (contains detection results)

Returns:

True if tensor contains detection results, False otherwise

GstStructure* gst_structure() const

Get ptr to underlying GstStructure.

Returns:

ptr to underlying GstStructure

std::string to_string() const

Returns a string representation of the underlying GstStructure.

Returns:

String with GstStructure contents.

bool convert_to_meta(
    GstAnalyticsMtd* mtd,
    GstAnalyticsODMtd* od_mtd,
    GstAnalyticsRelationMeta* meta
)

Convert tensor to GST analytic metadata.

Returns:

if conversion succesfull, ‘mtd’ is a handle to created metadata