Page Nav

HIDE

Breaking News:

latest

Ads Place

Working with Flux.jl Models on the Hugging Face Hub

https://ift.tt/3oFNEjl Image by author This article will go over the details of how to save a model in Flux.jl (the 100% Julia Deep Lea...

https://ift.tt/3oFNEjl
Image by author

This article will go over the details of how to save a model in Flux.jl (the 100% Julia Deep Learning package) and then upload or retrieve it from the Hugging Face Hub. For those who don’t know what Hugging Face (HF) is, it’s like GitHub, but for Machine Learning models. Traditionally, machine learning models would often be locked away and only accessible to the team which created them. HF is taking the machine learning ecosystem by storm so understanding how to use the platfrom in your machine learning workflow is critical. Let’s dive into how you can use your favorite Julia ML package to work with HF ðŸ¤—!

If you have never used Flux.jl before, you can find the installation instructions here: https://fluxml.ai/Flux.jl/stable/#Installation and a quick video on why you might want to use it below:

Okay, now that we have Flux installed and you know why we might want to use it, let’s create a simple model:

julia> using Flux
julia> model = Chain(Dense(10,5,relu),Dense(5,2),softmax)
Chain(Dense(10, 5, relu), Dense(5, 2), softmax)

We can then import the BSON package (which is how we will save our models so they can be consumed by others on Hugging Face)

julia> using BSON: @save

Lastly, we can actually save the model by doing:

julia> @save "mymodel.bson" model

Next, we are going to upload the model to the Hugging Face Hub and then try downloading it in a new session. If you want to learn more about model building in Flux, check out the below video:

Head to: https://huggingface.co and create an account. I won’t go through all of those steps since it is rather straightforward but post on the Hugging Face Discourse instance if you run into any issues: https://discuss.huggingface.co

Now that you are up and running on the HF Hub, we are going to select your profile icon in the top right and then select “New Model”.

Image by author

From there, fill out the relevant details and then create the model. You can find the basic model I created here: https://huggingface.co/LoganKilpatrick/BasicFluxjlModel

After you create the basic model card, the next step is to actually upload the model in BSON format. If you navigate back to the Julia REPL, you can type ; and then pwd to find where you saved your model locally. Mine is saved on my Desktop to make it easy to find. On HF, go to “Filed and versions” and “Add File” on the right side. You should now have a README and a model in BSON format saved to the Hugging Face Hub! Next up, how to get the model back to your computer or anyone else’s?

So now we have a model saved to the HF Hub, the next thing you probably want to do is download someone else’s Flux model (like mine: https://huggingface.co/LoganKilpatrick/BasicFluxjlModel).

HF provides a Python library to interact with the Hub. Since I did not have time (yet) to re-write the whole package in pure Julia, we are going to bust out the trusty PyCall.jl package which allows us to run Python code directly in a Julia REPL session or script.

Take some time to follow the PyCall installation guide: https://github.com/JuliaPy/PyCall.jl#installation and please do open issues if anything is not clear.

Okay, now that you have it up and running, let’s use it!

julia> using PyCall
julia> hf = pyimport("huggingface_hub")
PyObject <module 'huggingface_hub' from '/Users/logankilpatrick/.julia/conda/3/lib/python3.8/site-packages/huggingface_hub/__init__.py'>

Note that this assumes you have huggingface_hub Python package installed in the same Python environment we are using through PyCall.

So what do we really have here? We used PyCall to import a Python package into Julia? Yes! Now that we have it imported, we can use it the same way we would normally use a Python package:

julia> hf.snapshot_download(repo_id="LoganKilpatrick/BasicFluxjlModel", revision="main")
Downloading: 100%|██████████████████████████| 1.18k/1.18k [00:00<00:00, 345kB/s]
Downloading: 100%|██████████████████████████████| 711/711 [00:00<00:00, 130kB/s]
Downloading: 100%|█████████████████████████| 10.3k/10.3k [00:00<00:00, 2.07MB/s]
"/Users/logankilpatrick/.cache/huggingface/hub/LoganKilpatrick__BasicFluxjlModel.077a4b77d6175a09c156a20cf5bed0eac35c97ee"

Now we have the models repo saved locally, let’s again switch the Julia REPL to command line mode by typing in ; and then changing directories to the location of the model file.

shell> cd "/Users/logankilpatrick/.cache/huggingface/hub/LoganKilpatrick__BasicFluxjlModel.077a4b77d6175a09c156a20cf5bed0eac35c97ee"

Just to double check we did everything right, I am going to run a list command:

shell> ls -l
total 32
-rw - - - - 1 logankilpatrick staff 711 Oct 13 08:37 README.md
-rw - - - - 1 logankilpatrick staff 10260 Oct 13 08:37 mymodel.bson

Great! We see the model there. Let’s load it back into Flux now.

julia> using BSON: @load
julia> @load "mymodel.bson" model
julia> model
Chain(Dense(10, 5, relu), Dense(5, 2), softmax)

Boom! We did it 🎊! We successfully created a model with Flux, uploaded it to the Hub, and then downloaded a new model (or the same one you created) and loaded it back into Flux / Julia.

The next step is to keep training models and uploading / sharing them. The Hugging Face hub is a really snazzy way of collaborating with other ML researches and practitioners so I will see you there!

P.S. I also created a Julia Language organization on HF so if you have some impressive models you want to share, ping me and I can add you to the organization (https://huggingface.co/JuliaLanguage).

~ Logan Kilpatrick (https://twitter.com/OfficialLoganK)


Working with Flux.jl Models on the Hugging Face Hub 🤗 was originally published in Towards Data Science on Medium, where people are continuing the conversation by highlighting and responding to this story.



from Towards Data Science - Medium https://ift.tt/3dEmzqq
via RiYo Analytics

No comments

Latest Articles