Porting a MATLAB code, Numeric vs Analytic derivatives.

98 views
Skip to first unread message

xeo...@gmail.com

unread,
May 22, 2015, 1:36:45 PM5/22/15
to ceres-...@googlegroups.com
Hi,
I'm new to the Ceres and non linear least square in general. I've been given the task of porting this piece of MATLAB code. After reading tutorials I was wondering if it's possible to solve this using only numeric derivatives, or since I have the Jacobian, Maybe I should go with analytic derivatives.

Any help will be greatly appreciated!

function [f, J] = Cost(I, LD, lambda)
% The Cost function
% INPUT:
%     I: MxN matrix for the intensity
%     LD: 3xM matrix for normalized unit directions
%     lambda: Mx1 vector for the strength
% OUTPUT:
%     f: (M*N)x1 vector for intensity error
%     J: (M*N)xM matrix for Jacobian of 'f' over 'lambda'.


[M, N] = size(I);
L = repmat(lambda', [3 1]) .* LD;

f = L'*(L'\I) - I;
f = f(:);

if (nargout > 1)
   J = zeros(M*N, M);
   LLinv = inv(L*L');
   for i = 1:M
       dL = zeros(3, M);
       dL(:,i) = LD(:,i);
       df = dL'*LLinv*L - L'*LLinv*(dL*L'+L*dL')*LLinv*L + L'*LLinv*dL;
       Ji = df * I;
       J(:,i) = Ji(:);
    end
end


% Solve
lambda0 = ones(M, 1);
fcn = @(lambda)Cost(I, LD, lambda);
optimOpts = struct('Jacobian', 'on');
lambda = NonlinearLeastSquares(fcn, lambda0, [], [], optimOpts);


Sameer Agarwal

unread,
May 22, 2015, 1:37:58 PM5/22/15
to ceres-...@googlegroups.com
Please consider providing a mathematical description of your cost function.

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/a888589f-9cb3-49b1-83bd-16ffa9d5201d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

xeo...@gmail.com

unread,
May 22, 2015, 2:43:05 PM5/22/15
to ceres-...@googlegroups.com
Sure, I have a series of lighting vector directions (LD) and measured pixels intensities (I) and I would like to find the unknown lighting strength (lambda) parameters from the measured pixels intensities. The cost function can be in from of 


where M is the number of images and N number of pixels. By assuming the directional lighting model, the strength (lambda) can be estimated



where



the author suggested that the minimization over "bj " inside the sum can be analytically solved, resulting in a cost function below


Thank you for your help.

Sameer Agarwal

unread,
May 22, 2015, 2:48:36 PM5/22/15
to ceres-...@googlegroups.com
you could try not following the authors' advice and keep your life simpler and not optimize out b_j.
sure it reduces the size of the optimization problem, but it makes it more non-linear and makes derivatives a problem - it is possible to derive the derivatives, but it will take some work.

use analytical derivatives if possible.
Sameer


Reply all
Reply to author
Forward
0 new messages