comparison examples/Image Denoising/SMALL_ImgDenoise_dic_ODCT_solvers_OMP_BPDN_etc_test.m @ 43:984c3c175be2

(none)
author idamnjanovic
date Mon, 14 Mar 2011 15:41:59 +0000
parents
children
comparison
equal deleted inserted replaced
42:623fcf3a69b1 43:984c3c175be2
1 %% DICTIONARY LEARNING FOR IMAGE DENOISING
2 % This file contains an example of how SMALLbox can be used to test different
3 % dictionary learning techniques in Image Denoising problem.
4 % It calls generateImageDenoiseProblem that will let you to choose image,
5 % add noise and use noisy image to generate training set for dictionary
6 % learning.
7 % Three dictionary learning techniques were compared:
8 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
9 % Implementation of the K-SVD Algorithm using Batch Orthogonal
10 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
11 % - KSVDS - R. Rubinstein, M. Zibulevsky, and M. Elad, "Learning Sparse
12 % Dictionaries for Sparse Signal Approximation", Technical
13 % Report - CS, Technion, June 2009.
14 % - SPAMS - J. Mairal, F. Bach, J. Ponce and G. Sapiro. Online
15 % Dictionary Learning for Sparse Coding. International
16 % Conference on Machine Learning,Montreal, Canada, 2009
17 %
18 %
19 % Ivan Damnjanovic 2010
20 %%
21
22 clear;
23
24 % If you want to load the image outside of generateImageDenoiseProblem
25 % function uncomment following lines. This can be useful if you want to
26 % denoise more then one image for example.
27
28 % TMPpath=pwd;
29 % FS=filesep;
30 % [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
31 % cd([pathstr1,FS,'data',FS,'images']);
32 % [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes');
33 % [pathstr, name, ext, versn] = fileparts(filename);
34 % test_image = imread(filename);
35 % test_image = double(test_image);
36 % cd(TMPpath);
37 % SMALL.Problem.name=name;
38
39
40 % Defining Image Denoising Problem as Dictionary Learning
41 % Problem. As an input we set the number of training patches.
42
43 SMALL.Problem = generateImageDenoiseProblem('', 40000, '','', 20);
44
45 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
46 maxatoms = floor(prod(SMALL.Problem.blocksize)/2);
47 %% Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
48 %
49 % % Initialising Dictionary structure
50 % % Setting Dictionary structure fields (toolbox, name, param, D and time)
51 % % to zero values
52 %
53 % SMALL.DL(1)=SMALL_init_DL();
54 %
55 % % Defining the parameters needed for dictionary learning
56 %
57 % SMALL.DL(1).toolbox = 'KSVD';
58 % SMALL.DL(1).name = 'ksvd';
59 %
60 % % Defining the parameters for KSVD
61 % % In this example we are learning 256 atoms in 20 iterations, so that
62 % % every patch in the training set can be represented with target error in
63 % % L2-norm (EData)
64 % % Type help ksvd in MATLAB prompt for more options.
65 %
66 %
67 % SMALL.DL(1).param=struct(...
68 % 'Edata', Edata,...
69 % 'initdict', SMALL.Problem.initdict,...
70 % 'dictsize', SMALL.Problem.p,...
71 % 'iternum', 20,...
72 % 'memusage', 'high');
73 %
74 % % Learn the dictionary
75 %
76 % SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
77 %% Initialising Dictionary structure
78 % Setting Dictionary structure fields (toolbox, name, param, D and time)
79 % to zero values
80 %
81
82 SMALL.DL(1)=SMALL_init_DL();
83 % Take initial dictonary (overcomplete DCT) to be a final dictionary for
84 % reconstruction
85
86 SMALL.DL(1).D=SMALL.Problem.initdict;
87 %%
88
89 % Set SMALL.Problem.A dictionary
90 % (backward compatiblity with SPARCO: solver structure communicate
91 % only with Problem structure, ie no direct communication between DL and
92 % solver structures)
93 SMALL.Problem.A = SMALL.DL(1).D;
94
95 SparseDict=0;
96 SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem, SparseDict);
97
98 %%
99 % Initialising solver structure
100 % Setting solver structure fields (toolbox, name, param, solution,
101 % reconstructed and time) to zero values
102
103
104 SMALL.solver(1)=SMALL_init_solver;
105
106 % Defining the parameters needed for image denoising
107
108 SMALL.solver(1).toolbox='ompbox';
109 SMALL.solver(1).name='omp2';
110 SMALL.solver(1).param=struct(...
111 'epsilon',Edata,...
112 'maxatoms', maxatoms);
113
114 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
115 % but backward compatible with KSVD definition of denoising
116 % Pay attention that since implicit base dictionary is used, denoising
117 % can be much faster then using explicit dictionary in KSVD example.
118
119 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
120
121 %%
122 % Initialising solver structure
123 % Setting solver structure fields (toolbox, name, param, solution,
124 % reconstructed and time) to zero values
125 lam=2*SMALL.Problem.sigma;%*sqrt(2*log2(size(SMALL.Problem.A,1)))
126 for i=1:11
127 lambda(i)=lam+5-(i-1);
128 SMALL.DL(2)=SMALL_init_DL();
129 i
130 %SMALL.Problem.A = SMALL.Problem.initdict;
131 SMALL.DL(2).D=SMALL.Problem.initdict;
132 SMALL.solver(2)=SMALL_init_solver;
133
134 % Defining the parameters needed for image denoising
135
136 SMALL.solver(2).toolbox='SPAMS';
137 SMALL.solver(2).name='mexLasso';
138 SMALL.solver(2).param=struct(...
139 'mode', 2, ...
140 'lambda',lambda(i),...
141 'L', maxatoms);
142
143 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
144 % but backward compatible with KSVD definition of denoising
145 % Pay attention that since implicit base dictionary is used, denoising
146 % can be much faster then using explicit dictionary in KSVD example.
147
148 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
149
150
151 % show results %
152
153 %SMALL_ImgDeNoiseResult(SMALL);
154
155 time(1,i) = SMALL.solver(2).time;
156 psnr(1,i) = SMALL.solver(2).reconstructed.psnr;
157 end%% show time and psnr %%
158 figure('Name', 'SPAMS LAMBDA TEST');
159
160 subplot(1,2,1); plot(lambda, time(1,:), 'ro-');
161 title('time vs lambda');
162 subplot(1,2,2); plot(lambda, psnr(1,:), 'b*-');
163 title('PSNR vs lambda');