Error while loading Serialized BSON

I have a BSON file, I used to save a Flux network using

 @save modelpath model #Loss_params_1 batchSize NbBatch lr weigths

I’m trying to load “model”, but encountered the following error:

julia> d2 = BSON.load(myPath, @__MODULE__)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Tuple{Int64}

Closest candidates are:
  convert(::Type{T}, ::T) where T<:Tuple
   @ Base essentials.jl:411
  convert(::Type{T}, ::Tuple{Vararg{Any, N}}) where {N, T<:Tuple}
   @ Base essentials.jl:412
  convert(::Type{T}, ::CartesianIndex) where T<:Tuple
   @ Base multidimensional.jl:128
  ...

Stacktrace:
  [1] newstruct(::Type, ::Function, ::Vararg{Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/extensions.jl:159
  [2] (::BSON.var"#47#48")(d::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/extensions.jl:185
  [3] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:82
  [4] (::BSON.var"#49#50")(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/extensions.jl:194
  [5] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:92
  [6] (::BSON.var"#23#24"{IdDict{Any, Any}, Module})(x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:98
  [7] applychildren!(f::BSON.var"#23#24"{IdDict{Any, Any}, Module}, x::Vector{Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/BSON.jl:26
  [8] raise_recursive
    @ ~/.julia/packages/BSON/mzJoC/src/read.jl:98 [inlined]
  [9] (::BSON.var"#18#21"{IdDict{Any, Any}, Module})(x::Vector{Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:82
 [10] applychildren!(f::BSON.var"#18#21"{IdDict{Any, Any}, Module}, x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/BSON.jl:19
 [11] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:82
 [12] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:93
 [13] (::BSON.var"#23#24"{IdDict{Any, Any}, Module})(x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:98
 [14] applychildren!(f::BSON.var"#23#24"{IdDict{Any, Any}, Module}, x::Vector{Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/BSON.jl:26
 [15] raise_recursive
    @ ~/.julia/packages/BSON/mzJoC/src/read.jl:98 [inlined]
 [16] (::BSON.var"#18#21"{IdDict{Any, Any}, Module})(x::Vector{Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:82
 [17] applychildren!(f::BSON.var"#18#21"{IdDict{Any, Any}, Module}, x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/BSON.jl:19
 [18] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:82
 [19] (::BSON.var"#49#50")(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/extensions.jl:194
 [20] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:92
--- the last 8 lines are repeated 2 more times ---
 [37] (::BSON.var"#23#24"{IdDict{Any, Any}, Module})(x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:98
 [38] applychildren!(f::BSON.var"#23#24"{IdDict{Any, Any}, Module}, x::Vector{Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/BSON.jl:26
 [39] raise_recursive
    @ ~/.julia/packages/BSON/mzJoC/src/read.jl:98 [inlined]
 [40] (::BSON.var"#18#21"{IdDict{Any, Any}, Module})(x::Vector{Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:82
 [41] applychildren!(f::BSON.var"#18#21"{IdDict{Any, Any}, Module}, x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/BSON.jl:19
 [42] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:82
 [43] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:93
 [44] (::BSON.var"#23#24"{IdDict{Any, Any}, Module})(x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:98
 [45] applychildren!(f::BSON.var"#23#24"{IdDict{Any, Any}, Module}, x::Vector{Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/BSON.jl:26
 [46] raise_recursive
    @ ~/.julia/packages/BSON/mzJoC/src/read.jl:98 [inlined]
 [47] (::BSON.var"#18#21"{IdDict{Any, Any}, Module})(x::Vector{Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:82
 [48] applychildren!(f::BSON.var"#18#21"{IdDict{Any, Any}, Module}, x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/BSON.jl:19
 [49] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:82
 [50] (::BSON.var"#49#50")(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/extensions.jl:194
 [51] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:92
 [52] (::BSON.var"#19#22"{IdDict{Any, Any}, Module})(x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:86
 [53] applychildren!(f::BSON.var"#19#22"{IdDict{Any, Any}, Module}, x::Dict{Symbol, Any})
    @ BSON ~/.julia/packages/BSON/mzJoC/src/BSON.jl:19
 [54] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:86
 [55] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:93
 [56] raise_recursive
    @ ~/.julia/packages/BSON/mzJoC/src/read.jl:103 [inlined]
 [57] load(x::String, init::Module)
    @ BSON ~/.julia/packages/BSON/mzJoC/src/read.jl:108

It used to work fine, even with different sessions, but yesterday it returned the error and I don’t know why. I may have updated a package without knowing it, but not to my knowledge.

Working with Julia 1.9.3 ,

  [28312eec] Alert v1.2.0
  [fbb218c0] BSON v0.3.9
  [336ed68f] CSV v0.10.14
  [052768ef] CUDA v5.4.3
  [35d6a980] ColorSchemes v3.26.0
  [5ae59095] Colors v0.12.11
  [717857b8] DSP v0.7.9
  [a93c6f00] DataFrames v1.6.1
  [31a5f54b] Debugger v0.7.9
  [8bb1440f] DelimitedFiles v1.9.1
  [a7b11256] DigitalComm v1.2.0
  [7a1cc6ca] FFTW v1.8.0
  [4c728ea3] Flatten v0.4.3
  [587475ba] Flux v0.14.19
  [5903a43b] Infiltrator v1.8.3
  [3587e190] InverseFunctions v0.1.16
  [682c06a0] JSON v0.21.4
  [23992714] MAT v0.10.7
  [bac558e1] OrderedCollections v1.6.3
  [8314cec4] PGFPlotsX v1.6.1
  [d7d3b36b] ParameterSchedulers v0.4.2
  [fbb45041] Pickle v0.3.5
  [91a5bcdd] Plots v1.40.5
  [21216c6a] Preferences v1.4.3
  [08abe8d2] PrettyTables v2.3.2
  [92933f4c] ProgressMeter v1.10.2
  [6f49c342] RCall v0.14.4
  [9896bf05] RFImpairmentsModels v0.3.0
  [295af30f] Revise v3.5.18
  [02a925ec] cuDNN v1.3.2
  [8ba89e20] Distributed
  [b77e0a4c] InteractiveUtils
  [37e2e46d] LinearAlgebra
  [56ddb016] Logging
  [9a3f8284] Random
  [10745b16] Statistics v1.9.0

Has anyone an idea how to solve my issue ?

Here’s a MWE:

using CUDA
using Flux
using BSON: @save, @load



MyFunc(x1,x2,x3) = x1 + x1.*x2+ x1.*x3
function create_DAConv_chain(fea_dim, in_channels, out_channels, kernel_size, stride, padding)
    out_len = Int(floor((fea_dim + 2 * padding - (kernel_size - 1) - 1) / stride + 1))
    conv_branch = Conv((kernel_size,), in_channels => out_channels, pad=padding, stride=stride)
    spatial_att_conv = Conv((kernel_size,), in_channels => 1, stride=2, pad=0)
    spatial_att_pool = MaxPool((kernel_size,), stride=2, pad=0)
    spatial_att_deconv = ConvTranspose((kernel_size,), 1 => 1, stride=2, pad=0)
    upsample = Upsample(:bilinear, size=(out_len))
    channel_att_fc1 = Dense(fea_dim, out_channels)
    channel_att_fc2 = Dense(in_channels, 1)
    
    return Parallel(MyFunc; a = Chain(
        conv_branch,
    ),
    b = Chain(
        spatial_att_conv,
        relu,
        spatial_att_pool,
        spatial_att_deconv,
        relu,
        upsample,
        x -> Flux.sigmoid.(x)
    ),
    c = Chain(
        channel_att_fc1,
        x -> Flux.tanh.(x),
        x -> permutedims(x, (2, 1, 3)),
        channel_att_fc2,
        x -> sum(x, dims=3),
        x -> Flux.sigmoid.(x),
    ))

end

cnnPath = "/home/ebothere/Models/16_Aug_2024_10_19_06_454.bson"
@load cnnPath model

I can’t seems to be able to join the .bson to this post…

Thank you for your time !

Hi, and welcome to the Julia community!

Your MWE works fine for me.

MWE output
julia> using CUDA, Flux, BSON;

julia> MyFunc(x1,x2,x3) = x1 + x1.*x2+ x1.*x3;

julia> function create_DAConv_chain(fea_dim, in_channels, out_channels, kernel_size, stride, padding)
    out_len = Int(floor((fea_dim + 2 * padding - (kernel_size - 1) - 1) / stride + 1))
    conv_branch = Conv((kernel_size,), in_channels => out_channels, pad=padding, stride=stride)
    spatial_att_conv = Conv((kernel_size,), in_channels => 1, stride=2, pad=0)
    spatial_att_pool = MaxPool((kernel_size,), stride=2, pad=0)
    spatial_att_deconv = ConvTranspose((kernel_size,), 1 => 1, stride=2, pad=0)
    upsample = Upsample(:bilinear, size=(out_len))
    channel_att_fc1 = Dense(fea_dim, out_channels)
    channel_att_fc2 = Dense(in_channels, 1)
    
    return Parallel(MyFunc; a = Chain(
        conv_branch,
    ),
    b = Chain(
        spatial_att_conv,
        relu,
        spatial_att_pool,
        spatial_att_deconv,
        relu,
        upsample,
        x -> Flux.sigmoid.(x)
    ),
    c = Chain(
        channel_att_fc1,
        x -> Flux.tanh.(x),
        x -> permutedims(x, (2, 1, 3)),
        channel_att_fc2,
        x -> sum(x, dims=3),
        x -> Flux.sigmoid.(x),
    ))

end;

julia> model = create_DAConv_chain(16, 32, 32, 3, 1, 1)
Parallel(
  MyFunc,
  a = Chain(
    Conv((3,), 32 => 32, pad=1),        # 3_104 parameters
  ),
  b = Chain(
    Conv((3,), 32 => 1, stride=2),      # 97 parameters
    NNlib.relu,
    MaxPool((3,), stride=2),
    ConvTranspose((3,), 1 => 1, stride=2),  # 4 parameters
    NNlib.relu,
    Upsample(:bilinear, size = 16),
    var"#11#16"(),
  ),
  c = Chain(
    Dense(16 => 32),                    # 544 parameters
    var"#12#17"(),
    var"#13#18"(),
    Dense(32 => 1),                     # 33 parameters
    var"#14#19"(),
    var"#15#20"(),
  ),
)                   # Total: 10 arrays, 3_782 parameters, 16.320 KiB.

julia> BSON.@save "test.bson" model

julia> d2 = BSON.load("test.bson", @__MODULE__)
Dict{Symbol, Any} with 1 entry:
  :model => Parallel(MyFunc, a = Chain(Conv((3,), 32 => 32, pad=1)), b = Chain(Conv((3,), 32 => 1, stride=2), relu, Max…

julia> x = rand(16, 32, 32); d2[:model](x) ≈ model(x)
true
versioninfo and st
julia> versioninfo()
Julia Version 1.10.4
Commit 48d4fd4843 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, skylake)
Threads: 8 default, 0 interactive, 4 GC (on 8 virtual cores)
Environment:
  JULIA_NUM_THREADS = auto

(jl_jLEJ1D) pkg>  st
Status `(...)\Temp\jl_jLEJ1D\Project.toml`
  [fbb218c0] BSON v0.3.9
  [052768ef] CUDA v5.4.3
  [587475ba] Flux v0.14.19

You could try to work in a temporary environment with only the bare minimum of packages (as above): (@v1.9) pkg> activate --temp.

Do you only get your error for this specific bson file, or also for the ones produced from your MWE (or my interpretation thereof)?

Hi ! Thank you for your answer !
Yes, the issue is definitely due to my file. Is there any way I can force open/ignore this error to access at least some of the values contained in this file ?

You can try to see if BSON.parse(filename) gives you something useful.

The issue is likely due to the CUDA library. When loading on a machine without GPU, no error occurred. I don’t really know why, but I was able to retrieve my network ! Thank you for your help !