Skip to content

Commit

Permalink
fix issue with conv1D
Browse files Browse the repository at this point in the history
  • Loading branch information
mldiego committed Mar 29, 2024
1 parent 476917f commit 1eaadd5
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 60 deletions.
18 changes: 14 additions & 4 deletions code/nnv/engine/nn/NN.m
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
fprintf('\nPerform reachability analysis for the network %s \n', obj.Name);
end

outputSet = obj.reach_withConns(inputSet);
outputSet = obj.reach_withConns(inputSet, 'sequence');

end

Expand Down Expand Up @@ -1239,7 +1239,7 @@ function start_pool(obj)
end

% reach NN based on connections table (test it)
function outSet = reach_withConns(obj, inSet)
function outSet = reach_withConns(obj, inSet, varargin)
% Initialize variables to store reachable sets and computation time
obj.reachSet = cell(1, obj.numLayers);
obj.reachTime = zeros(1, obj.numLayers);
Expand All @@ -1248,6 +1248,12 @@ function start_pool(obj)
fprintf('\nPerform reachability analysis for the network %s...', obj.Name);
end

if nargin == 3
reachType = varargin{1};
else
reachType = 'default';
end

% Begin reachability computation
for i=1:height(obj.Connections)

Expand All @@ -1272,7 +1278,7 @@ function start_pool(obj)
fprintf('\nPerforming analysis for Layer %d (%s)...', i-1, source);
end
t = tic;
if isequal(class(obj.Layers{1,1}), 'SequenceInputLayer')
if strcmp(reachType, 'sequence')
outSet = obj.Layers{source_indx}.reachSequence(inSet, obj.reachMethod, obj.reachOption, obj.relaxFactor, obj.dis_opt, obj.lp_solver);
else
outSet = obj.Layers{source_indx}.reach(inSet, obj.reachMethod, obj.reachOption, obj.relaxFactor, obj.dis_opt, obj.lp_solver);
Expand Down Expand Up @@ -1338,7 +1344,11 @@ function start_pool(obj)
% Assume last layer in array is the output layer
if isempty(obj.reachSet{end})
inSet = obj.reachSet{end-1};
outSet = obj.Layers{end}.reach(inSet, obj.reachMethod, obj.reachOption, obj.relaxFactor, obj.dis_opt, obj.lp_solver);
if strcmp(reachType, 'sequence')
outSet = obj.Layers{end}.reachSequence(inSet, obj.reachMethod, obj.reachOption, obj.relaxFactor, obj.dis_opt, obj.lp_solver);
else
outSet = obj.Layers{end}.reach(inSet, obj.reachMethod, obj.reachOption, obj.relaxFactor, obj.dis_opt, obj.lp_solver);
end
obj.reachSet{end} = outSet;
end
end
Expand Down
80 changes: 24 additions & 56 deletions code/nnv/engine/nn/layers/Conv1DLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
methods

% constructor of the class
function obj = Conv1DLayer(varargin)
function obj = Conv1DLayer(varargin)

switch nargin

Expand All @@ -58,8 +58,10 @@
error('Layer name should be a char array');
end


w = size(filter_weights);
if length(w) == 2
w = [w 1]; % add 1 as the third dimension
end
b = size(filter_bias);

if length(w) ~= 3
Expand All @@ -86,12 +88,8 @@
error('Invalid padding matrix');
end
obj.PaddingSize = padding_mat;

obj.Stride = stride_mat;

obj.DilationFactor = dilation_mat;



case 6

Expand All @@ -108,7 +106,6 @@
error('Layer name should be a char array');
end


w = size(filter_weights);
b = size(filter_bias);

Expand Down Expand Up @@ -140,8 +137,6 @@

obj.DilationFactor = dilation_mat;



case 5

filter_weights = varargin{1};
Expand All @@ -166,11 +161,7 @@
obj.NumChannels = w(2);
obj.FilterSize = w(1);
obj.Weights = filter_weights;
% elseif length(w) == 4
% obj.NumFilters = w(4);
% obj.NumChannels = w(3);
% obj.FilterSize = w(1);
% obj.Weights = filter_weights;

else
error('Invalid weight matrix');
end
Expand All @@ -190,9 +181,7 @@
error('Invalid padding matrix');
end
obj.PaddingSize = padding_mat;

obj.Stride = stride_mat;

obj.DilationFactor = dilation_mat;


Expand All @@ -217,11 +206,7 @@
obj.NumChannels = w(2);
obj.FilterSize = w(1);
obj.Weights = filter_weights;
% elseif length(w) == 4
% obj.NumFilters = w(4);
% obj.NumChannels = w(3);
% obj.FilterSize = w(1);
% obj.Weights = filter_weights;

else
error('Invalid weight matrix');
end
Expand All @@ -243,15 +228,8 @@
error('Invalid number of inputs (should be 2, 5, or 6)');

end







end

end

% set padding
function set_padding(obj, padding)
Expand Down Expand Up @@ -345,13 +323,22 @@ function set_name(obj, name)

