# Using pretrained pytorch models in Flux

**URL:** <https://discourse.julialang.org/t/using-pretrained-pytorch-models-in-flux/39082>\
**Category:** Machine Learning\
**Tags:** flux\
**Created:** [May 8, 2020, 12:54pm UTC](https://discourse.julialang.org/t/using-pretrained-pytorch-models-in-flux/39082 "2020-05-08T12:54:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [May 8, 2020, 12:54pm UTC](https://discourse.julialang.org/t/using-pretrained-pytorch-models-in-flux/39082/1 "2020-05-08T12:54:56Z")

</div>

Hi all. I’m excited to be able to dip my toes back into the Julia pond. I’ve missed the community these last few years.

My question is about how to best leverage existing pytorch resources, such as the extensive pretrained NLP models in [🤗 Transformers](https://huggingface.co/transformers), while doing research using Flux and other Julia packages.

I understand I can always call it directly through PyCall as a backup, but I’m curious about the best Julia-only approach to exploiting the massive amount of resources going into pytorch packages.

Thanks in advance, and great work with the language and package ecosystem!

---

<div class="post-metadata">

**Author:** ![avik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avik/32/17_2.png) [@avik](https://discourse.julialang.org/u/avik)\
**Post date:** [May 8, 2020, 2:46pm UTC](https://discourse.julialang.org/t/using-pretrained-pytorch-models-in-flux/39082/2 "2020-05-08T14:46:31Z")

</div>

Not sure there is a very simple or clear answer yet, but I’ll point to [Torch.jl](https://github.com/dhairyagandhi96/Torch.jl) and [Peter’s ongoing GSoC project](https://summerofcode.withgoogle.com/projects/#6047607169744896)

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [May 8, 2020, 3:16pm UTC](https://discourse.julialang.org/t/using-pretrained-pytorch-models-in-flux/39082/3 "2020-05-08T15:16:46Z")

</div>

Thanks! The GSoC link looks very relevant 😀. Do you have more details on the planned scope and timing of that project?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 8, 2020, 3:32pm UTC](https://discourse.julialang.org/t/using-pretrained-pytorch-models-in-flux/39082/4 "2020-05-08T15:32:54Z")

</div>

The other way to do that is to export the pytorch net to onnx ([torch.onnx — PyTorch 1.12 documentation](https://pytorch.org/docs/stable/onnx.html)) and read it in with flux ([GitHub - FluxML/ONNX.jl: Read ONNX graphs in Julia](https://github.com/FluxML/ONNX.jl))

---

<div class="post-metadata">

**Author:** ![chengchingwen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chengchingwen/32/8390_2.png) [@chengchingwen](https://discourse.julialang.org/u/chengchingwen)\
**Post date:** [May 9, 2020, 4:40am UTC](https://discourse.julialang.org/t/using-pretrained-pytorch-models-in-flux/39082/5 "2020-05-09T04:40:51Z")

</div>

Hi, @tbreloff  
The current planned method of the GSoC project will be reading the serialized state\_dict and rebuild the model according to the weight names. PyTorch have several way to save a model, one was by extract the state\_dict of the model and save it in a pickle like format. I can release some draft code for loading the state\_dict next week if you need it.

---

<div class="post-metadata">

**Author:** ![JosePereiraUA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiraua/32/37640_2.png) [@JosePereiraUA](https://discourse.julialang.org/u/JosePereiraUA)\
**Post date:** [February 8, 2021, 11:08am UTC](https://discourse.julialang.org/t/using-pretrained-pytorch-models-in-flux/39082/6 "2021-02-08T11:08:33Z")

</div>

I there, do we have an update on this idea/project. How should one go about doing this, right now?

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [February 8, 2021, 5:11pm UTC](https://discourse.julialang.org/t/using-pretrained-pytorch-models-in-flux/39082/7 "2021-02-08T17:11:59Z")

</div>

[https://github.com/chengchingwen/Transformers.jl](https://github.com/chengchingwen/Transformers.jl) reads pre-trained weights from HuggingFace and more using [GitHub - chengchingwen/Pickle.jl: An experimental package for loading and saving object in Python Pickle format.](https://github.com/chengchingwen/Pickle.jl). You should be able to build off of the latter for other models (e.g. torch.hub).
