M.Winkens
M.Winkens

Reputation: 148

Exception when loading a Keras Model in Java with deeplearning4j

I am currently trying to implement this guide: https://towardsdatascience.com/deploying-keras-deep-learning-models-with-java-62d80464f34a

I already trained a model with tf and Keras and exported it to a file. I want to use the Model in java and tried to load it with deeplearning4j.

I already looked at other posts and nobody seems to get the same exception.

Training Model:

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")

Loading in 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);
...

I get the following exception (for both java codes):

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)

Upvotes: 2

Views: 893

Answers (1)

M.Winkens
M.Winkens

Reputation: 148

Turns out I had to update my maven dependencies for deeplearning4j:

Use

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

instead of

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

So the guide is outdated and wrong.

Upvotes: 2

Related Questions