comparison examples/Image Denoising/SMALL_ImgDenoise_DL_test_Training_size.m @ 6:f72603404233

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