-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuronCoverageForEveryLayerExample.m
More file actions
84 lines (67 loc) · 2.3 KB
/
Copy pathNeuronCoverageForEveryLayerExample.m
File metadata and controls
84 lines (67 loc) · 2.3 KB
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
%% Neuron Coverage for Every Layer
% Train an image regression network and compute the neuron coverage for every
% layer.
%%
% Load the training and test data. The data set contains synthetic images of
% handwritten digits together with the corresponding angles (in degrees) by which
% each image is rotated. Each digit image is 28-by-28 pixels.
[XTrain,~,YTrain] = digitTrain4DArrayData;
[XTest,~,YTest] = digitTrain4DArrayData;
%%
% Define the convolutional neural network architecture.
layers = [
imageInputLayer([28 28 1])
convolution2dLayer(3,8,Padding="same")
batchNormalizationLayer
reluLayer
averagePooling2dLayer(2,Stride=2)
convolution2dLayer(3,16,Padding="same")
batchNormalizationLayer
reluLayer
averagePooling2dLayer(2,Stride=2)
convolution2dLayer(3,32,Padding="same")
batchNormalizationLayer
reluLayer
convolution2dLayer(3,32,Padding="same")
batchNormalizationLayer
reluLayer
dropoutLayer(0.2)
fullyConnectedLayer(1)
regressionLayer];
%%
% Specify training options for stochastic gradient descent with momentum. Set
% the maximum number of epochs to 30 and start the training with an initial learning
% rate of 0.001.
options = trainingOptions("sgdm", ...
MaxEpochs=30, ...
InitialLearnRate=1e-3, ...
Plots="training-progress", ...
Verbose=false, ...
ExecutionEnvironment="auto");
%%
% Train the network.
net = trainNetwork(XTrain,YTrain,layers,options);
%%
% To compute the neuron coverage, you must convert the network to a |dlnetwork|
% object and the data to a |dlarray| object.
%
% Remove the output layer and convert the network to a |dlnetwork| object.
lgraph = layerGraph(net);
lgraph = removeLayers(lgraph,"regressionoutput");
net = dlnetwork(lgraph);
%%
% Convert the test data to a |dlarray| object.
XTest = dlarray(XTest,"SSCB");
%%
% Compute the neuron coverage for every layer in the network.
nc = neuronCoverage(net,Data=XTest,LayerNames={net.Layers.Name})
%%
% Plot the results.
bar(categorical(nc.LayerNames),nc.LayerCoverage{:,1})
ylabel("Neuron Coverage")
xlabel("Layer Name")
%%
% Get the coverage for specific layers.
getCoverageForLayer(nc,["conv_2","conv_3"])
%%
% _Copyright 2022 The MathWorks, Inc._