From ae149ddb720f3146ef40fa6c918172ee1e1f0226 Mon Sep 17 00:00:00 2001 From: Deniz Yuret Date: Sat, 12 Dec 2020 13:23:03 +0300 Subject: [PATCH] updated pool/unpool bug reports --- src/ops20/conv.jl | 15 +++++++++------ test/conv.jl | 24 ++++++++++++------------ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/ops20/conv.jl b/src/ops20/conv.jl index 8e6daa42e..018ac1a24 100644 --- a/src/ops20/conv.jl +++ b/src/ops20/conv.jl @@ -179,9 +179,6 @@ function checkpoolopts(x, window, padding, stride, mode, maxpoolingNanOpt, alpha @warn "Pool maxpoolingNanOpt=0 not yet implemented in NNlib, using 1 instead. See https://github.com/FluxML/NNlib.jl/issues/218" maxlog=1 maxpoolingNanOpt = 1 end - if padding != 0 && x isa Array - @warn "Pool padding is buggy in NNlib, use with caution. See https://github.com/FluxML/NNlib.jl/issues/229" maxlog=1 - end return (mode, maxpoolingNanOpt) end @@ -191,11 +188,17 @@ end Perform the reverse of pooling: `x == pool(unpool(x;o...); o...)` """ -function unpool(x; window=2, alpha=1, o...) # padding=0, stride=window, mode=0, maxpoolingNanOpt=0 +function unpool(x; window=2, padding=0, stride=window, mode=0, maxpoolingNanOpt=1, alpha=1) + if mode == 1 && x isa Array + @warn "unpool(mode=1), which uses poolx(mode=2) is not supported on the CPU; performing unpool(mode=2) instead, see https://github.com/FluxML/NNlib.jl/issues/218" maxlog=1 + end w = prod(psize(window,x)) - y = similar(x,updims(x; window=window, o...)) + y = similar(x,updims(x; window, padding, stride, mode, maxpoolingNanOpt, alpha)) + # pool0=>unpool1, pool1=>unpool2, pool2=>unpool1 + mode = (mode==0 ? 1 : mode==1 ? 2 : mode==2 ? 1 : mode==3 ? 1 : error("Unknown unpool mode $mode")) + alpha = 1/alpha # Leave unpool as a non-primitive, it is just a poolx call - poolx(y,x,x.*w; o..., window=window, mode=1, alpha=1/alpha) + poolx(y,x,x.*w; window, padding, stride, mode, maxpoolingNanOpt, alpha) end diff --git a/test/conv.jl b/test/conv.jl index a49bac44b..9d84fdbc7 100644 --- a/test/conv.jl +++ b/test/conv.jl @@ -93,15 +93,15 @@ struct M370; layer; end; @test gradcheck(pool, ax; kw=[(:mode,1),(:padding,1)]) @test gradcheck(unpool, ax; kw=[(:mode,1),(:padding,1)]) @test isapprox(pool(unpool(ax;mode=1);mode=1),ax) - @test_broken isapprox(pool(unpool(ax;mode=1,padding=1);mode=1,padding=1),ax) + @test_broken isapprox(pool(unpool(ax;mode=1,padding=1);mode=1,padding=1),ax) # unpool(mode=1) uses pool(mode=2) NNlib#218 @test gradcheck(conv41, (aw,ax); rtol=TOL, kw=[(:mode,1),(:padding,1)]) @test gradcheck(deconv41, (ad,ax); rtol=TOL, kw=[(:mode,1),(:padding,1)]) ### mode=2 (only for pool) -- is not supported in NNlib #218 - # @test gradcheck(pool, ax; kw=[(:mode,2),(:padding,1)]) - # @test gradcheck(unpool, ax; kw=[(:mode,2),(:padding,1)]) - # @test isapprox(pool(unpool(ax;mode=2);mode=2),ax) - # @test isapprox(pool(unpool(ax;mode=2,padding=1);mode=2,padding=1),ax) + @test gradcheck(pool, ax; kw=[(:mode,2),(:padding,1)]) + @test gradcheck(unpool, ax; kw=[(:mode,2),(:padding,1)]) + @test isapprox(pool(unpool(ax;mode=2);mode=2),ax) + @test_broken isapprox(pool(unpool(ax;mode=2,padding=1);mode=2,padding=1),ax) # pool(mode=2) not supported NNlib#218 ### alpha=2 (default=1) @test gradcheck(pool, ax; kw=[(:alpha,2)]) @@ -110,7 +110,7 @@ struct M370; layer; end; @test gradcheck(pool, ax; kw=[(:alpha,2),(:mode,1),(:padding,1)]) @test gradcheck(unpool, ax; kw=[(:alpha,2),(:mode,1),(:padding,1)]) @test isapprox(pool(unpool(ax;alpha=2,mode=1);alpha=2,mode=1),ax) - @test_broken isapprox(pool(unpool(ax;alpha=2,mode=1,padding=1);alpha=2,mode=1,padding=1),ax) + @test_broken isapprox(pool(unpool(ax;alpha=2,mode=1,padding=1);alpha=2,mode=1,padding=1),ax) # unpool(mode=1) uses pool(mode=2) unsupported by NNlib#218 @test gradcheck(conv41, (aw,ax); rtol=TOL, kw=[(:alpha,2)]) @test gradcheck(deconv41, (ad,ax); rtol=TOL, kw=[(:alpha,2)]) end @@ -197,7 +197,7 @@ struct M370; layer; end; ### mode=1 (default=0) @test isapprox(pool(kx;mode=1,padding=1), pool(ax;mode=1,padding=1)) @test gradcheck(pool, kx; kw=[(:mode,1),(:padding,1)]) - @test isapprox(unpool(kx;mode=1,padding=1), unpool(ax;mode=1,padding=1)) + @test_broken isapprox(unpool(kx;mode=1,padding=1), unpool(ax;mode=1,padding=1)) # unpool(mode=1) uses pool(mode=2) unsupported by NNlib#218 @test gradcheck(unpool, kx; kw=[(:mode,1),(:padding,1)]) @test isapprox(conv4(kw,kx;mode=1,padding=1), conv4(aw,ax;mode=1,padding=1)) @test gradcheck(conv41, (kw,kx); rtol=TOL, kw=[(:mode,1),(:padding,1)]) @@ -205,9 +205,9 @@ struct M370; layer; end; @test gradcheck(deconv41, (kd,kx); rtol=TOL, kw=[(:mode,1),(:padding,1)]) ### mode=2 (only for pool) - # @test isapprox(pool(kx;mode=2,padding=1), pool(ax;mode=2,padding=1)) ## mode=2 is not supported in NNlib #218. + @test_broken isapprox(pool(kx;mode=2,padding=1), pool(ax;mode=2,padding=1)) ## mode=2 is not supported in NNlib #218. @test gradcheck(pool, kx; kw=[(:mode,2),(:padding,1)]) - # @test isapprox(unpool(kx;mode=2,padding=1), unpool(ax;mode=2,padding=1)) ## mode=2 is not supported in NNlib #218. + @test isapprox(unpool(kx;mode=2,padding=1), unpool(ax;mode=2,padding=1)) @test gradcheck(unpool, kx; kw=[(:mode,2),(:padding,1)]) ### alpha=2 (default=1) @@ -217,12 +217,12 @@ struct M370; layer; end; @test gradcheck(unpool, kx; kw=[(:alpha,2)]) @test isapprox(pool(kx;alpha=2,mode=1,padding=1), pool(ax;alpha=2,mode=1,padding=1)) @test gradcheck(pool, kx; kw=[(:alpha,2),(:mode,1),(:padding,1)]) - @test isapprox(unpool(kx;alpha=2,mode=1,padding=1), unpool(ax;alpha=2,mode=1,padding=1)) + @test_broken isapprox(unpool(kx;alpha=2,mode=1,padding=1), unpool(ax;alpha=2,mode=1,padding=1)) # unpool(mode=1) uses pool(mode=2) unsupported by NNlib#218 @test gradcheck(unpool, kx; kw=[(:alpha,2),(:mode,1),(:padding,1)]) - # @test isapprox(pool(kx;alpha=2,mode=2,padding=1), pool(ax;alpha=2,mode=2,padding=1)) ## mode=2 is not supported in NNlib #218. + @test_broken isapprox(pool(kx;alpha=2,mode=2,padding=1), pool(ax;alpha=2,mode=2,padding=1)) ## broken: mode=2 is not supported in NNlib #218. @test gradcheck(pool, kx; kw=[(:alpha,2),(:mode,2),(:padding,1)]) - # @test isapprox(unpool(kx;alpha=2,mode=2,padding=1), unpool(ax;alpha=2,mode=2,padding=1)) ## mode=2 is not supported in NNlib #218. + @test isapprox(unpool(kx;alpha=2,mode=2,padding=1), unpool(ax;alpha=2,mode=2,padding=1)) @test gradcheck(unpool, kx; kw=[(:alpha,2),(:mode,2),(:padding,1)]) @test isapprox(conv4(kw,kx;alpha=2), conv4(aw,ax;alpha=2))