Tensor

Function Usage

Constructor of the Tensor class.

Prototype

1
Tensor::Tensor();   // Default constructor. When the construction fails, the "std::runtime_error" exception occurs.
1
Tensor::Tensor(const Tensor &other); // Supports copy constructor.
1
2
Tensor::Tensor(const std::vector<uint32_t> &shape, const MxBase::TensorDType &dataType, const int32_t &deviceId = -1);
// No memory is transferred. It can be used together with Malloc to allocate memory, and the allocated memory does not need to be managed or destroyed by a user. When the construction fails, the "std::runtime_error" exception occurs.
1
2
Tensor::Tensor(void* usrData,const std::vector<uint32_t> &shape, const MxBase::TensorDType &dataType, const int32_t &deviceId = -1);
// If the user-constructed memory is transferred, users need to allocate and destroy the memory. When the construction fails, the "std::runtime_error" exception occurs.
1
Tensor::Tensor(const std::vector<uint32_t> &shape, const MxBase::TensorDType &dataType, const int32_t &deviceId, bool isDvpp);
1
Tensor::Tensor(void *usrData, const std::vector<uint32_t> &shape, const MxBase::TensorDType &dataType,const int32_t &deviceId, const bool isDvpp, const bool isBorrowed);
1
Tensor::Tensor(const Tensor &tensor, const Rect &rect); // Copy constructor with a reference rectangle, which can be used to construct the ROI (supports only by Atlas inference products).

Parameters

Parameter

Input/Output

Description

other

Input

Other initialized tensors.

usrData

Input

Input memory constructed by the user. The memory is allocated and managed by the user.

shape

Input

Shape of a tensor.

dataType

Input

Tensor data type. For details, see TensorDType.

deviceId

Input

ID of the device where the tensor is located. The default value is -1, indicating that the tensor is on the host.

  • When the isDvpp parameter is used in the tensor constructor, the device ID has no default value and needs to be passed based on the actual situation.
  • If the user pointer usrData is passed, its memory and the device ID memory must be on the same side (-1 indicates the host; a specific device ID indicates the device). Otherwise, risks and exceptions will occur in subsequent services.

isDvpp

Input

Whether to allocate the DVPP memory. If deviceId is set to -1, the host memory is allocated. In this case, this parameter is invalid.

If the user pointer usrData is passed and isDvpp is set to true, ensure that the pointed memory of usrData exists on the device. Otherwise, risks and exceptions will occur in subsequent services.

isBorrowed

Input

Specifies whether to release the usrData pointed memory by a tensor. If isBorrowed is set to false, the tensor releases the usrData pointed memory. If isBorrowed is set to true, the user manages the usrData pointed memory.

NOTICE:

Delegates tensors to manage the memory. Only the memory that needs to be manually released is supported. Otherwise, the memory may be repeatedly released.

rect

Input

Coordinates (x0, y0, x1, y1) of the reference rectangle of the image, left-closed, right-open.

NOTICE:

If the Tensor(const Tensor &tensor, const Rect &rect) method is used for construction, the requirements are as follows:

  • The tensor cannot be empty. Only NHWC, HWC, and HW tensors are supported. The number of channels is 1, 3, or 4, and the batch dimension is 1.
  • In the rectangle region, x0 and y0 must be less than x1 and y1, respectively. x0, y0, x1, and y1 must be within the width and height range of the tensor.

tensor

Input

Other initialized tensors.