From cae3b8c33b68c91aab5405220f4ab22ab622eb35 Mon Sep 17 00:00:00 2001 From: Christof Stocker Date: Tue, 3 Jul 2018 21:40:43 +0200 Subject: [PATCH] add multiimage tests for Crop --- src/operations/crop.jl | 4 -- src/utils.jl | 1 + test/operations/tst_crop.jl | 128 +++++++++++++++++++++++++++++++++--- 3 files changed, 121 insertions(+), 12 deletions(-) diff --git a/src/operations/crop.jl b/src/operations/crop.jl index e7a3b561..c5da9eb9 100644 --- a/src/operations/crop.jl +++ b/src/operations/crop.jl @@ -173,10 +173,6 @@ CropNative(indexes::Range...) = CropNative(indexes) @inline applylazy(op::CropNative, img::AbstractArray, param) = applyview(op, img, param) -function applyeager(op::CropNative, img::AbstractArray, param) - plain_array(img[op.indexes...]) -end - function applyaffineview(op::CropNative, img::AbstractArray, param) applyview(op, prepareaffine(img), param) end diff --git a/src/utils.jl b/src/utils.jl index 32ba6c23..56b3f59e 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -117,6 +117,7 @@ function direct_indices(::Tuple{}, ::Tuple{}) throw(MethodError(direct_indices, ((),()))) end +# TODO: Figure out why this method exists function direct_indices(O::NTuple{N,IdentityRange}, I::NTuple{N,StepRange}) where N throw(MethodError(direct_indices, (O, I))) end diff --git a/test/operations/tst_crop.jl b/test/operations/tst_crop.jl index f8155bb4..bf7934b3 100644 --- a/test/operations/tst_crop.jl +++ b/test/operations/tst_crop.jl @@ -22,30 +22,93 @@ @test_throws MethodError Augmentor.applyeager(Crop(1:10), nothing) @test_throws MethodError Augmentor.applyeager(Crop(1:2,2:3), nothing) @test Augmentor.supports_eager(Crop) === false - for img in (Augmentor.prepareaffine(rect), rect, OffsetArray(rect, -2, -1), view(rect, IdentityRange(1:2), IdentityRange(1:3))) - @test @inferred(Augmentor.applyeager(Crop(1:2,2:3), img)) == rect[1:2, 2:3] - @test typeof(Augmentor.applyeager(Crop(1:2,2:3), img)) <: Array + imgs = [ + (rect, (1:2, 2:3)), + (Augmentor.prepareaffine(rect), (1:2, 2:3)), + (OffsetArray(rect, -2, -1), (-1:0, 1:2)), + (view(rect, IdentityRange(1:2), IdentityRange(1:3)), (1:2, 2:3)), + ] + @testset "single image" begin + for (img_in, inds) in imgs + res = @inferred(Augmentor.applyeager(Crop(1:2,2:3), img_in)) + @test collect(res) == rect[1:2, 2:3] + @test indices(res) == inds + @test typeof(res) <: OffsetArray{eltype(img_in),2} + end + end + @testset "multiple images" begin + for (img_in1, inds1) in imgs, (img_in2, inds2) in imgs + img_in = (img_in1, img_in2) + inds = (inds1, inds2) + res = @inferred(Augmentor.applyeager(Crop(1:2,2:3), img_in)) + @test collect.(res) == ntuple(i->rect[1:2, 2:3],2) + @test typeof(res) <: NTuple{2,OffsetArray{eltype(img_in1),2}} + end end end @testset "affine" begin @test Augmentor.supports_affine(Crop) === false end + imgs = [ + (rect), + (Augmentor.prepareaffine(rect)), + (OffsetArray(rect, -2, -1)), + (view(rect, IdentityRange(1:2), IdentityRange(1:3))), + ] @testset "affineview" begin @test Augmentor.supports_affineview(Crop) === true @test_throws MethodError Augmentor.applyaffineview(Crop(1:2,2:3), nothing) @test @inferred(Augmentor.applyaffineview(Crop(1:2,2:3), rect)) == view(Augmentor.prepareaffine(rect), IdentityRange(1:2), IdentityRange(2:3)) + @testset "multiple images" begin + for img_in in imgs + res1, res2 = @inferred(Augmentor.applyaffineview(Crop(1:2,2:3), (img_in, N0f8.(img_in)))) + # make sure both images are processed + @test res1 == res2 + @test typeof(res1) <: SubArray{Gray{N0f8}} + @test typeof(res2) <: SubArray{N0f8} + @test typeof(parent(res1)) <: InvWarpedView + @test typeof(parent(res2)) <: InvWarpedView + end + end end @testset "lazy" begin @test Augmentor.supports_lazy(Crop) === true @test @inferred(Augmentor.applylazy(Crop(1:2,2:3), rect)) === view(rect, IdentityRange(1:2), IdentityRange(2:3)) + @testset "multiple images" begin + for img_in in imgs + res1, res2 = @inferred(Augmentor.applylazy(Crop(1:2,2:3), (img_in, N0f8.(img_in)))) + # make sure both images are processed + @test res1 == res2 + @test typeof(res1) <: SubArray{Gray{N0f8}} + @test typeof(res2) <: SubArray{N0f8} + end + end end @testset "view" begin @test Augmentor.supports_view(Crop) === true @test @inferred(Augmentor.applyview(Crop(1:2,2:3), rect)) === view(rect, IdentityRange(1:2), IdentityRange(2:3)) + @testset "multiple images" begin + for img_in in imgs + res1, res2 = @inferred(Augmentor.applyview(Crop(1:2,2:3), (img_in, N0f8.(img_in)))) + # make sure both images are processed + @test res1 == res2 + @test typeof(res1) <: SubArray{Gray{N0f8}} + @test typeof(res2) <: SubArray{N0f8} + end + end end @testset "stepview" begin @test Augmentor.supports_stepview(Crop) === true @test @inferred(Augmentor.applystepview(Crop(1:2,2:3), rect)) === view(rect, 1:1:2, 2:1:3) + @testset "multiple images" begin + for img_in in imgs + res1, res2 = @inferred(Augmentor.applystepview(Crop(1:2,2:3), (img_in, N0f8.(img_in)))) + # make sure both images are processed + @test res1 == res2 + @test typeof(res1) <: SubArray{Gray{N0f8}} + @test typeof(res2) <: SubArray{N0f8} + end + end end @testset "permute" begin @test Augmentor.supports_permute(Crop) === false @@ -74,17 +137,27 @@ end @test str_showconst(CropNative(1:2,2:3,3:4)) == "CropNative(1:2, 2:3, 3:4)" @test str_showcompact(CropNative(1:2,2:3,3:4)) == "Crop native region (1:2, 2:3, 3:4)" end + imgs = [ + (rect, (1:2, 2:3)), + (Augmentor.prepareaffine(rect), (1:2, 2:3)), + (OffsetArray(rect, -2, -1), (-1:0, 1:2)), + (view(rect, IdentityRange(1:2), IdentityRange(1:3)), (1:2, 2:3)), + ] @testset "eager" begin @test_throws MethodError Augmentor.applyeager(CropNative(1:10), nothing) @test_throws MethodError Augmentor.applyeager(CropNative(1:2,2:3), nothing) @test Augmentor.supports_eager(CropNative) === false - for img in (Augmentor.prepareaffine(rect), rect, view(rect, IdentityRange(1:2), IdentityRange(1:3))) - @test @inferred(Augmentor.applyeager(CropNative(1:2,2:3), img)) == rect[1:2, 2:3] - @test typeof(Augmentor.applyeager(CropNative(1:2,2:3), img)) <: Array + @testset "single image" begin + for (img_in, inds) in imgs + res = @inferred(Augmentor.applyeager(CropNative(inds), img_in)) + @test collect(res) == rect[1:2, 2:3] + @test indices(res) == inds + @test typeof(res) <: OffsetArray{eltype(img_in),2} + end end img = OffsetArray(rect, -2, -1) - @test @inferred(Augmentor.applyeager(CropNative(-1:0,1:2), img)) == rect[1:2, 2:3] - @test typeof(Augmentor.applyeager(CropNative(-1:0,1:2), img)) <: Array + @test collect(@inferred(Augmentor.applyeager(CropNative(-1:0,1:2), img))) == rect[1:2, 2:3] + @test typeof(Augmentor.applyeager(CropNative(-1:0,1:2), img)) <: OffsetArray end @testset "affine" begin @test Augmentor.supports_affine(CropNative) === false @@ -93,18 +166,57 @@ end @test Augmentor.supports_affineview(CropNative) === true @test_throws MethodError Augmentor.applyaffineview(CropNative(1:2,2:3), nothing) @test @inferred(Augmentor.applyaffineview(CropNative(1:2,2:3), rect)) == view(Augmentor.prepareaffine(rect), IdentityRange(1:2), IdentityRange(2:3)) + @testset "multiple images" begin + for (img_in, inds) in imgs + res1, res2 = @inferred(Augmentor.applyaffineview(CropNative(inds), (img_in, N0f8.(img_in)))) + # make sure both images are processed + @test res1 == res2 + @test typeof(res1) <: SubArray{Gray{N0f8}} + @test typeof(res2) <: SubArray{N0f8} + @test typeof(parent(res1)) <: InvWarpedView + @test typeof(parent(res2)) <: InvWarpedView + end + end end @testset "lazy" begin @test Augmentor.supports_lazy(CropNative) === true @test @inferred(Augmentor.applylazy(CropNative(1:2,2:3), rect)) === view(rect, IdentityRange(1:2), IdentityRange(2:3)) + @testset "multiple images" begin + for (img_in, inds) in imgs + res1, res2 = @inferred(Augmentor.applylazy(CropNative(inds), (img_in, N0f8.(img_in)))) + # make sure both images are processed + @test res1 == res2 + @test typeof(res1) <: SubArray{Gray{N0f8}} + @test typeof(res2) <: SubArray{N0f8} + end + end end @testset "view" begin @test Augmentor.supports_view(CropNative) === true @test @inferred(Augmentor.applyview(CropNative(1:2,2:3), rect)) === view(rect, IdentityRange(1:2), IdentityRange(2:3)) + @testset "multiple images" begin + for (img_in, inds) in imgs + res1, res2 = @inferred(Augmentor.applyview(CropNative(inds), (img_in, N0f8.(img_in)))) + # make sure both images are processed + @test res1 == res2 + @test typeof(res1) <: SubArray{Gray{N0f8}} + @test typeof(res2) <: SubArray{N0f8} + end + end end @testset "stepview" begin @test Augmentor.supports_stepview(CropNative) === true @test @inferred(Augmentor.applystepview(CropNative(1:2,2:3), rect)) === view(rect, 1:1:2, 2:1:3) + # TODO: fix behaviour for stepview on IdentityRange + #@testset "multiple images" begin + # for (img_in, inds) in imgs + # res1, res2 = @inferred(Augmentor.applystepview(CropNative(inds), (img_in, N0f8.(img_in)))) + # # make sure both images are processed + # @test res1 == res2 + # @test typeof(res1) <: SubArray{Gray{N0f8}} + # @test typeof(res2) <: SubArray{N0f8} + # end + #end end @testset "permute" begin @test Augmentor.supports_permute(CropNative) === false