tiyasingh_21 asked . 2021-05-22
Error using activations with GoogLeNet in R2017b
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.
deep learning , matlab , simulink
Kshitij Singh answered . 2024-12-21 03:15:35
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';
Not satisfied with the answer ?? ASK NOW