Load Keras models in Julia.
This is not a wrapper around Keras. This is built on top of Flux, to directly load Keras models into Flux. [W.I.P]
Loading a model in Flux is fairly simple. Clone this repository into ~/.julia/v0.6
. Make sure you have all dependencies installed. In order to load a model, you need to have two files:
- The
model.json
file. This stores the structure of the model. This can be obtained from any Keras model using themodel.to_json()
method. - The
weights.h5
file. This stores the weights associated with different layers of the pre-trained Keras model. This file can be produced from a Keras model usingKeras.save_weights(weight_file_name)
.
(The files can have any other name (as long as they are in the correct format). I'm using model.json and weights.h5 as an example here)
Keras models can broadly be divided into two categories:
- The models using the
sequential
API. - The models using the
functional
API. (Also calledModel
API)
Due to subtle differences in their structure and functioning, you need to follow different steps to run these models in Flux. You can check the type of the model by:
>>> using Keras
>>> Keras.check_modeltype("model.json")
>>> using Keras
>>> model = Keras.load("model.json", "weights.h5")
model
is now the corresponding model in Flux. This can be used directly as:
>>> model(rand(28,28,1,1))
Another straight-forward way of running such models is:
>>> using Keras
>>> Keras.load("model.json", "weights.h5", ip)
Where ip
is our input. This directly returns the models output.
Functional models can be tricky as they may consist of a number of sub-graphs within themselves. Running such models is similar to the second way of running Sequential models mentioned above.
>>> using Keras
>>> Keras.load("model.json", "weight.h5", ip)
Where ip
is the input to our model. This directly returns the output. (Note: Currently there is no other way of running functional API models).
Keras.jl also allows you to get the intermediate outputs of a model. Suppose your model contains m
layers, and you need the output
after n
layers (m > n
).
>>> model[1:n](ip)
Should give you the output after exactly n
layers.
The process of loading and running a Keras model in Flux mainly consists of two parts:
- Converting all Keras operators to Flux ops.
- Generating the computation graph from the Flux operators obtained.
In order to get correct results, make sure that the value of mode
parameter is set to 0 (here). It's default value is 0, so if you haven't
played around with NNlib.jl, you're good to go!
Since this is currently under development, feel free to open any issue you encounter. You can post your queries on Julia Slack, generally on the #machine-learning channel.
Lambda layers cannot be handled at this moment. This is because we'd need to handle the Python AST, for parsing it as JSON.