axlarod

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2024 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package axlarod provides a Go wrapper for interacting with the Larod API, facilitating the management and execution of deep learning models on Axis devices.

The package includes types and functions for: - Establishing and terminating connections with the Larod service. - Querying, listing, and managing devices capable of running inference models. - Managing the lifecycle of models, including creation, execution, and resource cleanup. - Manipulating and transferring data between Go structures and the underlying hardware through Larod's memory management systems.

Index

Constants

View Source
const LAROD_TENSOR_MAX_LEN = 12 // Maximum length of the tensor.

Variables

This section is empty.

Functions

func ConvertRGBToImage

func ConvertRGBToImage(rgb []byte, width, height int) (*image.RGBA, error)

ConvertRGBToImage converts raw RGB bytes to an image.Image object

func CopyDataFromMappedMemory

func CopyDataFromMappedMemory(mappedAddr unsafe.Pointer, size int) ([]byte, error)

CopyDataFromMappedMemory copies data from the mapped memory

func CopyDataToMappedMemory

func CopyDataToMappedMemory(mappedAddr unsafe.Pointer, data []byte) error

CopyDataToMappedMemory copies data to the mapped memory

func CreateAndMapTmpFile

func CreateAndMapTmpFile(fileNamePattern string, fileSize uint) (unsafe.Pointer, *os.File, error)

CreateAndMapTmpFile creates a temporary file and maps it to memory

func InizalizeModelComposer added in v1.2.4

func InizalizeModelComposer(larod *Larod, modelFilePath string, chipString string, modelInput *MemMapFile, modelComposer *ModelComposer) error

InitializeModelComposer initializes a model composer with the necessary information to compose a model for inference.

func SaveImageAsJPEG

func SaveImageAsJPEG(rgb []byte, width, height int, filename string) error

SaveImageAsJPEG saves an image.Image as a JPEG file

Types

type BoundingBox

type BoundingBox struct {
	Top    float32
	Left   float32
	Bottom float32
	Right  float32
}

BoundingBox is a struct that holds the coordinates of a bounding box in float32

func (*BoundingBox) Scale

func (bbox *BoundingBox) Scale(w, h int) BoundingBox

Scale scales the bounding box to the given width and height

func (*BoundingBox) ToCords32

func (bbox *BoundingBox) ToCords32() Cords32

ToCords32 converts the bounding box to Cords32

func (*BoundingBox) ToCords64

func (bbox *BoundingBox) ToCords64() Cords64

ToCords64 converts the bounding box to Cords64

type Cords32

type Cords32 struct {
	X float32
	Y float32
	W float32
	H float32
}

Cords32 is a struct that holds the coordinates of a bounding box in float32

type Cords64

type Cords64 struct {
	X float64
	Y float64
	W float64
	H float64
}

Cords64 is a struct that holds the coordinates of a bounding box in float64

type Detection added in v1.2.4

type Detection struct {
	Box        BoundingBox
	Confidence float32
	ClassIndex int
	ClassLabel string
}

Detection is a struct that holds the information of a detection.

type JobRequest

type JobRequest struct {
	// contains filtered or unexported fields
}

func (*JobRequest) Destroy

func (job *JobRequest) Destroy()

DestroyJobRequest cleans up resources associated with a JobRequest.

type JobResult

type JobResult struct {
	ExecutionTime float64
	OutputData    any
}

type Larod

type Larod struct {
	Devices []*LarodDevice
	// contains filtered or unexported fields
}

func NewLarod

func NewLarod() *Larod

NewLarod creates a new Larod instance with uninitialized connection and device list.

func (*Larod) Connection

func (l *Larod) Connection() *LarodConnection

Connection returns the current connection to the Larod service.

func (*Larod) DestroyInputTensors

func (l *Larod) DestroyInputTensors(model *LarodModel) error

DestroyInputTensors cleans up resources associated with input tensors of a LarodModel.

func (*Larod) DestroyModel

func (l *Larod) DestroyModel(model *LarodModel) error

DestroyModel cleans up resources associated with a LarodModel.

func (*Larod) DestroyOutputTensors

func (l *Larod) DestroyOutputTensors(model *LarodModel) error

DestroyOutputTensors cleans up resources associated with output tensors of a LarodModel.

func (*Larod) ExecuteJob

func (l *Larod) ExecuteJob(model *LarodModel, dataSetterFunc func() error, dataGetterFunc func() (any, error)) (*JobResult, error)

ExecuteJob orchestrates the execution of a model with data setting and retrieving processes. It measures the execution time and returns a JobResult with the execution time and the output data. If any step (setting data, executing the model, or retrieving data) fails, it returns an error detailing the failure.

