diff --git a/README.md b/README.md index 4cfcbca..f829e79 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ It is recommended to use the contribution manager in the Processing app to insta ![image](https://user-images.githubusercontent.com/5220162/118391536-05b1ea80-b635-11eb-9704-2c5b780008df.png) ### Manual -Download the [latest](https://github.com/cansik/deep-vision-processing/releases/tag/v0.8.0-alpha) prebuilt version from the [release](https://github.com/cansik/deep-vision-processing/releases) sections and install it into your Processing library folder. +Download the [latest](https://github.com/cansik/deep-vision-processing/releases/tag/v0.8.1-alpha) prebuilt version from the [release](https://github.com/cansik/deep-vision-processing/releases) sections and install it into your Processing library folder. ## Usage The base of the library is the `DeepVision` class. It is used to download the pretrained models and create new networks. @@ -66,8 +66,16 @@ ResultList detections = network.run(myImg); Please have a look at the specific networks for further information or at the [examples](examples). -### CUDA Support -With version `0.6.0` it is possible to [download the CUDA bundled libraries](https://github.com/cansik/deep-vision-processing/releases/tag/v0.8.0-alpha). This enables to run most of the DNN's on CUDA enabled graphics cards. For most networks this is necessary to run them in real-time. If you have the cuda-bundled version installed and run deep-vision on a Linux or Windows with an NVIDIA graphics card, you are able to enable the CUDA backend: +### OpenCL Backend Support +With version `0.8.1` by default if OpenCL is enabled, it will be used as backend. If CUDA is enabled too, CUDA will be preferred. It is possible to force the CPU backend by setting the following option: + +```java +DeepVision vision = new DeepVision(this); +vision.setUseDefaultBackend(true); +``` + +### CUDA Backend Support +With version `0.6.0` it is possible to [download the CUDA bundled libraries](https://github.com/cansik/deep-vision-processing/releases/tag/v0.8.1-alpha). This enables to run most of the DNN's on CUDA enabled graphics cards. For most networks this is necessary to run them in real-time. If you have the cuda-bundled version installed and run deep-vision on a Linux or Windows with an NVIDIA graphics card, you are able to enable the CUDA backend: ```java // Second parameter (enableCUDABackend) enables CUDA diff --git a/build.gradle b/build.gradle index dfee85f..9f3ff77 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'ch.bildspur' -version '0.8.0' +version '0.8.1' sourceCompatibility = 1.8 diff --git a/library.properties b/library.properties index 8154877..05551c9 100644 --- a/library.properties +++ b/library.properties @@ -4,7 +4,7 @@ authors=[Florian Bruggisser](https://broox.ch) url=https://github.com/cansik/deep-vision-processing sentence=Deep computer-vision algorithms for Processing paragraph=Run deep neural networks on images in Processing. -version=008000 -prettyVersion=0.8.0 +version=008010 +prettyVersion=0.8.1 minRevision=0 maxRevision=0 diff --git a/src/main/java/ch/bildspur/vision/CRNNNetwork.java b/src/main/java/ch/bildspur/vision/CRNNNetwork.java index 04fddc0..3954020 100644 --- a/src/main/java/ch/bildspur/vision/CRNNNetwork.java +++ b/src/main/java/ch/bildspur/vision/CRNNNetwork.java @@ -26,10 +26,7 @@ public CRNNNetwork(Path model) { public boolean setup() { net = readNetFromTorch(model.toAbsolutePath().toString()); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); if (net.empty()) { System.out.println("Can't load network!"); diff --git a/src/main/java/ch/bildspur/vision/ColorizationNetwork.java b/src/main/java/ch/bildspur/vision/ColorizationNetwork.java index c97b5f5..92c935a 100644 --- a/src/main/java/ch/bildspur/vision/ColorizationNetwork.java +++ b/src/main/java/ch/bildspur/vision/ColorizationNetwork.java @@ -56,10 +56,7 @@ public boolean setup() { outputBlobs.push_back(emptyOutput); net.getLayer(new DictValue("conv8_313_rh")).blobs(outputBlobs); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); return true; } diff --git a/src/main/java/ch/bildspur/vision/DORNDepthEstimationNetwork.java b/src/main/java/ch/bildspur/vision/DORNDepthEstimationNetwork.java index 203f6b3..5a9ecbb 100644 --- a/src/main/java/ch/bildspur/vision/DORNDepthEstimationNetwork.java +++ b/src/main/java/ch/bildspur/vision/DORNDepthEstimationNetwork.java @@ -28,10 +28,7 @@ public DORNDepthEstimationNetwork(Path protoTextPath, Path modelPath) { public boolean setup() { net = readNetFromCaffe(protoTextPath.toAbsolutePath().toString(), modelPath.toAbsolutePath().toString()); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); return true; } diff --git a/src/main/java/ch/bildspur/vision/DeepVision.java b/src/main/java/ch/bildspur/vision/DeepVision.java index 6b4e182..171b874 100644 --- a/src/main/java/ch/bildspur/vision/DeepVision.java +++ b/src/main/java/ch/bildspur/vision/DeepVision.java @@ -3,6 +3,8 @@ import ch.bildspur.vision.dependency.Dependency; import ch.bildspur.vision.dependency.Repository; import ch.bildspur.vision.util.ProcessingUtils; +import org.bytedeco.opencv.global.opencv_dnn; +import org.bytedeco.opencv.opencv_dnn.Net; import processing.core.PApplet; import java.io.IOException; @@ -10,9 +12,11 @@ import java.nio.file.Paths; import static org.bytedeco.opencv.global.opencv_core.getCudaEnabledDeviceCount; +import static org.bytedeco.opencv.global.opencv_core.haveOpenCL; public class DeepVision { public static boolean ENABLE_CUDA_BACKEND = false; + public static boolean USE_DEFAULT_BACKEND = false; private PApplet sketch; private boolean storeNetworksInSketch = false; @@ -26,6 +30,26 @@ public DeepVision(PApplet sketch, boolean enableCUDABackend) { ENABLE_CUDA_BACKEND = enableCUDABackend; } + public static void enableDesiredBackend(Net net) { + if (USE_DEFAULT_BACKEND) return; + + if(haveOpenCL()) { + System.out.println("DNN OpenCL backend enabled"); + net.setPreferableBackend(opencv_dnn.DNN_BACKEND_OPENCV); + net.setPreferableTarget(opencv_dnn.DNN_TARGET_OPENCL); + } + + if (DeepVision.ENABLE_CUDA_BACKEND) { + System.out.println("DNN CUDA backend enabled"); + net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); + net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); + } + } + + public void setUseDefaultBackend(boolean value) { + USE_DEFAULT_BACKEND = value; + } + public boolean isCUDABackendEnabled() { return ENABLE_CUDA_BACKEND; } diff --git a/src/main/java/ch/bildspur/vision/Face3DDFAV2Network.java b/src/main/java/ch/bildspur/vision/Face3DDFAV2Network.java index 9847b9c..14b6d68 100644 --- a/src/main/java/ch/bildspur/vision/Face3DDFAV2Network.java +++ b/src/main/java/ch/bildspur/vision/Face3DDFAV2Network.java @@ -34,10 +34,7 @@ public Face3DDFAV2Network(Path model) { public boolean setup() { net = readNetFromONNX(model.toAbsolutePath().toString()); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); return true; } diff --git a/src/main/java/ch/bildspur/vision/MaskRCNN.java b/src/main/java/ch/bildspur/vision/MaskRCNN.java index c842001..b9c7d9e 100644 --- a/src/main/java/ch/bildspur/vision/MaskRCNN.java +++ b/src/main/java/ch/bildspur/vision/MaskRCNN.java @@ -46,10 +46,7 @@ public boolean setup() { outNames.push_back("detection_out_final"); outNames.push_back("detection_masks"); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); this.loadLabels(labelsPath); diff --git a/src/main/java/ch/bildspur/vision/MediaPipeBlazeFaceNetwork.java b/src/main/java/ch/bildspur/vision/MediaPipeBlazeFaceNetwork.java index 87496ff..0427f75 100644 --- a/src/main/java/ch/bildspur/vision/MediaPipeBlazeFaceNetwork.java +++ b/src/main/java/ch/bildspur/vision/MediaPipeBlazeFaceNetwork.java @@ -43,10 +43,7 @@ public MediaPipeBlazeFaceNetwork(Path modelPath, int width, int height) { public boolean setup() { net = readNetFromONNX(modelPath.toAbsolutePath().toString()); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); if (net.empty()) { System.out.println("Can't load network!"); diff --git a/src/main/java/ch/bildspur/vision/MediaPipeFaceLandmark.java b/src/main/java/ch/bildspur/vision/MediaPipeFaceLandmark.java index e40ab6f..9114012 100644 --- a/src/main/java/ch/bildspur/vision/MediaPipeFaceLandmark.java +++ b/src/main/java/ch/bildspur/vision/MediaPipeFaceLandmark.java @@ -36,10 +36,7 @@ public MediaPipeFaceLandmark(Path model, Path config) { public boolean setup() { net = readNetFromTensorflow(model.toAbsolutePath().toString(), config.toAbsolutePath().toString()); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); return true; } diff --git a/src/main/java/ch/bildspur/vision/MidasNetwork.java b/src/main/java/ch/bildspur/vision/MidasNetwork.java index 3a8d9d0..d66cc75 100644 --- a/src/main/java/ch/bildspur/vision/MidasNetwork.java +++ b/src/main/java/ch/bildspur/vision/MidasNetwork.java @@ -31,10 +31,7 @@ public MidasNetwork(Path model) { public boolean setup() { net = readNetFromONNX(model.toAbsolutePath().toString()); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); return true; } diff --git a/src/main/java/ch/bildspur/vision/OpenFaceNetwork.java b/src/main/java/ch/bildspur/vision/OpenFaceNetwork.java index 6ff84bd..4efd6bd 100644 --- a/src/main/java/ch/bildspur/vision/OpenFaceNetwork.java +++ b/src/main/java/ch/bildspur/vision/OpenFaceNetwork.java @@ -33,10 +33,7 @@ public OpenFaceNetwork(Path modelPath) { public boolean setup() { net = readNetFromTorch(modelPath.toAbsolutePath().toString()); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); return !net.empty(); } diff --git a/src/main/java/ch/bildspur/vision/SSDMobileNetwork.java b/src/main/java/ch/bildspur/vision/SSDMobileNetwork.java index c9ea8b7..0461e03 100644 --- a/src/main/java/ch/bildspur/vision/SSDMobileNetwork.java +++ b/src/main/java/ch/bildspur/vision/SSDMobileNetwork.java @@ -45,10 +45,7 @@ public boolean setup() { outNames = net.getUnconnectedOutLayersNames(); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); if (net.empty()) { System.out.println("Can't load network!"); diff --git a/src/main/java/ch/bildspur/vision/StyleTransferNetwork.java b/src/main/java/ch/bildspur/vision/StyleTransferNetwork.java index c5670e9..6c5362b 100644 --- a/src/main/java/ch/bildspur/vision/StyleTransferNetwork.java +++ b/src/main/java/ch/bildspur/vision/StyleTransferNetwork.java @@ -29,10 +29,7 @@ public StyleTransferNetwork(Path model) { public boolean setup() { net = readNetFromTorch(model.toAbsolutePath().toString()); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); return true; } diff --git a/src/main/java/ch/bildspur/vision/SuperResolutionNetwork.java b/src/main/java/ch/bildspur/vision/SuperResolutionNetwork.java index 7b71dd9..b06eba6 100644 --- a/src/main/java/ch/bildspur/vision/SuperResolutionNetwork.java +++ b/src/main/java/ch/bildspur/vision/SuperResolutionNetwork.java @@ -7,6 +7,7 @@ import org.bytedeco.opencv.global.opencv_dnn; import org.bytedeco.opencv.global.opencv_quality; import org.bytedeco.opencv.opencv_core.Mat; +import org.bytedeco.opencv.opencv_dnn.Net; import org.bytedeco.opencv.opencv_dnn_superres.DnnSuperResImpl; import processing.core.PImage; @@ -33,10 +34,7 @@ public boolean setup() { net.readModel(model.toAbsolutePath().toString()); net.setModel(name, scale); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(new Net(net)); return true; } diff --git a/src/main/java/ch/bildspur/vision/ULFGFaceDetectionNetwork.java b/src/main/java/ch/bildspur/vision/ULFGFaceDetectionNetwork.java index 64eba85..4a2e66f 100644 --- a/src/main/java/ch/bildspur/vision/ULFGFaceDetectionNetwork.java +++ b/src/main/java/ch/bildspur/vision/ULFGFaceDetectionNetwork.java @@ -54,10 +54,7 @@ public ULFGFaceDetectionNetwork(Path modelPath, int width, int height) { public boolean setup() { net = readNetFromONNX(modelPath.toAbsolutePath().toString()); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); if (net.empty()) { System.out.println("Can't load network!"); diff --git a/src/main/java/ch/bildspur/vision/YOLONetwork.java b/src/main/java/ch/bildspur/vision/YOLONetwork.java index cefcdb8..8a2e0ca 100644 --- a/src/main/java/ch/bildspur/vision/YOLONetwork.java +++ b/src/main/java/ch/bildspur/vision/YOLONetwork.java @@ -46,10 +46,7 @@ public boolean setup() { // setup output layers outNames = net.getUnconnectedOutLayersNames(); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); if (net.empty()) { System.out.println("Can't load network!"); diff --git a/src/main/java/ch/bildspur/vision/network/ClassificationNetwork.java b/src/main/java/ch/bildspur/vision/network/ClassificationNetwork.java index 51a8046..a396863 100644 --- a/src/main/java/ch/bildspur/vision/network/ClassificationNetwork.java +++ b/src/main/java/ch/bildspur/vision/network/ClassificationNetwork.java @@ -53,10 +53,7 @@ public ClassificationNetwork(int width, int height, boolean convertToGrayScale, public boolean setup() { net = createNetwork(); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); if (net.empty()) { System.out.println("Can't load network!"); diff --git a/src/main/java/ch/bildspur/vision/network/PoseNetwork.java b/src/main/java/ch/bildspur/vision/network/PoseNetwork.java index 4b7e4e7..9ec714c 100644 --- a/src/main/java/ch/bildspur/vision/network/PoseNetwork.java +++ b/src/main/java/ch/bildspur/vision/network/PoseNetwork.java @@ -43,10 +43,7 @@ public PoseNetwork(Path modelPath, int inputWidth, int inputHeight, double scale public boolean setup() { net = createNetwork(); - if (DeepVision.ENABLE_CUDA_BACKEND) { - net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA); - net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA); - } + DeepVision.enableDesiredBackend(net); if (net.empty()) { System.out.println("Can't load network!"); diff --git a/src/test/java/ch/bildspur/vision/test/MaskRCNNWebcamTest.java b/src/test/java/ch/bildspur/vision/test/MaskRCNNWebcamTest.java index 3cbb19c..fd2b8df 100644 --- a/src/test/java/ch/bildspur/vision/test/MaskRCNNWebcamTest.java +++ b/src/test/java/ch/bildspur/vision/test/MaskRCNNWebcamTest.java @@ -4,7 +4,6 @@ import ch.bildspur.video.Capture; import ch.bildspur.vision.DeepVisionPreview; import ch.bildspur.vision.MaskRCNN; -import ch.bildspur.vision.result.ObjectDetectionResult; import ch.bildspur.vision.result.ObjectSegmentationResult; import ch.bildspur.vision.test.tools.StopWatch; import ch.bildspur.vision.util.CvProcessingUtils;