tensorflow-example-java
tensorflow-example-java copied to clipboard
This is a Tensorflow Java example application what uses YOLOv2 model and Gradle for build and dependency management.
TensorFlow Java example with YOLOv2 built by Gradle
TensorFlow Java API is a new opportunity to use TensorFlow from Java applications. On the official TensorFlow site you can find a description about the Java API usage with Maven using an Inception model. This sample shows you how to use TensorFlow from Java programs using Gradle as build and dependency management tool. In my sample code I used the YOLO vesion 2 to detect and classify objects.
How it works?
![]() |
![]() |
Input image | Bird and cow detected by YOLO using TensorFlow Java API |
![]() |
![]() |
Input image | Bird detected by YOLO using TensorFlow Java API |
Compile and run
Preconditions:
- Java JDK 1.8 or greater;
- TensorFlow 1.6 or grater;
- Git version control system;
Strongly recommended to install:
- nVidia CUDA Toolkit 8.0 or higher version;
- nVidia cuDNN GPU accelerated deep learning framework;
Download the frozen graphs
Before compiling the application you have to create/download some graph definition files. To try out the application you
can use my frozen graphs, which are trained to the Pascal VOC dataset with 20 classes. You can download them from my
google drive here. Place these files under the
src/main/resources/YOLO
directory.
Please make sure that you've set properly the GRAPH_FILE and LABEL_FILE variables in the Configuration file.
Compile the source by using Gradle
By default it runs on CPU. If you want to run this program with GPU support please add this line to the build.gradle
file:
compile group: 'org.tensorflow', name: 'libtensorflow_jni_gpu', version: '1.6.0'
Specify the path for the image in the Main class (for sure it can be modified to read from the command line arguments).
Compile the code with the following command: ./gradlew clean build
Run the application
Type the ./gradlew run
command in the command line window and hit enter. You are done!
The output is printed out with the LogBack logging framework so, it looks like:
INFO edu.ml.tensorflow.ObjectDetector - Object: cow - confidence: 0.8864294
INFO edu.ml.tensorflow.ObjectDetector - Object: bird - confidence: 0.64604723
Note
If you would like to create a client-server architecture with Spring Framework check this project: TensorFlow Java tutorial with Spring.
FAQ
Is it much slower than the TensorFlow Python or TensorFlow C++ API?
No, because it communicates through Java Native Interface (JNI)
News about YoloV3 support
The current solution doesn't support the YoloV3 model and unfortunately, I do not have time to implement it, however I would be very happy if I could help to implement and I could review a PR with this feture. For this reason I've started a new branch here: https://github.com/szaza/tensorflow-java-examples-spring/tree/feature/add-yolov3-support; If you are interested in this feature and you would like to be a collabortor, please add a comment for this thread: https://github.com/szaza/tensorflow-java-examples-spring/issues/2;
Many-many thank for any support!