1. 关于全局最优化求解

  全局最优化是一个非常复杂的问题,目前还没有一个通用的办法可以对任意复杂函数求解全局最优值。上一篇文章讲解了一个求解局部极小值的方法——梯度下降法。这种方法对于求解精度不高的情况是实用的,可以用局部极小值近似替代全局最小值点。但是当要求精确求解全局最小值时,梯度下降法就不适用了,需要采用其他的办法求解。常见的求解全局最优的办法有拉格朗日法、线性规划法、以及一些人工智能算法比如遗传算法、粒子群算法、模拟退火算法等(可以参见我之前的博客)。而今天要讲的是一个操作简单但是不易陷入局部极小值的方法:随机游走算法。

2. 随机游走算法操作步骤

设f(x)f(x)是一个含有nn个变量的多元函数,x=(x1,x2,...,xn)x=(x1,x2,...,xn)为nn维向量。

  1. 给定初始迭代点xx,初次行走步长λλ,控制精度ϵϵ(ϵϵ是一个非常小的正数,用于控制结束算法)。
  2. 给定迭代控制次数NN,kk为当前迭代次数,置k=1k=1。
  3. 当 k<Nk<N时,随机生成一个(−1,1)(−1,1)之间的nn维向量u=(u1,u2,⋯,un),(−1<ui<1,i=1,2,⋯,n)u=(u1,u2,⋯,un),(−1<ui<1,i=1,2,⋯,n),并将其标准化得到u′=u∑ni=1u2i√u′=u∑i=1nui2。令x1=x+λu′x1=x+λu′,完成第一步游走。
  4. 计算函数值,如果 f(x1)<f(x)f(x1)<f(x),即找到了一个比初始值好的点,那么kk重新置为1,将x1x1变为xx,回到第2步;否则k=k+1k=k+1,回到第3步。
  5. 如果连续NN次都找不到更优的值,则认为,最优解就在以当前最优解为中心,当前步长为半径的NN维球内(如果是三维,则刚好是空间中的球体)。此时,如果λ<ϵλ<ϵ,则结束算法;否则,令λ=λ2λ=λ2,回到第1步,开始新一轮游走。

 

clear ;
close all;

addpath 'algorithms'
out = ['results\'];
if ~exist(out)
    mkdir(out);
end

%% parameters            
only_name= '41004';
img_name = ['./imgs/' only_name '.jpg'];
ref_name = ['./scribbles/' only_name '.bmp'];


nei = 1;            % 0: 4-neighbors, 1: 8-neighbors
c = 0.0004;%1e-3;%         % restarting probability of RWR
sigma_c = 60;       % color variance
scale = 1.0;        % image resize
lambda = 2e-10;     % parameter for unitary
isKeepConnect = 0;  % 1: only consinder the connected regions with seeds;  0: otherwise.
reset(RandStream.getGlobalStream); % fix the random seed for initalization of GMM

saveProb = 0; % 1: save the probability image
%% main routine
img = imread(img_name); img = imresize(img,scale);
[K, labels, idx] = seed_generation(ref_name,scale);

%% RWR with prior
% run('vlfeat-0.9.13/toolbox/vl_setup');
st=clock;
[posteriors label_img] = do_RWR_prior(img,idx,labels,c,lambda,nei,sigma_c,isKeepConnect);
fprintf('subRW took %.2f second\n',etime(clock,st));

% display
[imgMasks,segOutline,imgMarkup]=segoutput(im2double(img),label_img); %clear imgMasks segOutline;

outPath = [out,'ours\'];
if ~exist(outPath)
    mkdir(outPath);
end

figure; clf;set(gcf,'Position',[100,500,size(img,2)*(K+1),size(img,1)]);
for k=1:K 
    prob_img = sc(posteriors(:,:,k),'prob_jet');
    if saveProb == 1
        imwrite(prob_img,[outPath,only_name,'_prob',num2str(k),'.png']);
    end
    subplot(1,K+1,k); imshow(prob_img); clear prob_img;
end;
subplot(1,K+1,K+1); imshow(imgMarkup);
figure,imshow((K-imgMasks)/(K-1));

imwrite(imgMarkup,[outPath,only_name,'_bound.png']);
imwrite((K-imgMasks)/(K-1),[outPath,only_name,'_binary.png']);

【图像分割】基于随机游走算法的图像分割matlab源码_全局最优化

【图像分割】基于随机游走算法的图像分割matlab源码_全局最优化_02