Can I use the "activation" function to get multiple layer activations from a "SeriesNetwork" with one function call in R2022b?

6 views (last 30 days)
I would like to obtain activations for multiple layers in a "SeriesNetwork" using "activations". However, "activations" only allows for specifying one desired layer at a time. Therefore, I have to call "activations" multiple times, which can be slow and result in duplicated computations. Is there a faster way to do this?
net = alexnet; % A SeriesNetwork
img = rand(227,227,3); % Random image
ac2 = activations(net, img, 2);
ac3 = activations(net, img, 3);
ac4 = activations(net, img, 4);
ac5 = activations(net, img, 5);
% ...

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 22 Sep 2022
As of R2022b, it is not currently possible to use the "activations" function to compute activations for more than one layer in the same function call. However, the development team is aware of this limitation and may consider addressing it in the future releases.
As a workaround, you could convert the “SeriesNetwork” to a “dlnetwork”. The “dlnetwork” type supports computing activations in multiple layers using “predict”. Below is an example of this workaround: 
net = alexnet; % SeriesNetwork
% Convert to dlnetwork
lgraph = layerGraph(net);
lgraph = removeLayers(lgraph,["prob" "output"]); % To convert to dlnetwork we need to remove the output layer
dlnet = dlnetwork(lgraph);
% Prepare data
img = rand(227,227,3); % Random image
img = dlarray(img, 'SSCB') % “dlnetwork.predict” requires a formatted “dlarray” as input
% Get activations
outputActivationLayers = ["conv1", "conv2", "fc6"]; % “dlnetwork.predict” requires specifying desired layers using strings
[ac1, ac2, ac3] = dlnet.predict(img, Outputs=outputActivationLayers);
Though this workaround requires some conversions of both network and data, it should be faster than calling “activations” numerous times. Note that if you would like to switch to working with "dlnetwork" entirely, to train a “dlnetwork” you need to write a custom training loop.

More Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!