pac*_*man 5 python java pyjnius tensorflow
我试图让这个示例在 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')
Run Code Online (Sandbox Code Playgroud)
据我了解,我得到了异常jnius.JavaException,因为类java.lang.String和org.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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1816 次 |
| 最近记录: |