func (*Larod) GetDeviceByName

func (l *Larod) GetDeviceByName(name string) (*LarodDevice, error)

GetDeviceByName searches for a device by name among the cached devices in a Larod instance.

func (*Larod) Initalize

func (l *Larod) Initalize() error

Initialize establishes a connection to the Larod service and retrieves a list of available devices. It handles errors that occur during the connection or device listing process.

func (*Larod) ListDevices

func (l *Larod) ListDevices() ([]*LarodDevice, error)

ListDevices queries and lists all devices managed by a Larod instance's connection.

https://axiscommunications.github.io/acap-documentation/docs/acap-sdk-version-3/api/src/api/larod/html/larod_8h.html#a23e4b862c843907b8c25164df07b22ed

func (*Larod) LoadModel

func (l *Larod) LoadModel(file_path *string, dev *LarodDevice, access LarodAccess, name string, params *LarodMap) (*LarodModel, error)

LarodLoadModel loads a new model onto a specified device.

func (*Larod) LoadModelWithDeviceName

func (l *Larod) LoadModelWithDeviceName(file_path *string, dev_name string, access LarodAccess, name string, params *LarodMap) (*LarodModel, error)

LoadModelWithDeviceName loads a new model onto a specified device by name.

func (*Larod) NewInferModel

func (l *Larod) NewInferModel(filename, device string, model_defs MemMapConfiguration, job_params *LarodMap) (*LarodModel, error)

NewInferModel creates a new model for inference, just a tiny wrapper around LoadModelWithDeviceName, CreateModelTensors and CreateJobRequest

func (*Larod) NewPreProccessModel

func (l *Larod) NewPreProccessModel(device string, inputSize LarodResolution, outputSize LarodResolution, outputFormat PreProccessOutputFormat, job_params *LarodMap) (*LarodModel, error)

NewPreProccessModel creates a new pre-processing model with the specified device, input size, output size, and output format.

type LarodAccess

type LarodAccess int
const (
	LarodAccessInvalid LarodAccess = iota
	LarodAccessPrivate
	LarodAccessPublic
)

type LarodConnection

type LarodConnection struct {
	// contains filtered or unexported fields
}

type LarodDevice

type LarodDevice struct {
	Name string
	// contains filtered or unexported fields
}

type LarodError

type LarodError struct {
	Code LarodErrorCode
	Msg  string
}

func (*LarodError) Error

func (l *LarodError) Error() string

type LarodErrorCode

type LarodErrorCode int
const (
	LarodErrorNone            LarodErrorCode = 0
	LarodErrorJob             LarodErrorCode = -1
	LarodErrorLoadModel       LarodErrorCode = -2
	LarodErrorFD              LarodErrorCode = -3
	LarodErrorModelNotFound   LarodErrorCode = -4
	LarodErrorPermission      LarodErrorCode = -5
	LarodErrorConnection      LarodErrorCode = -6
	LarodErrorCreateSession   LarodErrorCode = -7
	LarodErrorKillSession     LarodErrorCode = -8
	LarodErrorInvalidChipID   LarodErrorCode = -9
	LarodErrorInvalidAccess   LarodErrorCode = -10
	LarodErrorDeleteModel     LarodErrorCode = -11
	LarodErrorTensorMismatch  LarodErrorCode = -12
	LarodErrorVersionMismatch LarodErrorCode = -13
	LarodErrorAlloc           LarodErrorCode = -14
	LarodErrorMaxErrno        LarodErrorCode = 1024
)

type LarodMap

type LarodMap struct {
	// contains filtered or unexported fields
}

LarodMap wraps a pointer to a larodMap, which is a key-value store for parameters used in Larod operations.

func CreateCropMap

func CreateCropMap(inputWidth int, inputHeight int, streamWidth int, streamHeight int) (*LarodMap, error)

CreateCropMap creates a LarodMap with the crop values for the input and stream dimensions

func NewLarodMapWithEntries

func NewLarodMapWithEntries(entries []*LarodMapEntries) (*LarodMap, error)

NewLarodMapWithEntries creates a new LarodMap and populates it with the provided entries.

func (*LarodMap) Destroy

func (m *LarodMap) Destroy()

Destroy cleans up resources associated with the LarodMap.

func (*LarodMap) GetIntArr2

func (m *LarodMap) GetIntArr2(key string) ([2]int64, error)

GetIntArr2 retrieves an array of two int64 values from the map by its key.

func (*LarodMap) GetIntArr4

func (m *LarodMap) GetIntArr4(key string) ([4]int64, error)

