deeplearning4j Expected model class name Model (found Functional) - java

deeplearning4j returning "Expected model class name Model (found Functional)." when trying to load a keras model.
same happens when try to load it as sequential.
python :
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
model.save("resnet50.h5")
java(spring)
public INDArray useModel(String name, Long id) throws Exception{
File file= new File("src/main/uploads/image.jpg");
String resnet = "src/main/uploads/model/resnet50.h5";
ComputationGraph model = KerasModelImport.importKerasModelAndWeights(resnet);
NativeImageLoader loader = new NativeImageLoader(224, 224, 3);
INDArray image = loader.asMatrix(file);
INDArray[] output = model.output(image);
return output[0];
}
mavn deps :
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-modelimport</artifactId>
<version>1.0.0-beta7</version>
</dependency>
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-core</artifactId>
<version>1.0.0-beta7</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>1.0.0-beta7</version>
</dependency>
<dependency>
<groupId>org.datavec</groupId>
<artifactId>datavec-data-image</artifactId>
<version>1.0.0-beta7</version>
</dependency>
any help fixing the problem or guidence to another library would be appreciated,

I got the same problem when I tried to load my model either functional or sequentiel using DPL4J.
I solved the problem by downgrading tensorflow on python and re-train my model.
Then I used the loaded the file on Java.

Related

How to use JUnit5 Parametrized Test CSV file source?

I am trying to run a parametrized test from CSV-file.
It is working if I use just CSVSource like that:
#ParameterizedTest
#CsvSource({ "a,A", "b,B" })
void csvSourceTest(String input, String expected) {
String actualValue = input.toUpperCase();
assertEquals(expected, actualValue);
}
But if I try the same thing from a file it won't work:
#ParameterizedTest
#CsvFileSource(resources = "/data.csv")
void csvFileSourceTest(String input, String expected) {
String actualValue = input.toUpperCase();
assertEquals(expected, actualValue);
}
I have also tried using a hard path to my file but for the file test in Eclipse I always get the message
No tests found with test runnter 'JUnit 5'.
Where does JUnit expect the file?
These are my dependencies:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
Does anybody know what I might be missing or where the error is?
Thanks in advance
PAUL
The JUnit5 user guide states that the CSV file needs to be on the classpath. In case of a Maven project that would be src/test/resources.
In Kotlin
CSV file is placed here:
/src/test/resources/post_data.csv
Test read csv test data:
#ParameterizedTest(name = "{index} => post with {0} : {1} : {2}")
#CsvFileSource(resources = ["/post_data.csv"],)
fun testPostWithValidData(id: String?, text: String?, completed: String?) {
//test body
......
}
Gradle dependencies:
// JUnit5
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-params:$junitVersion")

Unirest JSONElement NoSuchMethodError

I am using Unirest library to call publically available REST endpoint with following lines of code:
public void callRest() {
String url = "https://upstream.com/token"
HttpResponse<JsonNode> response = (HttpResponse<JsonNode>) Unirest.post(url).
field("username", "###").
field("password", "###").
field("grant_type", "password").
field("client_id", "####").
field("client_secret", "####").asJson().getBody();
}
I am getting the following error:
Exception in thread "main" java.lang.NoSuchMethodError: com.google.gson.Gson.newBuilder()Lcom/google/gson/GsonBuilder;
at kong.unirest.json.JSONElement.<clinit>(JSONElement.java:39)
at kong.unirest.JsonNode.<init>(JsonNode.java:44)
at kong.unirest.JsonResponse.toJsonNode(JsonResponse.java:49)
at kong.unirest.JsonResponse.getNode(JsonResponse.java:43)
at kong.unirest.JsonResponse.<init>(JsonResponse.java:35)
at kong.unirest.apache.BaseApacheClient.transformBody(BaseApacheClient.java:53)
at kong.unirest.apache.ApacheClient.request(ApacheClient.java:127)
at kong.unirest.BaseRequest.asJson(BaseRequest.java:213)
The line that stands out is
at kong.unirest.json.JSONElement.<clinit>(JSONElement.java:39)
where the unirest library is just declaring
private static transient final Gson PRETTY_GSON = new Gson().newBuilder().setPrettyPrinting().create();
In JSONElement class. I have followed the exact guideline given on Unirest documentation which you can find here.
The POM dependencies I have added are as follows :
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.3.00</version>
</dependency>
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.3.00</version>
<classifier>standalone</classifier>
</dependency>
Actually it was a version conflict with Unirest. When I deleted
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.3.00</version>
<classifier>standalone</classifier>
</dependency>
and fetched result by just .asJson() then everything worked for me.

