quantizationDetails
R2026bDescription
Examples
This example shows how to display the quantization details 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
netnet =
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 verify that the network is not quantized.
qDetails_original = quantizationDetails(net)
qDetails_original = struct with fields:
IsQuantized: 0
TargetLibrary: ""
QuantizedLayerNames: [0×0 string]
QuantizedLearnables: [0×3 table]
NetworkInputEmbeddedDataType: [0×2 table]
LayerOutputEmbeddedDataType: [0×2 table]
The IsQuantized field returns 0 (false) because the original network uses the single-precision floating-point data type.
Unzip and load the MerchData images as an image data store. 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 quantizationDetails method to extract the quantization details.
qDetails = quantizationDetails(qNet)
qDetails = struct with fields:
IsQuantized: 1
TargetLibrary: "none"
QuantizedLayerNames: [66×1 string]
QuantizedLearnables: [52×3 table]
NetworkInputEmbeddedDataType: [1×2 table]
LayerOutputEmbeddedDataType: [66×2 table]
Inspect the QuantizedLayerNames field to see a list of the quantized layers.
qDetails.QuantizedLayerNames
ans = 66×1 string array
"data"
"conv1"
"relu_conv1"
"pool1"
"fire2-squeeze1x1"
"fire2-relu_squeeze1x1"
"fire2-expand1x1"
"fire2-relu_expand1x1"
"fire2-expand3x3"
"fire2-relu_expand3x3"
⋮
Inspect the QuantizedLearnables field to see the quantized values for learnable parameters in the network.
qDetails.QuantizedLearnables
ans = 52×3 table
Layer Parameter Value
__________________ _________ ________________________
"conv1" "Weights" {3×3×3×64 embedded.fi}
"conv1" "Bias" {1×1×64 embedded.fi}
"fire2-squeeze1x1" "Weights" {1×1×64×16 embedded.fi}
"fire2-squeeze1x1" "Bias" {1×1×16 embedded.fi}
"fire2-expand1x1" "Weights" {1×1×16×64 embedded.fi}
"fire2-expand1x1" "Bias" {1×1×64 embedded.fi}
"fire2-expand3x3" "Weights" {3×3×16×64 embedded.fi}
"fire2-expand3x3" "Bias" {1×1×64 embedded.fi}
"fire3-squeeze1x1" "Weights" {1×1×128×16 embedded.fi}
"fire3-squeeze1x1" "Bias" {1×1×16 embedded.fi}
"fire3-expand1x1" "Weights" {1×1×16×64 embedded.fi}
"fire3-expand1x1" "Bias" {1×1×64 embedded.fi}
"fire3-expand3x3" "Weights" {3×3×16×64 embedded.fi}
"fire3-expand3x3" "Bias" {1×1×64 embedded.fi}
"fire4-squeeze1x1" "Weights" {1×1×128×32 embedded.fi}
"fire4-squeeze1x1" "Bias" {1×1×32 embedded.fi}
⋮
Inspect the NetworkInputEmbeddedDataType field to see the expected data type for the input data. If your input data does not already match the expected type, the quantized network casts input data to this data type before passing it to the input layer.
qDetails.NetworkInputEmbeddedDataType
ans = 1×2 table
InputNames DataType
__________ _____________________
"data" "numerictype(1,10,1)"
Inspect the LayerOutputEmbeddedType field to see the data types of the outputs of each quantized layer in your network.
qDetails.LayerOutputEmbeddedDataType
ans = 66×2 table
Outputs DataType
___________________________ _____________________
"data/out" "numerictype(1,8,-1)"
"conv1/out" "numerictype(1,8,-3)"
"relu_conv1/out" "numerictype(1,8,-3)"
"pool1/out" "numerictype(1,8,-3)"
"fire2-squeeze1x1/out" "numerictype(1,8,-4)"
"fire2-relu_squeeze1x1/out" "numerictype(1,8,-4)"
"fire2-expand1x1/out" "numerictype(1,8,-3)"
"fire2-relu_expand1x1/out" "numerictype(1,8,-3)"
"fire2-expand3x3/out" "numerictype(1,8,-4)"
"fire2-relu_expand3x3/out" "numerictype(1,8,-4)"
"fire2-concat/out" "numerictype(1,8,-4)"
"fire3-squeeze1x1/out" "numerictype(1,8,-4)"
"fire3-relu_squeeze1x1/out" "numerictype(1,8,-4)"
"fire3-expand1x1/out" "numerictype(1,8,-3)"
"fire3-relu_expand1x1/out" "numerictype(1,8,-3)"
"fire3-expand3x3/out" "numerictype(1,8,-4)"
⋮
Input Arguments
Quantized neural network, specified as a dlnetwork, SeriesNetwork,
or a DAGNetwork
object.
Output Arguments
Quantization details, returned as a 1-by-1 structure array. The structure contains these fields:
IsQuantized— Returns1(true) if the network is quantized; otherwise, returns0(false)TargetLibrary— Target library for code generationQuantizedLayerNames— List of quantized layersQuantizedLearnables— Quantized network learnable parametersNetworkInputEmbeddedDataType— Embedded data types for network inputs, returned as a table with these columns:InputNames— Name of the network input layer.DataType— Embedded data type for the input.
If your input data does not already match the expected type, the quantized network casts input data to this data type before passing it to the input layer. If the input layer is not quantized, the data type is
single.LayerOutputEmbeddedDataType— Embedded data types for outputs of quantized layers, returned as a table with these columns:Outputs— Layer output, in the format"LayerName/PortName".DataType— Embedded data type for the layer output.
Outputs of layers are represented in fixed-point when the data is passed between quantized layers. Layers that feed into a floating-point layer output
single.
Version History
Introduced in R2022aThe quantizationDetails function now returns
NetworkInputEmbeddedDataType and
LayerOutputEmbeddedDataType fields that list the expected network input
data type and layer output data types for your quantized network.
The quantizationDetails function now supports quantized dlnetwork objects.
See Also
Apps
Functions
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)