DrChainsaw / ONNXNaiveNASflux.jl

Import/export ONNX models
MIT License
44 stars 2 forks source link

"ERROR: Use broadcasting (`relu.(x)`)" when importing resnet18 #17

Closed natema closed 4 years ago

natema commented 4 years ago

After failing to import ResNet with ONNX.jl (onnx file here), I gave ONNXmutable.jl a try:

using ONNXmutable
g = ONNXmutable.CompGraph("models/resnet18-v1-7.onnx")

However, when I run g(ones(224,224,3,1)), I get

ERROR: Use broadcasting (`relu.(x)`) to apply activation functions to arrays.

Any idea?

DrChainsaw commented 4 years ago

Hi and thanks for the report.

The problem is most likely that I forgot that activation functions in flux must be broadcasted.

When they are inside Flux-layers this is done by the layer, but sometimes they end up as their own nodes in the graph and in that case they must be broadcasted.

I'll make a PR with a fix soon.

DrChainsaw commented 4 years ago

Ok try this branch and see if it works for you

] https://github.com/DrChainsaw/ONNXmutable.jl#bcact

I could load the model, but I haven't tested that it produces correct output.

natema commented 4 years ago

Thanks for the prompt intervention!

I've run

] add https://github.com/DrChainsaw/ONNXmutable.jl#bcact

and verified that I'm on the correct branch

(@v1.4) pkg> status
Status `~/.julia/environments/v1.4/Project.toml`
  [c5f51814] CUDAdrv v6.3.0
  [be33ccc6] CUDAnative v3.1.0
  [3a865a2d] CuArrays v2.2.1
  [587475ba] Flux v0.10.4
  [6218d12a] ImageMagick v1.1.5
  [86fae568] ImageView v0.10.8
  [916415d5] Images v0.22.2
  [dbeba491] Metalhead v0.5.0
  [85610aed] NaiveNASflux v1.2.1
  [d0dd6a25] ONNX v0.1.1 [`dev/ONNX`]
  [cf2a63a0] ONNXmutable v0.1.0 #bcact (https://github.com/DrChainsaw/ONNXmutable.jl)
...

However, after re-running

using ONNXmutable
g = ONNXmutable.CompGraph("models/resnet18-v1-7.onnx")

as above and then issuing g(ones(224,224,3,1)), I still get the same error.

DrChainsaw commented 4 years ago

Thats odd...

Did you restart the repl between installing and using?

Here is from a fresh environment on my computer:

(testonnx) pkg> add https://github.com/DrChainsaw/ONNXmutable.jl#bcact
   ...
  [cf2a63a0] + ONNXmutable v0.1.0 #bcact (https://github.com/DrChainsaw/ONNXmutable.jl)
  ...

(testonnx) pkg> add NaiveNASflux
  [85610aed] + NaiveNASflux v1.2.1
 [no changes]

julia> using NaiveNASflux

julia> using ONNXmutable

julia> g = CompGraph("models/resnet18-v1-7.onnx");

julia> g(ones(Float32, 224,224,3,1))
1000×1 Array{Float32,2}:
 -1.2703923
 -0.509475
 -1.2314956
 -0.8486617
natema commented 4 years ago

Sorry, I'm a julia newbie and I had only naively activated a new environment. Now, re-running the commands from a fresh REPL, everything works. Thanks!

Probably unrelated, but when I precompiled ONNXmutable I got these warnings:

julia> using ONNXmutable
[ Info: Precompiling ONNXmutable [cf2a63a0-f8ae-421c-82b7-306ecfceaf66]
WARNING: Method definition iterate(DataFlow.ObjectArraySet{T} where T, Any...) in module DataFlow at /home/natema/.julia/packages/Lazy/KYseE/src/macros.jl:297 overwritten at /home/natema/.julia/packages/Lazy/KYseE/src/macros.jl:297.
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition iterate##kw(Any, typeof(Base.iterate), DataFlow.ObjectArraySet{T} where T, Any...) in module DataFlow at /home/natema/.julia/packages/Lazy/KYseE/src/macros.jl:297 overwritten at /home/natema/.julia/packages/Lazy/KYseE/src/macros.jl:297.
  ** incremental compilation may be fatally broken for this module **
DrChainsaw commented 4 years ago

Glad things worked out.

Restarting the repl after updating a package is certainly a bit of a gottcha. I'm not sure if it can be avoided somehow as I have developed the habit of restarting after some big enough change has been made. Apart from the new packages thing it also helps avoiding things like bugs hidden by global variables from past REPL commands.

Those warnings are annoying for sure. They come from ONNX.jl which in turn uses the obsolete package DataFlow. There is an issue on it here.