【问题标题】:Unable to import onnx in JAVA无法在JAVA中导入onnx
【发布时间】:2023-02-17 00:41:13
【问题描述】:

我用 JAVA 的工作不多,但我需要加载一个用 python 训练的模型,并检查我是否可以用 JAVA 进行推理。我正在尝试在 JAVA 中加载一个 onnx 文件。为此,我在 JAVA 中导入了 onnx,但它抛出了一个包不存在的错误。

test_first % javac src/main/java/org/example/Main.java
src/main/java/org/example/Main.java:3: error: package ai.onnxruntime.OrtSession.Result does not exist
import ai.onnxruntime.OrtSession.Result.*;
^
1 error

我已将 onnx 添加为我的代码的依赖项:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>test_first</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.microsoft.onnxruntime/onnxruntime -->
        <dependency>
            <groupId>com.microsoft.onnxruntime</groupId>
            <artifactId>onnxruntime</artifactId>
            <version>1.13.1</version>
        </dependency>
    </dependencies>

</project>

在此之后运行 pom.xml 以获取依赖项。我可以在我的外部库中看到它: directory structure image

但是,当我在添加 import 语句后尝试编译 java 代码时,出现了上述错误。

我也试过跑步:

mvn install:install-file -Dfile=/Users/XXXX/.m2/repository/com/microsoft/onnxruntime/onnxruntime/1.13.1/onnxruntime-1.13.1.jar -DgroupId=com.microsoft.onnxruntime -DartifactId=onnxruntime -Dversion=1.13.1 -Dpackaging=jar -DgeneratePom=true

但它说工件已经存在于本地存储库中。

我正在运行以下代码来检查导入是否成功。

package org.example;


import ai.onnxruntime.OrtEnvironment;

public class Main {
    public static void main(String[] args) {
        var env = OrtEnvironment.getEnvironment();
    }
}

【问题讨论】:

  • 使用 import ai.onnxruntime.OrtSession.Result 导入单个类结果。使用 ai.onnxruntime.OrtSession.* 从包中导入所有类。使用 import static ai.onnxruntime.OrtSession.Result.* 从结果类导入所有静态方法。

标签: java java-11 onnx onnxruntime


【解决方案1】:

我不完全确定这是否有帮助,但是将导入从 import ai.onnxruntime.OrtSession.Result.*; 更改为 import ai.onnxruntime.OrtSession.Result; 对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2021-09-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多