使用Pyjnius时如何修复jnius.JavaException

时间:2019-06-04 14:27:48

标签: java python tensorflow pyjnius

我试图通过导入示例导入Java中的TensorFlow来运行:https://github.com/deeplearning4j/tf-import

经过一些修改,我设法将Mnist.java中的Java导入工作。 但是,我无法使用mnist_jumpy.py来在Python的DeepLearning4J中使用该模型。我通过以下修改使其运行,但是在加载模型时出现此异常:

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')

据我了解,我得到了异常jnius.JavaException,因为类java.lang.Stringorg.tensorflow.framework.GraphDef处于不同的上下文中,但是我不知道如何解决这个问题(完全不熟悉jnius)。

任何帮助将不胜感激。

这是我的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)

0 个答案:

没有答案