Main Content

estimateNetworkMetrics

R2026b

Estimate network metrics for specific layers of a neural network

Since R2022a

Description

Add-On Required: This feature requires the Deep Learning Toolbox Model Compression Library add-on.

dataTable = estimateNetworkMetrics(net) returns a table containing estimated layer-wise metrics for a deep neural network.

This function estimates metrics for learnable layers, which have weights and bias, in the network. Estimated metrics are provided for the following supported layers.

example

[dataTable1,dataTable2,…,dataTableN] = estimateNetworkMetrics(net1,net2,…,netN) returns metrics for multiple networks.

example

Examples

collapse all

This example shows how to estimate layer-wise metrics for a neural network.

Load the pretrained network. net is a SqueezeNet convolutional neural network that has been retrained using transfer learning to classify images in the MerchData data set.

load squeezedlnetmerch
net
net = 
  dlnetwork with properties:

         Layers: [67×1 nnet.cnn.layer.Layer]
    Connections: [74×2 table]
     Learnables: [52×3 table]
          State: [0×3 table]
     InputNames: {'data'}
    OutputNames: {'prob'}
    Initialized: 1

  View summary with summary.

Use the estimateNetworkMetrics function to estimate metrics for the supported layers in your network.

estNet = estimateNetworkMetrics(net)
estNet = 26×8 table
               "conv1"    "2-D Convolution"     1792    44129664    0.0068    0    22064832     25.7391
    "fire2-squeeze1x1"    "2-D Convolution"     1040     6422528    0.0040    0     3211264     12.7480
     "fire2-expand1x1"    "2-D Convolution"     1088     6422528    0.0042    0     3211264     12.7480
     "fire2-expand3x3"    "2-D Convolution"     9280    57802752    0.0354    0    28901376    111.1181
    "fire3-squeeze1x1"    "2-D Convolution"     2064    12845056    0.0079    0     6422528     14.1580
     "fire3-expand1x1"    "2-D Convolution"     1088     6422528    0.0042    0     3211264     12.7480
     "fire3-expand3x3"    "2-D Convolution"     9280    57802752    0.0354    0    28901376    111.1181
    "fire4-squeeze1x1"    "2-D Convolution"     4128     6422528    0.0157    0     3211264     24.7905
     "fire4-expand1x1"    "2-D Convolution"     4224     6422528    0.0161    0     3211264     24.7905
     "fire4-expand3x3"    "2-D Convolution"    36992    57802752    0.1411    0    28901376    178.0694
    "fire5-squeeze1x1"    "2-D Convolution"     8224    12845056    0.0314    0     6422528     27.4486
     "fire5-expand1x1"    "2-D Convolution"     4224     6422528    0.0161    0     3211264     24.7905
     "fire5-expand3x3"    "2-D Convolution"    36992    57802752    0.1411    0    28901376    178.0694
    "fire6-squeeze1x1"    "2-D Convolution"    12336     4816896    0.0471    0     2408448     33.5102
      ⋮

This example shows how to estimate the metrics for a floating-point and quantized neural network.

Load the pretrained network. net is a SqueezeNet convolutional neural network that has been retrained using transfer learning to classify images in the MerchData data set.

load squeezedlnetmerch
net
net = 
  dlnetwork with properties:

         Layers: [67×1 nnet.cnn.layer.Layer]
    Connections: [74×2 table]
     Learnables: [52×3 table]
          State: [0×3 table]
     InputNames: {'data'}
    OutputNames: {'prob'}
    Initialized: 1

  View summary with summary.

Unzip and load the MerchData images as an image datastore. Define an augmentedImageDatastore object to resize the data for the network, and split the data into calibration and validation data sets to use for quantization.

unzip('MerchData.zip');
imds = imageDatastore('MerchData', ...
    'IncludeSubfolders',true, ...
    'LabelSource','foldernames');
[calData, valData] = splitEachLabel(imds, 0.7,'randomized');
aug_calData = augmentedImageDatastore([227 227],calData);
aug_valData = augmentedImageDatastore([227 227],valData);

Create a dlquantizer object and specify the network to quantize. Set the execution environment to MATLAB. When you use the MATLAB execution environment, quantization is performed using the fi fixed-point data type which requires a Fixed-Point Designer™ license.

quantObj = dlquantizer(net,'ExecutionEnvironment','MATLAB');

Use the calibrate function to exercise the network with sample inputs and collect range information.

calResults = calibrate(quantObj,aug_calData);

Use the quantize method to quantize the network object and return a simulatable quantized network.

qNet = quantize(quantObj)
qNet = 
  Quantized dlnetwork with properties:

         Layers: [67×1 nnet.cnn.layer.Layer]
    Connections: [74×2 table]
     Learnables: [52×3 table]
          State: [0×3 table]
     InputNames: {'data'}
    OutputNames: {'prob'}
    Initialized: 1

  View summary with summary.
  Use the quantizationDetails function to extract quantization details.

Use the estimateNetworkMetrics function to estimate metrics for the floating-point and quantized networks.

