Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove gpu call #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function extract_bboxes(mask)
end
boxes[i,:] = [y1, x1, y2, x2]
end
boxes
return boxes
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably could do without the return keyword

end

function extract_bboxes(masks::AbstractArray{T,4}) where T
Expand All @@ -26,7 +26,7 @@ function extract_bboxes(masks::AbstractArray{T,4}) where T
b = extract_bboxes(masks[:,:,:,i])
push!(bs, b)
end
reduce(vcat, bs)
return reduce(vcat, bs)
end

expand_dims(x,n::Int) = reshape(x,ones(Int64,n)...,size(x)...)
Expand All @@ -40,13 +40,13 @@ function squeeze(x)
end
end

function bce(ŷ, y; ϵ=gpu(fill(eps(first(ŷ)), size(ŷ)...)))
function bce(ŷ, y; ϵ=eps(first(ŷ)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you confirm that it works on the GPU as well

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues on cpu. Does not work on gpu for me. but the error seems to be from somewhere else.

with

u = Unet()
w = rand(Float32, 256,256,1,1);
wp = rand(Float32, 256,256,1,1);

u = u |> gpu
w = w |> gpu
wp = wp |> gpu

function loss(x, y)
    op = clamp.(u(x), 0.001f0, 1.f0)
    return mean(bce(op, y)) 
end

rep = Iterators.repeated((w, wp), 10);
opt = Momentum()
Flux.train!(loss, Flux.params(u), rep, opt)

I get the following error.

MethodError: no method matching Float32(::ForwardDiff.Dual{Nothing,Float32,1})
Closest candidates are:
  Float32(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:200
  Float32(::T) where T<:Number at boot.jl:715
  Float32(!Matched::Int8) at float.jl:60
  ...

Stacktrace:
 [1] (::CuArrays.var"#68#69"{Float32})(::ForwardDiff.Dual{Nothing,Float32,1}) at /home/spv/.julia/packages/CuArrays/l0gXB/src/broadcast.jl:21
 [2] (::Zygote.var"#1774#1777"{CuArrays.var"#68#69"{Float32}})(::Float32) at /home/spv/.julia/packages/Zygote/CcpvC/src/lib/broadcast.jl:175
 [3] _broadcast_getindex_evalf at ./broadcast.jl:631 [inlined]
 [4] _broadcast_getindex at ./broadcast.jl:604 [inlined]
 [5] getindex at ./broadcast.jl:564 [inlined]
 [6] copy at ./broadcast.jl:854 [inlined]
 [7] materialize(::Base.Broadcast.Broadcasted{CuArrays.CuArrayStyle{1},Nothing,Zygote.var"#1774#1777"{CuArrays.var"#68#69"{Float32}},Tuple{CuArrays.CuArray{Float32,1,CuArrays.CuArray{Float32,6,Nothing}}}}) at ./broadcast.jl:820
 [8] broadcast_forward(::Function, ::CuArrays.CuArray{Float32,1,CuArrays.CuArray{Float32,6,Nothing}}) at /home/spv/.julia/packages/Zygote/CcpvC/src/lib/broadcast.jl:181
 [9] adjoint at /home/spv/.julia/packages/Zygote/CcpvC/src/lib/broadcast.jl:197 [inlined]
 [10] _pullback at /home/spv/.julia/packages/ZygoteRules/6nssF/src/adjoint.jl:47 [inlined]
 [11] adjoint at /home/spv/.julia/packages/Zygote/CcpvC/src/lib/lib.jl:179 [inlined]
 [12] _pullback at /home/spv/.julia/packages/ZygoteRules/6nssF/src/adjoint.jl:47 [inlined]
 [13] broadcasted at ./broadcast.jl:1232 [inlined]
 [14] BatchNorm at /home/spv/.julia/packages/Flux/de8v2/src/layers/normalise.jl:193 [inlined]
 [15] _pullback(::Zygote.Context, ::BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32}, ::CuArrays.CuArray{Float32,6,CuArrays.CuArray{Float32,4,Nothing}}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [16] applychain at /home/spv/.julia/packages/Flux/de8v2/src/layers/basic.jl:36 [inlined]
 [17] _pullback(::Zygote.Context, ::typeof(Flux.applychain), ::Tuple{BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32},UNet.var"#15#17"}, ::CuArrays.CuArray{Float32,6,CuArrays.CuArray{Float32,4,Nothing}}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [18] applychain at /home/spv/.julia/packages/Flux/de8v2/src/layers/basic.jl:36 [inlined]
 [19] _pullback(::Zygote.Context, ::typeof(Flux.applychain), ::Tuple{UNet.var"#14#16",BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32},UNet.var"#15#17"}, ::CuArrays.CuArray{Float32,4,Nothing}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [20] Chain at /home/spv/.julia/packages/Flux/de8v2/src/layers/basic.jl:38 [inlined]
 [21] _pullback(::Zygote.Context, ::Chain{Tuple{UNet.var"#14#16",BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32},UNet.var"#15#17"}}, ::CuArrays.CuArray{Float32,4,Nothing}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [22] applychain at /home/spv/.julia/packages/Flux/de8v2/src/layers/basic.jl:36 [inlined]
 [23] _pullback(::Zygote.Context, ::typeof(Flux.applychain), ::Tuple{Chain{Tuple{UNet.var"#14#16",BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32},UNet.var"#15#17"}},UNet.var"#18#19"}, ::CuArrays.CuArray{Float32,4,Nothing}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [24] applychain at /home/spv/.julia/packages/Flux/de8v2/src/layers/basic.jl:36 [inlined]
 [25] _pullback(::Zygote.Context, ::typeof(Flux.applychain), ::Tuple{Conv{2,2,typeof(identity),CuArrays.CuArray{Float32,4,Nothing},CuArrays.CuArray{Float32,1,Nothing}},Chain{Tuple{UNet.var"#14#16",BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32},UNet.var"#15#17"}},UNet.var"#18#19"}, ::CuArrays.CuArray{Float32,4,Nothing}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [26] Chain at /home/spv/.julia/packages/Flux/de8v2/src/layers/basic.jl:38 [inlined]
 [27] _pullback(::Zygote.Context, ::Chain{Tuple{Conv{2,2,typeof(identity),CuArrays.CuArray{Float32,4,Nothing},CuArrays.CuArray{Float32,1,Nothing}},Chain{Tuple{UNet.var"#14#16",BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32},UNet.var"#15#17"}},UNet.var"#18#19"}}, ::CuArrays.CuArray{Float32,4,Nothing}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [28] applychain at /home/spv/.julia/packages/Flux/de8v2/src/layers/basic.jl:36 [inlined]
 [29] _pullback(::Zygote.Context, ::typeof(Flux.applychain), ::Tuple{Chain{Tuple{Conv{2,2,typeof(identity),CuArrays.CuArray{Float32,4,Nothing},CuArrays.CuArray{Float32,1,Nothing}},Chain{Tuple{UNet.var"#14#16",BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32},UNet.var"#15#17"}},UNet.var"#18#19"}},Chain{Tuple{Conv{2,2,typeof(identity),CuArrays.CuArray{Float32,4,Nothing},CuArrays.CuArray{Float32,1,Nothing}},Chain{Tuple{UNet.var"#14#16",BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32},UNet.var"#15#17"}},UNet.var"#18#19"}}}, ::CuArrays.CuArray{Float32,4,Nothing}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [30] Chain at /home/spv/.julia/packages/Flux/de8v2/src/layers/basic.jl:38 [inlined]
 [31] _pullback(::Zygote.Context, ::Chain{Tuple{Chain{Tuple{Conv{2,2,typeof(identity),CuArrays.CuArray{Float32,4,Nothing},CuArrays.CuArray{Float32,1,Nothing}},Chain{Tuple{UNet.var"#14#16",BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32},UNet.var"#15#17"}},UNet.var"#18#19"}},Chain{Tuple{Conv{2,2,typeof(identity),CuArrays.CuArray{Float32,4,Nothing},CuArrays.CuArray{Float32,1,Nothing}},Chain{Tuple{UNet.var"#14#16",BatchNorm{typeof(identity),CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing},Float32},UNet.var"#15#17"}},UNet.var"#18#19"}}}}, ::CuArrays.CuArray{Float32,4,Nothing}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [32] Unet at /home/spv/.julia/dev/UNet/src/model.jl:72 [inlined]
 [33] _pullback(::Zygote.Context, ::Unet, ::CuArrays.CuArray{Float32,4,Nothing}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [34] loss at ./In[35]:2 [inlined]
 [35] _pullback(::Zygote.Context, ::typeof(loss), ::CuArrays.CuArray{Float32,4,Nothing}, ::CuArrays.CuArray{Float32,4,Nothing}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [36] adjoint at /home/spv/.julia/packages/Zygote/CcpvC/src/lib/lib.jl:179 [inlined]
 [37] _pullback at /home/spv/.julia/packages/ZygoteRules/6nssF/src/adjoint.jl:47 [inlined]
 [38] #17 at /home/spv/.julia/packages/Flux/de8v2/src/optimise/train.jl:89 [inlined]
 [39] _pullback(::Zygote.Context, ::Flux.Optimise.var"#17#25"{typeof(loss),Tuple{CuArrays.CuArray{Float32,4,Nothing},CuArrays.CuArray{Float32,4,Nothing}}}) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface2.jl:0
 [40] pullback(::Function, ::Params) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface.jl:174
 [41] gradient(::Function, ::Params) at /home/spv/.julia/packages/Zygote/CcpvC/src/compiler/interface.jl:54
 [42] macro expansion at /home/spv/.julia/packages/Flux/de8v2/src/optimise/train.jl:88 [inlined]
 [43] macro expansion at /home/spv/.julia/packages/Juno/f8hj2/src/progress.jl:134 [inlined]
 [44] train!(::typeof(loss), ::Params, ::Base.Iterators.Take{Base.Iterators.Repeated{Tuple{CuArrays.CuArray{Float32,4,Nothing},CuArrays.CuArray{Float32,4,Nothing}}}}, ::Momentum; cb::Flux.Optimise.var"#18#26") at /home/spv/.julia/packages/Flux/de8v2/src/optimise/train.jl:81
 [45] train!(::Function, ::Params, ::Base.Iterators.Take{Base.Iterators.Repeated{Tuple{CuArrays.CuArray{Float32,4,Nothing},CuArrays.CuArray{Float32,4,Nothing}}}}, ::Momentum) at /home/spv/.julia/packages/Flux/de8v2/src/optimise/train.jl:79
 [46] top-level scope at In[36]:3

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just backprop through bce to see if that works, since that is the scope of the PR

l1 = -y.*log.(ŷ .+ ϵ)
l2 = (1 .- y).*log.(1 .- ŷ .+ ϵ)
l1 .- l2
return l1 .- l2
end

function loss(x, y)
op = clamp.(u(x), 0.001f0, 1.f0)
mean(bce(op, y))
return mean(bce(op, y))
end