-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDemo_Test_Real.m
147 lines (106 loc) · 4.08 KB
/
Demo_Test_Real.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
clear; clc, close all;
addpath(fullfile('utilities'));
format compact;
% run vl_setupnn in the MatConvNet directory
run /home/alex/matconvnet-1.0-beta25/matlab/vl_setupnn
%-------------------------------------------------------------------------
% parameter setting
%-------------------------------------------------------------------------
global sigmas; %input noise level
folderTest = 'testsets/real_datasets';
folderResults= 'Results';
save_result = 1; %1 if you want t save the denoised HSI, 0 otherwise
imageSet = {'Indian'};
%imageSet = {'Pavia'};
showResult = 1;
useGPU = 1; % CPU or GPU.
nch = 25;
inputNoiseSigma = 50; % input noise level
%-------------------------------------------------------------------------
% load model
%-------------------------------------------------------------------------
load(fullfile('BestModel','best_model')); %load model
net.layers = net.layers(1:end-1);
net = vl_simplenn_tidy(net);
if useGPU
net = vl_simplenn_move(net, 'gpu') ;
end
%vl_simplenn_display(net);
%-------------------------------------------------------------------------
% load target HSI
%-------------------------------------------------------------------------
test = load(fullfile(folderTest, imageSet{1}));
label = test.img;
[w,h,depth] = size(label);
%-------------------------------------------------------------------------
% pre-processing
%-------------------------------------------------------------------------
K = nch-1;
nz = depth + K;
data = zeros(w,h,nz);
order_init = (K/2+1):-1:2;
order_final = (depth-1):-1:(depth-K/2);
data(:,:,1:K/2) = label(:,:,order_init);
data(:,:,(K/2 + 1):(end-K/2)) = label;
data(:,:,(end-K/2+1):end) = label(:,:,order_final);
sigmas = inputNoiseSigma/255; % see "vl_simplenn.m".
inputs = data;
if mod(w,2)==1
inputs = cat(1,inputs, inputs(end,:,:)) ;
end
if mod(h,2)==1
inputs = cat(2,inputs, inputs(:,end,:)) ;
end
if useGPU
inputs = gpuArray(inputs);
end
[nx,ny,nz] = size(inputs);
output_img = zeros(nx,ny,depth,'gpuArray');
%-------------------------------------------------------------------------
% denoising process
%-------------------------------------------------------------------------
tic
for z = 1 : depth
input = inputs(:,:,z:z+K);
% perform denoising
% res = vl_simplenn(net,input,[],[],'conserveMemory',true,'mode','test'); % matconvnet default
res = vl_net_concise(net, input); % concise version of vl_simplenn
output = res(end).x;
output_img(:,:,z) = output;
end
toc
if mod(w,2)==1
output_img = output_img(1:end-1,:,:);
inputs = inputs(1:end-1,:,:);
end
if mod(h,2)==1
output_img = output_img(:,1:end-1,:);
inputs = inputs(:,1:end-1,:);
end
if useGPU
output_img = gather(output_img);
inputs = gather(inputs);
end
inputs = inputs(:,:,K/2+1:end-K/2);
if showResult
% input/ Groundtruth / predicted
% show false color image
denoised_img = im2uint8(output_img(:,:,2));
original_img = im2uint8(label(:,:,2));
figure, imshow(cat(2,original_img,denoised_img));
% Indian-pines
if strcmp(imageSet{1}, 'Indian')
denoised_img = cat(3,im2uint8(output_img(:,:,2)),im2uint8(output_img(:,:,3)),im2uint8(output_img(:,:,203)));
original_img = cat(3,im2uint8(label(:,:,2)),im2uint8(label(:,:,3)),im2uint8(label(:,:,203)));
% University of Pavia
elseif strcmp(imageSet{1}, 'Pavia')
denoised_img = cat(3,im2uint8(output_img(:,:,97)),im2uint8(output_img(:,:,3)),im2uint8(output_img(:,:,2)));
original_img = cat(3,im2uint8(label(:,:,97)),im2uint8(label(:,:,3)),im2uint8(label(:,:,2)));
end
figure, imshow(cat(2,original_img,denoised_img));
end
if ~exist(fullfile(folderResults, imageSet{1}), 'dir'), mkdir(fullfile(folderResults, imageSet{1})); end
if save_result
image_name = strcat(fullfile(folderResults, imageSet{1}, 'denoised_noiselevel_'), int2str(inputNoiseSigma));
save(image_name,'output_img');
end