How to fix a jnius.JavaException when using Pyjnius - java

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)

Related

NoSuchMethodError in neo4j when calling the DatabaseManagementServiceBuilder( path ).build() method

I´m devolopping a Java application with neo4j embedded. I don´t know why but it is not possible to build a Database. When I call the the build()-Method in the DatabaseManagementServiceBuilder class I get the following warnings and exeption:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access using Lookup on org.neo4j.internal.unsafe.UnsafeUtil (file:/C:/Users/Benny_Uni/eclipse-workspace/BSc_cocitation_coauthor_graph_B_Birner_9719989/libs/neo4j-community-4.4.13-windows/neo4j-community-4.4.13/lib/neo4j-unsafe-4.4.13.jar) to class java.nio.Buffer
WARNING: Please consider reporting this to the maintainers of org.neo4j.internal.unsafe.UnsafeUtil
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
Exception in thread "main" java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V
at org.neo4j.cypher.internal.options.CypherVersion$.<init>(CypherQueryOptions.scala:131)
at org.neo4j.cypher.internal.options.CypherVersion$.<clinit>(CypherQueryOptions.scala)
at org.neo4j.cypher.internal.config.CypherConfiguration.<init>(CypherConfiguration.scala:60)
at org.neo4j.cypher.internal.config.CypherConfiguration$.fromConfig(CypherConfiguration.scala:40)
at org.neo4j.cypher.internal.config.CypherConfiguration.fromConfig(CypherConfiguration.scala)
at org.neo4j.fabric.bootstrap.FabricServicesBootstrap.bootstrapServices(FabricServicesBootstrap.java:144)
at org.neo4j.graphdb.factory.module.edition.CommunityEditionModule.bootstrapFabricServices(CommunityEditionModule.java:401)
at org.neo4j.graphdb.facade.DatabaseManagementServiceFactory.build(DatabaseManagementServiceFactory.java:148)
at org.neo4j.dbms.api.DatabaseManagementServiceBuilder.newDatabaseManagementService(DatabaseManagementServiceBuilder.java:101)
at org.neo4j.dbms.api.DatabaseManagementServiceBuilder.build(DatabaseManagementServiceBuilder.java:94)
at HeaderEx.getHeader(HeaderEx.java:37)
at PdfParser.main(PdfParser.java:46)
By this code:
String db_path = System.getProperty("user.dir") + File.separator + "author_database";
File database = new File(db_path);
Path paths = Paths.get(db_path);
managementService = new DatabaseManagementServiceBuilder(paths).build();
graphDb = managementService.database( "DEFAULT_DATABASE_NAME" );
I don´t know, what the problem is. It would be very nice if someone can help me.
I tried to clean the project. I changed the neo4j version. I checkt the configurations rgarding the JDK and JVM. I´m using JDK 11. However, nothing works.

can't work generate JavaDoc in Android Studio - Error "javadoc" finished with exit code 4

"C:\Program Files\Android\Android Studio1\jre\bin\javadoc.exe" -locale en -protected -splitindex -d "F:\Abdeaziz Daoud\Documents\PPC App" #C:\Users\Abdelaziz\AppData\Local\Temp\javadoc_args
Loading source file C:\Users\Abdelaziz\AndroidStudioProjects\PPCApp\app\src\main\java\com\hyperone\ppcapp\MainActivity.java...
Constructing Javadoc information...
C:\Users\Abdelaziz\AndroidStudioProjects\PPCApp\app\src\main\java\com\hyperone\ppcapp\MainActivity.java:4: error: package android.os does not exist
import android.os.Bundle;
^
C:\Users\Abdelaziz\AndroidStudioProjects\PPCApp\app\src\main\java\com\hyperone\ppcapp\MainActivity.java:9: error: cannot access ViewGroup
public class MainActivity extends AppCompatActivity {
^
class file for android.view.ViewGroup not found
javadoc: error - fatal error encountered: java.lang.NullPointerException
javadoc: error - Please file a bug against the javadoc tool via the Java bug reporting page
(http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com)
for duplicates. Include error messages and the following diagnostic in your report. Thank you.
java.lang.NullPointerException
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$MembersPhase.runPhase(TypeEnter.java:934)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$Phase.doCompleteEnvs(TypeEnter.java:282)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$MembersPhase.doCompleteEnvs(TypeEnter.java:877)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$Phase.completeEnvs(TypeEnter.java:251)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$Phase.completeEnvs(TypeEnter.java:266)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$Phase.completeEnvs(TypeEnter.java:266)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter$Phase.completeEnvs(TypeEnter.java:266)
at jdk.compiler/com.sun.tools.javac.comp.TypeEnter.complete(TypeEnter.java:198)
at jdk.compiler/com.sun.tools.javac.code.Symbol.complete(Symbol.java:642)
at jdk.compiler/com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:1326)
at jdk.compiler/com.sun.tools.javac.comp.Enter.complete(Enter.java:583)
at jdk.compiler/com.sun.tools.javac.comp.Enter.main(Enter.java:560)
at jdk.javadoc/jdk.javadoc.internal.tool.JavadocEnter.main(JavadocEnter.java:79)
at jdk.javadoc/jdk.javadoc.internal.tool.JavadocTool.getEnvironment(JavadocTool.java:206)
at jdk.javadoc/jdk.javadoc.internal.tool.Start.parseAndExecute(Start.java:576)
at jdk.javadoc/jdk.javadoc.internal.tool.Start.begin(Start.java:432)
at jdk.javadoc/jdk.javadoc.internal.tool.Start.begin(Start.java:345)
at jdk.javadoc/jdk.javadoc.internal.tool.Main.execute(Main.java:63)
at jdk.javadoc/jdk.javadoc.internal.tool.Main.main(Main.java:52)
4 errors
"javadoc" finished with exit code 4

