Flux.jl/test/layers/conv.jl

84 lines
2.1 KiB
Julia
Raw Normal View History

2018-09-04 13:35:20 +00:00
using Flux, Test
using Flux: maxpool, meanpool
2018-08-24 02:31:13 +00:00
2018-09-04 13:35:20 +00:00
@testset "Pooling" begin
2018-09-07 00:25:32 +00:00
x = randn(Float32, 10, 10, 3, 2)
2018-08-24 02:31:13 +00:00
mp = MaxPool((2, 2))
@test mp(x) == maxpool(x, PoolDims(x, 2))
2018-08-24 02:31:13 +00:00
mp = MeanPool((2, 2))
@test mp(x) == meanpool(x, PoolDims(x, 2))
2018-08-24 02:31:13 +00:00
end
2018-09-04 13:35:20 +00:00
@testset "CNN" begin
2018-09-07 00:25:32 +00:00
r = zeros(Float32, 28, 28, 1, 5)
2018-09-04 13:35:20 +00:00
m = Chain(
Conv((2, 2), 1=>16, relu),
MaxPool((2,2)),
Conv((2, 2), 16=>8, relu),
MaxPool((2,2)),
x -> reshape(x, :, size(x, 4)),
Dense(288, 10), softmax)
2018-08-24 02:31:13 +00:00
2018-09-04 13:35:20 +00:00
@test size(m(r)) == (10, 5)
2018-08-24 02:31:13 +00:00
end
2019-01-24 13:23:04 +00:00
@testset "asymmetric padding" begin
r = ones(Float32, 28, 28, 1, 1)
m = Conv((3, 3), 1=>1, relu; pad=(0,1,1,2))
m.weight.data[:] .= 1.0
m.bias.data[:] .= 0.0
y_hat = Flux.data(m(r))[:,:,1,1]
@test size(y_hat) == (27, 29)
@test y_hat[1, 1] 6.0
@test y_hat[2, 2] 9.0
@test y_hat[end, 1] 4.0
@test y_hat[1, end] 3.0
@test y_hat[1, end-1] 6.0
@test y_hat[end, end] 2.0
end
2019-01-24 13:23:04 +00:00
@testset "Depthwise Conv" begin
r = zeros(Float32, 28, 28, 3, 5)
m1 = DepthwiseConv((2, 2), 3=>15)
2019-01-24 13:23:04 +00:00
@test size(m1(r), 3) == 15
2019-03-09 14:47:22 +00:00
m3 = DepthwiseConv((2, 3), 3=>9)
@test size(m3(r), 3) == 9
# Test that we cannot ask for non-integer multiplication factors
@test_throws AssertionError DepthwiseConv((2,2), 3=>10)
2019-01-24 13:23:04 +00:00
end
@testset "ConvTranspose" begin
x = zeros(Float32, 28, 28, 1, 1)
y = Conv((3,3), 1 => 1)(x)
x_hat = ConvTranspose((3, 3), 1 => 1)(y)
@test size(x_hat) == size(x)
end
2019-03-26 11:45:22 +00:00
@testset "Conv with non quadratic window #700" begin
2019-03-27 16:29:31 +00:00
data = zeros(Float32, 7,7,1,1)
2019-03-26 11:45:22 +00:00
data[4,4,1,1] = 1
l = Conv((3,3), 1=>1)
2019-03-27 16:29:31 +00:00
expected = zeros(eltype(l.weight),5,5,1,1)
2019-03-26 11:45:22 +00:00
expected[2:end-1,2:end-1,1,1] = l.weight
@test expected == l(data)
l = Conv((3,1), 1=>1)
2019-03-27 16:29:31 +00:00
expected = zeros(eltype(l.weight),5,7,1,1)
2019-03-26 11:45:22 +00:00
expected[2:end-1,4,1,1] = l.weight
2019-05-03 20:36:32 +00:00
@test expected == l(data)
2019-03-26 11:45:22 +00:00
l = Conv((1,3), 1=>1)
2019-03-27 16:29:31 +00:00
expected = zeros(eltype(l.weight),7,5,1,1)
2019-03-26 11:45:22 +00:00
expected[4,2:end-1,1,1] = l.weight
2019-05-03 20:36:32 +00:00
@test expected == l(data)
2019-03-26 11:45:22 +00:00
2019-05-03 20:36:32 +00:00
@test begin
2019-03-26 11:45:22 +00:00
# we test that the next expression does not throw
randn(Float32, 10,10,1,1) |> Conv((6,1), 1=>1, Flux.σ)
true
end
end