# How to import a pre-trained neural network into Flux.jl ?

**URL:** <https://discourse.julialang.org/t/how-to-import-a-pre-trained-neural-network-into-flux-jl/9659>\
**Category:** Machine Learning\
**Created:** [March 12, 2018, 8:41am UTC](https://discourse.julialang.org/t/how-to-import-a-pre-trained-neural-network-into-flux-jl/9659 "2018-03-12T08:41:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nirvan101](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@Nirvan101](https://discourse.julialang.org/u/Nirvan101)\
**Post date:** [March 12, 2018, 8:41am UTC](https://discourse.julialang.org/t/how-to-import-a-pre-trained-neural-network-into-flux-jl/9659/1 "2018-03-12T08:41:12Z")

</div>

I’m using the Flux library in Julia. How can I import an Inception neural network into it?

I’ve seen that the following can be done in Mocha.jl to import a pre-trained net.

```
using HDF5 
h5open("model/bvlc_reference_caffenet.hdf5", "r") do h5
load_network(h5, net)
end
init(net)

```

Is there some way I can import a pre-trained model in Flux?

---

<div class="post-metadata">

**Author:** ![MikeInnes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikeinnes/32/3656_2.png) [@MikeInnes](https://discourse.julialang.org/u/MikeInnes)\
**Post date:** [March 12, 2018, 12:22pm UTC](https://discourse.julialang.org/t/how-to-import-a-pre-trained-neural-network-into-flux-jl/9659/2 "2018-03-12T12:22:04Z")

</div>

What models are you looking to load?

For models you trained in Flux itself, see [loading and saving models](https://fluxml.github.io/Flux.jl/stable/saving.html) in the docs.

For models trained in other frameworks there are fewer options, though it’s not that hard to write a parser for most model formats. We have [ONNX.jl](https://github.com/FluxML/ONNX.jl/), which we used to [load VGG19](https://github.com/FluxML/Metalhead.jl), but it’s pretty rough around the edges right now.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 12, 2018, 3:56pm UTC](https://discourse.julialang.org/t/how-to-import-a-pre-trained-neural-network-into-flux-jl/9659/3 "2018-03-12T15:56:25Z")

</div>

xref: [neural network - How to use a pre-trained model in Flux.jl? - Stack Overflow](https://stackoverflow.com/questions/49229103/how-to-use-a-pre-trained-model-in-flux-jl)

---

<div class="post-metadata">

**Author:** ![Nirvan101](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@Nirvan101](https://discourse.julialang.org/u/Nirvan101)\
**Post date:** [March 12, 2018, 5:47pm UTC](https://discourse.julialang.org/t/how-to-import-a-pre-trained-neural-network-into-flux-jl/9659/4 "2018-03-12T17:47:21Z")

</div>

I want to load the InceptionV3 model. Any way to do this?

[https://keras.io/applications/#inceptionv3](https://keras.io/applications/#inceptionv3)

---

<div class="post-metadata">

**Author:** ![MikeInnes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikeinnes/32/3656_2.png) [@MikeInnes](https://discourse.julialang.org/u/MikeInnes)\
**Post date:** [March 12, 2018, 7:36pm UTC](https://discourse.julialang.org/t/how-to-import-a-pre-trained-neural-network-into-flux-jl/9659/5 "2018-03-12T19:36:52Z")

</div>

You have two options:

(1) Write a keras model reader (which would be a great contribution!)  
(2) Export the model to ONNX from keras, and then load it via ONNX.jl

---

<div class="post-metadata">

**Author:** ![Phil\_Tomson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phil_tomson/32/3575_2.png) [@Phil\_Tomson](https://discourse.julialang.org/u/Phil_Tomson)\
**Post date:** [March 17, 2018, 2:12am UTC](https://discourse.julialang.org/t/how-to-import-a-pre-trained-neural-network-into-flux-jl/9659/6 "2018-03-17T02:12:20Z")

</div>

Is there any documentation on using ONNX.jl to read in an onnx file into Flux?

Also, is there a way to go from flux → onnx? (If not, I’d be interesting in working on something like this and would welcome pointers on getting started)

---

<div class="post-metadata">

**Author:** ![wizofe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wizofe/32/13056_2.png) [@wizofe](https://discourse.julialang.org/u/wizofe)\
**Post date:** [April 23, 2020, 9:57pm UTC](https://discourse.julialang.org/t/how-to-import-a-pre-trained-neural-network-into-flux-jl/9659/7 "2020-04-23T21:57:19Z")

</div>

@MikeInnes, could you provide a bit more info/pointers on creating a Keras model reader? 🙂 Sounds like an interesting project.

---

<div class="post-metadata">

**Author:** ![MikeInnes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikeinnes/32/3656_2.png) [@MikeInnes](https://discourse.julialang.org/u/MikeInnes)\
**Post date:** [April 24, 2020, 10:42am UTC](https://discourse.julialang.org/t/how-to-import-a-pre-trained-neural-network-into-flux-jl/9659/8 "2020-04-24T10:42:37Z")

</div>

Sure. The steps are basically:

- Read the Keras format into a Julia data structure
- Turn the data structure into Julia code (probably via IRTools)
- Either `eval` this code or turn it into a `.jl` script.

I’m not that familiar with the Keras format, but I think they may separate the weights and architecture into HDF5 and JSON files. If so that’d make step 1 pretty easy, since we have readers for both of those formats.
