Skip to content

Commit

Permalink
Merge pull request #3 from zygmuntszpak/aml_hessian
Browse files Browse the repository at this point in the history
Adds function to compute the Hessian matrix of the AML cost
  • Loading branch information
zygmuntszpak authored Apr 15, 2018
2 parents d6f73af + 67dddaa commit 1d9f357
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cost_function/ModuleCostFunction.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ModuleCostFunction
using MultipleViewGeometry.ModuleTypes, MultipleViewGeometry.ModuleOperators, MultipleViewGeometry.ModuleMathAliases
using MultipleViewGeometry.ModuleCarriers
export cost, X
export cost, X, H
include("cost_functions.jl")
end
61 changes: 61 additions & 0 deletions src/cost_function/cost_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,64 @@ end
function ฮฃโ‚™(entity::FundamentalMatrix, ๐›‰::Matrix, ๐โ‚™::Matrix)
๐›‰' * ๐โ‚™ * ๐›‰
end

function H(c::CostFunction, entity::FundamentalMatrix, ๐›‰::Matrix, ฮ›::Vector{T1}, matches...) where T1 <: Matrix
โ„ณ, โ„ณสน = matches
N = length(โ„ณ)
if (N != length(โ„ณสน))
throw(ArgumentError("There should be an equal number of points for each view."))
end
if (N != length(ฮ›))
throw(ArgumentError("There should be a covariance matrix for each point correspondence."))
end
_H(c, entity, ๐›‰, ฮ›, โ„ณ, โ„ณสน)
end


function _H(c::CostFunction, entity::ProjectiveEntity, ๐›‰::Matrix, ฮ›::Vector{T1}, ๐’Ÿ...) where T1 <: Matrix
๐— = X(c, entity, ๐›‰, ฮ›, ๐’Ÿ...)
๐“ = T(c, entity, ๐›‰, ฮ›, ๐’Ÿ...)
๐‡ = 2*(๐—-๐“)
end


function T(c::CostFunction, entity::ProjectiveEntity, ๐›‰::Matrix, ฮ›::Vector{T1}, ๐’Ÿ...) where T1 <: Matrix
l = length(๐›‰)
๐ˆโ‚— = eye(l)
๐ˆโ‚˜ = Iโ‚˜(entity)
๐ = fill(0.0,(l,l))
๐Œ = fill(0.0,(l,l))
๐“ = fill(0.0,(l,l))
n = 1
for dataโ‚™ in zip(๐’Ÿ...)
๐’Ÿโ‚™ = dataโ‚™
๐šฒโ‚™ = ฮ›[n]
๐”โ‚™ = uโ‚“(entity,๐’Ÿโ‚™)
โˆ‚โ‚“๐ฎโ‚™ = โˆ‚โ‚“u(entity, ๐’Ÿโ‚™)
๐โ‚™ = โˆ‚โ‚“๐ฎโ‚™ * ๐šฒโ‚™ * โˆ‚โ‚“๐ฎโ‚™'
๐šบโ‚™ = ฮฃโ‚™(entity,๐›‰, ๐โ‚™)
๐šบโ‚™โปยน = inv(๐šบโ‚™)
๐“โ‚ = fill(0.0,(l,l))
๐“โ‚‚ = fill(0.0,(l,l))
๐“โ‚ƒ = fill(0.0,(l,l))
๐“โ‚„ = fill(0.0,(l,l))
๐“โ‚… = fill(0.0,(l,l))
for k = 1:l
๐žโ‚– = fill(0.0,(l,1))
๐žโ‚–[k] = 1
โˆ‚๐žโ‚–๐šบโ‚™ = (๐ˆโ‚˜ โŠ— ๐žโ‚–') * ๐โ‚™ * (๐ˆโ‚˜ โŠ— ๐›‰) + (๐ˆโ‚˜ โŠ— ๐›‰') * ๐โ‚™ * (๐ˆโ‚˜ โŠ— ๐žโ‚–)
๐“โ‚ = ๐“โ‚ + ๐”โ‚™ * ๐šบโ‚™โปยน * (โˆ‚๐žโ‚–๐šบโ‚™) * ๐šบโ‚™โปยน * ๐”โ‚™' * ๐›‰ * ๐žโ‚–'
๐“โ‚‚ = ๐“โ‚‚ + (๐žโ‚–' * ๐”โ‚™ * ๐šบโ‚™โปยน โŠ— ๐ˆโ‚—) * ๐โ‚™ * (๐šบโ‚™โปยน * ๐”โ‚™' * ๐›‰ โŠ— ๐ˆโ‚—) * ๐›‰ * ๐žโ‚–'
๐“โ‚ƒ = ๐“โ‚ƒ + (๐›‰' * ๐”โ‚™ * ๐šบโ‚™โปยน โŠ— ๐ˆโ‚—) * ๐โ‚™ * (๐ˆโ‚˜ โŠ— ๐›‰) * ๐šบโ‚™โปยน * ๐”โ‚™'
๐“โ‚„ = ๐“โ‚„ + (๐›‰' * ๐”โ‚™ * ๐šบโ‚™โปยน * (โˆ‚๐žโ‚–๐šบโ‚™) * ๐šบโ‚™โปยน โŠ— ๐ˆโ‚—) * ๐โ‚™ * (๐šบโ‚™โปยน * ๐”โ‚™' * ๐›‰ โŠ— ๐ˆโ‚—) * ๐›‰ * ๐žโ‚–'
๐“โ‚… = ๐“โ‚… + (๐›‰' * ๐”โ‚™ * ๐šบโ‚™โปยน โŠ— ๐ˆโ‚—) * ๐โ‚™ * (๐šบโ‚™โปยน * (โˆ‚๐žโ‚–๐šบโ‚™) * ๐šบโ‚™โปยน * ๐”โ‚™' * ๐›‰ โŠ— ๐ˆโ‚—) * ๐›‰ * ๐žโ‚–'
end
๐“ = ๐“ + ๐“โ‚ + ๐“โ‚‚ + ๐“โ‚ƒ - ๐“โ‚„ - ๐“โ‚…
n = n + 1
end
๐“
end

function Iโ‚˜(entity::FundamentalMatrix)
eye(1)
end
9 changes: 9 additions & 0 deletions test/cost_functions_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ Jโ‚โ‚˜โ‚— = cost(AML(),FundamentalMatrix(), ๐Ÿ ,
# The true parameters should lie in the null space of the matrix X.
@test all(isapprox.(๐— * ๐Ÿ, 0.0; atol = 1e-10))

# Verify that the the vectorised fundamental matrix is in the null space of H.
# H represents the Hessian matrix of the AML cost function.
๐‡ = H(AML(),FundamentalMatrix(), reshape(๐…,9,1),
[eye(4) for i = 1:length(โ„ณ)], โ„ณ, โ„ณสน)

# The true parameters should lie in the null space of the matrix H.
@test all(isapprox.(๐‡ * ๐Ÿ, 0.0; atol = 1e-10))


# matches = โ„ณ, โ„ณสน
# ฮ› = [eye(4) for i = 1:length(โ„ณ)]
# zip(matches, ฮ›)
Expand Down

0 comments on commit 1d9f357

Please sign in to comment.