maix.tensor

maix.tensor module

You can use maix.tensor to access this module with MaixPy
This module is generated from MaixPy and MaixCDK

Module

No module

Enum

DType

Tensor data types

item describe
values UINT8:
INT8:
UINT16:
INT16:
UINT32:
INT32:
FLOAT16:
FLOAT32:
FLOAT64:
BOOL:
DTYPE_MAX:

C++ defination code:

enum DType
        {
            UINT8 = 0,
            INT8,
            UINT16,
            INT16,
            UINT32,
            INT32,
            FLOAT16,
            FLOAT32,
            FLOAT64,
            BOOL,
            // STRING,
            // OBJECT,
            DTYPE_MAX
        }

Variable

dtype_size

Tensor data type size in bytes

item description
attention It's a copy of this variable in MaixPy,
so change it in C++ (e.g. update var in hello function) will not take effect the var inMaixPy.
So we add const for this var to avoid this mistake.
value {
1, // UINT8
1, // INT8
2, // UINT16
2, // INT16
4, // UINT32
4, // INT32
2, // FLOAT16
4, // FLOAT32
8, // FLOAT64
1, // BOOL
// 1, // STRING
// 1, // OBJECT
0
}
readonly True

C++ defination code:

const std::vector<int> dtype_size = {
            1, // UINT8
            1, // INT8
            2, // UINT16
            2, // INT16
            4, // UINT32
            4, // INT32
            2, // FLOAT16
            4, // FLOAT32
            8, // FLOAT64
            1, // BOOL
            // 1, // STRING
            // 1, // OBJECT
            0
        }

dtype_name

Tensor data type name

item description
value {
"uint8",
"int8",
"uint16",
"int16",
"uint32",
"int32",
"float16",
"float32",
"float64",
"bool",
// "string",
// "object",
"invalid"
}
readonly True

C++ defination code:

const std::vector<std::string> dtype_name = {
            "uint8",
            "int8",
            "uint16",
            "int16",
            "uint32",
            "int32",
            "float16",
            "float32",
            "float64",
            "bool",
            // "string",
            // "object",
            "invalid"
        }

Function

tensor_from_numpy_float32

def tensor_from_numpy_float32(array: numpy.ndarray[numpy.float32], copy: bool = True) -> Tensor

float32 type numpy ndarray object to tensor.Tensor object.

item description
param array: numpy array object.
copy: if true, will alloc new buffer and copy data, else will directly use array's data buffer, default true.
Use this arg carefully, when set to false, ther array MUST keep alive until we don't use the return tensor of this func, or will cause program crash.
return tensor.Tensor object.

C++ defination code:

tensor::Tensor *tensor_from_numpy_float32(py::array_t<float, py::array::c_style> array, bool copy = true)

tensor_from_numpy_uint8

def tensor_from_numpy_uint8(array: numpy.ndarray[numpy.uint8], copy: bool = True) -> Tensor

uint8 type numpy ndarray object to tensor.Tensor object.

item description
param array: numpy array object.
copy: if true, will alloc new buffer and copy data, else will directly use array's data buffer, default true.
Use this arg carefully, when set to false, ther array MUST keep alive until we don't use the return tensor of this func, or will cause program crash.
return tensor.Tensor object.

C++ defination code:

tensor::Tensor *tensor_from_numpy_uint8(py::array_t<uint8_t, py::array::c_style> array, bool copy = true)

tensor_from_numpy_int8

def tensor_from_numpy_int8(array: numpy.ndarray[numpy.int8], copy: bool = True) -> Tensor

int8 type numpy ndarray object to tensor.Tensor object.

item description
param array: numpy array object.
copy: if true, will alloc new buffer and copy data, else will directly use array's data buffer, default true.
Use this arg carefully, when set to false, ther array MUST keep alive until we don't use the return tensor of this func, or will cause program crash.
return tensor.Tensor object.

C++ defination code:

tensor::Tensor *tensor_from_numpy_int8(py::array_t<int8_t, py::array::c_style> array, bool copy = true)

tensor_to_numpy_float32

def tensor_to_numpy_float32(t: Tensor, copy: bool = True) -> numpy.ndarray[numpy.float32]

tensor.Tensor object to float32 type numpy ndarray object.

item description
param t: tensor.Tensor object.
copy: Whether alloc new Tensor and copy data or not,
if not copy, array object will directly use arg's data buffer, will faster but change array will affect arg's data, default true.
return numpy array object

C++ defination code:

py::array_t<float, py::array::c_style> tensor_to_numpy_float32(tensor::Tensor *t, bool copy = true)

tensor_to_numpy_uint8

