Error using activations with GoogLeNet in R2017b

T
tiyasingh_21 · May 22, 2021 · 2.3K views
Question
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.
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Sep 17, 2026

The error using activations() with GoogLeNet in MATLAB R2017b usually stems from three issues: GoogLeNet is a DAGNetwork requiring exact layer names (like 'pool5-7x7_s1'), input images are not resized to [224, 224, 3], or the resulting 4D feature array is not flattened using squeeze().

Primary Root Causes in MATLAB R2017b

  1. DAGNetwork Layer Name Conflict: Unlike linear networks such as AlexNet or VGG-16, GoogLeNet contains parallel branched Inception modules. Passing integer indices or generic layer names like 'fc' throws an error. You must pass exact string identifiers such as 'pool5-7x7_s1' (global average pooling) or 'loss3-classifier' (final classification layer).
  2. Color Channel & Dimension Mismatch: GoogLeNet strictly enforces an input size of 224x224x3. If your test image is grayscale (2D) or not preprocessed, activations() will throw a dimension mismatch error. Grayscale images must be replicated across three channels using cat(3, img, img, img).
  3. Unflattened 4D Output Tensor: Extracting features from 'pool5-7x7_s1' returns a 4D numeric array of shape [1, 1, 1024, N]. If passed directly into downstream classifiers such as fitcsvm(), MATLAB throws an invalid predictor matrix error unless converted to [1024, N] using squeeze().

Executable MATLAB Code: Correct GoogLeNet Feature Extraction

% =========================================================================
% Feature Extraction via activations() with GoogLeNet in MATLAB R2017b
% =========================================================================

% 1. Load the pre-trained GoogLeNet model
% Requires the "Deep Learning Toolbox Model for GoogLeNet Network" support package
net = googlenet; 

% 2. Inspect valid layer names in the DAGNetwork
% layerNames = {net.Layers.Name}'; % Uncomment to inspect all 144 layers

% 3. Read and preprocess test image
rawImage = imread('peppers.png');

% Ensure image has 3 channels (convert grayscale if necessary)
if size(rawImage, 3) == 1
    rawImage = cat(3, rawImage, rawImage, rawImage);
end

% Resize to exact GoogLeNet input dimensions [224, 224, 3]
inputSize = net.Layers(1).InputSize(1:2); % [224, 224]
processedImage = imresize(rawImage, inputSize);

% 4. Extract activations from the global pooling layer
% Valid feature layer in R2017b: 'pool5-7x7_s1' (produces 1024-dimensional feature vector)
featureLayer = 'pool5-7x7_s1'; 

try
    rawFeatures = activations(net, processedImage, featureLayer, ...
        'OutputAs', 'channels', ...
        'ExecutionEnvironment', 'auto'); % Uses GPU if available, else CPU
    
    % Squeeze the [1, 1, 1024] array into a flat 1D feature vector [1024, 1]
    featureVector = squeeze(rawFeatures);
    
    fprintf('Success! Extracted %d-dimensional feature vector.\n', length(featureVector));
    
catch ME
    fprintf('Error during activations extraction: %s\n', ME.message);
end

% 5. Batch Feature Extraction from an imageDatastore (Recommended for datasets)
% imds = imageDatastore('path_to_images', 'IncludeSubfolders', true);
% augmentedImds = augmentedImageDatastore(inputSize, imds, 'ColorPreprocessing', 'gray2rgb');
% batchFeatures = activations(net, augmentedImds, featureLayer, 'OutputAs', 'rows');
% size(batchFeatures); % Returns [numImages x 1024] directly, ready for SVM training

Key Diagnostic Checklist

  • Support Package Check: In R2017b, if googlenet returns an unrecognized function error, open Add-On Explorer and install Deep Learning Toolbox Model for GoogLeNet Network.
  • Using 'OutputAs', 'rows': In R2017b, adding the name-value pair 'OutputAs', 'rows' automatically flattens 4D tensors into a standard 2D observation matrix [N, numFeatures], eliminating manual squeeze() and reshape() operations.
  • Layer Verification: If testing with alternative backbones like Inception-v3 or ResNet-50, verify the final pooling layer identifier using net.Layers(end-2).Name.
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!