Passing updated parameters to simulannealbnd objective function

3 views (last 30 days)
I'm working on image segmentation, and I have a computationally expensive objective function. Code's at the bottom. I want my code to run faster, so I've been looking at ways to improve performance. Part of my objective function iterates over every pixel in the image, and compares it with it's 8-neighbourhood. If I'm only changing a small number of pixels, this is a huge source of inefficiency. I'd rather precompute the objective function, then only calculate deltas using the updated pixel labels. This means I need to pass in some data that is updated at each annealing step, and I'm at a loss as to how this can be done. Globals would work, but I usually think of them as a last resort. Any other suggestions?
My simulannealbnd call:
% Configure simulated annealing options
saopt = saoptimset('TemperatureFcn', @temperaturefcn, ...
'AnnealingFcn', @(o, p) annealingfcn(o, p, numLabel), ...
'ReannealInterval', numel(label), ...
'MaxFunEval', 100 * numel(label), ...
'Display', 'iter', ...
'OutputFcns', @(o, p, f) outputfcn(o, p, f, row, col) ...
);
% Update objective function with new alpha every iteration.
objectiveE = @(x) objectivefcn(x, image, pd, iter);
[label, E] = simulannealbnd(objectiveE, label, [], [], ...
saopt);
My objective function:
function E = objectivefcn(label, image, pd, iter)
[row, col, numFeat] = size(image);
% From Equation 11 (Page 2327)
c1 = 80;
c2 = 1 / numFeat;
alpha = c1*0.9^iter + c2;
% Calculate Region and Feature energy
Er = regionEnergy(label, row, col);
Ef = featureEnergy(label, image, pd);
% Total energy
E = Er + alpha * Ef;
end
function Er = regionEnergy(label, row, col)
% From paper (Page 2327)
beta = 1;
% Get 8-neighbourhood of element.
[element, neighbour] = getneighbors(label, row, col);
% Get region energy for each class
Er = 0;
for m = 1:numel(unique(label))
index = (element == m);
% Equation 4 (Page 2325)
Er = Er + beta * cliquePotential(element(index), neighbour(index));
end
end
function potential = cliquePotential(x, y)
% Pairwise MLL potential energy (Equation 4, Page 2325)
potential = sum(x ~= y) - sum(x == y);
end
function [element, neighbour] = getneighbors(A, row, col)
paddedA = nan(row + 2, col + 2);
paddedA(2:row + 1, 2:col + 1) = reshape(A, row, col);
index = repmat(find(~isnan(paddedA))', 8, 1);
element = paddedA(index(:));
neighbourIndex = [(-1:1) - row - 2, -1, 1, (-1:1) + row+2]';
index = index + repmat(neighbourIndex, 1, numel(A));
neighbour = paddedA(index(:));
isValid = ~isnan(neighbour);
element = element(isValid);
neighbour = neighbour(isValid);
end
function Ef = featureEnergy(label, image, pd)
% Iterate over all labels
% Todo: Iterate over all features, too (k superscript in paper)
Ef = 0;
for m = 1:numel(pd);
Ef = Ef + featPotential(image(label == m), pd{m}.mu, pd{m}.sigma);
end
end
function potential = featPotential(image, mu, sigma)
% Equation 7 (Page 2326) (Second term computed in second step)
potential = sum((image - mu).^2 / (2 * sigma ^ 2));
potential = potential + numel(image) * log(sqrt(2 * pi) * sigma);
end

Accepted Answer

Alan Weiss
Alan Weiss on 10 Mar 2014
I have two suggestions:
  1. Learn how to use nested functions. These give you some of the benefits of global variables. For example, see this part of the documentation and this example of using nested functions in an output function.
  2. Try using patternsearch instead of simulannealbnd. patternsearch will probably converge much faster. You might have to start it at several points if you are looking for a global minimum, but I would expect patternsearch to perform better even when using a large number of restarts.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Jason
Jason on 10 Mar 2014
Shoot, thanks for pointing me to the documentation I had even read, and managed to forget in the space of a day and a half of work. Nested functions look to work.
Patternsearch is likely more efficient, but unfortunately I'm bound to the implementation described in the paper I'm working with, at the moment. I've really only started exploring the optimization toolbox.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!