tensorflow / java

Java bindings for TensorFlow
Apache License 2.0
831 stars 202 forks source link

ivy dependency not working on windows or linux, native TF code not found on classpath #496

Open tobidelbruck opened 1 year ago

tobidelbruck commented 1 year ago

Please make sure that this is a build/installation issue. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:build_template

System information

Describe the problem I'm trying to use tensorflow 2 with ivy.xml in java project. The problem is that the maven download is not complete. It only downloads either 1. no native platform, or 2. if I specify 2 platforms (windows and linux), it only downloads one of them. AND 3. when I manually download the relevant jars, it cannot find the native hooks.

Provide the exact sequence of commands / steps that you executed before running into the problem I first tried ivy using these dependencies in various combinations, each time making sure to delete existing tensorflow jars from my ivy local cache folder:

    <dependency org="org.tensorflow" name="tensorflow-core-api" rev="0.4.2" m:classifier="linux-x86_64-gpu"/>
    <dependency org="org.tensorflow" name="tensorflow-core-api" rev="0.4.2" m:classifier="windows-x86_64-gpu"/>
    <dependency org="org.tensorflow" name="tensorflow-core-api" rev="0.4.2"/>

Since this did not work (I never got the full set of native jars), I downloaded the jars and put them in my local project jars folder, which is on the classpath. jars/tensorflow-core-api-0.4.2.jar jars/tensorflow-core-api-0.4.2-linux-x86_64-gpu.jar jars/tensorflow-core-api-0.4.2-windows-x86_64-gpu.jar

These jars are added to the project classpath in netbeans 8.2 project dialog

Any other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.

Here is the log output:

WARNING: java.lang.NoClassDefFoundError: java.lang.ClassNotFoundException: org.tensorflow.internal.c_api
    at org.bytedeco.javacpp.Loader.load(Loader.java:918)
    at org.bytedeco.javacpp.Loader.load(Loader.java:870)
    at org.tensorflow.NativeLibrary.load(NativeLibrary.java:48)
    at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:156)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at org.tensorflow.SavedModelBundle.<clinit>(SavedModelBundle.java:651)
    at net.sf.jaer.eventprocessing.filter.MLPNoiseFilter.loadNetwork(MLPNoiseFilter.java:720)
    at net.sf.jaer.eventprocessing.filter.MLPNoiseFilter.initFilter(MLPNoiseFilter.java:550)
    at net.sf.jaer.eventprocessing.filter.NoiseTesterFilter.initFilter(NoiseTesterFilter.java:1073)
    at net.sf.jaer.eventprocessing.FilterChain.initFilters(FilterChain.java:454)
    at net.sf.jaer.graphics.AEViewer.setChip(AEViewer.java:5863)
    at net.sf.jaer.graphics.AEViewer.constructChip(AEViewer.java:1020)
    at net.sf.jaer.graphics.AEViewer.setAeChipClass(AEViewer.java:928)
    at net.sf.jaer.graphics.AEViewer.<init>(AEViewer.java:520)
    at net.sf.jaer.JAERViewer$RunningThread.run(JAERViewer.java:231)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: org.tensorflow.internal.c_api
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.bytedeco.javacpp.Loader.load(Loader.java:913)
    ... 29 more

Here is the screenshot showing contents of the linux native jar:

Screenshot from 2023-06-12 16-34-54

Craigacp commented 1 year ago

Do you have other TF-Java dependencies in Ivy? This is the tree I get when depending on TF-Java in Maven:

[INFO] |  +- org.tensorflow:tensorflow-core-api:jar:0.5.0:compile
[INFO] |  |  +- org.bytedeco:javacpp:jar:1.5.8:compile
[INFO] |  |  \- org.tensorflow:ndarray:jar:0.4.0:compile
[INFO] |  +- org.tensorflow:tensorflow-core-platform:jar:0.5.0:compile
[INFO] |  |  +- org.bytedeco:javacpp:jar:linux-x86_64:1.5.8:compile
[INFO] |  |  +- org.bytedeco:javacpp:jar:macosx-x86_64:1.5.8:compile
[INFO] |  |  +- org.bytedeco:javacpp:jar:windows-x86_64:1.5.8:compile
[INFO] |  |  +- org.tensorflow:tensorflow-core-api:jar:linux-x86_64:0.5.0:compile
[INFO] |  |  +- org.tensorflow:tensorflow-core-api:jar:macosx-x86_64:0.5.0:compile
[INFO] |  |  \- org.tensorflow:tensorflow-core-api:jar:windows-x86_64:0.5.0:compile
[INFO] |  \- org.tensorflow:tensorflow-framework:jar:0.5.0:compile
tobidelbruck commented 1 year ago