n = length(size(input));
if n==2
input = dlarray(input');
y = dlconv(input, obj.Weights, obj.Bias,"Stride",obj.Stride,"DilationFactor",obj.DilationFactor,"Padding",obj.PaddingSize,"DataFormat","SC");
y = extractdata(y)';
try % not sure how whis work so we'll try both ways and one should do it
x = dlarray(input');
y = dlconv(x, obj.Weights, obj.Bias,"Stride",obj.Stride,"DilationFactor",obj.DilationFactor,"Padding",obj.PaddingSize,"DataFormat","SC");
y = extractdata(y)';
catch
x = dlarray(input);
y = dlconv(x, obj.Weights, obj.Bias,"Stride",obj.Stride,"DilationFactor",obj.DilationFactor,"Padding",obj.PaddingSize,"DataFormat","SC");
y = extractdata(y);
end
elseif n==3
input = dlarray(input);
y = dlconv(input, obj.Weights, obj.Bias,"Stride",obj.Stride,"DilationFactor",obj.DilationFactor,"Padding",obj.PaddingSize,"DataFormat","TSC");
y = extractdata(y);
end

end


end

Expand Down Expand Up @@ -389,7 +376,6 @@ function set_name(obj, name)

% author: Neelanjana Pal
% date: 10/28/2021


n = size(input);
if length(paddingSize) ~= 2
Expand All @@ -398,7 +384,6 @@ function set_name(obj, name)

l = paddingSize(1);
r = paddingSize(2);


if length(n) == 2
% input volume has only one channel
Expand All @@ -420,14 +405,12 @@ function set_name(obj, name)
I(1:h, l+1:l+w, i) = input(:, :, i); % new constructed input volume
end


else
error('Invalid input');
end

end


% compute feature map for specific input and weight
function featureMap = compute_featureMap(input, W, padding, stride, dilation)
% @input: is input
Expand All @@ -437,7 +420,6 @@ function set_name(obj, name)
% @dilation: factor for dilated convolution
% @featureMap: convolved feature (also called feature map)


% author: Neelanjana Pal
% date: 10/28/2021

Expand Down Expand Up @@ -473,7 +455,6 @@ function set_name(obj, name)
% TODO: explore power of using GPU for this problem

featureMap = zeros(1, h*w); % using single vector for parallel computation


for l=1:h*w
a = mod(l, w);
Expand Down Expand Up @@ -512,7 +493,6 @@ function set_name(obj, name)

end


% precompute height and width of feature map
function [h, w] = get_size_featureMap(input, W, padding, stride, dilation)
% @input: is input
Expand All @@ -539,12 +519,9 @@ function set_name(obj, name)

end



end

% reachability analysis using star set

methods
% reachability analysis method using Stars
% a star represent a set of images (2D matrix of h x w)
Expand All @@ -556,9 +533,9 @@ function set_name(obj, name)
error('The input is not an ImageStar object');
end

if input.height ~= obj.NumChannels
error("Input set contains %d channels while the convolutional layers has %d channels", input.numChannel, obj.NumChannels);
end
% if input.height ~= obj.NumChannels
% error("Input set contains %d channels while the convolutional layers has %d channels", input.numChannel, obj.NumChannels);
% end

if isempty(input.im_lb) && isempty(input.im_ub)
layer = obj;
Expand Down Expand Up @@ -602,7 +579,6 @@ function set_name(obj, name)

end


% hangle multiple inputs
function images = reach_star_multipleInputs(obj, in_signals, option)
% @in_images: an array of ImageStars input set
Expand Down Expand Up @@ -648,8 +624,6 @@ function set_name(obj, name)

end



% reach star with multiple inputs
function images = reachSequence(varargin)
% @inputs: an array of ImageStar or ImageZono input set
Expand All @@ -662,23 +636,18 @@ function set_name(obj, name)
in_images = varargin{2};
method = varargin{3};
option = varargin{4};
% relaxFactor = varargin{5}; do not use
% dis_opt = varargin{6}; do not use
% lp_solver = varargin{7}; do not use

case 6
obj = varargin{1};
in_images = varargin{2};
method = varargin{3};
option = varargin{4};
%relaxFactor = varargin{5}; do not use
% dis_opt = varargin{6}; do not use

case 5
obj = varargin{1};
in_images = varargin{2};
method = varargin{3};
option = varargin{4};
%relaxFactor = varargin{5}; do not use

case 4
obj = varargin{1};
Expand Down Expand Up @@ -710,7 +679,6 @@ function set_name(obj, name)

end


end

end
Expand Down
29 changes: 29 additions & 0 deletions code/nnv/examples/other/issues/chenyi10/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
%% Reproduce and fix error on issue (#215
% https://github.com/verivital/nnv/issues/215

net = importNetworkFromTensorFlow('my_model');
dataArray1 = rand(8,10);
dlX1 = dlarray(dataArray1,"TC");
net = initialize(net, dlX1);
summary(net)
F = matlab2nnv(net);

%% Evaluate

% Random input
IM = rand(8,10);
y = F.evaluateSequence(IM);

%% Reach

% create input set
IM = rand(8,10);
LB = IM-0.01;
UB = IM+0.01;
K = ImageStar(IM, LB, UB);

% compute reachability
reachMethod = 'approx-star'; % 使用近似星形集方法
F.reachMethod = reachMethod; % 设置网络的可达集计算方法
R = F.reachSequence(K);

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 1eaadd5

Please sign in to comment.