java.lang.NoSuchFieldError: DEFAULT

I am currently using vscode and apache poi, created a program to automatically create a .xlsx program and have A1 cell input a String called "Tester", and that error pops up.
Codes in my program:
package excel_reader;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelWriter {
public static void main (String[] args) throws Exception {
//ExcelReader eR = new ExcelReader();
XSSFWorkbook workbook = new XSSFWorkbook(); // here is the7 line 13
// first sheet create
XSSFSheet sheet = workbook.createSheet("FirstExcelSheet");
// first row create - A
XSSFRow row = sheet.createRow(0);
// first cell create - 1
XSSFCell cell = row.createCell(0); // A-1
// give data into A-1 cell
cell.setCellValue("Tester");
// Output as an excel file
workbook.write(new FileOutputStream("C:\\Users\\Sonic\\Desktop\\book.xlsx"));
workbook.close();
// C:\\Users\\Sonic\\Desktop\\book.xlsx
}
}
Error Code:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell aka.ms/pscore6
PS E:\excel_reader_v.1_beta1-0> cd 'e:\excel_reader_v.1_beta1-0'; & 'C:\Users\Sonic\.vscode\extensions\vscjava.vscode-java-debug-0.24.0\scripts\launcher.bat' 'C:\Users\Sonic\AppData\Local\Programs\AdoptOpenJDK\bin\java' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Sonic\AppData\Local\Temp\cp_5wuvlu562pjj6rfd2pconxvl4.jar' 'excel_reader.ExcelWriter'
Exception in thread "main" java.lang.NoSuchFieldError: DEFAULT
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:161)
at excel_reader.ExcelWriter.main(ExcelWriter.java:13)
pom.xml (dependency):
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
Apache POI is new to me, please do help me, I will be very appreciated, thank you very much.
Cause of java.lang.NoSuchFieldError
If you search StackOverflow for tag nosuchfieldexception you will find a lot of similar questions. Most of them have 2 things in common:
the error says that a CONSTANT field could not be found
the cause is that 2 or more dependencies (i.e. third-party JAR files) which are related have different version.
This version mismatch may also cause your error:
java.lang.NoSuchFieldError: DEFAULT
Since the constant DEFAULT of class org.apache.poi.ss.formula.udf.UDFFinder existed in versions prior to 3.17, but now (versions 4.0 and newer) has been deprecated. Means, it does not exist in version 4.0 and above.
Problem at runtime:
If statement new XSSFWorkbook(); (at line 13) is executed using dependency (JAR) poi-ooxml, then it uses a UDFFinder of the core dependency (JAR) poi and thus tries to find the CONSTANT field DEFAULT (which may not be there, since versions mismatch).
So I suppose, your versions of Apache poi and poi-ooxml are mismatching (not the same level).
See similar question with version mismatch using Apache Poi.
Solution: matching versions!
If you are using Maven, make sure you have these dependencies on your POM:
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
At least both have to be the same version!
If you are managing your classpath and JARs manually, watch out for the filename of all apache-poi JAR files and make sure the versions are same.
Code seems very close to working
If you just want to write your freshly created Excel-Workbook to a file, e.g. /tmp/MyFirstExcel.xlsx (on Linux) or C:/Users/Sonic/MyFirstExcel.xlsx (on Windows, supposed your Windows-User is Sonic) then use this code:
public class ExcelWriter {
static final String FILE_PATH_LINUX = "/tmp/MyFirstExcel.xlsx";
static final String FILE_PATH_WIN = "C:/Users/Sonic/MyFirstExcel.xlsx"; // note: replace Window's backslash '\' by either double-backslash '\\' or single forward-slash '/'
public static void main(String[] args) throws FileNotFoundException, IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("FirstExcelSheet"); // first sheet create
XSSFRow row = sheet.createRow(0); // first row create - A
XSSFCell cell = row.createCell(0); // first cell create - A-1
cell.setCellValue("Tester"); // give data into A-1 cell
// Output as an excel file
workbook.write(new FileOutputStream(FILE_PATH_WIN));
workbook.close();
}
}
Unclear usage of class ExcelReader:
There is no obvious reason to extend from ExcelReader.
Thus and for simplicity you can omit the extends ExcelReader, declaration of ExcelReader eR = new ExcelReader(); and usage eR.getImpoFileLink() to get the file-path. Otherwise please post the source-code of class ExcelReader to your question, to make it a minimal reproducible example.
Minimal working solution (above) explained:
Instead just put a file-path to desired folder and file, like constant FILE_PATH_WIN here.
I saw your related question few hours ago, where you did that.
Supposed you fixed your other issues/questions here, as well as resolved the version-mismatch with POI dependencies here, the solution given above should compile and write out the desired Excel workbook (.xlsx) with sheet FirstExcelSheet and cell A1 containing string Tester.
See also
See this tutorial on Apache POI – Reading and Writing Excel file in Java.
Because putting this in the comment would not be feasible. Could you try to remove this portion of code in the pom.xml and try it again?
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
based on cursory reading. it has been defined in the bellow code
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
The only apache-poi dependencies you need in pom.xml are
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0</version>
</dependency>
Please remove others.
When I removed the others, my code worked.

