Skip to content

Commit

Permalink
Working on 3d verification, add gurobi as solver option
Browse files Browse the repository at this point in the history
  • Loading branch information
mldiego committed Jun 19, 2024
1 parent a2c79f6 commit eb619b4
Show file tree
Hide file tree
Showing 13 changed files with 266 additions and 751 deletions.
32 changes: 31 additions & 1 deletion code/nnv/engine/utils/lpsolver.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,38 @@
Aeq = double(Aeq); Beq = double(Beq); ub = double(ub);
end

if strcmp(lp_solver, 'gurobi') % no backup solver, should be better than the others
% Create gurobi model
model.obj = f; % objective function
model.A = [sparse(A); sparse(Aeq)]; % A must be sparse
model.sense = [repmat('<',size(A,1),1); repmat('=',size(Aeq,1),1)];
model.rhs = full([b(:); Beq(:)]); % rhs must be dense
if ~isempty(lb)
model.lb = lb;
else
model.lb = -inf(size(model.A,2),1); % default lb for MATLAB is -inf
end
if ~isempty(ub)
model.ub = ub;
end
% Define solver parameters
params = struct; % for now, leave default options/params
params.OutputFlag = 0; % no display
result = gurobi(model, params);
fval = result.objval; % get fval value from results
% get exitflag and match those of linprog for easier parsing
if strcmp(result.status,'OPTIMAL')
exitflag = "l1"; % converged to a solution
elseif strcmp(result.status,'UNBOUNDED')
exitflag = "l-5"; % problem is unbounded
elseif strcmp(result.status,'ITERATION_LIMIT')
exitflag = "l-2"; % maximum number of iterations reached
else
exitflag = "l-2"; % no feasible point found
end

% Solve using linprog (glpk as backup)
if strcmp(lp_solver, 'linprog')
elseif strcmp(lp_solver, 'linprog')
options = optimoptions(@linprog, 'Display','none');
options.OptimalityTolerance = 1e-10; % set tolerance
% first try solving using linprog
Expand Down
7 changes: 0 additions & 7 deletions code/nnv/examples/Submission/WiP_3d/functions/L_inf_attack.m

This file was deleted.

8 changes: 4 additions & 4 deletions code/nnv/examples/Submission/WiP_3d/functions/dark_attack.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
% 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; noise_disturbance-1];
V(:,:,:,:,1) = vol; % center of set
V(:,:,:,:,2) = noise; % basis vectors
C = [1; -1]; % constraints
d = [1; noise_disturbance-1]; % constraints
I = VolumeStar(V, C, d, 1-noise_disturbance, 1); % input set

end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


21 changes: 4 additions & 17 deletions code/nnv/examples/Submission/WiP_3d/functions/verify_instance_3d.m
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
function results = verify_instance_3d(net, vol, target, attack, reachOptions, max_value, min_value)
function results = verify_instance_3d(net, vol, target, attack, reachOptions)
% verify medmnist with inputs (input images), targets (labels) and attack
% (struct with adversarial attack info)
% results = verify_medmnist(net, matlabNet, inputs, targets, attack, max_value*, min_value*)

% Check what type of attack to consider
if strcmp(attack.Name, 'linf')
epsilon = attack.epsilon;
max_pixels = attack.max_pixels;
elseif strcmp(attack.Name, 'dark') || strcmp(attack.Name, 'bright')
if strcmp(attack.Name, 'dark') || strcmp(attack.Name, 'bright')
max_pixels = attack.max_pixels;
threshold = attack.threshold;
noise_disturbance = attack.noise_de;
else
error("Adversarial attack not supported.");
end

% check if max_value and min_value are provided
if ~exist("max_value", 'var')
max_value = inf;
end
if ~exist("min_value", 'var')
min_value = -inf;
end

% Choose attack
if strcmp(attack.Name,'linf')
I = L_inf_attack(vol, epsilon, max_pixels, max_value, min_value);
elseif strcmp(attack.Name, 'dark')
if strcmp(attack.Name, 'dark')
I = dark_attack(vol, max_pixels, threshold, noise_disturbance);
elseif strcmp(attack.Name, 'bright')
I = bright_attack(vol, max_pixels, threshold, noise_disturbance);
Expand All @@ -48,7 +35,7 @@
return
end

% Check for falsification (TODO)
% Check for falsification
n_samples = 100; % number of random samples to try for falsification
xRand = I.sample(n_samples);
for k = 1:n_samples
Expand Down
60 changes: 15 additions & 45 deletions code/nnv/examples/Submission/WiP_3d/run_all.m
Original file line number Diff line number Diff line change
@@ -1,53 +1,23 @@
%% Shape only data
%% Shape only data (3d)

% poolobj = gcp('nocreate');
% delete(poolobj);
verify_adrenal;

% verify_adrenal;
verify_vessel;

% poolobj = gcp('nocreate');
% delete(poolobj);
%% Volume data (general 3D)

% verify_vessel;

%% Volume data (general 3D)
disp("... fracture ...");
verify_fracture;


disp("... nodule ...")
verify_nodule;


disp("... organ ...")
verify_organ;

% poolobj = gcp('nocreate');
% delete(poolobj);
%
% disp("... fracture ...");
% try
% verify_fracture;
% catch ME
% warning(ME.message);
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% poolobj = gcp('nocreate');
% delete(poolobj);
%
% disp("... nodule ...")
% try
% verify_nodule;
% catch ME
% warning(ME.message);
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% poolobj = gcp('nocreate');
% delete(poolobj);

% disp("... organ ...")
% try
% verify_organ;
% catch ME
% warning(ME.message);
% end
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% poolobj = gcp('nocreate');
% delete(poolobj);

disp("... synapse ...")
try
verify_synapse;
catch ME
warning(ME.message)
end
verify_synapse;
1 change: 1 addition & 0 deletions code/nnv/examples/Submission/WiP_3d/verify_adrenal.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
reachOptions = struct;
reachOptions.reachMethod = 'relax-star-area';
reachOptions.relaxFactor = 0.95;
reachOptions.lp_solver = "gurobi";


%% Attack 1
Expand Down
Loading

0 comments on commit eb619b4

Please sign in to comment.