GetIntArr4 retrieves an array of four int64 values from the map by its key.

func (*LarodMap) GetStr

func (m *LarodMap) GetStr(key string) (string, error)

GetStr retrieves a string value from the map by its key.

func (*LarodMap) SetInt

func (m *LarodMap) SetInt(key string, value int64) error

SetInt stores an int64 value in the map with the provided key.

func (*LarodMap) SetIntArr2

func (m *LarodMap) SetIntArr2(key string, value [2]int64) error

SetIntArr2 stores an array of two int64 values in the map with the provided key.

func (*LarodMap) SetIntArr4

func (m *LarodMap) SetIntArr4(key string, value [4]int64) error

SetIntArr4 stores an array of four int64 values in the map with the provided key.

func (*LarodMap) SetStr

func (m *LarodMap) SetStr(key, value string) error

SetStr stores a string value in the map with the provided key.

type LarodMapEntries

type LarodMapEntries struct {
	Key       string            // Key of the entry.
	Value     interface{}       // Value of the entry, can be of different types based on ValueType.
	ValueType LarodMapValueType // Type of the value stored.
}

LarodMapEntries represents a key-value pair to be used in a LarodMap.

type LarodMapValueType

type LarodMapValueType int

LarodMapValueType enumerates the types of values that can be stored in a LarodMap.

const (
	LarodMapValueTypeInt LarodMapValueType = iota
	LarodMapValueTypeStr
	LarodMapValueTypeIntArr2
	LarodMapValueTypeIntArr4
)

type LarodModel

type LarodModel struct {
	Fd            uintptr
	Job           *JobRequest
	Name          string
	Inputs        []*LarodTensor
	InputsCount   uint
	InputPitches  *LarodTensorPitches
	Outputs       []*LarodTensor
	OutputsCount  uint
	OutputPitches *LarodTensorPitches
	// contains filtered or unexported fields
}

func (*LarodModel) CreateJobRequest

func (model *LarodModel) CreateJobRequest(inputTensors []*LarodTensor, outputTensors []*LarodTensor, params *LarodMap) (*JobRequest, error)

CreateJobRequest initializes a job request for a model using specified input and output tensors and optional parameters.

https://axiscommunications.github.io/acap-documentation/docs/acap-sdk-version-3/api/src/api/larod/html/larod_8h.html#af25e0293b11b12fb8e296fb20c828159

func (*LarodModel) CreateModelInputs

func (model *LarodModel) CreateModelInputs() ([]*LarodTensor, uint, error)

CreateModelInputs allocates and returns pointers to input tensors for a model, along with the count of these tensors.

func (*LarodModel) CreateModelOutputs

func (model *LarodModel) CreateModelOutputs() ([]*LarodTensor, uint, error)

CreateModelOutputs allocates and returns pointers to output tensors for a model, along with the count of these tensors.

func (*LarodModel) CreateModelTensors

func (model *LarodModel) CreateModelTensors(model_defs *MemMapConfiguration) error

CreateModelTensors initializes and configures model tensors according to provided memory configurations.

func (*LarodModel) Destroy

func (m *LarodModel) Destroy()

LoadModelWithDeviceID loads a new model onto a specified device by ID.

func (*LarodModel) Execute

func (model *LarodModel) Execute(conn *LarodConnection) error

Execute runs a job using a LarodModel on a given LarodConnection. It submits the job described by model.Job for execution.

https://axiscommunications.github.io/acap-documentation/docs/acap-sdk-version-3/api/src/api/larod/html/larod_8h.html#a7c492dcfb18e0a32407dc6078bd50dbe

func (*LarodModel) MapModelTmpFiles

func (model *LarodModel) MapModelTmpFiles(m *MemMapConfiguration) error

MapModelTmpFiles maps the temporary files for the model.

func (*LarodModel) RewindAllOutputsMemMapFiles

func (m *LarodModel) RewindAllOutputsMemMapFiles() error

Seek the memory mapped file to the beginning for all output tensors.

type LarodResolution

type LarodResolution struct {
	Width  int
	Height int
}

func (*LarodResolution) RgbSize

func (lr *LarodResolution) RgbSize() uint

func (*LarodResolution) ToArray

func (lr *LarodResolution) ToArray() [2]int64

type LarodTensor

type LarodTensor struct {
	MemMapFile *MemMapFile
	// contains filtered or unexported fields
}

LarodTensor encapsulates a tensor structure with a pointer to its C representation.

func (*LarodTensor) CopyDataInto

func (tensor *LarodTensor) CopyDataInto(data []byte) error

CopyDataInto copies data into the memory mapped file associated with a tensor.

func (*LarodTensor) GetData

