Skip to content

TensorFlow Java example

Zoltán Szabó edited this page Mar 19, 2018 · 4 revisions

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?

tensorflow java example cow and bird tensorflow java sample
Input image Bird and cow detected by YOLO using TensorFlow Java API
tensorflow yolo java eagle java tensorflow yolo sample
Input image Bird detected by YOLO using TensorFlow Java API

Compile and run

Download 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 data set 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)

tensorflow java tesla java tensorflow tesla
Me and a tesla Unfortunately, it is not my car.
Hopefully, one day...

Clone this wiki locally