DJL运行报错

文章讲述了用户在使用DJL库时遇到的初始化错误,涉及SLF4J和TensorFlow/MXNet引擎加载问题。通过逐步排查和调整Maven依赖,最终确定了正确的配置以解决运行错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在首次运行DJL库时出现报错

Simulation progress : [ 0 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
ai.djl.engine.EngineException: No deep learning engine found.
Please refer to https://github.com/deepjavalibrary/djl/blob/master/docs/development/troubleshooting.md for more details.
	at ai.djl.engine.Engine.getInstance(Engine.java:140)
	at ai.djl.Model.newInstance(Model.java:72)
	at ai.djl.Model.newInstance(Model.java:61)

进程已结束,退出代码0

跳转到GitHub文档的问题解释:

链接地址 https://github.com/deepjavalibrary/djl/blob/master/docs/development/troubleshooting.md

github里提出的两种解决方案:

  1. 打成胖包导致的问题
  2. IntelliJ 和 Gradle 之间不匹配导致的

结果发现并不能解决我的问题


需要选择不同的本地库构建,如 Apache MXNet、PyTorch、TensorFlow 和 ONNX Runtime,

发现需要引用相关的引擎(ps:还是太天真了)

跳转链接
MXNet Engine
PyTorch Engine
TensorFlow Engine
ONNX Engine

里面介绍需要在Maven中添加依赖

       <dependency>
            <groupId>ai.djl.tensorflow</groupId>
            <artifactId>tensorflow-engine</artifactId>
            <version>0.25.0</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>ai.djl.tensorflow</groupId>
            <artifactId>tensorflow-native-auto</artifactId>
            <version>2.2.0</version>
        </dependency>

添加之后又有新的错误问题

Simulation progress : [ 0 Exception in thread "main" java.lang.ExceptionInInitializerError
	at ai.djl.tensorflow.engine.TfEngineProvider.getEngine(TfEngineProvider.java:36)
	at ai.djl.engine.Engine.initEngine(Engine.java:59)
	at ai.djl.engine.Engine.<clinit>(Engine.java:49)
	at ai.djl.Model.newInstance(Model.java:71)
	at ai.djl.Model.newInstance(Model.java:60)
	at examples.MyClass.DQNLearning.<init>(DQNLearning.java:93)
	...
Caused by: ai.djl.engine.EngineException: Failed to load TensorFlow native library
	at ai.djl.tensorflow.engine.TfEngine.newInstance(TfEngine.java:75)
	at ai.djl.tensorflow.engine.TfEngineProvider$InstanceHolder.<clinit>(TfEngineProvider.java:40)
	... 20 more
Caused by: java.lang.NoSuchMethodError: 'java.lang.String ai.djl.util.Utils.getEnvOrSystemProperty(java.lang.String)'
	at ai.djl.tensorflow.engine.javacpp.LibUtils.findOverrideLibrary(LibUtils.java:72)
	at ai.djl.tensorflow.engine.javacpp.LibUtils.getLibName(LibUtils.java:64)
	at ai.djl.tensorflow.engine.javacpp.LibUtils.loadLibrary(LibUtils.java:48)
	at ai.djl.tensorflow.engine.TfEngine.newInstance(TfEngine.java:53)

我认为是是模型库没有这个方法的实现
我又将Maven换为

        <dependency>
            <groupId>ai.djl.mxnet</groupId>
            <artifactId>mxnet-native-auto</artifactId>
            <version>1.7.0-backport</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>basicdataset</artifactId>
            <version>0.8.0</version>
        </dependency>

        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>model-zoo</artifactId>
            <version>0.8.0</version>
        </dependency>

之后报错就变为了(PS难受啊)

Exception in thread "main" java.lang.NoSuchMethodError: 'ai.djl.Device ai.djl.Device.defaultIfNull(ai.djl.Device)'
	at ai.djl.mxnet.engine.MxModel.<init>(MxModel.java:64)
	at ai.djl.mxnet.engine.MxEngine.newModel(MxEngine.java:88)
	at ai.djl.Model.newInstance(Model.java:71)
	at ai.djl.Model.newInstance(Model.java:60)
	at com.mechalikh.pureedgesim.simulationmanager.DefaultSimulationManager.processEvent(DefaultSimulationManager.java:178)
	at com.mechalikh.pureedgesim.simulationengine.PureEdgeSim.processEvent(PureEdgeSim.java:178)
	at com.mechalikh.pureedgesim.simulationengine.PureEdgeSim.processFutureEventsHappeningAtSameTimeOfTheFirstOne(PureEdgeSim.java:158)
	at com.mechalikh.pureedgesim.simulationengine.PureEdgeSim.runClockTickAndProcessFutureEvents(PureEdgeSim.java:133)
	at com.mechalikh.pureedgesim.simulationengine.PureEdgeSim.start(PureEdgeSim.java:106)
	at com.mechalikh.pureedgesim.simulationmanager.DefaultSimulationManager.startSimulation(DefaultSimulationManager.java:106)
	at com.mechalikh.pureedgesim.simulationmanager.SimulationThread.startSimulation(SimulationThread.java:137)
	at com.mechalikh.pureedgesim.simulationmanager.Simulation.launchSimulation(Simulation.java:158)
	at examples.Example8.main(Example8.java:69)
经过不断奋斗

将maven文件换成如下就可以正确运行–问题解决

 <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>api</artifactId>
            <version>0.8.0</version>
        </dependency>

        <dependency>
            <groupId>ai.djl.mxnet</groupId>
            <artifactId>mxnet-engine</artifactId>
            <version>0.8.0</version>
        </dependency>

        <dependency>
            <groupId>ai.djl.mxnet</groupId>
            <artifactId>mxnet-native-auto</artifactId>
            <version>1.7.0-backport</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.26</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.26</version>
        </dependency>

        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.3.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>basicdataset</artifactId>
            <version>0.8.0</version>
        </dependency>

        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>model-zoo</artifactId>
            <version>0.8.0</version>
        </dependency>

        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.4</version>
        </dependency>

### 使用 DJL 运行 PTH 文件 PTH 文件通常是 PyTorch 模型保存的格式。为了使用 Deep Java Library (DJL) 加载并运行这些模型,可以遵循以下方法: #### 准备工作 确保已经安装了必要的依赖项来支持 PyTorch 模型加载。这可以通过 Maven 或 Gradle 来完成,在构建工具配置文件中加入相应的依赖声明[^1]。 对于 Maven 用户来说,应该在 `pom.xml` 中添加如下片段: ```xml <dependency> <groupId>ai.djl.pytorch</groupId> <artifactId>pytorch-engine</artifactId> <version>0.23.0</version> </dependency> ``` 而对于采用 Gradle 的项目,则需向 `build.gradle` 添加下面的内容: ```groovy implementation 'ai.djl.pytorch:pytorch-engine:0.23.0' ``` #### 载入模型 一旦设置了环境,就可以通过指定路径来读取 `.pth` 文件中的预训练模型。这里给出一段简单的代码示例用于展示如何实现这一点: ```java import ai.djl.Model; import ai.djl.inference.Predictor; import ai.djl.modality.cv.Image; import ai.djl.repository.zoo.Criteria; import ai.djl.translate.TranslateException; public class LoadPthModel { public static void main(String[] args) throws Exception { Criteria<Image, Classifications> criteria = Criteria.builder() .setTypes(Image.class, Classifications.class) .optEngine("PyTorch") // 设置引擎为 PyTorch .optModelUrls("/path/to/your/model.pth") // 替换为实际的 pth 文件位置 .optTranslator(new ImageClassificationTranslator()) .build(); try (Predictor<Image, Classifications> predictor = ModelZoo.loadModel(criteria).newPredictor()) { // 接下来可以根据需求调用predictor.predict()来进行预测操作... } catch (TranslateException e) { System.err.println(e.getMessage()); } } } ``` 这段程序展示了怎样利用 DJL 创建一个基于图像分类的任务准则,并指定了要使用的 PyTorch 模型的位置以及翻译器实例化的方式[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值