|
| 1 | +export grid_sample, ∇grid_sample |
| 2 | + |
| 3 | +@inline in_bounds(h, w, H, W) = 1 ≤ h ≤ H && 1 ≤ w ≤ W |
| 4 | +# Borders are considered out-of-bounds for gradient. |
| 5 | +@inline clip_coordinate(coordinate, dim_size) = min(dim_size, max(1, coordinate)) |
| 6 | +@inline function ∇clip_coordinate(coordinate::C, dim_size) where C |
| 7 | + if coordinate ≤ 1 |
| 8 | + return C(1), C(0) |
| 9 | + elseif coordinate ≥ dim_size |
| 10 | + return C(dim_size), C(0) |
| 11 | + end |
| 12 | + coordinate, C(1) |
| 13 | +end |
| 14 | + |
| 15 | +@inline unnormalize(coordinate, dim_size) = ((coordinate + 1.0) * 0.5) * (dim_size - 1.0) + 1.0 |
| 16 | +@inline ∇unnormalize(coordinate, dim_size) = unnormalize(coordinate, dim_size), (dim_size - 1.0) * 0.5 |
| 17 | + |
| 18 | +@inline compute_source_index(coordinate, dim_size, ::Val{:zeros}) = unnormalize(coordinate, dim_size) |
| 19 | +@inline compute_source_index(coordinate, dim_size, ::Val{:border}) = clip_coordinate(unnormalize(coordinate, dim_size), dim_size) |
| 20 | + |
| 21 | +@inline ∇compute_source_index(coordinate, dim_size, ::Val{:zeros}) = ∇unnormalize(coordinate, dim_size) |
| 22 | +@inline function ∇compute_source_index(coordinate, dim_size, ::Val{:border}) |
| 23 | + source_coordinate, grad_in = ∇unnormalize(coordinate, dim_size) |
| 24 | + source_coordinate, grad_clip = ∇clip_coordinate(source_coordinate, dim_size) |
| 25 | + source_coordinate, grad_in * grad_clip |
| 26 | +end |
| 27 | + |
| 28 | +""" |
| 29 | + grid_sample(input::AbstractArray{T, 4}, grid::AbstractArray{T, 4}; padding_mode = :zeros) |
| 30 | +
|
| 31 | +Given `input`, compute output by sampling `input` values at pixel |
| 32 | +locations from `grid`. Uses bilinear interpolation to calculate output values. |
| 33 | +
|
| 34 | +This implementation assumes the extrema (`-1` and `1`) are considered |
| 35 | +as referring to the center points of the input’s corner pixels |
| 36 | +(i.e. align corners is `true`). |
| 37 | +
|
| 38 | +# Arguments |
| 39 | +
|
| 40 | +- `input`: Input array in `(W_in, H_in, C, N)` shape. |
| 41 | +- `grid`: Input grid in `(2, W_out, H_out, N)` shape. |
| 42 | + Where for each `(W_out, H_out, N)` grid contains `(x, y)` |
| 43 | + coordinates that specify sampling locations normalized by the `input` shape. |
| 44 | +
|
| 45 | + Therefore, `x` and `y` should have values in `[-1, 1]` range. |
| 46 | + For example, `(x = -1, y = -1)` is the left-top pixel of `input`, |
| 47 | + and `(x = 1, y = 1)` is the right-bottom pixel of `input`. |
| 48 | +
|
| 49 | + Out-of-bound values are handled according to the `padding_mode`. |
| 50 | +- `padding_mode`: Out-of-bound padding. |
| 51 | + `:zeros` to use `0` for out-of-bound grid locations. |
| 52 | + `:border` to use border values for out-of-bound grid locations. |
| 53 | + Default is `:zeros`. |
| 54 | +
|
| 55 | +# Returns |
| 56 | +
|
| 57 | +`(W_out, H_out, C, N)` sampled grid from `input`. |
| 58 | +
|
| 59 | +# Examples |
| 60 | +
|
| 61 | +In the example below, grid contains two out-of-bound sampling locations, |
| 62 | +which are handled differently, depending on the `padding_mode`. |
| 63 | +
|
| 64 | +```jldoctest |
| 65 | +julia> x = reshape(collect(1.0:4.0), (2, 2, 1, 1)) |
| 66 | +2×2×1×1 Array{Float64, 4}: |
| 67 | +[:, :, 1, 1] = |
| 68 | + 1.0 3.0 |
| 69 | + 2.0 4.0 |
| 70 | +
|
| 71 | +julia> grid = Array{Float64}(undef, 2, 3, 2, 1); |
| 72 | +
|
| 73 | +julia> grid[:, 1, 1, 1] .= (-3, -1); |
| 74 | +
|
| 75 | +julia> grid[:, 2, 1, 1] .= (0, -1); |
| 76 | +
|
| 77 | +julia> grid[:, 3, 1, 1] .= (1, -1); |
| 78 | +
|
| 79 | +julia> grid[:, 1, 2, 1] .= (-1, 1); |
| 80 | +
|
| 81 | +julia> grid[:, 2, 2, 1] .= (0, 1); |
| 82 | +
|
| 83 | +julia> grid[:, 3, 2, 1] .= (3, 1); |
| 84 | +
|
| 85 | +julia> grid_sample(x, grid; padding_mode=:zeros) |
| 86 | +3×2×1×1 Array{Float64, 4}: |
| 87 | +[:, :, 1, 1] = |
| 88 | + 0.0 3.0 |
| 89 | + 1.5 3.5 |
| 90 | + 2.0 0.0 |
| 91 | +
|
| 92 | +julia> grid_sample(x, grid; padding_mode=:border) |
| 93 | +3×2×1×1 Array{Float64, 4}: |
| 94 | +[:, :, 1, 1] = |
| 95 | + 1.0 3.0 |
| 96 | + 1.5 3.5 |
| 97 | + 2.0 4.0 |
| 98 | +``` |
| 99 | +""" |
| 100 | +function grid_sample(input::AbstractArray{T, 4}, grid; padding_mode = :zeros) where T |
| 101 | + _, _, iC, iN = size(input) |
| 102 | + _, gW, gH, _ = size(grid) |
| 103 | + output = similar(input, T, (gW, gH, iC, iN)) |
| 104 | + grid_sample!(output, input, grid, padding_mode) |
| 105 | +end |
| 106 | +function grid_sample!(output, input, grid, padding_mode) |
| 107 | + pad = Val(padding_mode) |
| 108 | + iW, iH, iC, iN = size(input) |
| 109 | + _, gW, gH, _ = size(grid) |
| 110 | + # Loop over each output pixel. |
| 111 | + Threads.@threads for n in 1:iN |
| 112 | + for w in 1:gW, h in 1:gH |
| 113 | + _grid_sample_kernel!(output, input, grid, pad, w, h, n, iW, iH, iC) |
| 114 | + end |
| 115 | + end |
| 116 | + output |
| 117 | +end |
| 118 | +@inline function _grid_sample_kernel!( |
| 119 | + output, input, grid, padding_mode, w, h, n, iW, iH, iC, |
| 120 | +) |
| 121 | + # Get the corresponding (x, y) coordinates from the grid. |
| 122 | + @inbounds x, y = grid[1, w, h, n], grid[2, w, h, n] |
| 123 | + ix = compute_source_index(x, iW, padding_mode) |
| 124 | + iy = compute_source_index(y, iH, padding_mode) |
| 125 | + # Get corner pixel values from (ix, iy) in north-east-south-west directions. |
| 126 | + ix_nw, iy_nw = floor(Int, ix), floor(Int, iy) |
| 127 | + ix_ne, iy_ne = ix_nw + 1, iy_nw |
| 128 | + ix_sw, iy_sw = ix_nw, iy_nw + 1 |
| 129 | + ix_se, iy_se = ix_ne, iy_sw |
| 130 | + # Get surfaces to each neighbor (a.k.a. interpolation weights). |
| 131 | + nw = (ix_se - ix) * (iy_se - iy) |
| 132 | + ne = (ix - ix_sw) * (iy_sw - iy) |
| 133 | + sw = (ix_ne - ix) * (iy - iy_ne) |
| 134 | + se = (ix - ix_nw) * (iy - iy_nw) |
| 135 | + # ∀ channel: Calculate bilinear weighted pixel value. |
| 136 | + @inbounds for c in 1:iC |
| 137 | + r = 0.0 |
| 138 | + if in_bounds(iy_nw, ix_nw, iH, iW) |
| 139 | + r += input[ix_nw, iy_nw, c, n] * nw |
| 140 | + end |
| 141 | + if in_bounds(iy_ne, ix_ne, iH, iW) |
| 142 | + r += input[ix_ne, iy_ne, c, n] * ne |
| 143 | + end |
| 144 | + if in_bounds(iy_sw, ix_sw, iH, iW) |
| 145 | + r += input[ix_sw, iy_sw, c, n] * sw |
| 146 | + end |
| 147 | + if in_bounds(iy_se, ix_se, iH, iW) |
| 148 | + r += input[ix_se, iy_se, c, n] * se |
| 149 | + end |
| 150 | + output[w, h, c, n] = r |
| 151 | + end |
| 152 | +end |
| 153 | + |
| 154 | +""" |
| 155 | + ∇grid_sample(Δ::AbstractArray{T, 4}, input::AbstractArray{T, 4}, grid::AbstractArray{T, 4}; padding_mode = :zeros) where T |
| 156 | +
|
| 157 | +# Arguments |
| 158 | +
|
| 159 | +- `Δ`: Input gradient in `(W_out, H_out, C, N)` shape |
| 160 | + (same as output of the primal computation). |
| 161 | +- `input`: Input from primal computation in `(W_in, H_in, C, N)` shape. |
| 162 | +- `grid`: Grid from primal computation in `(2, W_out, H_out, N)` shape. |
| 163 | +- `padding_mode`: Out-of-bound padding. |
| 164 | + `:zeros` to use `0` for out-of-bound grid locations. |
| 165 | + `:border` to use border values for out-of-bound grid locations. |
| 166 | + Should be the same as in primal computation. |
| 167 | + Default is `:zeros`. |
| 168 | +
|
| 169 | +# Returns |
| 170 | +
|
| 171 | +`dinput` (same shape as `input`) and `dgrid` (same shape as `grid`) gradients. |
| 172 | +""" |
| 173 | +function ∇grid_sample(Δ::AbstractArray{T, 4}, input::AbstractArray{T, 4}, grid; padding_mode = :zeros) where T |
| 174 | + dx = zeros(T, size(input)) |
| 175 | + dgrid = similar(grid) |
| 176 | + ∇grid_sample!(dx, dgrid, Δ, input, grid, padding_mode) |
| 177 | +end |
| 178 | +function ∇grid_sample!(dx, dgrid, Δ, input, grid, padding_mode) |
| 179 | + pad = Val(padding_mode) |
| 180 | + iW, iH, iC, iN = size(input) |
| 181 | + gW, gH = size(grid, 2), size(grid, 3) |
| 182 | + # Loop over each output pixel. |
| 183 | + Threads.@threads for n in 1:iN |
| 184 | + for w in 1:gW, h in 1:gH |
| 185 | + _∇grid_sample_kernel!(dx, dgrid, Δ, input, grid, pad, w, h, n, iW, iH, iC) |
| 186 | + end |
| 187 | + end |
| 188 | + dx, dgrid |
| 189 | +end |
| 190 | +@inline function _∇grid_sample_kernel!( |
| 191 | + dx, dgrid, Δ, input, grid, padding_mode, w, h, n, iW, iH, iC, |
| 192 | +) |
| 193 | + # Get corresponding (x, y) from grid. |
| 194 | + @inbounds x, y = grid[1, w, h, n], grid[2, w, h, n] |
| 195 | + # Compute multipliers for gradinets on ix, iy. |
| 196 | + ix, gix_mult = ∇compute_source_index(x, iW, padding_mode) |
| 197 | + iy, giy_mult = ∇compute_source_index(y, iH, padding_mode) |
| 198 | + # Get corner pixel values from (ix, iy) in north-east-south-west directions. |
| 199 | + ix_nw, iy_nw = floor(Int, ix), floor(Int, iy) |
| 200 | + ix_ne, iy_ne = ix_nw + 1, iy_nw |
| 201 | + ix_sw, iy_sw = ix_nw, iy_nw + 1 |
| 202 | + ix_se, iy_se = ix_ne, iy_sw |
| 203 | + # Get surfaces to each neighbor (a.k.a. interpolation weights). |
| 204 | + nw = (ix_se - ix) * (iy_se - iy) |
| 205 | + ne = (ix - ix_sw) * (iy_sw - iy) |
| 206 | + sw = (ix_ne - ix) * (iy - iy_ne) |
| 207 | + se = (ix - ix_nw) * (iy - iy_nw) |
| 208 | + # ∀ channel: Calculate billinear weighted pixel value. |
| 209 | + gix, giy = 0.0, 0.0 |
| 210 | + @inbounds for c in 1:iC |
| 211 | + g_out = Δ[w, h, c, n] |
| 212 | + # Calculate dx and dgrid partials. |
| 213 | + if in_bounds(iy_nw, ix_nw, iH, iW) |
| 214 | + _safe_add!(dx, g_out * nw, ix_nw, iy_nw, c, n) |
| 215 | + nw_val = input[ix_nw, iy_nw, c, n] |
| 216 | + gix -= nw_val * (iy_se - iy) * g_out |
| 217 | + giy -= nw_val * (ix_se - ix) * g_out |
| 218 | + end |
| 219 | + if in_bounds(iy_ne, ix_ne, iH, iW) |
| 220 | + _safe_add!(dx, g_out * ne, ix_ne, iy_ne, c, n) |
| 221 | + ne_val = input[ix_ne, iy_ne, c, n] |
| 222 | + gix += ne_val * (iy_sw - iy) * g_out |
| 223 | + giy -= ne_val * (ix - ix_sw) * g_out |
| 224 | + end |
| 225 | + if in_bounds(iy_sw, ix_sw, iH, iW) |
| 226 | + _safe_add!(dx, g_out * sw, ix_sw, iy_sw, c, n) |
| 227 | + sw_val = input[ix_sw, iy_sw, c, n] |
| 228 | + gix -= sw_val * (iy - iy_ne) * g_out |
| 229 | + giy += sw_val * (ix_ne - ix) * g_out |
| 230 | + end |
| 231 | + if in_bounds(iy_se, ix_se, iH, iW) |
| 232 | + _safe_add!(dx, g_out * se, ix_se, iy_se, c, n) |
| 233 | + se_val = input[ix_se, iy_se, c, n] |
| 234 | + gix += se_val * (iy - iy_nw) * g_out |
| 235 | + giy += se_val * (ix - ix_nw) * g_out |
| 236 | + end |
| 237 | + end |
| 238 | + @inbounds dgrid[1, w, h, n] = gix_mult * gix |
| 239 | + @inbounds dgrid[2, w, h, n] = giy_mult * giy |
| 240 | +end |
| 241 | + |
| 242 | +@inline function _safe_add!(dx, value, ix, iy, c, n) |
| 243 | + @inbounds dx[ix, iy, c, n] += value |
| 244 | +end |
| 245 | + |
| 246 | +function rrule(::typeof(grid_sample), x, grid; padding_mode) |
| 247 | + y = grid_sample(x, grid; padding_mode=padding_mode) |
| 248 | + function grid_sample_pullback(Δ) |
| 249 | + ∇x, ∇grid = ∇grid_sample(unthunk(Δ), x, grid; padding_mode=padding_mode) |
| 250 | + NoTangent(), ∇x, ∇grid |
| 251 | + end |
| 252 | + return y, grid_sample_pullback |
| 253 | +end |
0 commit comments