def tensor_to_numpy_uint8(t: Tensor, copy: bool = True) -> numpy.ndarray[numpy.uint8]

tensor.Tensor object to int8 type numpy ndarray object.

item description
param t: tensor.Tensor object.
copy: Whether alloc new Tensor and copy data or not,
if not copy, array object will directly use arg's data buffer, will faster but change array will affect arg's data, default true.
return numpy array object

C++ defination code:

py::array_t<uint8_t, py::array::c_style> tensor_to_numpy_uint8(tensor::Tensor *t, bool copy = true)

tensor_to_numpy_int8

def tensor_to_numpy_int8(t: Tensor, copy: bool = True) -> numpy.ndarray[numpy.int8]

tensor.Tensor object to int8 type numpy ndarray object.

item description
param t: tensor.Tensor object.
copy: Whether alloc new Tensor and copy data or not,
if not copy, array object will directly use arg's data buffer, will faster but change array will affect arg's data, default true.
return numpy array object

C++ defination code:

py::array_t<int8_t, py::array::c_style> tensor_to_numpy_int8(tensor::Tensor *t, bool copy = true)

Class

Tensor

Tensor class

C++ defination code:

class Tensor

__init__

def __init__(self, shape: list[int], dtype: DType) -> None

Tensor constructor

item description
type func
param shape: tensor shape, a int list
dtype: tensor element data type, see DType of this module
static False

C++ defination code:

Tensor(std::vector<int> shape, tensor::DType dtype)

to_str

def to_str(self) -> str

To string

item description
type func
static False

C++ defination code:

std::string to_str()

__str__

def __str__(self) -> str

To string

item description
type func
static False

C++ defination code:

std::string __str__()

shape

def shape(self) -> list[int]

get tensor shape

item description
type func
return tensor shape, a int list
static False

C++ defination code:

std::vector<int> shape()

expand_dims

def expand_dims(self, axis: int) -> None

expand tensor shape

item description
type func
param axis: axis to expand
static False

C++ defination code:

void expand_dims(int axis)

reshape

def reshape(self, shape: list[int]) -> None

reshape tensor shape, if size not match, it will throw an err::Exception

item description
type func
param shape: new shape
static False

C++ defination code:

void reshape(std::vector<int> shape)

flatten

def flatten(self) -> None

Flatten tensor shape to 1D

item description
type func
static False

C++ defination code:

void flatten()

dtype

def dtype(self) -> DType

get tensor data type

item description
type func
return tensor data type, see DType of this module
static False

C++ defination code:

tensor::DType  dtype()

to_float_list

def to_float_list(self) -> list[float]

get tensor data and return a list

item description
type func
return list type data
static False

C++ defination code:

std::valarray<float>* to_float_list()

argmax

def argmax(self, axis: int = 65535) -> Tensor

argmax of tensor

item description
type func
param axis: By default, the index is into the flattened array, otherwise along the specified axis., wrong axis will throw an err::Exception
return argmax result, you need to delete it after use in C++.
static False

C++ defination code:

tensor::Tensor *argmax(int axis = 0xffff)

argmax1

def argmax1(self) -> int

argmax1, flattened data max index

item description
type func
return argmax result, int type
static False

C++ defination code:

int argmax1()

Tensors

Tensors

C++ defination code:

class Tensors

__init__

def __init__(self) -> None

Constructor of Tensors

item description
type func
static False

C++ defination code:

Tensors()

add_tensor

def add_tensor(self, key: str, tensor: Tensor, copy: bool, auto_delete: bool) -> None

Add tensor

item description
type func
static False

C++ defination code:

void add_tensor(const std::string &key, tensor::Tensor *tensor, bool copy, bool auto_delete)

rm_tensor

def rm_tensor(self, key: str) -> None

Remove tensor

item description
type func
static False

C++ defination code:

void rm_tensor(const std::string &key)

clear

def clear(self) -> None

Clear tensors

item description
type func
static False

C++ defination code:

void clear()

get_tensor

def get_tensor(self, key: str) -> Tensor

Get tensor by key

item description
type func
static False

C++ defination code:

tensor::Tensor &get_tensor(const std::string &key)

__getitem__

def __getitem__(self, key: str) -> Tensor

Operator []

item description
type func
static False

C++ defination code:

tensor::Tensor &operator[](const std::string &key)

__len__

def __len__(self) -> int

Size

item description
type func
static False

C++ defination code:

size_t size()

keys

def keys(self) -> list[str]

Get names

item description
type func
static False

C++ defination code:

std::vector<std::string> keys()

tensors

Tensors data, dict type

item description
type var
static False
readonly False

C++ defination code:

std::map<std::string, tensor::Tensor*> tensors