JPype class not found, $1 with no inner class

I have these java files:
LDF1File.java
LDFFile.java <-- super class
which generate these class files:
LDF1File.class -- there is no inner class
LDF1File$1.class <-- no idea where this comes from
LDFFile.class
In my python code, I can import LDF1File$1, but not LDF1File. I get:
>>> JClass('aero.blue.bdms.ldf.stream.LDF1File')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/eric/Software/anaconda3/envs/blue3/lib/python3.5/site-packages/JPype1-0.6.1-py3.5-linux-x86_64.egg/jpype/_jclass.py", line 55, in JClass
raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name)
jpype._jexception.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class aero.blue.bdms.ldf.stream.LDF1File not found
Here's the full code:
from jpype import *
startJVM(getDefaultJVMPath(), "-ea", '-Xms1024m', '-Xmx4096m', '-Djava.class.path=./jars/bdms-chunkjava-lib-1.0.9-SNAPSHOT.jar:./jars/bdms-ldfjava-lib-1.0.9-SNAPSHOT.jar')
LDF1File = JClass('aero.blue.bdms.ldf.stream.LDF1File')
shutdownJVM()
So i'm not sure why there is a class file with a dollar sign in it's name, and I'm not sure why JPype can't locate LDF1File. Just to rule some possible suggestions out, there is no dependency injection, no aspectj stuff, no spring, no guava. This is just plain java.
I had only included in my classpath the jar of the package i was working with and none of the dependencies. Having added all dependency jars to the classpath, it was able to load LDF1File.java

sklearn2pmml and jpmml-sklearn Usage Errors

