Class DjlTools

java.lang.Object
qupath.ext.djl.DjlTools

public class DjlTools extends Object
Tools to help work with Deep Java Library within QuPath.
  • Field Details

    • ENGINE_PYTORCH

      public static String ENGINE_PYTORCH
      PyTorch engine name
    • ENGINE_TENSORFLOW

      public static String ENGINE_TENSORFLOW
      TensorFlow engine name
    • ENGINE_MXNET

      public static String ENGINE_MXNET
      MXNet engine name
    • ENGINE_TFLITE

      public static String ENGINE_TFLITE
      TensorFlow Lite engine name
    • ENGINE_ONNX_RUNTIME

      public static String ENGINE_ONNX_RUNTIME
      ONNX Runtime engine name
    • ENGINE_XGBOOST

      public static String ENGINE_XGBOOST
      XGBoost engine name
    • ENGINE_LIGHTGBM

      public static String ENGINE_LIGHTGBM
      LightGBM engine name
    • ENGINE_DLR

      public static String ENGINE_DLR
      Neo DLR engine name
    • ENGINE_TENSORRT

      public static String ENGINE_TENSORRT
      TensorRT engine name
    • ENGINE_PADDLEPADDLE

      public static String ENGINE_PADDLEPADDLE
      PaddlePaddle engine name
    • loadedEngines

      public static Set<String> 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

      public static qupath.opencv.dnn.DnnModel createDnnModel(URI uri, String ndLayout, int[] inputShape)
      Create a DnnModel wrapping a model from Deep Java Library.
      Parameters:
      uri - URI of the model file
      ndLayout - a layout string, e.g. NCHW
      inputShape - 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 a DnnModel wrapping a model from Deep Java Library.
      Parameters:
      engine - name of the engine to use, or null to try to determine this from the URIs
      uri - URI of the model file
      ndLayout - a layout string, e.g. NCHW
      inputs - 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

      public static boolean hasEngine(String name)
      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

      public static boolean isEngineAvailable(String name)
      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 if hasEngine(name) is false or if getEngine(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 an Engine by 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 engine
      downloadIfNeeded - 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 that hasEngine(String) returns false
      See Also:
    • setOverrideDevice

      public static void setOverrideDevice(String engineName, ai.djl.Device device)
      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

      public static ai.djl.Device getOverrideDevice(String engineName)
      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 Opencv Mat to a Deep Java Library NDArray. Note that this ass
      Parameters:
      manager - an NDManager, required to create the NDArray
      mat - the mat to convert
      ndLayout - 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 an NDArray into an OpenCV Mat, automatically squeezing singleton dimensions. The Mat should have no more than 3 dimensions (height, width and channels).
      Parameters:
      array - the NDArray to convert
      ndLayout - 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 Mat with pixels corresponding to the NDArray.
    • ndArrayToMat

      public static org.bytedeco.opencv.opencv_core.Mat ndArrayToMat(ai.djl.ndarray.NDArray array, String ndLayout, boolean doSqueeze)
      Convert an NDArray into an OpenCV Mat, optionally squeezing singleton dimensions. The Mat should have no more than 3 dimensions (height, width and channels).
      Parameters:
      array - the NDArray to convert
      ndLayout - 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 Mat with pixels corresponding to the NDArray.
    • getLongs

      public static long[] getLongs(ai.djl.ndarray.NDArray array)
      Extract array values as longs, converting if necessary.
      Parameters:
      array -
      Returns: