Error using activations with GoogLeNet in R2017b
2 views (last 30 days)
Show older comments
MathWorks Support Team
on 10 Nov 2017
Answered: MathWorks Support Team
on 29 Jan 2018
I am trying to use an "activations" function on a pretrained googlenet network. It runs ok, but returns a 4-D matrix. So I tried using ('OutputAs', 'columns') Name-Value pair. But this produces an error. Here are the reproduction steps and the error message:
net = googlenet;
mockImage = randn(224, 224, 3);
layer = 'loss3-classifier';
trainingFeatures = activations(net, mockImage, layer, 'OutputAs', 'columns');
Error using DAGNetwork>iParseAndValidateActivationsNameValuePairs (line 598)
'OutputAs' is not a recognized parameter. For a list of valid name-value pair arguments, see the
documentation for this function.
Error in DAGNetwork/activations (line 230)
[miniBatchSize, executionEnvironment] =
iParseAndValidateActivationsNameValuePairs(varargin{:});
Error in reproduce_OutputAs_error (line 4)
trainingFeatures = activations(net, mockImage, layer, 'OutputAs', 'columns');
How can I get the activations in a correct format from googlenet? The reason I am trying to do this, is to follow feature extraction example, https://www.mathworks.com/help/nnet/examples/feature-extraction-using-alexnet.html, using googlenet instead of alexnet.
Accepted Answer
MathWorks Support Team
on 10 Nov 2017
The pretrained networks "alexnet" and "googlenet" belong to different MATLAB classes: "alexnet" is a SeriesNetwork, while "googlenet" is a DAGNetwork (where layers do not have to be arranged as one single chain). For a DAGNetwork, the "activations" method is not fully supported yet – this functionality will be available in a future MATLAB release.
But as you found out, the "activations" method does work on a "googlenet" to some extent. It returns a _h_-by-_w_-by-_c_-by-_n_ array, where _h_, _w_, and _c_ are the height, width, and number of channels for the output of the chosen layer, and _n_ is the number of observations.
The workaround is to reshape the matrix to be in the form of (number of observations) x (number of features) as is needed for "fitcecoc" for feature extraction. Here (number of features) = h*w*c. You should be able to reshape the training and testing matrices as follows:
>> nfeatures = size(trainingFeatures, 1) * size(trainingFeatures, 2) * size(trainingFeatures, 3);
>> nobservations = size(trainingFeatures, 4);
>> trainingFeatures = reshape(trainingFeatures, nfeatures, nobservations);
>> trainingFeatures = trainingFeatures';
For your reference, here are the documentation pages for SeriesNetwork and DAGNetwork:
www.mathworks.com/help/nnet/ref/seriesnetwork.html
www.mathworks.com/help/nnet/ref/dagnetwork.html
0 Comments
More Answers (0)
See Also
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!