Java bindings for TensorFlow.
WARNING: The TensorFlow Java API is incomplete and experimental and can change without notice. Progress can be followed in issue #5.
Till then, for using TensorFlow on Android refer to contrib/android, makefile and/or the Android camera demo.
Environment to build TensorFlow from source code (Linux or Mac OS X). If you'd like to skip reading those details and do not care about GPU support, try the following:
# On Linux sudo apt-get install python swig python-numpy # On Mac OS X with homebrew brew install swig
Build the Java Archive (JAR) and native library:
bazel build -c opt \ //tensorflow/java:tensorflow \ //tensorflow/java:libtensorflow-jni
To use the library in an external Java project, publish the library to a Maven repository. For example, publish the library to the local Maven repository using the mvn
tool (installed separately):
mvn install:install-file \ -Dfile=bazel-bin/tensorflow/java/libtensorflow.jar \ -DpomFile=tensorflow/java/pom.xml
Refer to the library using Maven coordinates. For example, if you're using Maven then place this dependency into your pom.xml
file:
<dependency> <groupId>org.tensorflow</groupId> <artifactId>libtensorflow</artifactId> <version>0.12.0-SNAPSHOT</version> </dependency>
Add a dependency on //tensorflow/java:tensorflow
to the java_binary
or java_library
rule. For example:
bazel run -c opt //tensorflow/java/src/main/java/org/tensorflow/examples:label_image
javac
Add libtensorflow.jar
to classpath for compilation. For example:
javac \ -cp ../../bazel-bin/tensorflow/java/libtensorflow.jar \ ./src/main/java/org/tensorflow/examples/LabelImage.java
Make libtensorflow.jar
and libtensorflow-jni.so
(libtensorflow-jni.dylib
on OS X) available during execution. For example:
java \ -Djava.library.path=../../bazel-bin/tensorflow/java \ -cp ../../bazel-bin/tensorflow/java/libtensorflow.jar:./src/main/java \ org.tensorflow.examples.LabelImage