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