MichaelFeng87 / tensorflow-java

Tensorflow Java examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tensorflow-java

Tensorflow Java pipeline and examples. This simple Java pipeline for TensorFlow Java API supports Tensorflow >= 1.4 and it has been tested with TF 1.4.0 and TF 1.6.0.

How To Install

You need to run the jni.sh to install the right Java bindings for your platform and the download.sh script that will download the inception5 model in order to be ready to run a simple examples:

git clone https://github.com/loretoparisi/tensorflow-java.git \
cd tensorflow-java \
sh jni.sh \
sh download.sh \

A simple Hello World example

Create a simple Java class with a main to be executable and import org.tensorflow.TensorFlow

import org.tensorflow.TensorFlow;

public class TensorFlowExample {
  public static void main(String[] args) {
    System.out.println("TensorFlowExample using TensorFlow version: " +  TensorFlow.version());
  }
}

Save it and then from command line compile and run

cd tensorflow-java
javac -cp lib/libtensorflow-1.6.0.jar TensorFlowExample.java
java -cp lib/libtensorflow-1.6.0.jar:. -Djava.library.path=./jni TensorFlowExample

If you get the TensorFlow version as output it worked!

TensorFlowExample using TensorFlow version: 1.0.0-rc2

Real world (Inception) example

We use the LabelImage official Tensorflow example to label an example image with the inception graph model.

$ javac -cp lib/libtensorflow-1.6.0.jar LabelImage.java 
$ java -cp lib/libtensorflow-1.6.0.jar:. -Djava.library.path=./jni LabelImage models/ images/example-400x288.jpg 
BEST MATCH: lakeside (19,00% likely)

Disclaimer

This example is provided as it is and it is based on the official Tensorflow Java JNI wrapper and example available here.

About

Tensorflow Java examples

License:MIT License


Languages

Language:Java 86.9%Language:Shell 13.1%