[dataTableFloat,dataTableQuantized] = estimateNetworkMetrics(net,qNet)
dataTableFloat = 26×8 table
               "conv1"    "2-D Convolution"     1792    44129664    0.0068    0    22064832     25.7391
    "fire2-squeeze1x1"    "2-D Convolution"     1040     6422528    0.0040    0     3211264     12.7480
     "fire2-expand1x1"    "2-D Convolution"     1088     6422528    0.0042    0     3211264     12.7480
     "fire2-expand3x3"    "2-D Convolution"     9280    57802752    0.0354    0    28901376    111.1181
    "fire3-squeeze1x1"    "2-D Convolution"     2064    12845056    0.0079    0     6422528     14.1580
     "fire3-expand1x1"    "2-D Convolution"     1088     6422528    0.0042    0     3211264     12.7480
     "fire3-expand3x3"    "2-D Convolution"     9280    57802752    0.0354    0    28901376    111.1181
    "fire4-squeeze1x1"    "2-D Convolution"     4128     6422528    0.0157    0     3211264     24.7905
     "fire4-expand1x1"    "2-D Convolution"     4224     6422528    0.0161    0     3211264     24.7905
     "fire4-expand3x3"    "2-D Convolution"    36992    57802752    0.1411    0    28901376    178.0694
    "fire5-squeeze1x1"    "2-D Convolution"     8224    12845056    0.0314    0     6422528     27.4486
     "fire5-expand1x1"    "2-D Convolution"     4224     6422528    0.0161    0     3211264     24.7905
     "fire5-expand3x3"    "2-D Convolution"    36992    57802752    0.1411    0    28901376    178.0694
    "fire6-squeeze1x1"    "2-D Convolution"    12336     4816896    0.0471    0     2408448     33.5102
      ⋮

dataTableQuantized = 26×8 table
               "conv1"    "2-D Convolution"     1750    44129664        0.0019    0    22064832     25.7403
    "fire2-squeeze1x1"    "2-D Convolution"      987     6422528    9.8705e-04    0     3211264     12.7507
     "fire2-expand1x1"    "2-D Convolution"     1038     6422528        0.0012    0     3211264     12.7505
     "fire2-expand3x3"    "2-D Convolution"     8784    57802752        0.0086    0    28901376    111.3304
    "fire3-squeeze1x1"    "2-D Convolution"     2001    12845056        0.0020    0     6422528     14.1600
     "fire3-expand1x1"    "2-D Convolution"     1062     6422528        0.0012    0     3211264     12.7493
     "fire3-expand3x3"    "2-D Convolution"     8850    57802752        0.0086    0    28901376    111.3021
    "fire4-squeeze1x1"    "2-D Convolution"     3878     6422528        0.0038    0     3211264     24.8385
     "fire4-expand1x1"    "2-D Convolution"     4084     6422528        0.0043    0     3211264     24.8173
     "fire4-expand3x3"    "2-D Convolution"    34573    57802752        0.0333    0    28901376    180.7635
    "fire5-squeeze1x1"    "2-D Convolution"     7900    12845056        0.0076    0     6422528     27.4866
     "fire5-expand1x1"    "2-D Convolution"     4056     6422528        0.0042    0     3211264     24.8227
     "fire5-expand3x3"    "2-D Convolution"    34547    57802752        0.0333    0    28901376    180.7929
    "fire6-squeeze1x1"    "2-D Convolution"    11734     4816896        0.0113    0     2408448     33.7933
      ⋮

Compare the parameter memory requirements of the layers supported by estimateNetworkMetrics for the floating-point and quantized networks.

totalMemoryFloat = sum(dataTableFloat.("ParameterMemory (MB)"));
totalMemoryQuantized = sum(dataTableQuantized.("ParameterMemory (MB)"));
percentReduction = (totalMemoryFloat - totalMemoryQuantized)*100/totalMemoryFloat
percentReduction = 
76.0673

Input Arguments

collapse all

Neural network, specified as one of these values:

estimateNetworkMetrics supports both floating-point and quantized networks.

Neural networks, specified as a comma-separated list of any of the following values:

estimateNetworkMetrics supports both floating-point and quantized networks.

Output Arguments

collapse all

Estimated layer metrics, returned as a table containing these metrics:

  • LayerName — Name of layer.

  • LayerType — Type of layer.

  • NumberOfLearnables — Number of non-zero learnable parameters (weights and biases) in the network.

  • NumberOfOperations — Total number of multiplications and additions.

  • ParameterMemory (MB) — Memory required to store all of the learnable parameters.

  • StateMemory (MB) — Memory required to store all of the state parameters. For layers without state parameters, this value is zero.

  • NumberOfMACs — Number of multiply-accumulate operations.

  • ArithmeticIntensity (FLOP/B) — Amount of reuse of data fetched from memory, measured as the number of floating-point operations performed per the bytes of memory access required to support those operations. For example, convolutional layers reuse the same weight data across computations for multiple input features, resulting in a relatively high arithmetic intensity.

Estimated layer metrics, returned as tables containing the metrics for each deep neural network you input. Each table contains the same metrics as dataTable.

Limitations

  • To estimate metrics for a fullyConnectedLayer layer, the InputLearnables and OutputLearnables values must be {}.

Version History

Introduced in R2022a

expand all