Getting started with importing Keras functional Models

Let’s say you start with defining a simple MLP using Keras’ functional API:

  1. from keras.models import Model
  2. from keras.layers import Dense, Input
  3. inputs = Input(shape=(100,))
  4. x = Dense(64, activation='relu')(inputs)
  5. predictions = Dense(10, activation='softmax')(x)
  6. model = Model(inputs=inputs, outputs=predictions)
  7. model.compile(loss='categorical_crossentropy',optimizer='sgd', metrics=['accuracy'])

In Keras there are several ways to save a model. You can store the whole model(model definition, weights and training configuration) as HDF5 file, just themodel configuration (as JSON or YAML file) or just the weights (as HDF5 file).Here’s how you do each:

  1. model.save('full_model.h5') # save everything in HDF5 format
  2. model_json = model.to_json() # save just the config. replace with "to_yaml" for YAML serialization
  3. with open("model_config.json", "w") as f:
  4. f.write(model_json)
  5. model.save_weights('model_weights.h5') # save just the weights.

If you decide to save the full model, you will have access to the training configuration ofthe model, otherwise you don’t. So if you want to further train your model in DL4J after import,keep that in mind and use model.save(…) to persist your model.

Loading your Keras model

Let’s start with the recommended way, loading the full model back into DL4J (we assume it’son your class path):

  1. String fullModel = new ClassPathResource("full_model.h5").getFile().getPath();
  2. ComputationGraph model = KerasModelImport.importKerasModelAndWeights(fullModel);

In case you didn’t compile your Keras model, it will not come with a training configuration.In that case you need to explicitly tell model import to ignore training configuration bysetting the enforceTrainingConfig flag to false like this:

  1. ComputationGraph model = KerasModelImport.importKerasModelAndWeights(fullModel, false);

To load just the model configuration from JSON, you use KerasModelImport as follows:

  1. String modelJson = new ClassPathResource("model_config.json").getFile().getPath();
  2. ComputationGraphConfiguration modelConfig = KerasModelImport.importKerasModelConfiguration(modelJson)

If additionally you also want to load the model weights with the configuration, here’s what you do:

  1. String modelWeights = new ClassPathResource("model_weights.h5").getFile().getPath();
  2. MultiLayerNetwork network = KerasModelImport.importKerasModelAndWeights(modelJson, modelWeights)

In the latter two cases no training configuration will be read.


KerasModel

[source]

Build ComputationGraph from Keras (Functional API) Model orSequential model configuration.

KerasModel
  1. public KerasModel(KerasModelBuilder modelBuilder)
  2. throws UnsupportedKerasConfigurationException, IOException, InvalidKerasConfigurationException

(Recommended) Builder-pattern constructor for (Functional API) Model.

  • param modelBuilder builder object
  • throws IOException IO exception
  • throws InvalidKerasConfigurationException Invalid Keras config
  • throws UnsupportedKerasConfigurationException Unsupported Keras config
getComputationGraphConfiguration
  1. public ComputationGraphConfiguration getComputationGraphConfiguration()
  2. throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException

(Not recommended) Constructor for (Functional API) Model from model configuration(JSON or YAML), training configuration (JSON), weights, and “training mode”boolean indicator. When built in training mode, certain unsupported configurations(e.g., unknown regularizers) will throw Exceptions. When enforceTrainingConfig=false, thesewill generate warnings but will be otherwise ignored.

  • param modelJson model configuration JSON string
  • param modelYaml model configuration YAML string
  • param enforceTrainingConfig whether to enforce training-related configurations
  • throws IOException IO exception
  • throws InvalidKerasConfigurationException Invalid Keras config
  • throws UnsupportedKerasConfigurationException Unsupported Keras config
getComputationGraph
  1. public ComputationGraph getComputationGraph()
  2. throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException

Build a ComputationGraph from this Keras Model configuration and import weights.

  • return ComputationGraph
getComputationGraph
  1. public ComputationGraph getComputationGraph(boolean importWeights)
  2. throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException

Build a ComputationGraph from this Keras Model configuration and (optionally) import weights.

  • param importWeights whether to import weights
  • return ComputationGraph