Skip to content

Commit

Permalink
add CUDA testset for Gabor and LogGabor
Browse files Browse the repository at this point in the history
Currently we don't have very native CUDA support for these two kernels,
so the test set mainly serves as "okay, we can convert the lazy kernel
array to CuArray by following the steps in tests and then use GPU to do
multiplication and fft"
  • Loading branch information
johnnychen94 committed Nov 2, 2021
1 parent 7f29c9e commit a0f1e28
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/cuda/Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
ImageBase = "c817782e-172a-44cc-b673-b171935fbb9e"
ImageFiltering = "6a3955dd-da59-5b1f-98d4-e7296123deb5"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
ImageQualityIndexes = "2996bd0c-7a13-11e9-2da2-2f5ce47296a9"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
66 changes: 66 additions & 0 deletions test/cuda/gaborkernels.jl
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/cuda/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a0f1e28

Please sign in to comment.