# Flux save and load model

**URL:** <https://discourse.julialang.org/t/flux-save-and-load-model/86693>\
**Category:** New to Julia\
**Created:** [September 2, 2022, 2:33pm UTC](https://discourse.julialang.org/t/flux-save-and-load-model/86693 "2022-09-02T14:33:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)\
**Post date:** [September 2, 2022, 2:33pm UTC](https://discourse.julialang.org/t/flux-save-and-load-model/86693/1 "2022-09-02T14:33:09Z")

</div>

Hello  
I’m saving the UNet model as  
`function save_unet_train_state(filename::String, model::Unet, opt, loss::Function) @assert endswith(filename, ".jld2") @save filename model opt loss`  
where opt is ` opt = RMSProp(learning_rate, momentum)`  
but on loading, i wrote `

```julia
> model_loaded, opt_loaded, loss_loaded = UNet.load_unet(joinpath(pwd(), "UNet.jld2"))
> ┌ Warning: saved type Unet is missing field labels in workspace type; reconstructing
> └ @ JLD2 ~/.julia/packages/JLD2/k9Gt0/src/data/reconstructing_datatypes.jl:152
> ERROR: MethodError: no method matching iterate(::JLD2.ReconstructedTypes.var"##Unet#298")
> Closest candidates are:
> iterate(::Union{LinRange, StepRangeLen}) at range.jl:664
> iterate(::Union{LinRange, StepRangeLen}, ::Int64) at range.jl:664

```

Not sure how to fix this?  
Also, the field label is a vector of classes, e.g. [1,2,3]. I thought saving model will save it as model.labels automatically?

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [September 2, 2022, 11:05pm UTC](https://discourse.julialang.org/t/flux-save-and-load-model/86693/2 "2022-09-02T23:05:47Z")

</div>

You might have to run all of the `using` and `include`s that were present when you saved the module, so that the deserialiser knows how to construct the objects.

EDIT: I am not familiar with UNet, but shouldn’t you be using a `@load` to load the data back? Similar to how it is saved in the first place?

---

<div class="post-metadata">

**Author:** ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)\
**Post date:** [September 3, 2022, 2:41am UTC](https://discourse.julialang.org/t/flux-save-and-load-model/86693/3 "2022-09-03T02:41:58Z")

</div>

> [@jmair](#):
>
> Similar to how it is saved in the first place?

Sorry I didn’t include the original definition of load.  
`@load filename model opt loss labels`  
However, the @save didn’t save one field which is labels.  
Labels was added at training when model was initialized  
` model = Unet(num_channels, labels) |> device`
