idamnjanovic@6
|
1 %% DICTIONARY LEARNING FOR IMAGE DENOISING
|
idamnjanovic@6
|
2 % This file contains an example of how SMALLbox can be used to test different
|
idamnjanovic@6
|
3 % dictionary learning techniques in Image Denoising problem.
|
idamnjanovic@6
|
4 % It calls generateImageDenoiseProblem that will let you to choose image,
|
idamnjanovic@6
|
5 % add noise and use noisy image to generate training set for dictionary
|
idamnjanovic@6
|
6 % learning.
|
idamnjanovic@6
|
7 % We tested time and psnr for two dictionary learning techniques. This
|
idamnjanovic@6
|
8 % example does not represnt any extensive testing. The aim of this
|
idamnjanovic@6
|
9 % example is just to show how SMALL structure can be used for testing.
|
idamnjanovic@6
|
10 %
|
idamnjanovic@6
|
11 % Two dictionary learning techniques were compared:
|
idamnjanovic@6
|
12 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
|
idamnjanovic@6
|
13 % Implementation of the K-SVD Algorithm using Batch Orthogonal
|
idamnjanovic@6
|
14 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
|
idamnjanovic@6
|
15 % - SPAMS - J. Mairal, F. Bach, J. Ponce and G. Sapiro. Online
|
idamnjanovic@6
|
16 % Dictionary Learning for Sparse Coding. International
|
idamnjanovic@6
|
17 % Conference on Machine Learning,Montreal, Canada, 2009
|
idamnjanovic@6
|
18 %
|
idamnjanovic@6
|
19 %
|
idamnjanovic@6
|
20 % Ivan Damnjanovic 2010
|
idamnjanovic@6
|
21 %%
|
idamnjanovic@6
|
22
|
idamnjanovic@6
|
23 clear all;
|
idamnjanovic@6
|
24
|
idamnjanovic@6
|
25 %% Load an image
|
idamnjanovic@6
|
26 TMPpath=pwd;
|
idamnjanovic@6
|
27 FS=filesep;
|
idamnjanovic@6
|
28 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
|
idamnjanovic@6
|
29 cd([pathstr1,FS,'data',FS,'images']);
|
idamnjanovic@6
|
30 [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes');
|
idamnjanovic@6
|
31 [pathstr, name, ext, versn] = fileparts(filename);
|
idamnjanovic@6
|
32 test_image = imread(filename);
|
idamnjanovic@6
|
33 test_image = double(test_image);
|
idamnjanovic@6
|
34 cd(TMPpath);
|
idamnjanovic@6
|
35
|
idamnjanovic@6
|
36 % number of different values we want to test
|
idamnjanovic@6
|
37 n =5;
|
idamnjanovic@6
|
38 step = floor((size(test_image,1)-8+1)*(size(test_image,2)-8+1)/n);
|
idamnjanovic@6
|
39 Training_size=zeros(1,n);
|
idamnjanovic@6
|
40 time = zeros(2,n);
|
idamnjanovic@6
|
41 psnr = zeros(2,n);
|
idamnjanovic@6
|
42 for i=1:n
|
idamnjanovic@6
|
43
|
idamnjanovic@6
|
44 % Here we want to test time spent and quality of denoising for
|
idamnjanovic@6
|
45 % different sizes of training sample.
|
idamnjanovic@6
|
46 Training_size(i)=i*step;
|
idamnjanovic@6
|
47
|
idamnjanovic@6
|
48 SMALL.Problem = generateImageDenoiseProblem(test_image,Training_size(i));
|
idamnjanovic@6
|
49 SMALL.Problem.name=name;
|
idamnjanovic@6
|
50 %%
|
idamnjanovic@6
|
51 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
|
idamnjanovic@6
|
52
|
idamnjanovic@6
|
53 % Initialising Dictionary structure
|
idamnjanovic@6
|
54 % Setting Dictionary structure fields (toolbox, name, param, D and time)
|
idamnjanovic@6
|
55 % to zero values
|
idamnjanovic@6
|
56
|
idamnjanovic@6
|
57 SMALL.DL(1)=SMALL_init_DL();
|
idamnjanovic@6
|
58
|
idamnjanovic@6
|
59 % Defining the parameters needed for dictionary learning
|
idamnjanovic@6
|
60
|
idamnjanovic@6
|
61 SMALL.DL(1).toolbox = 'KSVD';
|
idamnjanovic@6
|
62 SMALL.DL(1).name = 'ksvd';
|
idamnjanovic@6
|
63
|
idamnjanovic@6
|
64 % Defining the parameters for KSVD
|
idamnjanovic@6
|
65 % In this example we are learning 256 atoms in 20 iterations, so that
|
idamnjanovic@6
|
66 % every patch in the training set can be represented with target error in
|
idamnjanovic@6
|
67 % L2-norm (EData)
|
idamnjanovic@6
|
68 % Type help ksvd in MATLAB prompt for more options.
|
idamnjanovic@6
|
69
|
idamnjanovic@6
|
70 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
|
idamnjanovic@6
|
71 SMALL.DL(1).param=struct(...
|
idamnjanovic@6
|
72 'Edata', Edata,...
|
idamnjanovic@6
|
73 'initdict', SMALL.Problem.initdict,...
|
idamnjanovic@6
|
74 'dictsize', SMALL.Problem.p,...
|
idamnjanovic@6
|
75 'iternum', 20,...
|
idamnjanovic@6
|
76 'memusage', 'high');
|
idamnjanovic@6
|
77
|
idamnjanovic@6
|
78 % Learn the dictionary
|
idamnjanovic@6
|
79
|
idamnjanovic@6
|
80 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
|
idamnjanovic@6
|
81
|
idamnjanovic@6
|
82 % Set SMALL.Problem.A dictionary
|
idamnjanovic@6
|
83 % (backward compatiblity with SPARCO: solver structure communicate
|
idamnjanovic@6
|
84 % only with Problem structure, ie no direct communication between DL and
|
idamnjanovic@6
|
85 % solver structures)
|
idamnjanovic@6
|
86
|
idamnjanovic@6
|
87 SMALL.Problem.A = SMALL.DL(1).D;
|
idamnjanovic@6
|
88
|
idamnjanovic@6
|
89
|
idamnjanovic@6
|
90 %%
|
idamnjanovic@6
|
91 % Initialising solver structure
|
idamnjanovic@6
|
92 % Setting solver structure fields (toolbox, name, param, solution,
|
idamnjanovic@6
|
93 % reconstructed and time) to zero values
|
idamnjanovic@6
|
94
|
idamnjanovic@6
|
95
|
idamnjanovic@6
|
96 SMALL.solver(1)=SMALL_init_solver;
|
idamnjanovic@6
|
97
|
idamnjanovic@6
|
98 % Defining the parameters needed for denoising
|
idamnjanovic@6
|
99
|
idamnjanovic@6
|
100 SMALL.solver(1).toolbox='ompbox';
|
idamnjanovic@6
|
101 SMALL.solver(1).name='ompdenoise';
|
idamnjanovic@6
|
102
|
idamnjanovic@6
|
103 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
|
idamnjanovic@6
|
104 % but backward compatible with KSVD definition of denoising
|
idamnjanovic@6
|
105
|
idamnjanovic@6
|
106 SMALL.solver(1)=SMALL_denoise(SMALL.Problem, SMALL.solver(1));
|
idamnjanovic@6
|
107
|
idamnjanovic@6
|
108 %%
|
idamnjanovic@6
|
109 % Use SPAMS Online Dictionary Learning Algorithm
|
idamnjanovic@6
|
110 % to Learn overcomplete dictionary (Julien Mairal 2009)
|
idamnjanovic@6
|
111 % (If you have not installed SPAMS please comment the following two cells)
|
idamnjanovic@6
|
112
|
idamnjanovic@6
|
113 % Initialising Dictionary structure
|
idamnjanovic@6
|
114 % Setting Dictionary structure fields (toolbox, name, param, D and time)
|
idamnjanovic@6
|
115 % to zero values
|
idamnjanovic@6
|
116
|
idamnjanovic@6
|
117 SMALL.DL(2)=SMALL_init_DL();
|
idamnjanovic@6
|
118
|
idamnjanovic@6
|
119 % Defining fields needed for dictionary learning
|
idamnjanovic@6
|
120
|
idamnjanovic@6
|
121 SMALL.DL(2).toolbox = 'SPAMS';
|
idamnjanovic@6
|
122 SMALL.DL(2).name = 'mexTrainDL';
|
idamnjanovic@6
|
123
|
idamnjanovic@6
|
124 % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
|
idamnjanovic@6
|
125
|
idamnjanovic@6
|
126 SMALL.DL(2).param=struct(...
|
idamnjanovic@6
|
127 'D', SMALL.Problem.initdict,...
|
idamnjanovic@6
|
128 'K', SMALL.Problem.p,...
|
idamnjanovic@6
|
129 'lambda', 2,...
|
idamnjanovic@6
|
130 'iter', 300,...
|
idamnjanovic@6
|
131 'mode', 3,...
|
idamnjanovic@6
|
132 'modeD', 0 );
|
idamnjanovic@6
|
133
|
idamnjanovic@6
|
134 % Learn the dictionary
|
idamnjanovic@6
|
135
|
idamnjanovic@6
|
136 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
|
idamnjanovic@6
|
137
|
idamnjanovic@6
|
138 % Set SMALL.Problem.A dictionary
|
idamnjanovic@6
|
139 % (backward compatiblity with SPARCO: solver structure communicate
|
idamnjanovic@6
|
140 % only with Problem structure, ie no direct communication between DL and
|
idamnjanovic@6
|
141 % solver structures)
|
idamnjanovic@6
|
142
|
idamnjanovic@6
|
143 SMALL.Problem.A = SMALL.DL(2).D;
|
idamnjanovic@6
|
144
|
idamnjanovic@6
|
145
|
idamnjanovic@6
|
146 %%
|
idamnjanovic@6
|
147 % Initialising solver structure
|
idamnjanovic@6
|
148 % Setting solver structure fields (toolbox, name, param, solution,
|
idamnjanovic@6
|
149 % reconstructed and time) to zero values
|
idamnjanovic@6
|
150
|
idamnjanovic@6
|
151 SMALL.solver(2)=SMALL_init_solver;
|
idamnjanovic@6
|
152
|
idamnjanovic@6
|
153 % Defining the parameters needed for denoising
|
idamnjanovic@6
|
154
|
idamnjanovic@6
|
155 SMALL.solver(2).toolbox='ompbox';
|
idamnjanovic@6
|
156 SMALL.solver(2).name='ompdenoise';
|
idamnjanovic@6
|
157
|
idamnjanovic@6
|
158 % Denoising the image - SMALL_denoise function is similar to SMALL_solve,
|
idamnjanovic@6
|
159 % but backward compatible with KSVD definition of denoising
|
idamnjanovic@6
|
160
|
idamnjanovic@6
|
161 SMALL.solver(2)=SMALL_denoise(SMALL.Problem, SMALL.solver(2));
|
idamnjanovic@6
|
162
|
idamnjanovic@6
|
163
|
idamnjanovic@6
|
164
|
idamnjanovic@6
|
165 %% show results %%
|
idamnjanovic@6
|
166 % This will show denoised images and dictionaries for all training sets.
|
idamnjanovic@6
|
167 % If you are not interested to see them and do not want clutter your
|
idamnjanovic@6
|
168 % screen comment following line
|
idamnjanovic@6
|
169
|
idamnjanovic@6
|
170 SMALL_ImgDeNoiseResult(SMALL);
|
idamnjanovic@6
|
171
|
idamnjanovic@6
|
172 time(1,i) = SMALL.DL(1).time;
|
idamnjanovic@6
|
173 psnr(1,i) = SMALL.solver(1).reconstructed.psnr;
|
idamnjanovic@6
|
174
|
idamnjanovic@6
|
175 time(2,i) = SMALL.DL(2).time;
|
idamnjanovic@6
|
176 psnr(2,i) = SMALL.solver(2).reconstructed.psnr;
|
idamnjanovic@6
|
177
|
idamnjanovic@6
|
178 clear SMALL
|
idamnjanovic@6
|
179 end
|
idamnjanovic@6
|
180
|
idamnjanovic@6
|
181 %% show time and psnr %%
|
idamnjanovic@6
|
182 figure('Name', 'KSVD vs SPAMS');
|
idamnjanovic@6
|
183
|
idamnjanovic@6
|
184 subplot(1,2,1); plot(Training_size, time(1,:), 'ro-', Training_size, time(2,:), 'b*-');
|
idamnjanovic@6
|
185 legend('KSVD','SPAMS',0);
|
idamnjanovic@6
|
186 title('Time vs Training size');
|
idamnjanovic@6
|
187 subplot(1,2,2); plot(Training_size, psnr(1,:), 'ro-', Training_size, psnr(2,:), 'b*-');
|
idamnjanovic@6
|
188 legend('KSVD','SPAMS',0);
|
idamnjanovic@6
|
189 title('PSNR vs Training size'); |