Skip to content

Commit

Permalink
3d shape verification functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mldiego committed Aug 15, 2024
1 parent c6a0cc2 commit 2039833
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 54 deletions.
30 changes: 9 additions & 21 deletions code/nnv/examples/Submission/WiP_3d/functions/add_voxels.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,15 @@
vol = single(vol);
at_vol = vol;

% Create brightening attack
for i=1:size(vol,1)
for j=1:size(vol,2)
for k=1:size(vol,3)
if vol(i,j,k) < threshold
at_vol(i,j,k) = 255;
ct = ct + 1;
if ct >= voxels
flag = 1;
break;
end
end
end
if flag == 1
break
end
end
if flag == 1
break;
end
end
% we can find the edge of the shape
shape = edge3(vol); % this should be okay for this data, but let's test it

% select a random pixel
idxs = find(shape) && ~find(vol);
voxels = max(voxels,length(idxs));

% For now, we can select the first ones
at_vol(idxs(1:voxels)) = 255;

% Define input set as VolumeStar
dif_vol = -vol + at_vol;
Expand Down
34 changes: 11 additions & 23 deletions code/nnv/examples/Submission/WiP_3d/functions/remove_voxels.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,28 @@
% Return a VolumeStar of a brightening attack on a few pixels

% Initialize vars
ct = 0; % keep track of pixels modified
ct = 0; % keep track of pixels removed
flag = 0; % determine when to stop modifying pixels
vol = single(vol);
at_vol = vol;

% Like darkening attack
for i=1:size(vol,1)
for j=1:size(vol,2)
for k=1:size(vol,3)
if vol(i,j,k) < threshold
at_vol(i,j,k) = 255;
ct = ct + 1;
if ct >= voxels
flag = 1;
break;
end
end
end
if flag == 1
break
end
end
if flag == 1
break;
end
end
% we can find the edge of the shape
shape = edge3(vol); % this should be okay for this data, but let's test it

% select a random pixel
idxs = find(shape) && find(vol);
voxels = max(voxels,length(idxs));

% For now, we can select the first ones
at_vol(idxs(1:voxels)) = 0;

% Define input set as VolumeStar
dif_vol = -vol + at_vol;
noise = dif_vol;
V(:,:,:,:,1) = vol; % center of set
V(:,:,:,:,2) = noise; % basis vectors
C = [1; -1]; % constraints
d = [1; -1]; % constraints
d = [1; noise_disturbance-1]; % constraints
I = VolumeStar(V, C, d, 1-noise_disturbance, 1); % input set


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
% Check what type of attack to consider
if strcmp(attack.Name, 'add') || strcmp(attack.Name, 'remove')
max_pixels = attack.max_pixels;
threshold = attack.threshold;
noise_disturbance = attack.noise_de;
else
error("Adversarial attack not supported.");
end

% Choose attack
if strcmp(attack.Name, 'add')
I = add_voxels(vol, max_pixels, threshold, noise_disturbance);
I = add_voxels(vol, max_pixels, noise_disturbance);
elseif strcmp(attack.Name, 'remove')
I = remove_voxels(vol, max_pixels, threshold, noise_disturbance);
I = remove_voxels(vol, max_pixels, noise_disturbance);
end

% Begin analysis
Expand Down
4 changes: 2 additions & 2 deletions code/nnv/examples/Submission/WiP_3d/run_all.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
%% Shape only data (3d)

% verify_adrenal;
verify_adrenal;

% verify_vessel;
verify_vessel;

%% Volume data (general 3D)

Expand Down
33 changes: 32 additions & 1 deletion code/nnv/examples/Submission/WiP_3d/verify_adrenal.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
net = matlab2nnv(net);

% select volumes to verify
% N = 24; % even for numCores
N = 50;
inputs = single(test_images(:,:,:,:,1:N));
targets = single(test_labels(1:N));
Expand All @@ -32,4 +31,36 @@
reachOptions.lp_solver = "gurobi";


% Study variables
advType = ["add", "remove"];
maxpixels = [50, 100, 500, 1000]; %out of 28x28x28 pixels
epsilon = 1; % ep / 255


%% Verification analysis
for a=advType
for mp=maxpixels
for ep=epsilon

% 1) Initialize results var
results = zeros(N,2);

% 2) Create adversarial attack
adv_attack = struct;
adv_attack.Name = a; % add or remove
adv_attack.max_pixels = mp; % Max number of pixels to modify from input image
adv_attack.noise_de = ep/255; % disturbance (noise) on pixels

% 3) Begin verification analysis
for i=1:N
img = squeeze(inputs(:,:,:,:,i));
target = targets(i);
results(i,:) = verify_instance_shape(net, img, target, adv_attack, reachOptions);
end

% 4) % save results
save("results/verification_adrenal_"+ a +"_" + ep +"_" + mp + ".mat", "results");

end
end
end
35 changes: 31 additions & 4 deletions code/nnv/examples/Submission/WiP_3d/verify_vessel.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,37 @@
reachOptions.relaxFactor = 0.95;
reachOptions.lp_solver = "gurobi";

%% Attack 1
% Study variables
advType = ["add", "remove"];
maxpixels = [50, 100, 500, 1000]; %out of 28x28x28 pixels
epsilon = 1; % ep / 255

% adv_attack = struct;
%???????

% results = zeros(N,2); % verification result, time
%% Verification analysis
for a=advType
for mp=maxpixels
for ep=epsilon

% 1) Initialize results var
results = zeros(N,2);

% 2) Create adversarial attack
adv_attack = struct;
adv_attack.Name = a; % add or remove
adv_attack.max_pixels = mp; % Max number of pixels to modify from input image
adv_attack.noise_de = ep/255; % disturbance (noise) on pixels

% 3) Begin verification analysis
for i=1:N
img = squeeze(inputs(:,:,:,:,i));
target = targets(i);
results(i,:) = verify_instance_shape(net, img, target, adv_attack, reachOptions);
end

% 4) % save results
save("results/verification_vessel_"+ a +"_" + ep +"_" + mp + ".mat", "results");

end
end
end

0 comments on commit 2039833

Please sign in to comment.