Skip to content

ForwardDiff not working for simple 2D linear interpolation #430

@arbenede

Description

@arbenede

I am trying to compute the Jacobian of a plain Sum of Square Difference (SSD) cost function between two shifted images where interpolation is computed using Interpolations.jl with respect to the shift variables. Here is the code:

using Images
using TestImages
using ForwardDiff
using Interpolations

img1 = Gray.(testimage("cameraman.tif"))
mat1 = convert(Array{Float64}, img1)
(nr, nc) = size(mat1)

# Define image shift ranges
Δx_min = -2.5
Δx_max = 2.5

Δy_min = -2.5
Δy_max = 2.5

# Define img1 coordinates
xcoord = range(ceil(Int, abs(Δx_min)), stop=nc - 1 - ceil(Int, abs(Δx_max)))
ycoord = range(ceil(Int, abs(Δy_min)), stop=nr - 1 - ceil(Int, abs(Δy_max)))

Δx = -1.34
Δy = 0.78

# Create interpolator
interp_linear = LinearInterpolation((range(0,stop=nr-1), range(0,stop=nc-1)), mat1)
mat2 = interp_linear(ycoord .+ Δy, xcoord .+ Δx)

function cost(Δ::Vector{T}) where T <: Real
    subimg1 = mat1[ycoord, xcoord]
    subimg2 = interp_linear(ycoord .+ Δ[2], xcoord .+ Δ[1])
    
    @. subimg1 = (subimg1 - subimg2)^2
    SSD = sum(vec(subimg1))
    
    return SSD
end

ForwardDiff.gradient(cost, [0.0,0.0])

and I get this error:

MethodError: no method matching Integer(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2})
Closest candidates are:
  (::Type{T})(::T) where T<:Number at boot.jl:760
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
  (::Type{T})(::Base.TwicePrecision) where T<:Number at twiceprecision.jl:243
  ...

Stacktrace:
  [1] unsafe_length(r::UnitRange{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}})
    @ Base ./range.jl:559
  [2] length(r::UnitRange{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}})
    @ Base ./range.jl:561
  [3] size(r::UnitRange{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}})
    @ Base ./range.jl:515
  [4] axes
    @ ./abstractarray.jl:89 [inlined]
  [5] axes1
    @ ./abstractarray.jl:109 [inlined]
  [6] shape(v::UnitRange{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}}, rest::UnitRange{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}})
    @ Interpolations ~/.julia/packages/Interpolations/GIn2o/src/utils.jl:71
  [7] (::Interpolations.Extrapolation{Float64, 2, ScaledInterpolation{Float64, 2, Interpolations.BSplineInterpolation{Float64, 2, Matrix{Float64}, BSpline{Linear}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, BSpline{Linear}, Tuple{UnitRange{Int64}, UnitRange{Int64}}}, BSpline{Linear}, Throw{Nothing}})(::UnitRange{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}}, ::UnitRange{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}})
    @ Interpolations ~/.julia/packages/Interpolations/GIn2o/src/extrapolation/extrapolation.jl:56
  [8] cost(Δ::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}})
    @ Main ./In[10]:3
  [9] vector_mode_dual_eval
    @ ~/.julia/packages/ForwardDiff/QOqCN/src/apiutils.jl:37 [inlined]
 [10] vector_mode_gradient(f::typeof(cost), x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}}})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/QOqCN/src/gradient.jl:106
 [11] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}}}, ::Val{true})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/QOqCN/src/gradient.jl:19
 [12] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(cost), Float64}, Float64, 2}}}) (repeats 2 times)
    @ ForwardDiff ~/.julia/packages/ForwardDiff/QOqCN/src/gradient.jl:17
 [13] top-level scope
    @ In[12]:1
 [14] eval
    @ ./boot.jl:360 [inlined]
 [15] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1094

By the way, I also tried using the translation image transformation defined in ImageTransformations.jl with no success. More details on that attempt are at forwarddiff-and-imagetransformations
It seems that ForwardDiff should be able to handle this, right?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions