7

FXApps: Using tensorflow from a Java application

 3 years ago
source link: http://fxapps.blogspot.com/2017/04/using-tensorflow-from-java-application.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Using tensorflow from a Java application

At this point you probably have heard about TensorFlow, the famous Google library for deep learning.
Although the TensorFlow core is C/C++, the most famous API is Python (as you can see in Siraj or sintdex tutorials), but recently TensorFlow released a Java version for the API.

TensorFlow has a small tutorial about the Java API and a single more complex example to classify image. On this small tutorial the link to the tensorflow JAR is broken. I found a working JAR in this URL. The upstream tutorial was already corrected as you can see in github.

There's also the labeled image example, which is very simple as well, but a few additional steps must be taken:

  • Download the content from this URL and unzip it. It is the labeled dataset
  • Download the LabelImage class from github. When saving it locally remember to remove the package declaration.
  • Having the tensorflow JAR you downloaded previously and the native library, you can compile and run the class passing the directory where you unzipped the labeled dataset and a test image.
In the example above you can see it found that the image contained a "Zebra". There are 1001 labeled images on the downloaded dataset and it usually labels the image correctly.

Maven

A few days ago the tensorflow artifacts was added to maven  central repository. To use it you just need the dependency org.tensorflow:tensorflow:1.1.0-rc1. Seriously, no other configuration is required, you just need to add this dependency and everything should work. A test sample maven application can be found at my github.

You will find a simple JavaFX Application that allows you to select a file that will be analyzed by the same code used in the LabelImage official example. See some screenshot:
Screenshot%2Bfrom%2B2017-04-14%2B23-55-27.png
Screenshot%2Bfrom%2B2017-04-14%2B23-57-22.png
Screenshot%2Bfrom%2B2017-04-14%2B23-57-45.png
To run this application locally you must have maven and Java. Then download the dataset and the labels and modify the PATH_TO_DATA_DIR in App class to the path were you extracted it. Finally, just run:

mvn -q compile exec:java

And it should open the application. All my tests were made on a linux machine, hence I don't know if it would work on Windows.

Conclusion

TensorFlow Java API is very easy to "install" and use. I would say that it is easier than python installation!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK