5

I am trying to get this example for importing importing TensorFlow in Java to run: https://github.com/deeplearning4j/tf-import

I have managed to get the Java import in Mnist.java to work after some modifications. However, I am unable to get mnist_jumpy.py to work in order to use the model in DeepLearning4J from Python. I got it run with the modifications below, but I get this exception when loading the model:

log4j:WARN No appenders could be found for logger (org.nd4j.linalg.factory.Nd4jBackend).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.rits.cloning.Cloner (file:/home/micha/.deeplearning4j/pydl4j/pydl4j-1.0.0-SNAPSHOT-cpu-core-datavec-spark2-2.11/pydl4j-1.0.0-SNAPSHOT-bin.jar) to field java.util.TreeSet.m
WARNING: Please consider reporting this to the maintainers of com.rits.cloning.Cloner
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Traceback (most recent call last):
  File "/home/micha/Documents/01_work/git/tf_java_import_testing/tf-import/mnist/mnist_jumpy.py", line 17, in <module>
    tf_model = jp.TFModel(path + '/mnist.pb')
  File "/home/micha/Documents/01_work/git/tf_java_import_testing/tf-import/venv-deepmom/lib/python3.7/site-packages/jumpy/tf_model.py", line 22, in __init__
    self.sd = TFGraphMapper.getInstance().importGraph(filepath)
  File "jnius/jnius_export_class.pxi", line 906, in jnius.JavaMultipleMethod.__call__
  File "jnius/jnius_export_class.pxi", line 638, in jnius.JavaMethod.__call__
  File "jnius/jnius_export_class.pxi", line 715, in jnius.JavaMethod.call_method
  File "jnius/jnius_utils.pxi", line 93, in jnius.check_exception
jnius.JavaException: JVM exception occurred: class java.lang.String cannot be cast to class org.tensorflow.framework.GraphDef (java.lang.String is in module java.base of loader 'bootstrap'; org.tensorflow.framework.GraphDef is in unnamed module of loader 'app')

From what I understand I am getting the exception jnius.JavaException, because the classes java.lang.String and org.tensorflow.framework.GraphDef live in different contexts, but I don't know how to resolve this (I am completely unfamiliar with jnius).

Any help would be greatly appreciated.

This is my version of mnist_jumpy.py:

import os
try:
     # from jnius import autoclass
     import jumpy as jp
except KeyError:
     os.environ['JDK_HOME'] = "/usr/lib/jvm/java-11-openjdk-amd64"
     os.environ['JAVA_HOME'] = "/usr/lib/jvm/java-11-openjdk-amd64"
     import jumpy as jp

from scipy import ndimage
# import numpy as np
import os

path = os.path.dirname(os.path.abspath(__file__))

# load tensorflow model
tf_model = jp.TFModel(path + '/mnist.pb')

# load jpg to numpy array
image = ndimage.imread(path + '/img (1).jpg').reshape((1, 28, 28))

# inference - uses nd4j
prediction = tf_model(image)  # prediction is a jumpy array

# get label from predction using argmax
label = jp.argmax(prediction.reshape((10,)))

print(label)
packoman
  • 1,230
  • 1
  • 16
  • 36

0 Answers0