Difference between revisions of "Tensorflow JNI development"

From ElphelWiki
Jump to: navigation, search
(Install TF to local maven repo)
Line 1: Line 1:
==About==
+
==<font color='blue'>About</font>==
  
 
Based on [https://medium.com/@maxime.durand.54/build-tennsorflow-2-0-for-java-on-windows-2ab51b9cac45 Build TensorFlow 2.0 for Java on Windows] article.
 
Based on [https://medium.com/@maxime.durand.54/build-tennsorflow-2-0-for-java-on-windows-2ab51b9cac45 Build TensorFlow 2.0 for Java on Windows] article.
Line 14: Line 14:
 
There's [https://github.com/bytedeco/javacpp-presets JavaCPP Presets] project. Seems useless.
 
There's [https://github.com/bytedeco/javacpp-presets JavaCPP Presets] project. Seems useless.
  
==Install==
+
==<font color='blue'>Install</font>==
 
In Kubuntu:
 
In Kubuntu:
 
* Get [https://github.com/tensorflow/tensorflow/archive/v1.15.0.tar.gz TensorFlow-1.15.0]
 
* Get [https://github.com/tensorflow/tensorflow/archive/v1.15.0.tar.gz TensorFlow-1.15.0]
Line 21: Line 21:
 
Based on [[Feeding_Tensorflow_from_GPU|Feeding Tensorflow from GPU]].
 
Based on [[Feeding_Tensorflow_from_GPU|Feeding Tensorflow from GPU]].
  
==Build==
+
==<font color='blue'>Build</font>==
 
  <font size=2>cd ~/git/tensorflow-1.15.0
 
  <font size=2>cd ~/git/tensorflow-1.15.0
 
  ./configure # do not forget CUDA
 
  ./configure # do not forget CUDA
Line 39: Line 39:
 
* '''so''' will have to be in the library path - set LD_LIBRARY_PATH or PATH or go "java -Djava.library.path"
 
* '''so''' will have to be in the library path - set LD_LIBRARY_PATH or PATH or go "java -Djava.library.path"
  
==Install JAR to local Maven Repository==
+
==<font color='blue'>Install JAR to local Maven Repository</font>==
 
  <font size=2><b>mvn install:install-file -Dfile=bazel-bin/tensorflow/java/libtensorflow.jar -DpomFile=bazel-bin/tensorflow/java/pom.xml</b></font>
 
  <font size=2><b>mvn install:install-file -Dfile=bazel-bin/tensorflow/java/libtensorflow.jar -DpomFile=bazel-bin/tensorflow/java/pom.xml</b></font>
  
Line 45: Line 45:
 
  https://stackoverflow.com/questions/15358851/how-to-remove-jar-file-from-local-maven-repository-which-was-added-with-install
 
  https://stackoverflow.com/questions/15358851/how-to-remove-jar-file-from-local-maven-repository-which-was-added-with-install
  
==Modify TF JNI==
+
==<font color='blue'>Modify TF JNI</font>==
 
For example, one wants to create a new function in org.tensorflow.TensorFlow package.
 
For example, one wants to create a new function in org.tensorflow.TensorFlow package.
 
Then, see:
 
Then, see:
Line 57: Line 57:
 
* rebuild and reinstall to mvn
 
* rebuild and reinstall to mvn
  
==Java maven project in Eclipse==
+
==<font color='blue'>Java Maven project in Eclipse==
 
* Create a new maven project
 
* Create a new maven project
 
* Edit pom.xml:
 
* Edit pom.xml:

Revision as of 16:59, 4 March 2020

About

Based on Build TensorFlow 2.0 for Java on Windows article. Also this one - tensorflow/java/README.md.

These instructions are for Linux and old TensorFlow 1.15.0.

How to:

  • Build TF JNI - libtensorflow.jar, libtensorflow_jni.so and pom.xml
  • Add TF JAR to local Maven which will override the Central Maven Repository
  • Modify TF JNI functions
  • Create Elipse project

There's JavaCPP Presets project. Seems useless.

Install

In Kubuntu:

Based on Feeding Tensorflow from GPU.

Build

cd ~/git/tensorflow-1.15.0
./configure # do not forget CUDA
bazel build -c opt //tensorflow/java:tensorflow //tensorflow/java:libtensorflow_jni //tensorflow/java:pom

With TF, bazel tends to rebuild everything from scratch - takes a ton of time. Is it because it gets restarted after idle timeout or something else? A somewhat solution might be

At launch bazel starts its server which, to prevent it, add to ~/.bazelrc:
startup --max_idle_secs=0

Artifacts of interest are in bazel-bin/tensorflow/java/:

libtensorflow_jni.so
libtensorflow.jar
pom.xml
  • xml and jar will be taken care of by mvn command.
  • so will have to be in the library path - set LD_LIBRARY_PATH or PATH or go "java -Djava.library.path"

Install JAR to local Maven Repository

mvn install:install-file -Dfile=bazel-bin/tensorflow/java/libtensorflow.jar -DpomFile=bazel-bin/tensorflow/java/pom.xml

How to uninstall maven local repo - and switch back to official versions form Central:

https://stackoverflow.com/questions/15358851/how-to-remove-jar-file-from-local-maven-repository-which-was-added-with-install

Modify TF JNI

For example, one wants to create a new function in org.tensorflow.TensorFlow package. Then, see:

tensorflow/java/src/main/java/org/tensorflow/
tensorflow/java/src/main/native/
  • add native method to tensorflow/java/src/main/java/org/tensorflow/TensorFlow.java
  • add to header file tensorflow/java/src/main/native/tensorflow_jni.h
  • add to c file tensorflow/java/src/main/native/tensorflow_jni.cc
  • rebuild and reinstall to mvn

Java Maven project in Eclipse

  • Create a new maven project
  • Edit pom.xml:
<project>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>org.tensorflow</groupId>
      <artifactId>libtensorflow</artifactId>
      <version>1.15.0</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>

Basic example code

tfhello.java:

import org.tensorflow.TensorFlow;

public class tfhello{
	public static void main(String[] args){
		System.out.println(TensorFlow.version());
	}
}