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;

    std::vector<guint> dims() const;
    Precision precision() const;
    Layout layout() const;
    std::string layer_name() const;
    std::string model_name() const;
    std::string format() const;
    std::string name() const;
    double confidence() const;
    std::string label() const;
    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;
    void set_string(const std::string& field_name, const std::string& value);
    void set_int(const std::string& field_name, int value);
    void set_double(const std::string& field_name, double value);
    void set_name(const std::string& name);
    void set_label(const std::string& label);
    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;
};

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

std::vector<guint> dims() const

Get inference result blob dimensions info.

Returns:

vector of dimensions. Empty vector if dims are not set

Precision precision() const

Get inference output blob precision.

Returns:

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

Layout layout() const

Get inference result blob layout.

Returns:

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

std::string layer_name() const

Get inference result blob layer name.

Returns:

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

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

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

std::string name() const

Get tensor name as a string.

Returns:

Tensor instance’s name

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

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

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

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_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_name(const std::string& name)

Set Tensor instance’s name.

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::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