Java: IndentingXMLStreamWriter alternative?

I am using StAX to create a quite large xml document. Until now I was using the IndentingXMLStreamwriter class to get a well formatted document (see also this answer). A few days ago we setup a jenkins server with an older jdk version (6.26), on which i get build errors.
package com.sun.xml.internal.txw2.output does not exist
I assume the package cannot be found because of the installed jdk version. For different reasons this cannot be changed
(by the way, does anyone know the jdk version, at which this package (com.sun.xml.internal.txw2.output) was added?).
Therefore I am looking for an alternative to do the indenting. I would prefer a solution similar to the one I was using, which means without reparsing the document. Any ideas or suggestions?
Thanks
Lars
Instead of com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter use com.sun.xml.txw2.output.IndentingXMLStreamWriter that can be found in:
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>txw2</artifactId>
<version>2.2.11</version>
</dependency>
Just expanding on Michael Kay's answer ( https://stackoverflow.com/a/10108591/2722227 ).
maven dependencies:
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>9.6.0-5</version>
</dependency>
java code:
import java.io.OutputStream;
import net.sf.saxon.Configuration;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.Serializer.Property;
public class Main {
public static void main(String[] args) throws Exception {
OutputStream outputStream = System.out;
writeXmlDocument(outputStream);
}
private static void writeXmlDocument(OutputStream outputStream){
Configuration config = new Configuration();
Processor processor = new Processor(config);
Serializer serializer = processor.newSerializer();
serializer.setOutputProperty(Property.METHOD, "xml");
serializer.setOutputProperty(Property.INDENT, "yes");
serializer.setOutputStream(outputStream);
try {
XMLStreamWriter writer = serializer.getXMLStreamWriter();
try {
writer.writeStartDocument();
{
writer.writeStartElement("root_element_name");
{
writer.writeStartElement("child_element");
writer.writeEndElement();
}
writer.writeEndElement();
}
writer.writeEndDocument();
writer.flush();
writer.close();
} catch (XMLStreamException e) {
e.printStackTrace();
}
} catch (SaxonApiException e) {
e.printStackTrace();
}
}
}
If other suggestions don't work, you can get an indenting XMLStreamWriter from Saxon like this:
Processor p = new net.sf.saxon.s9api.Processor();
Serializer s = p.newSerializer();
s.setOutputProperty(Property.METHOD, "xml");
s.setOutputProperty(Property.INDENT, "yes");
s.setOutputStream(....);
XMLStreamWriter writer = s.getXMLStreamWriter();
One advantage is that this allows you a lot of control over the serialization using other serialization properties.
There is an alternative implementation of IndentingXmlStreamWriter, which is provided as part of the open source stax-utils project here: http://java.net/projects/stax-utils/pages/Home
stax-utils seems to be a project set up to provide utilities based around the jsr-173 streaming xml api for Java
You'd need to add the stax-utils jar as a dependency for your project. Then you can import javanet.staxutils.IndentingXmlStreamWriter
Since stax-utils is in the maven central repository, if you use maven for your dependencies you can get it with:
<dependency>
<groupId>net.java.dev.stax-utils</groupId>
<artifactId>stax-utils</artifactId>
<version>20070216</version>
<exclusions>
<exclusion>
<groupId>com.bea.xml</groupId>
<artifactId>jsr173-ri</artifactId>
</exclusion>
</exclusions>
</dependency>
Functionality seems very similar / equivalent to the txw2 class
I have excluded jsr173-ri since I am using jdk 1.7. I think 1.6+ has the jsr173 api as a standard feature, but if you are using 1.5 or lower you'd need the extra jsr173 jar.
If you are using Maven and Java 8, you can import the following to use this class:
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.17</version>
</dependency>
And then, you import it as: import com.sun.xml.txw2.output.IndentingXMLStreamWriter;