Thanks for quick response! I added the other dependences you have explictly and see they got downloaded, and they are newer than some I had so evicted the older ones:

I deleted some unrelated dependencies in the ivy output below:

ant -f "F:\\tobi\\Dropbox (Personal)\\GitHub\\SensorsINI\\jaer" -Dnb.internal.action.name=run run
download-deps:
:: Apache Ivy 2.4.0 - 20141213170938 :: http://ant.apache.org/ivy/ ::
:: loading settings :: file = F:\tobi\Dropbox (Personal)\GitHub\SensorsINI\jaer\ivysettings.xml
:: resolving dependencies :: org.jaerproject#jaer;working@tobi-joule-amd
    confs: [default]
    found org.bytedeco.javacpp-presets#hdf5;1.10.1-1.4 in public
    found org.bytedeco.javacpp-presets#opencv;3.2.0-1.3 in public
    found org.bytedeco#javacv;1.0 in public
    found org.bytedeco.javacpp-presets#ffmpeg;2.7.1-1.0 in public
    found org.bytedeco.javacpp-presets#flycapture;2.7.3.19-1.0 in public
    found org.bytedeco.javacpp-presets#libdc1394;2.2.3-1.0 in public
    found org.bytedeco.javacpp-presets#libfreenect;0.5.2-1.0 in public
    found org.bytedeco.javacpp-presets#videoinput;0.200-1.0 in public
    found org.bytedeco.javacpp-presets#artoolkitplus;2.3.1-1.0 in public
    found org.bytedeco.javacpp-presets#flandmark;1.07-1.0 in public
    found org.tensorflow#tensorflow-core-api;0.4.2 in public
    found com.google.protobuf#protobuf-java;3.19.4 in public
    found org.tensorflow#tensorflow-core-api;0.4.2 in public
    [0.4.2] org.tensorflow#tensorflow-core-api;0.4.2
    found org.tensorflow#tensorflow-core-platform-gpu;0.4.2 in public
    found org.tensorflow#ndarray;0.4.0 in public
    found org.bytedeco#javacpp;1.5.8 in public
    [1.5.8] org.bytedeco#javacpp;1.5.8
:: resolution report :: resolve 951ms :: artifacts dl 72ms
    :: evicted modules:
    org.bytedeco#javacpp;1.4 by [org.bytedeco#javacpp;1.5.8] in [default]
    org.bytedeco#javacpp;1.3 by [org.bytedeco#javacpp;1.5.8] in [default]
    org.bytedeco#javacpp;1.0 by [org.bytedeco#javacpp;1.5.8] in [default]
    org.bytedeco.javacpp-presets#opencv;3.0.0-1.0 by [org.bytedeco.javacpp-presets#opencv;3.2.0-1.3] in [default]
    org.bytedeco#javacpp;1.5.6 by [org.bytedeco#javacpp;1.5.8] in [default]
org.tensorflow#ndarray;0.3.3 by [org.tensorflow#ndarray;0.4.0] in [default]

But I get the same missing native code hook errors.

  WARNING: java.lang.NoClassDefFoundError: java.lang.ClassNotFoundException: org.tensorflow.internal.c_api
      at org.bytedeco.javacpp.Loader.load(Loader.java:918)
      at org.bytedeco.javacpp.Loader.load(Loader.java:870)
      at org.tensorflow.NativeLibrary.load(NativeLibrary.java:48)
      at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:156)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:264)
      at org.tensorflow.SavedModelBundle.<clinit>(SavedModelBundle.java:651)
Craigacp commented 1 year ago

That stack trace is weird because Loader.java line 870 in JavaCPP 1.5.8 is Loader.findResources not Loader.load - https://github.com/bytedeco/javacpp/blob/1.5.8/src/main/java/org/bytedeco/javacpp/Loader.java#L870. Do you have multiple versions of JavaCPP on the classpath?

tobidelbruck commented 1 year ago

Thanks very much for your insights! I have no idea about class loaders or bytedeco. Let me dig deeper. I probably do have multiple versions resulting from opencv and jogl use in this project. And there may be even more javacpp in the static jars we must include (since libs not available by maven central). It may be that we are ignorant of best practices. I'll let you know what I find out.

saudet commented 1 year ago

@tobidelbruck Please upgrade TensorFlow, JavaCV, OpenCV, HDF5, and any others with a dependency on JavaCPP, to their latest versions.