You are here: Start » PyAVL » Deep Learning (PyAVL-DL)

Deep Learning (PyAVL-DL)

PyAVL-DL is the AVLDL module. It runs trained Aurora Deep Learning models on images from Python. The module requires PyAVL, and it uses the types of PyAVL, such as Image, Region and Rectangle2D. Objects created with AVL can therefore be passed directly to the functions of AVLDL and the other way round.

The functions of PyAVL-DL are described in this page. The functions of PyAVL, such as AVL.EnumerateImages or the drawing functions, are used together with them.

Workflow

Working with a model has three steps:

  1. Deploy. A DL_..._Deploy function loads the model from its directory and prepares it for execution on the selected device. It fills a model identifier object, which is passed to the next functions. The only exception is DL_LocateBarcodes_Deploy, which needs no model directory and returns the identifier.
  2. Run. The DL_... function with the same model name processes one image. It can be called any number of times.
  3. Unload. A DL_..._Unload function releases the memory used by the model.
import AVL
import AVLDL

# Deploy the model from the "model" directory on the CPU
model = AVLDL.ClassifyObjectModelId()
AVLDL.DL_ClassifyObject_Deploy("model", model, inDeviceType=AVLDL.DeviceKind.CPU)

# Run the model on every image of a directory
for image, _, filename in AVL.EnumerateImages("images/test"):
    heatmap = AVL.Heatmap()
    result = AVLDL.DL_ClassifyObject(image, model, heatmap)
    print(f"{filename}: {result.outClassName} ({result.outScore:.2f})")

# Release the model
AVLDL.DL_ClassifyObject_Unload(model, inLeaveForRedeploy=False)

The device is selected with inDeviceType, using the values AVLDL.DeviceKind.CPU and AVLDL.DeviceKind.CUDA, and inDeviceIndex for systems with more than one device. The functions that return several values return a named result object, as in the other functions of PyAVL, for example result.outClassName.

Complete scripts for most of the model types are included in the examples, see Python Examples.

Model Types

Every model type has its own identifier type and its own set of functions. The names of the functions follow one pattern. The function that runs the model has the name given in the table, and the functions that deploy and unload the model have the suffixes _Deploy and _Unload. For example, the Classify Object model uses DL_ClassifyObject_Deploy, DL_ClassifyObject and DL_ClassifyObject_Unload.

ModelFunctionIdentifierAdditional functions
Classify ObjectDL_ClassifyObjectClassifyObjectModelId_AccessModel
Detect Anomalies 1DL_DetectAnomalies1DetectAnomalies1ModelId—
Detect Anomalies 2DL_DetectAnomalies2DetectAnomalies2ModelId_WithThresholds, _AccessModel
Detect FeaturesDL_DetectFeaturesDetectFeaturesModelId_AccessModel
Locate PointsDL_LocatePointsLocatePointsModelId_AccessModel
Locate ObjectsDL_LocateObjectsLocateObjectsModelId—
Segment InstancesDL_SegmentInstancesSegmentInstancesModelId—
Read CharactersDL_ReadCharactersReadCharactersModelId_Multiple
Locate TextDL_LocateTextLocateTextModelId—
Locate BarcodesDL_LocateBarcodesLocateBarcodesModelId—

The additional functions have the name of the function from the table followed by the suffix:

  • _AccessModel reads the information stored in a trained model, such as the names of its classes or its thresholds, from the model directory, without deploying the model.
  • _WithThresholds runs the model with thresholds given as arguments.
  • _Multiple reads the characters in several regions of interest in one call.

The deploy functions of Read Characters and Locate Text can load a pretrained model instead of a model directory. The pretrained model is selected with inPretrainedModelType, for example AVLDL.OcrPretrainedModel.Extended.

Custom Models

Trained models that do not belong to any of the model types above are executed on tensors with the following functions. A model is identified by WeaverModelId.

FunctionDescription
WEAVER_DeployModelLoads a model and prepares its execution on a specific target device.
WEAVER_InferSingleTensorExecutes a model on one input tensor.
WEAVER_InferMultipleTensorsExecutes a model on several input tensors.
WEAVER_UnloadModelUnloads a model and frees the allocated memory.

Tensor Conversion

The input and the output of the custom models are Tensor objects. The following functions convert images and arrays to tensors and back:

FunctionDescription
ConvertImageToTensorConverts an image to an NHWC tensor, with N equal to 1.
ConvertTensorToImageConverts an NHWC tensor to an image. Only the data for N equal to 0 is converted.
ConvertImageArrayToTensorConverts an array of images with the same format to an NHWC tensor.
ConvertTensorToImageArrayConverts an NHWC tensor to an array of images.
ConvertRealArrayToTensorConverts an array of real numbers to a tensor with the given dimensions and ordering.
ConvertTensorToRealArrayConverts a tensor to an array of real numbers.
ConvertRealArrayArrayToTensorConverts a two-dimensional array of real numbers to a tensor with the NL ordering.
ConvertTensorToRealArrayArrayConverts a tensor with the NL ordering to a two-dimensional array of real numbers.

Devices and Service

FunctionDescription
DL_ListCudaDevicesLists the available CUDA devices.
DL_CleanDeviceFrees the memory allocated for the execution of models on a device.
DL_ConfigureLoggingSets the minimum level of the logged messages.
DL_IsServiceRunningChecks whether the Deep Learning service is running. Without a timeout the function waits until the connection is established.

See Also

Previous: Python Examples Next: Operators on Geometry Types