Skip to content

Commit

Permalink
add multiimage tests for Crop
Browse files Browse the repository at this point in the history
  • Loading branch information
Evizero committed Jul 3, 2018
1 parent 73669dd commit cae3b8c
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 12 deletions.
4 changes: 0 additions & 4 deletions src/operations/crop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
128 changes: 120 additions & 8 deletions test/operations/tst_crop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit cae3b8c

Please sign in to comment.