Skip to content

Commit

Permalink
added opencl support for faster inferencing on non cuda devices #10
Browse files Browse the repository at this point in the history
  • Loading branch information
cansik committed Feb 17, 2022
1 parent be902ae commit d71ac5e
Show file tree
Hide file tree
Showing 21 changed files with 55 additions and 71 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -66,8 +66,16 @@ ResultList<ObjectDetectionResult> 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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'ch.bildspur'
version '0.8.0'
version '0.8.1'

sourceCompatibility = 1.8

Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/CRNNNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/ColorizationNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/ch/bildspur/vision/DeepVision.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
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;
import java.nio.file.Files;
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;
Expand All @@ -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;
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/Face3DDFAV2Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/MaskRCNN.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/MediaPipeFaceLandmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/MidasNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/OpenFaceNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/SSDMobileNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/StyleTransferNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/ch/bildspur/vision/SuperResolutionNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/YOLONetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/bildspur/vision/network/PoseNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d71ac5e

Please sign in to comment.