XPath can't find an engine

I inherited some code that is using XPath for which I am a novice. I have it now so that it loads the document, but when the document.selectPath(queryPath) it always fails with the following error:
java.lang.RuntimeException: Trying XBeans path engine... Trying XQRL... Trying delegated path engine... FAILED on //
at org.apache.xmlbeans.impl.store.Path.getCompiledPath(Path.java:173)
at org.apache.xmlbeans.impl.store.Path.getCompiledPath(Path.java:130)
at org.apache.xmlbeans.impl.store.Cursor._selectPath(Cursor.java:902)
at org.apache.xmlbeans.impl.store.Cursor.selectPath(Cursor.java:2634)
at org.apache.xmlbeans.impl.values.XmlObjectBase.selectPath(XmlObjectBase.java:462)
at org.apache.xmlbeans.impl.values.XmlObjectBase.selectPath(XmlObjectBase.java:446)
Thank you jor for the post. I was confused as earlier commands to xml beans were successful.
Without saxon, this still works:
MapDocument doc;
...
String cityQuery = "$this//City";
XmlObject[] cities = doc.selectPath(cityQuery);
However saxon is required for explicit selection of fields within tags:
String aveQuery= "$this//Street[Kind='Avenue']";
XmlObject[] avenues = doc.selectPath(aveQuery); // RuntimeException without saxon on path
java.lang.RuntimeException:
Trying XBeans path engine... Trying XQRL... Trying delegated path engine... FAILED on $this//Street[Kind='Avenue']
I hope this might be of use to others that encounter a similar issue.
You need an XPath engine in your classpath, which one bepends on the XMLBeans version, see
http://wiki.apache.org/xmlbeans/XmlBeansFaq#whatJars
The movement if you have [] in your xpath, it is searching for the external xpath enginer.. you have to download saxonb9-0-0-4j & xmlbeans-xpath-2.4.0.jar and add to the classpath
worked for me:
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>10.6</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>

Categories