From bfa966ac3f0c4f187a438db819c2e92e56ad8d47 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Thu, 13 Apr 2017 10:02:35 +0900 Subject: [PATCH 1/2] fixes bilinear initializer following approach in #34 --- src/initializer.jl | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/initializer.jl b/src/initializer.jl index dc484eb2dfe8..5e34dce8d8f9 100644 --- a/src/initializer.jl +++ b/src/initializer.jl @@ -48,29 +48,28 @@ function _init_loc_bias(self :: AbstractInitializer, name :: Base.Symbol, array end function _init_bilinear(self :: AbstractInitializer, name :: Base.Symbol, array :: NDArray) - # ported from python version: - #weight = np.zeros(np.prod(arr.shape), dtype='float32') - #shape = arr.shape - #f = np.ceil(shape[3] / 2.) - #c = (2 * f - 1 - f % 2) / (2. * f) - #for i in range(np.prod(shape)): - # x = i % shape[3] - # y = (i / shape[3]) % shape[2] - # weight[i] = (1 - abs(x / f - c)) * (1 - abs(y / f - c)) - #arr[:] = weight.reshape(shape) - - weight=zeros(array) - - h,w,channels,n=size(array) - f = ceil(w / 2.) - c = (2 * f - 1 - f % 2) / (2. * f) - - for i=1:length(weight) - x = i % w - y = (i / w) % h - weight[i] = (1 - abs(x / f - c)) * (1 - abs(y / f - c)) + @assert ndims(array) == 4 + + W, H, C, N = size(array) # Inverse of NCHW layout + filter = Base.zeros(eltype(array), W, H) + + @assert H == W + + f = ceil(Int, W / 2) # factor + c = (2 * f - 1 - f % 2) / (2 * f) # center + for x in 0:(W-1) + for y in 0:(H-1) + filter[x+1, y+1] = (1 - abs(x / f - c)) * (1 - abs(y / f - c)) + end + end + + @nd_as_jl rw=array begin + for i in 1:N + for j in 1:C + array[:,:, j, i] = filter + end + end end - array[:,:,:,:]=weight end function _init_bias(self :: AbstractInitializer, name :: Base.Symbol, array :: NDArray) From 8947eadb6b17f702e0e550274f69131150157f3d Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Thu, 13 Apr 2017 10:38:58 +0900 Subject: [PATCH 2/2] adds test for bilinear initializer --- test/unittest/initializer.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/unittest/initializer.jl diff --git a/test/unittest/initializer.jl b/test/unittest/initializer.jl new file mode 100644 index 000000000000..750959f2eb20 --- /dev/null +++ b/test/unittest/initializer.jl @@ -0,0 +1,18 @@ +@testset "Initializers" begin + @testset "Bilinear initializer" begin + # Setup a filter with scale = 2 + expectedFilter = Float32[ + 0.0625 0.1875 0.1875 0.0625; + 0.1875 0.5625 0.5625 0.1875; + 0.1875 0.5625 0.5625 0.1875; + 0.0625 0.1875 0.1875 0.0625] + filter = mx.zeros(Float32, 4, 4, 1, 4) + mx.init(mx.XavierInitializer(), :upsampling0_weight, filter) + + mx.@nd_as_jl ro=filter begin + for s in 1:size(filter, 4) + @test all(filter[:, :, 1, s] .== expectedFilter) + end + end + end +end