Skip to content

Commit ce8ac3a

Browse files
committed
Finalize tutorial
1 parent a8f0218 commit ce8ac3a

File tree

13 files changed

+394
-7
lines changed

13 files changed

+394
-7
lines changed

code/nnv/engine/nn/layers/ConcatenationLayer.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128

129129
% Create output set
130130
outputs = ImageStar(new_V, inputs{indexMax}.C, inputs{indexMax}.d, inputs{indexMax}.pred_lb, inputs{indexMax}.pred_ub);
131+
131132
end
132133

133134
% handle multiple inputs
Binary file not shown.
3.35 MB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
% verify_bias_field;
2+
%
3+
% clc;clear;
4+
5+
verify_gamma_correction;
6+
7+
clc;clear;
8+
9+
verify_random_noise;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function verOut = output_vs_mask(img, mask)
2+
3+
diff_pixels = find(img ~= mask);
4+
5+
verOut = img;
6+
7+
for i=1:length(diff_pixels)
8+
if img(diff_pixels(i)) == 2
9+
continue
10+
elseif img(diff_pixels(i)) == 1
11+
verOut(diff_pixels(i)) = -1;
12+
else
13+
verOut(diff_pixels(i)) = -2;
14+
end
15+
end
16+
17+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function ver_img = verify_output(imageSet)
2+
3+
% Get data ranges
4+
[lb,ub] = imageSet.estimateRanges;
5+
6+
% 1) get correctly classified as 0 (background)
7+
ver_background = (ub <= 0);
8+
ver_background = ~ver_background;
9+
10+
% 2) get correctly classified as 1 (lession)
11+
ver_lesion = (lb > 0);
12+
13+
% 3) get verified output
14+
ver_img = 2*ones(size(lb)); % pixel = 2 -> unknown
15+
16+
background = find(ver_background == 0); % 0
17+
ver_img(background) = 0;
18+
19+
lesion = find(ver_lesion == 1); % 1
20+
ver_img(lesion) = 1;
21+
22+
23+
end
24+

code/nnv/examples/Tutorial/SPIE/Segmentation/verify_bias_field.m

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,120 @@
33
% Load one slice from ISBI
44
load("data/slice_ISBI_1_1_75.mat");
55

6-
% Create input set
6+
% Load Unet
7+
onnx = importNetworkFromONNX("models/model64.onnx");
8+
% analyzeNetwork(onnx)
9+
net = matlab2nnv(onnx);
10+
windowSize = 64; % from trained model
11+
12+
% Bias Field
13+
order = 3;
14+
coeff = 0.01;
15+
img_bf = BiasField(flair, order, coeff);
16+
17+
% Crop background before patches (for all data)
18+
flair = flair(31:158, 31:158);
19+
img_bf = img_bf(31:158, 31:158);
20+
mask = mask(31:158, 31:158);
21+
22+
% From here, to verify slice, we need to verify 4 patches
23+
24+
% Define verification parameters (reach method)
25+
reachOptions.reachMethod = "relax-star-area-reduceMem";
26+
reachOptions.relaxFactor = 1;
27+
reachOptions.lp_solver = "gurobi"; % (optional)
28+
29+
% Create bounds of input set
730
lb = flair;
831
ub = img_bf;
932

10-
windowSize = 64;
1133

12-
% Crop background before patches
13-
flair = flair(35:158, 30:153);
1434

15-
% From here, to verify slice, we need to verify 4 patches
35+
% Patch 1: Left-top corner
36+
InputSet1 = ImageStar(lb(1:64,1:64), ub(1:64,1:64));
37+
38+
% Compute reachability
39+
tic;
40+
OutputSet1 = net.reach(InputSet1, reachOptions);
41+
toc;
42+
43+
% Verified output
44+
verOut1 = verify_output(OutputSet1);
45+
46+
47+
48+
% Patch 2: Right-top corner
49+
InputSet2 = ImageStar(lb(1:64,65:128), ub(1:64,65:128));
50+
51+
% Compute reachability
52+
tic;
53+
OutputSet2 = net.reach(InputSet2, reachOptions);
54+
toc;
55+
56+
% Verified output
57+
verOut2 = verify_output(OutputSet2);
58+
59+
60+
61+
% Patch 3: Right-bottom corner
62+
InputSet3 = ImageStar(lb(65:128,65:128), ub(65:128,65:128));
63+
64+
% Compute reachability
65+
tic;
66+
OutputSet3 = net.reach(InputSet3, reachOptions);
67+
toc;
68+
69+
% Verified output
70+
verOut3 = verify_output(OutputSet3);
71+
72+
73+
74+
% Patch 4: Lft-bottom corner
75+
InputSet4 = ImageStar(lb(65:128,1:64), ub(65:128,1:64));
76+
77+
% Compute reachability
78+
tic;
79+
OutputSet4 = net.reach(InputSet4, reachOptions);
80+
toc;
81+
82+
% Verified reachability
83+
verOut4 = verify_output(OutputSet4);
84+
85+
% Output reachability
86+
outputSlice = [verOut1 verOut2; verOut3 verOut4];
87+
save("results/biasfield_output.mat","outputSlice")
88+
89+
imshow(outputSlice, [0,2], colormap=hsv(3))
90+
colorbar('XTickLabel', {'Background', 'Lesion', 'Unknown'}, 'XTick',[0,1,2])
91+
92+
% Verified output
93+
verifiedSlice = output_vs_mask(outputSlice, mask);
94+
imshow(verifiedSlice, [-2,2], colormap=hsv(5))
95+
colorbar('XTickLabel', {'False Negative', 'False Positive', 'Background', 'Lesion', 'Unknown'}, 'XTick',[-2,-1,0,1,2])
96+
97+
% Verified lesion
98+
overlay = labeloverlay(flair,verifiedSlice,'transparency',0.3);
99+
imshow(overlay,[-2 2]);
100+
101+
figure;
102+
subplot(1,4,1);
103+
mi_f = min(flair, [], 'all');
104+
ma_f = max(flair,[], 'all');
105+
overlay = labeloverlay(flair,mask,'transparency',0.3);
106+
imshow(overlay,[mi_f, ma_f]);
107+
title("Label mask")
108+
109+
subplot(1,4,2);
110+
overlay = labeloverlay(flair,verifiedSlice==1,'transparency',0.3);
111+
imshow(overlay,[mi_f, ma_f]);
112+
title('Verified lession')
113+
114+
subplot(1,4,3);
115+
overlay = labeloverlay(flair,verifiedSlice==2,'transparency',0.3);
116+
imshow(overlay,[mi_f, ma_f]);
117+
title("Unknown")
16118

17-
% Left-top corner
119+
subplot(1,4,4);
120+
overlay = labeloverlay(flair,verifiedSlice==-1,'transparency',0.3);
121+
imshow(overlay,[mi_f, ma_f]);
122+
title("False positives")

0 commit comments

Comments
 (0)