func (tensor *LarodTensor) GetData(size int) ([]byte, error)

GetData retrieves data from the memory mapped file associated with a tensor.

func (*LarodTensor) GetDataAsFloat32

func (tensor *LarodTensor) GetDataAsFloat32() (float32, error)

GetDataAsFloat32 retrieves a single float32 value from the memory mapped file associated with a tensor.

func (*LarodTensor) GetDataAsFloat32Slice

func (tensor *LarodTensor) GetDataAsFloat32Slice(size int) ([]float32, error)

GetDataAsFloat32Slice retrieves data from the memory mapped file associated with a tensor and converts it to a float32 slice.

func (*LarodTensor) GetDataAsInt

func (tensor *LarodTensor) GetDataAsInt() (int, error)

func (*LarodTensor) GetTensorPitches

func (tensor *LarodTensor) GetTensorPitches() (*LarodTensorPitches, error)

GetTensorPitches retrieves the pitch information of a tensor and converts it to a Go struct.

https://axiscommunications.github.io/acap-documentation/docs/acap-sdk-version-3/api/src/api/larod/html/larod_8h.html#aaade104831363ee594d9b1bb49583fe5

type LarodTensorDataType

type LarodTensorDataType int

LarodTensorDataType defines different data types that a tensor can represent.

const (
	LarodTensorDataTypeInvalid LarodTensorDataType = iota
	LarodTensorDataTypeUnspecified
	LarodTensorDataTypeBool
	LarodTensorDataTypeUint8
	LarodTensorDataTypeInt8
	LarodTensorDataTypeUint16
	LarodTensorDataTypeInt16
	LarodTensorDataTypeUint32
	LarodTensorDataTypeInt32
	LarodTensorDataTypeUint64
	LarodTensorDataTypeInt64
	LarodTensorDataTypeFloat16
	LarodTensorDataTypeFloat32
	LarodTensorDataTypeFloat64
	LarodTensorDataTypeMax
)

type LarodTensorLayout

type LarodTensorLayout int

LarodTensorLayout represents the memory layout of a tensor.

const (
	LarodTensorLayoutInvalid LarodTensorLayout = iota
	LarodTensorLayoutUnspecified
	LarodTensorLayoutNHWC  // NHWC layout: channels last
	LarodTensorLayoutNCHW  // NCHW layout: channels first
	LarodTensorLayout420SP // 420SP layout: semi-planar YCbCr format
	LarodTensorLayoutMax
)

type LarodTensorPitches

type LarodTensorPitches struct {
	Pitches [LAROD_TENSOR_MAX_LEN]uint
	Len     uint
}

LarodTensorPitches represents the memory layout pitches of a tensor.

type MemMapConfiguration

type MemMapConfiguration struct {
	InputTmpMapFiles  map[int]*MemMapFile
	OutputTmpMapFiles map[int]*MemMapFile
}

MemMapConfiguration represents the configuration for memory mapped files.

type MemMapFile

type MemMapFile struct {
	MemoryAddress unsafe.Pointer
	File          *os.File
	Size          uint
	FilePattern   string
	UsePitch0Size bool
}

MemMapFile represents a memory mapped file.

func (*MemMapFile) Rewind

func (t *MemMapFile) Rewind() error

Seek the memory mapped file to the beginning

func (*MemMapFile) UnmapMemory

func (t *MemMapFile) UnmapMemory() error

UnmapMemory unmaps the memory mapped file

type ModelComposer added in v1.2.4

type ModelComposer struct {
	Labels         []string
	DequantizeFunc func(byte) float32
	OutputParser   func(rawModelOuput []float32, mc *ModelComposer) []Detection

	Threshold float32

	IouThreshold        float64
	OutputTensorPitches *LarodTensorPitches
	// contains filtered or unexported fields
}

ModelComposer is a struct that holds the necessary information to compose a model for inference. It contains the labels, dequantization function, output parser, model, threshold, Larod instance, IoU threshold, and output tensor pitches. Its suppose to be used to compose a model for inference, with the necessary information to do so.

func (*ModelComposer) Clean added in v1.2.4

func (mc *ModelComposer) Clean() error

Clean cleans the model.

func (*ModelComposer) Inference added in v1.2.4

func (mc *ModelComposer) Inference() (*JobResult, error)

Inference runs the inference of the model.

type PreProccessOutputFormat

type PreProccessOutputFormat string
var (
	PreProccessOutputFormatRgbInterleaved PreProccessOutputFormat = "rgb-interleaved"
	PreProccessOutputFormatRgbPlanar      PreProccessOutputFormat = "rgb-planar"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL