Package qupath.ext.djl
Class DjlTools
java.lang.Object
qupath.ext.djl.DjlTools
Tools to help work with Deep Java Library within QuPath.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic StringNeo DLR engine namestatic StringLightGBM engine namestatic StringMXNet engine namestatic StringONNX Runtime engine namestatic StringPaddlePaddle engine namestatic StringPyTorch engine namestatic StringTensorFlow engine namestatic StringTensorRT engine namestatic StringTensorFlow Lite engine namestatic StringXGBoost engine nameMaintain a set of all engines that are known to have been loaded. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic qupath.opencv.dnn.DnnModelcreateDnnModel(String engine, URI uri, String ndLayout, Map<String, qupath.opencv.dnn.DnnShape> inputs, Map<String, qupath.opencv.dnn.DnnShape> outputs) Create aDnnModelwrapping a model from Deep Java Library.static qupath.opencv.dnn.DnnModelcreateDnnModel(URI uri, String ndLayout, int[] inputShape) Create aDnnModelwrapping a model from Deep Java Library.static ai.djl.engine.EngineGet anEngineby name.static long[]getLongs(ai.djl.ndarray.NDArray array) Extract array values as longs, converting if necessary.static ai.djl.DevicegetOverrideDevice(String engineName) Get the default device for the specified engine, which overrides DJL's default device for the specified engine.static booleanCheck if an Deep Java Library engine is potentially available.static booleanisEngineAvailable(String name) Check if an engine is available and ready for use without any additional downloading.static ai.djl.ndarray.NDArraymatToNDArray(ai.djl.ndarray.NDManager manager, org.bytedeco.opencv.opencv_core.Mat mat, String ndLayout) Convert an OpencvMatto a Deep Java LibraryNDArray.static org.bytedeco.opencv.opencv_core.MatndArrayToMat(ai.djl.ndarray.NDArray array, String ndLayout) Convert anNDArrayinto an OpenCVMat, automatically squeezing singleton dimensions.static org.bytedeco.opencv.opencv_core.MatndArrayToMat(ai.djl.ndarray.NDArray array, String ndLayout, boolean doSqueeze) Convert anNDArrayinto an OpenCVMat, optionally squeezing singleton dimensions.static voidsetOverrideDevice(String engineName, ai.djl.Device device) Set the default device for the specified engine.
-
Field Details
-
ENGINE_PYTORCH
PyTorch engine name -
ENGINE_TENSORFLOW
TensorFlow engine name -
ENGINE_MXNET
MXNet engine name -
ENGINE_TFLITE
TensorFlow Lite engine name -
ENGINE_ONNX_RUNTIME
ONNX Runtime engine name -
ENGINE_XGBOOST
XGBoost engine name -
ENGINE_LIGHTGBM
LightGBM engine name -
ENGINE_DLR
Neo DLR engine name -
ENGINE_TENSORRT
TensorRT engine name -
ENGINE_PADDLEPADDLE
PaddlePaddle engine name -
loadedEngines
Maintain a set of all engines that are known to have been loaded. This enables us to check for available (and downloaded) engines without having to try to instantiate a new one.
-
-
Constructor Details
-
DjlTools
public DjlTools()
-
-
Method Details
-
createDnnModel
Create aDnnModelwrapping a model from Deep Java Library.- Parameters:
uri- URI of the model filendLayout- a layout string, e.g. NCHWinputShape- expected input shape, according to ndLayout- Returns:
-
createDnnModel
public static qupath.opencv.dnn.DnnModel createDnnModel(String engine, URI uri, String ndLayout, Map<String, qupath.opencv.dnn.DnnShape> inputs, Map<String, qupath.opencv.dnn.DnnShape> outputs) Create aDnnModelwrapping a model from Deep Java Library.- Parameters:
engine- name of the engine to use, or null to try to determine this from the URIsuri- URI of the model filendLayout- a layout string, e.g. NCHWinputs- input shapes, if known; if these are null, an attempt will be made to get them from DJL (but this does not always work)outputs- outputs shapes, if known; if these are null, an attempt will be made to get them from DJL (but this does not always work)- Returns:
-
hasEngine
Check if an Deep Java Library engine is potentially available. Note that this does not necessarily mean that it has been downloaded or is supported on this platform, but only that the necessary engine jar is on the classpath.- Parameters:
name-- Returns:
- true if the engine jars are on the classpath, false otherwise
- See Also:
-
isEngineAvailable
Check if an engine is available and ready for use without any additional downloading. This first checks whether an engine with the given name has already been seen by this class; if so, the method returns true without attempting to instantiate the engine. Otherwise, the method returns true only ifhasEngine(name)is false or ifgetEngine(name, false)is not null.- Parameters:
name-- Returns:
- See Also:
-
getEngine
public static ai.djl.engine.Engine getEngine(String name, boolean downloadIfNeeded) throws IllegalArgumentException Get anEngineby name.This is similar to
Engine.getEngine(String)except provides control over whether the native libraries for the engine are downloaded if required. This avoids unexpectedly long blocking calls for an engine request if the native libraries need to be downloaded.- Parameters:
name- the name of the enginedownloadIfNeeded- if true, download the necessary native libraries if needed. If false, return null if the engine cannot be loaded.- Returns:
- Throws:
IllegalArgumentException- if the engine is not available, which means thathasEngine(String)returns false- See Also:
-
setOverrideDevice
Set the default device for the specified engine. This will be used only whenever the model is build using this class, overriding DJL's default.Note that the default device chosen automatically by DJL is usually fine, and so it is generally not necessary to set this. However it can be useful for exploring, or if DJL does not use the device you want.
- Parameters:
engineName-device-
-
getOverrideDevice
Get the default device for the specified engine, which overrides DJL's default device for the specified engine.- Parameters:
engineName-- Returns:
- the default device, or null if not set
-
matToNDArray
public static ai.djl.ndarray.NDArray matToNDArray(ai.djl.ndarray.NDManager manager, org.bytedeco.opencv.opencv_core.Mat mat, String ndLayout) Convert an OpencvMatto a Deep Java LibraryNDArray. Note that this ass- Parameters:
manager- anNDManager, required to create the NDArraymat- the mat to convertndLayout- a layout string for the NDArray, e.g. "CHW"; currently, HW must appear together (in that order)- Returns:
- an NDArray containing the values in the Mat, with the specified layout
-
ndArrayToMat
public static org.bytedeco.opencv.opencv_core.Mat ndArrayToMat(ai.djl.ndarray.NDArray array, String ndLayout) Convert anNDArrayinto an OpenCVMat, automatically squeezing singleton dimensions. TheMatshould have no more than 3 dimensions (height, width and channels).- Parameters:
array- the NDArray to convertndLayout- a layout string, e.g. NCHW. If this is null, an attempt will be made to request the layout from the array - however this can often fail if the dimensions are unknown.- Returns:
- a
Matwith pixels corresponding to theNDArray.
-
ndArrayToMat
public static org.bytedeco.opencv.opencv_core.Mat ndArrayToMat(ai.djl.ndarray.NDArray array, String ndLayout, boolean doSqueeze) Convert anNDArrayinto an OpenCVMat, optionally squeezing singleton dimensions. TheMatshould have no more than 3 dimensions (height, width and channels).- Parameters:
array- the NDArray to convertndLayout- a layout string, e.g. NCHW. If this is null, an attempt will be made to request the layout from the array - however this can often fail if the dimensions are unknown.doSqueeze- if true, squeeze singleton dimensions- Returns:
- a
Matwith pixels corresponding to theNDArray.
-
getLongs
public static long[] getLongs(ai.djl.ndarray.NDArray array) Extract array values as longs, converting if necessary.- Parameters:
array-- Returns:
-