现在的位置: 首页 > 综合 > 正文

MOD 之”Hello World”

2012年01月17日 ⁄ 综合 ⁄ 共 3741字 ⁄ 字号 评论关闭

本文转自:http://blog.csdn.net/abcjennifer/article/details/8603214

首先声明,MOD不是取模函数!MOD是字典学习和sparse coding的一种方法… 最近在看KSVD,其简化版就是MOD(method of directions),这么说吧,KSVD和MOD的优化目标函数是相同的,MOD之所以可以称作KSVD的简化版是因为KSVD在MOD的基础上做了顺序更新列的优化。关于KSVD和MOD的理论知识请见下面我给出的一页note和referenc中的paper。本文主要给出其基本思想及我的代码,已经过测试,如有bug欢迎提出。


Reference

<<From Sparse Solutions of Systems of Equations to Sparse Modeling of Signals and Images>>,
Page 68~70



KSVD & MOD's principle & objective function 

Principle:

简单来说,其优化就是一个OMP(orthogonal matching pursuit)与Regression的迭代过程,因此代码包括一个OMP.m, regression.m.


Objective Function & the variation from MOD to KSVD:




Code

CODE1. MOD

运行Main(Main中通过MOD)学习字典和稀疏表示,MOD迭代调用Regression学习字典,调用和OMP获得sparse representation.


Main.m

  1. %% Main.m  
  2. clc;  
  3. clear;  
  4. P = 512;  
  5. N = 256;  
  6. M = 128;  
  7. K = 100;  
  8.   
  9. %% Data Generator Method 1  
  10. % sparsity_X = 0.4;  
  11. % Y = randi(10,M,P);  
  12. % X = floor(sprand(N,P,sparsity_X)*10);  
  13.   
  14. %% Data Generator Method 2  
  15. Y = randn(M,P);%Notice that Y should be full rank, that is, rank(Y) = N  
  16. X = randn(N,P);% initialization of X  
  17.   
  18. %% Main Iteration  
  19. [D,X] = MOD(Y,X,K,1e-4);  

MOD.m

  1. %   @Function: Method Of Dirction of 2D signal  
  2. %   For dictionary and sparse representation learning  
  3. %   @CreateTime: 2013-2-22  
  4. %   @Author: Rachel Zhang  @  http://blog.csdn.net/abcjennifer  
  5. %     
  6. %   @Reference: From Sparse Solutions of Systems of Equations to   
  7. %   Sparse Modeling of Signals and Images  
  8.   
  9. function [ D , X ] = MOD( Y ,X ,K ,ErrorThreshold )  
  10. %MOD Summary of this function goes here  
  11. %   Detailed explanation goes here  
  12. %   Sample_Data is Y  
  13. %   Coefficient is X  
  14. %   Dictionary is D  
  15. %   sparsity is K  
  16.   
  17. disp('Run Method of directions');  
  18. iteration_time = 1;  
  19. error = ErrorThreshold+1;  
  20.   
  21.   
  22. while error>=ErrorThreshold;  
  23.     disp(['iteration time = ' num2str(iteration_time)]);  
  24.     D = Regression(Y,X);  
  25.     X = OMP(Y,D,K);  
  26.     iteration_time = iteration_time+1;  
  27.     error = sum(sum(abs(Y-D*X)))  
  28. end  
  29.   
  30. end  



OMP.m

  1. %   @Function: Orthogonal Matching Pursuit of 2D signal  
  2. %   Learning Sparse Representation Given Dictionary  
  3. %   @CreateTime: 2013-2-21  
  4. %   @Author: Rachel Zhang  @  http://blog.csdn.net/abcjennifer  
  5. %     
  6. %   @Reference: http://www.eee.hku.hk/~wsha/Freecode/freecode.htm     
  7.   
  8. function [ X ] = OMP( Y,D,K )  
  9. % Y is the sample data to be recovered M*P  
  10. % D is the dictionary M*N  
  11. % X is the sparse coefficient N*P  
  12. % K is the sparsity  
  13.   
  14. if nargin==2  
  15.     K = size(D,2);  
  16. end;  
  17.   
  18. M = size(D,1);  
  19. P = size(Y,2);  
  20. N = size(D,2);  
  21. m = K*2;  % execute iterations  
  22.   
  23. for idx = 1:P  
  24.     % recover the idx-th column sample  
  25.     y = Y(:,idx);  
  26.     residual = y;  
  27.     Aug_D = [];  
  28.     D1 = D;  
  29.       
  30.     for times = 1:m;  
  31.         product = abs(D1'*residual);  
  32.         [~,pos] = max(product); %  最大投影系数对应的位置  
  33.         Aug_D = [Aug_D, D1(:,pos)];  
  34.         D1(:,pos) = zeros(M,1);    %去掉选中的列  
  35.         indx(times) = pos;  
  36.         Aug_x = (Aug_D'*Aug_D)^-1*Aug_D'*y; %  最小二乘,使残差最小,i.e. x = pinv(Aug_D)*y  
  37.         residual = y - Aug_D*Aug_x;  
  38.           
  39.         if sum(residual.^2)<1e-6  
  40.             break;  
  41.         end  
  42.     end  
  43.     temp = zeros(N,1);  
  44.     temp(indx(1:times)) = Aug_x;  
  45.     X(:,idx) = sparse(temp);  
  46. end  
  47. end  



Regression.m

  1. %   @Function: Dictionary learning & Regression  
  2. %   Learning Dictionary Given Sparse Representation  
  3. %   @CreateTime: 2013-2-21  
  4. %   @Author: Rachel Zhang  @  http://blog.csdn.net/abcjennifer  
  5. %     
  6. function [ D ] = Regression( Y,X )  
  7. % Y is the sample data to be recovered M*P  
  8. % D is the dictionary M*N  
  9. % X is the sparse coefficient N*P  
  10. % P>N>M  
  11.   
  12. %由于X是扁矩阵,需要转置求D0 = min(D) ||Y^T-X^TD^T||  
  13. %这样就是N个未知数,P个方程去求解;  
  14. %每次解得D中的一列,共解M次  
  15.   
  16. Y = Y';  
  17. X = X';  
  18. P = size(Y,1);  
  19. N = size(X,2);  
  20. M = size(Y,2);  
  21. D = zeros(N,M);  
  22.   
  23. for i = 1:M;  
  24.     y = Y(:,i);  
  25.     D(:,i) =

抱歉!评论已关闭.