Difference between revisions of "Tensorflow JNI development"

From ElphelWiki
Jump to: navigation, search
(Java maven project in Eclipse)
(Java maven project in Eclipse)
Line 50: Line 50:
 
   <dependencies>
 
   <dependencies>
 
     ...
 
     ...
     <dependency>
+
     '''<dependency>
 
       <groupId>org.tensorflow</groupId>
 
       <groupId>org.tensorflow</groupId>
 
       <artifactId>libtensorflow</artifactId>
 
       <artifactId>libtensorflow</artifactId>
 
       <version>1.15.0</version>
 
       <version>1.15.0</version>
     </dependency>
+
     </dependency>'''
 
     ...
 
     ...
 
   </dependencies>
 
   </dependencies>
 
   ...
 
   ...
 
  </project>
 
  </project>

Revision as of 15:23, 4 March 2020

About

Based on this.

And this one - tensorflow/java/README.md.

How to:

  • Install things
  • Build libtensorflow.jar, libtensorflow_jni.so
  • Install pom.xml and libtensorflow.jar to local maven repo in ~/.m2
  • Modify Tensorflow JNI functions
  • Create a maven project and use modified JNI

Install

  • In Kubuntu
  • Get TF 1.15.0 - git or archive
  • Install bazel 0.25.2 - see *.deb in releases

Build

cd ~/git/tensorflow-1.15.0
./configure
bazel build -c opt //tensorflow/java:tensorflow //tensorflow/java:libtensorflow_jni
bazel build -c opt //tensorflow/java:pom
mvn install:install-file -Dfile=bazel-bin/tensorflow/java/libtensorflow.jar -DpomFile=bazel-bin/tensorflow/java/pom.xml

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

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

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>