-
Notifications
You must be signed in to change notification settings - Fork 86
max pool #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
max pool #110
Changes from 4 commits
dde8f86
fea2613
039b89e
449810a
53cc937
46b4e8b
1e1104e
7886e5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import CUDAnative | ||
|
||
function maxpool2d_kernel(state, A::AbstractArray{T}, out, Asize, pool, stride, outSize) where T | ||
ilin = linear_index(state) | ||
idx = GPUArrays.gpu_ind2sub(Asize, ilin) | ||
if (idx[1] > outSize[1] || idx[2] > outSize[2] || idx[3] > outSize[3] || idx[4] > outSize[4]) | ||
return | ||
end | ||
|
||
temp_max = A[((idx[1] - 1) * stride) + Asize[1] * (idx[2] - 1) * stride + (Asize[1] * Asize[2]) * (idx[3] - 1) + (Asize[1] * Asize[2] * Asize[3]) * (idx[4] - 1) + 1] | ||
max_pos = ((idx[1] - 1) * stride) + Asize[1] * (idx[2] - 1) * stride + (Asize[1] * Asize[2]) * (idx[3] - 1) + (Asize[1] * Asize[2] * Asize[3]) * (idx[4] - 1) + 1 | ||
curr_pos = ((idx[1] - 1) * stride) + Asize[1] * (idx[2] - 1) * stride + (Asize[1] * Asize[2]) * (idx[3] - 1) + (Asize[1] * Asize[2] * Asize[3]) * (idx[4] - 1) + 1 | ||
|
||
for p in 1:pool | ||
for p in 1:pool | ||
m = A[curr_pos] | ||
if (m > temp_max) | ||
temp_max = m | ||
max_pos = curr_pos | ||
end | ||
curr_pos += 1 | ||
end | ||
curr_pos += Asize[1] - pool | ||
end | ||
out[(idx[1] - 1) + outSize[1] * (idx[2] - 1) + (outSize[1] * outSize[2]) * (idx[3] - 1) + (outSize[1] * outSize[2] * outSize[3]) * (idx[4] - 1) + 1] = temp_max | ||
return | ||
end | ||
|
||
|
||
function maxpool2d{T <: Integer}(a, pool::T; stride = pool, pad = 0) | ||
b = zeros(typeof(a), size(a,1) + pad * 2, size(a,2) + pad * 2, size(a,3), size(a,4)) | ||
b[pad + 1 : pad + size(a,1), pad + 1 : pad + size(a,2), :, :] = a | ||
Asize = UInt32.(size(b)) | ||
pool = UInt32(pool) | ||
stride = UInt32(stride) | ||
out = similar(b) | ||
out = out[1:(div(Asize[1] - pool, stride) + 1), 1:(div(Asize[2] - pool, stride) + 1), :, :] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I was unaware of this. It should be |
||
outSize = UInt32.(size(out)) | ||
gpu_call(maxpool2d_kernel, b, (b, out, Asize, pool, stride, outSize)) | ||
GPUArrays.synchronize(out) | ||
out | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using GPUArrays.TestSuite, Base.Test, Flux | ||
|
||
function run_pool(Typ) | ||
for ET in supported_eltypes() | ||
T = Typ{ET} | ||
if (ET == Complex{Float32} || ET == Complex{Float64}) | ||
continue | ||
end | ||
@testset "$ET" begin | ||
@testset "maxpool with padding" begin | ||
pool = 3 | ||
stride = 3 | ||
pad = 3 | ||
|
||
a = rand(ET, 9,9,3,1) | ||
b = zeros(eltype(a), size(a,1) + pad * 2, size(a,2) + pad * 2, size(a,3), size(a,4)) | ||
b[pad + 1 : pad + size(a,1), pad + 1 : pad + size(a,2), :, :] = a | ||
out1 = maxpool(b, (3, 3)) | ||
|
||
a = T(a) | ||
out2 = GPUArrays.maxpool2d(a, pool, pad = pad) | ||
|
||
@test out1 ≈ out2 | ||
end | ||
|
||
@testset "maxpool without padding" begin | ||
pool = 3 | ||
stride = 3 | ||
|
||
a = rand(ET, 9,9,3,1) | ||
out1 = maxpool(a, (3, 3)) | ||
|
||
a = T(a) | ||
out2 = GPUArrays.maxpool2d(a, pool) | ||
|
||
@test out1 ≈ out2 | ||
end | ||
|
||
|
||
@testset "maxpool with full kernel" begin | ||
pool = 9 | ||
stride = 1 | ||
|
||
a = rand(ET, 9,9,3,1) | ||
out1 = maxpool(a, (9, 9)) | ||
|
||
a = T(a) | ||
out2 = GPUArrays.maxpool2d(a, pool, stride = stride) | ||
|
||
@test out1 ≈ out2 | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Flux | ||
CUDAnative |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not needed, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