diff --git a/test/cuda/gaborkernels.jl b/test/cuda/gaborkernels.jl new file mode 100644 index 0000000..4b217f7 --- /dev/null +++ b/test/cuda/gaborkernels.jl @@ -0,0 +1,66 @@ +@testset "GaborKernels" begin + +@testset "Gabor" begin + # Gray + img = float32.(TestImages.shepp_logan(127)) + kern = Kernel.Gabor(size(img), 3.0f0, 0f0) + img_freq = fft(channelview(img)) + kern_freq = ifftshift(fft(kern)) + out = abs.(ifft(img_freq .* kern_freq)) + + # TODO(johnnychen94): currently Gabor can't be converted to CuArray directly and thus + # FFTW is applied in the CPU side. + img_cu = CuArray(img) + img_freq = fft(channelview(img_cu)) + kern_freq = CuArray(ifftshift(fft(kern))) + out_cu = abs.(ifft(img_freq .* kern_freq)) + + @test out ≈ Array(out_cu) + + # RGB + img = float32.(testimage("monarch")) + kern = Kernel.Gabor(size(img), 3.0f0, 0f0) + kern_freq = reshape(ifftshift(fft(kern)), 1, size(kern)...) + img_freq = fft(channelview(img), 2:3) + out = colorview(RGB, abs.(ifft(img_freq .* kern_freq))) + + img_cu = CuArray(img) + kern_freq = CuArray(reshape(ifftshift(fft(kern)), 1, size(kern)...)) + img_freq = fft(channelview(img_cu), 2:3) + out_cu = colorview(RGB, abs.(ifft(img_freq .* kern_freq))) + + @test out ≈ Array(out_cu) +end + +@testset "LogGabor" begin + # Gray + img = float32.(TestImages.shepp_logan(127)) + kern = Kernel.LogGabor(size(img), 1.0f0/6, 0f0) + kern_freq = OffsetArrays.no_offset_view(ifftshift(kern)) + img_freq = fft(channelview(img)) + out = abs.(ifft(kern_freq .* img_freq)) + + # TODO(johnnychen94): remove this no_offset_view wrapper + img_cu = CuArray(img) + kern_freq = CuArray(OffsetArrays.no_offset_view(ifftshift(kern))) + img_freq = fft(channelview(img_cu)) + out_cu = abs.(ifft(kern_freq .* img_freq)) + + @test out ≈ Array(out_cu) + + # RGB + img = float32.(testimage("monarch")) + kern = Kernel.LogGabor(size(img), 1.0f0/6, 0f0) + kern_freq = reshape(ifftshift(kern), 1, size(kern)...) + img_freq = fft(channelview(img), 2:3) + out = colorview(RGB, abs.(ifft(img_freq .* kern_freq))) + + img_cu = CuArray(img) + kern_freq = CuArray(reshape(ifftshift(kern), 1, size(kern)...)) + img_freq = fft(channelview(img_cu), 2:3) + out_cu = colorview(RGB, abs.(ifft(img_freq .* kern_freq))) + + @test out ≈ Array(out_cu) +end + +end diff --git a/test/cuda/runtests.jl b/test/cuda/runtests.jl index 5789d00..f86b6dc 100644 --- a/test/cuda/runtests.jl +++ b/test/cuda/runtests.jl @@ -7,11 +7,15 @@ using ImageBase using ImageQualityIndexes using Test using Random +using FFTW +using OffsetArrays +using CUDA.Adapt CUDA.allowscalar(false) @testset "ImageFiltering" begin if CUDA.functional() include("models.jl") + include("gaborkernels.jl") end end