1

I’m working on a project which needs to train a DNN and then run the trained model in a Java environment. As the training data requires a lot of manipulation I’ve used Knime and its Keras libraries to do most of the work, and I’m exported the trained model to a Keras h5 file which can be loaded into DL4J (I’m using Knime Keras Integration 4.1.0v201911171432 and DL4J 1.0.0-beta6).

The modelling is ongoing, but for a POC I’ve created a network by chaining the following nodes:

  • Keras Input Layer (shape 144)
  • Keras Dense Layer (units 500)
  • Keras Dense Layer (units 500)
  • Keras Dense Layer (units 1)
  • Keras Set Output Layers (single output shape [1] pointing to the last layer)

This then gets trained and outputted with a “Keras Network Writer” node which writes a .h5 file.

In Java I have includes the following dependencies:

 <dependency>
      <groupId>org.deeplearning4j</groupId>
      <artifactId>deeplearning4j-core</artifactId>
      <version>1.0.0-beta6</version>
    </dependency>
    <dependency>
      <groupId>org.nd4j</groupId>
      <artifactId>nd4j-native-platform</artifactId>
      <version>1.0.0-beta6</version>
    </dependency>

And run the following code:

import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;
…
ComputationGraph model = KerasModelImport.importKerasModelAndWeights("2x2x2keras.h5");

Which returns the following exception

java.lang.IllegalStateException: Invalid configuration: network has no outputs. Use .setOutput(String...) to specify (and give an ordering to) the output vertices, or use allowNoOutputs(true) to disable this check
    at org.deeplearning4j.nn.conf.ComputationGraphConfiguration.validate(ComputationGraphConfiguration.java:375) ~[deeplearning4j-nn-1.0.0-beta6.jar:na]
    at org.deeplearning4j.nn.conf.ComputationGraphConfiguration$GraphBuilder.build(ComputationGraphConfiguration.java:1197) ~[deeplearning4j-nn-1.0.0-beta6.jar:na]
    at org.deeplearning4j.nn.modelimport.keras.KerasModel.getComputationGraphConfiguration(KerasModel.java:394) ~[deeplearning4j-modelimport-1.0.0-beta6.jar:na]
    at org.deeplearning4j.nn.modelimport.keras.KerasModel.getComputationGraph(KerasModel.java:415) ~[deeplearning4j-modelimport-1.0.0-beta6.jar:na]
    at org.deeplearning4j.nn.modelimport.keras.KerasModel.getComputationGraph(KerasModel.java:404) ~[deeplearning4j-modelimport-1.0.0-beta6.jar:na]
    at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasModelAndWeights(KerasModelImport.java:173) ~[deeplearning4j-modelimport-1.0.0-beta6.jar:na]
    at dev.aisandbox.tftest.TFRunner.run(TFRunner.java:18) ~[classes/:na]

As this is the first time I’ve tried using Knime to manage my dataflow, I can’t be certain if the issues is with the file (being created in Knime) or the code loading it (into DL4J+Java). I’m assuming the comment about setOutput is intended for when you are creating the network with a python script, but can’t see anyway to set allowNoOutputs when KerasModelImport only has static methods.

2 Answers2

0

Is the Keras model a Sequential? or a Model? If it's a Sequential use KerasModelImport.importKerasSequentialModelAndWeights() instead.

wm_eddie
  • 3,938
  • 22
  • 22
  • Like you, I assumed that it would be a sequential model (as there is a sequence of layers with a single output); but when using importKerasSequentialModelAndWeights I got: InvalidKerasConfigurationException: Model class name must be Sequential (found Model). For more information, see http://deeplearning4j.org/docs/latest/keras-import-overview – Graham Evans May 10 '20 at 13:57
0

You can try use Deep Java Library. Here is document about how to load Keras model into DJL.

And you can find example project here.

Frank Liu
  • 281
  • 1
  • 4