I recently came across sklearn2pmml and jpmml-sklearn when looking for a way to convert scikit-learn models to PMML. However, I've been hitting errors when trying to use the basic usage examples that I'm unable to figure out.
When attempting to usage example in sklearn2pmml, I've been receiving the following issue around casting a long as an int:
Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at numpy.core.NDArrayUtil.getShape(NDArrayUtil.java:66)
at org.jpmml.sklearn.ClassDictUtil.getShape(ClassDictUtil.java:92)
at org.jpmml.sklearn.ClassDictUtil.getShape(ClassDictUtil.java:76)
at sklearn.linear_model.BaseLinearClassifier.getCoefShape(BaseLinearClassifier.java:144)
at sklearn.linear_model.BaseLinearClassifier.getNumberOfFeatures(BaseLinearClassifier.java:56)
at sklearn.Classifier.createSchema(Classifier.java:50)
at org.jpmml.sklearn.Main.run(Main.java:104)
at org.jpmml.sklearn.Main.main(Main.java:87)
Traceback (most recent call last):
File "C:\Users\user\workspace\sklearn_pmml\test.py", line 40, in <module>
sklearn2pmml(iris_classifier, iris_mapper, "LogisticRegressionIris.pmml")
File "C:\Python27\lib\site-packages\sklearn2pmml\__init__.py", line 49, in sklearn2pmml
os.remove(dump)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\user\\appdata\\local\\temp\\tmpmxyp2y.pkl'
Any suggestions as to what is going on here?
Usage code:
#
# Step 1: feature engineering
#
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
import pandas
import sklearn_pandas
iris = load_iris()
iris_df = pandas.concat((pandas.DataFrame(iris.data[:, :], columns = ["Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"]), pandas.DataFrame(iris.target, columns = ["Species"])), axis = 1)
iris_mapper = sklearn_pandas.DataFrameMapper([
(["Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"], PCA(n_components = 3)),
("Species", None)
])
iris = iris_mapper.fit_transform(iris_df)
#
# Step 2: training a logistic regression model
#
from sklearn.linear_model import LogisticRegressionCV
iris_X = iris[:, 0:3]
iris_y = iris[:, 3]
iris_classifier = LogisticRegressionCV()
iris_classifier.fit(iris_X, iris_y)
#
# Step 3: conversion to PMML
#
from sklearn2pmml import sklearn2pmml
sklearn2pmml(iris_classifier, iris_mapper, "LogisticRegressionIris.pmml")
EDIT 12/6:
After the new update, the same issue comes up farther down the line:
Dec 06, 2015 5:56:49 PM sklearn_pandas.DataFrameMapper updatePMML
INFO: Updating 1 target field and 3 active field(s)
Dec 06, 2015 5:56:49 PM sklearn_pandas.DataFrameMapper updatePMML
INFO: Mapping target field y to Species
Dec 06, 2015 5:56:49 PM sklearn_pandas.DataFrameMapper updatePMML
INFO: Mapping active field(s) [x1, x2, x3] to [Sepal.Length, Sepal.Width, Petal.Length, Petal.Width]
Traceback (most recent call last):
File "C:\Users\user\workspace\sklearn_pmml\test.py", line 40, in <module>
sklearn2pmml(iris_classifier, iris_mapper, "LogisticRegressionIris.pmml")
File "C:\Python27\lib\site-packages\sklearn2pmml\__init__.py", line 49, in sklearn2pmml
os.remove(dump)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\user\\appdata\\local\\temp\\tmpqeblat.pkl'
JPMML-SkLearn expected that ndarray.shape is tuple of i4 (mapped to java.lang.Integer by the Pyrolite library). However, in this case it was a tuple of i8 (mapped to java.lang.Long). Hence the cast exception.
This issue has been addressed in JPMML-SkLearn commit f7c16ac2fb.
If you should encounter another exception (data translation between platforms could be tricky), then you should also open a JPMML-SkLearn issue about it.

Using a functional java construct (Predicate) from jython

So, I'm attempting to use the selenium java libraries from jython (yes, I know selenium has a python interface, but for good reasons of corporate teamwork accessing the pure java libraries makes more sense if it can be done well).
I'm just trying to do the example script here: http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
Which I've implemented with the following jython code:
from org.openqa.selenium.firefox import FirefoxDriver
from org.openqa.selenium import By
from org.openqa.selenium import WebDriver
from org.openqa.selenium import WebElement
from org.openqa.selenium.support.ui import ExpectedCondition
from org.openqa.selenium.support.ui import WebDriverWait
driver = FirefoxDriver()
driver.get('http://www.google.com')
element = driver.findElement(By.name('q'))
# The array wrapper around the string is the only weird thing I encountered
element.sendKeys(["Cheese!"])
print "Page title is: " + driver.getTitle()
class ExpectedConditionTitle(ExpectedCondition):
def apply(d):
print(type(d))
return d.title.toLowerCase().startsWith(["cheese!"])
def equals(d):
pass
print(type(driver))
WebDriverWait(driver, 10).until(ExpectedConditionTitle().apply())
print driver.getTitle()
driver.quit()
And it's puking on the ExpectedCondition bit. I can't figure out how to make a subclass for the variety desired by until. I've gotten the following errors with variations in my code:
last
Traceback (innermost last):
File "Example.py", line 24, in ?
File "Example.py", line 19, in apply
AttributeError: 'instance' object has no attribute 'title'
and
Traceback (innermost last):
File "Example.py", line 24, in ?
File "Example.py", line 19, in apply
AttributeError: getTitle
and
Traceback (innermost last):
File "Example.py", line 22, in ?
TypeError: until(): 1st arg can't be coerced to com.google.common.base.Function or com.google.common.base.Predicate
The selenium ExpectedCondition interface is basically just a front for the Guava Predicate interface.
I'm not well versed enough in python or java to figure this out. Anyone have any ideas how I might accomplish this?

Categories

Resources