Path for loading PyTorch model in Java with DJL - pytorch

I trained a custom PyTorch model and saved it as a .pt file.
I'm now trying to load this model in Java using DJL.
Path modelDir = Paths.get("/Users/myname/eclipse-workspace/myProject/src/ML/");
Model model = Model.newInstance("model.pt");
model.load(modelDir);
However, this gives the following exception:
ai.djl.engine.EngineException: No deep learning engine found.
I found that even when I change the path to something totally invalid, I get the same error. So I think the issue is with the path or the model name. What am I doing wrong? I'm running the project via Maven in Eclipse by the way.
Thank you!

From the error message, looks like you haven't specify PyTorch Engine/Native dependencies in your projects. You need to supply that first. Then please follow the instruction here: https://docs.djl.ai/jupyter/load_pytorch_model.html to load your pytorch model

You need include pytorch engine package in your project:
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-engine</artifactId>
<version>0.16.0</version>
<scope>runtime</scope>
</dependency>
DJL support multiple engines (e.g. PyTorch, TensorFlow, Apache MXNet). These engine packages are shipped in separate maven packages. DJL will locate the engine in the class path at runtime.

Related

ONNX model with sub operator error does not bind

I am trying to create a session with a squeezenet ONNX model using:
session = winrt::Windows::AI::MachineLearning::LearningModelSession{ model, winrt::Windows::AI::MachineLearning::LearningModelDevice(deviceKind) };
I have two versions of squeezenet. One has a 'Sub' layer at the beginning, and the other does not. The one with 'Sub' throws an error when the above is executed.
Any ideas what is happening here?
What is the shape of the input for the squeezenet that is failing? If you post the failing model we can help you troubleshoot it.
2 key things to make it work for OS build 17763:
Make sure you are using ONNX versions 1.2 (opset 7)
Make sure your input has the right shape that the model is expecting.
What error are you getting when you create the LearningModelSession ?
The existence of 'Sub' operator in ONNX graph should not affect whether or not you can run that model on Windows. I think the more important question is the ONNX version (or the operator set version) and the target Windows version. Starting in October 2018 update, windows machine learning will be compatible with ONNX version 1.2.2 (https://github.com/onnx/onnx/releases/tag/v1.2.2). Double check if you have the model 1.2.2 and October 2018 update SDK (10.0.17763.x)

Was OpenSagres allowed to use Apache POI-like packages?

While searching for a free solution to implement a DOC(x)-to-PDF transformation for an internal business application, I stumbled upon the org.apache.poi.xwpf.converter.* packages.
I found some examples e.g.
http://www.programcreek.com/java-api-examples/index.php?api=org.apache.poi.xwpf.converter.pdf.PdfOptions
I saw the packages starting with "org.apache.poi..." so I thought "oh great, OSS". But after further search, I realized that source code isn't coming from Apache, rather from an obscure French company I never heard of, OpenSagres: http://www.opensagres.fr/
For example http://poi.apache.org/apidocs/index.html has nothing about those packages.
I then found the matching Javadoc API: http://oss.opensagres.fr/xdocreport/javadoc/1.0.3/org/apache/poi/xwpf/converter/core/package-summary.html
...as well as source code on GitHub:
https://github.com/opensagres/xdocreport/wiki/XWPFConverterPDFViaIText org.apache.poi.xwpf.converter.pdf provides...
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>${XDOCREPORT_VERSION}</version>
</dependency>
https://github.com/opensagres/xdocreport/wiki/XWPFConverterXHTML org.apache.poi.xwpf.converter.xhtml provides...
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>XDOCREPORT_VERSION</version>
</dependency>
Jars are also available (for download) inside docx.converters-xxx-sample.zip archive file, packaged (under the "libs" folder) as:
org.apache.poi.xwpf.converter.core-1.0.4.jar
org.apache.poi.xwpf.converter.pdf-1.0.4.jar
org.apache.poi.xwpf.converter.xhtml-1.0.4.jar
And when you look at the source code, there is even a copyright for The XDocReport Team e.g. see
https://github.com/opensagres/xdocreport/blob/master/thirdparties-extension/org.apache.poi.xwpf.converter.core/src/main/java/org/apache/poi/xwpf/converter/core/AbstractXWPFConverter.java
Copyright (C) 2011-2015 The XDocReport Team <xdocreport#googlegroups.com>
How is that even possible? I mean, prefixing a package from a well-known entity which releases OSS should be restricted, no?
Moreover, even if not coming from Apache, I can't use this source code/library/whatever, because:
(it) depends on iText 2.1.7 due to licensing issue(s), see: iText 5.5.0 with XDocReport 1.0.4 which should not be used anymore (but can I really trust the commenter... as he's the author of the code??? https://stackoverflow.com/users/1622493/bruno-lowagie )
there's a version with iText 5.x (https://github.com/opensagres/xdocreport/tree/master/thirdparties-extension/org.apache.poi.xwpf.converter.pdf.itext5) but it's not free! see http://itextpdf.com/Pricing
So how am I supposed to do?

SLF4J version mismatch with GATE stanford parser

I am trying to use stanford parser in GATE and this is the error I am getting.
The requested version 1.5.6 by your slf4j binding is not compatible with [1.6, 1.7]
Now, I have downloaded a file a slf4j-api-1.5.6 and have put it in same location as I had my slf4j-api-1.7 file. (ivy2\cache\org.slf4j\slf4j-api\jars) This path is also set as a classpath.
I also deleted slf-4j-1.7 but it keeps reoccurring itself every time I start GATE.
How should I resolve this issue? Any suggestions will be helpful.
Thanks!

May I use jxls and apache poi together?

I'm making an application to analize some data and the result must be presented in excel files. In that sense I started to use Apache POI (3.11). Due to some reports consumes a lot of time and memory to be reproduce, I made an investigation and I found jxls, after some test I thought was the solution. But now I found a problem: can´t work both frameworks together.
I have to update Apache POI from 3.11 to 3.14, in order to work with jxls-2.3.0
I made an extra package in order to make my tests with jxls, not problem
I try to migrated one of my classes from Apache POI to jxls, and a I got this error: java.lang.IllegalStateException: Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath. This is the code of my method:
private void prepareNewReport(File excelFile) {
List perforaciones = makePerforacionReport
.makePerforacionData(escenario);
try (InputStream is = ReportePerforacionTotalDialog.class
.getResourceAsStream("PerforacionTotal_template.xls")){
try (OutputStream os = new FileOutputStream(excelFile)) {
Context context = new Context();
context.putVar("perforaciones", perforaciones);
JxlsHelper.getInstance().processTemplate(is, os, context);
LOGGER.logger.log(Level.INFO, "Archivo de perfortacion generado con éxito");
}
} catch (IOException e) {
LOGGER.logger.log(Level.SEVERE, "Problemas buscando el archivo", e);
}
}
How could be this possible?. In the same project I have my test class, just another package and its working fine. As you can see it´s not so much different from the example in the jxls page and the imports are the same.
But even worst, when I tried to make clean & build of my project, then I got this other error:
java.lang.RuntimeException: com.sun.tools.javac.code.Symbol$CompletionFailure: class file for org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes.CTArray not found
I looked at every library that I importede in order to work with jxls and apache poi, and that´s rigth, that class is not there. Just to see if there a conflict among these two framewoks, I eliminated from the class path all libraries needed to use jxls. Clean & build again, and not problem, I have my .jar file to send to my customer, but incomplete.
I could try to replace all classes that use Apache POI, but that means a lot of work, since POI is used in my project to read excel files with data many times and to write another many files to excel. I planned to use jxls in order to take advantage of use templates.
I will apreciate any help or suggestion.
For the first error, it would appear that the JXLS transformer for Apache POI is missing in your classpath when running the application. Check the JXLS getting started info here: http://jxls.sourceforge.net/getting_started.html
As it is explained in Transformers section (see Main Concepts)) Jxls core module does not depend on any specific Java-Excel library and works with Excel exclusively through a predefined interface. Currently Jxls supplies two implementations of this interface in separate modules based on the well-known Apache POI and Java Excel API libraries.
If you're using maven, be sure to include in your pom.xml the jxls-poi dependency listed on the JXLS getting started page:
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.9</version>
</dependency>
For the second issue, org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes.CTArray is not in the apache POI ooxml schemas jar files for either 3.11 (poi-ooxml-schemas-3.11-20141221.jar) or 3.14 (poi-ooxml-schemas-3.14-20160307.jar). POI uses a stripped down set of ooxml schema classes, you will need to get the ooxml schemas complete jar from http://central.maven.org/maven2/org/apache/poi/ooxml-schemas/1.3/ or if you're using maven (or another build tool), get the dependency for your build from https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas/1.3
e.g for maven:
<!-- https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
Be sure to remove the poi-ooxml-schemas dependency from your maven pom.xml so that ooxml-schemas above takes precedence instead.

What driver are you expected to provide for Reflections.collect() to work from a Groovy script?

I have the following snippet of scratch code
import com.google.appengine.api.datastore.Entity
import org.reflections.Reflections
Reflections r = Reflections.collect()
Set<Class<?>> entities = r.getTypesAnnotatedWith(Entity.class)
print entities
that throws the following exception:
org.xml.sax.SAXException: Can't create default XMLReader; is system property org.xml.sax.driver set?
at org.xml.sax.helpers.XMLReaderFactory.createXMLReader(Unknown Source)
at org.dom4j.io.SAXHelper.createXMLReader(SAXHelper.java:83)
Googling for org.xml.sax.SAXException: Can't create default XMLReader; is system property org.xml.sax.driver set? brings up questions, mostly about Android with link only answers or code based answers that do not actually address the issue of providing the correct system property value.
The same code works as Java code from the same IDE project.
So what do I have to supply to get this to work as a Groovy script?
I have this script in the src/test/groovy in my Maven project so I added.
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.crimson</artifactId>
<version>1.1.3_2</version>
<scope>test</scope>
</dependency>
to my pom.xml
And I added -Dorg.xml.sax.driver=org.apache.crimson.parser.XMLReaderImpl to the VM Options: in the Run/Debug Configuration for the script.
This makes it work, but I would still like to know what I can used without having to add a dependency to get things in the test scope to run since things in the main scope work without this dependency.

Resources