使用deeplearning4j在Java中加载Keras模型时发生异常

时间:2019-05-23 13:54:22

标签: keras deeplearning4j

我目前正在尝试实施本指南: https://towardsdatascience.com/deploying-keras-deep-learning-models-with-java-62d80464f34a

我已经用tf和Keras训练了一个模型,并将其导出到文件中。我想在Java中使用模型,并尝试用deeplearning4j加载它。

我已经看过其他帖子,似乎没有人遇到同样的异常。

培训模式:

model = keras.Sequential()
...
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
...
history = model.fit(X, y, epochs=30, batch_size=512, validation_split=0.1)
model.save("model.h5")

以Java加载:

...
String simpleMlp = new ClassPathResource(path).getFile().getPath();
model = KerasModelImport.importKerasSequentialModelAndWeights(simpleMlp);
...

OR

...
String json = new ClassPathResource(path1).getFile().getPath();
String weights = new ClassPathResource(path2).getFile().getPath();
model = KerasModelImport.importKerasSequentialModelAndWeights(json, weights);
...

我收到以下异常(对于两个Java代码):

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.
Exception in thread "main" java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.util.List (java.util.LinkedHashMap and java.util.List are in module java.base of loader 'bootstrap')
    at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:102)
    at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:61)
    at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildSequential(KerasModelBuilder.java:320)
    at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasSequentialModelAndWeights(KerasModelImport.java:195)
    at seminar.java_model_loading.machinelearning.Predictor.<init>(Predictor.java:19)
    at seminar.java_model_loading.App.main(App.java:27)

1 个答案:

答案 0 :(得分:0)

原来,我必须为deeplearning4j更新我的Maven依赖项:

使用

...
version>1.0.0-beta4</version>
...

代替

...
version>1.0.0-beta2</version>
...

因此,指南已过时且错误。