How it works...

In step 1, we used KerasModelImport to load the external Keras model from disk. If the model was saved separately by calling model.to_json() and model.save_weights()  (in Keras), then we need to use the following variant:

String modelJsonFileLocation = new ClassPathResource("kerasModel.json").getFile().getPath();
String modelWeightsFileLocation = new ClassPathResource("kerasModelWeights.h5").getFile().getPath();
MultiLayerNetwork model = KerasModelImport.importKerasSequentialModelAndWeights(modelJsonFileLocation, modelWeightsFileLocation, enforceTrainConfig);

Note the following:

  • importKerasSequentialModelAndWeights(): Imports and creates MultiLayerNetwork from the Keras model
  • importKerasModelAndWeights(): Imports and creates ComputationGraph from the Keras model

Consider the following implementation for the importKerasModelAndWeights() method to perform step 2:

KerasModelImport.importKerasModelAndWeights(modelJsonFileLocation,modelWeightsFileLocation,enforceTrainConfig);

The third attribute, enforceTrainConfig, is a Boolean type, which indicates whether to enforce a training configuration or not. Again, if the model was saved separately using the model.to_json() and model.save_weights() Keras calls, then we need to use the following variant:

String modelJsonFileLocation = new ClassPathResource("kerasModel.json").getFile().getPath();
String modelWeightsFileLocation = new ClassPathResource("kerasModelWeights.h5").getFile().getPath();
ComputationGraph model = KerasModelImport.importKerasModelAndWeights(modelJsonFileLocation,modelWeightsFileLocation,enforceTrainConfig);

In step 3, we discussed how to load ComputationGraph from the external model using KerasModelBuilder. One of the builder methods is inputShape()It assigns input shape to the imported Keras model. DL4J requires the input shape to be specified. However, you don't have to deal with these if you go for the first two methods, discussed earlier, for the Keras model import. Those methods (importKerasModelAndWeights() and importKerasSequentialModelAndWeights()) internally make use oKerasModelBuilder to import models. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset