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.
net =
DAGNetwork with properties:
Layers: [68×1 nnet.cnn.layer.Layer]
Connections: [75×2 table]
InputNames: {'data'}
OutputNames: {'new_classoutput'}
Use the quantizationDetails function to see that the network is not quantized.
qDetails_original = struct with fields:
IsQuantized: 0
TargetLibrary: ""
QuantizedLayerNames: [0×0 string]
QuantizedLearnables: [0×3 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 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.
Create a dlquantizer
object and specify the network to quantize. Set the execution environment to MATLAB.
Use the calibrate function to exercise the network with sample inputs and collect range information.
Use the quantize
method to quantize the network object and return a simulatable quantized network.
qNet =
Quantized DAGNetwork with properties:
Layers: [68×1 nnet.cnn.layer.Layer]
Connections: [75×2 table]
InputNames: {'data'}
OutputNames: {'new_classoutput'}
Use the quantizationDetails method to extract quantization details.
Use the quantizationDetails
method to extract the quantization details.
qDetails = struct with fields:
IsQuantized: 1
TargetLibrary: "none"
QuantizedLayerNames: [26×1 string]
QuantizedLearnables: [52×3 table]
Inspect the QuantizedLayerNames
field to see a list of the quantized layers.
ans = 26×1 string
"conv1"
"fire2-squeeze1x1"
"fire2-expand1x1"
"fire2-expand3x3"
"fire3-squeeze1x1"
"fire3-expand1x1"
"fire3-expand3x3"
"fire4-squeeze1x1"
"fire4-expand1x1"
"fire4-expand3x3"
"fire5-squeeze1x1"
"fire5-expand1x1"
"fire5-expand3x3"
"fire6-squeeze1x1"
"fire6-expand1x1"
"fire6-expand3x3"
"fire7-squeeze1x1"
"fire7-expand1x1"
"fire7-expand3x3"
"fire8-squeeze1x1"
"fire8-expand1x1"
"fire8-expand3x3"
"fire9-squeeze1x1"
"fire9-expand1x1"
"fire9-expand3x3"
"new_conv"
Inspect the QuantizedLearnables
field to see the quantized values for learnable parameters in the network.
ans=52×3 table
Layer Parameter Value
__________________ _________ __________________
"conv1" "Weights" {3×3×3×64 int8 }
"conv1" "Bias" {1×1×64 int32}
"fire2-squeeze1x1" "Weights" {1×1×64×16 int8 }
"fire2-squeeze1x1" "Bias" {1×1×16 int32}
"fire2-expand1x1" "Weights" {1×1×16×64 int8 }
"fire2-expand1x1" "Bias" {1×1×64 int32}
"fire2-expand3x3" "Weights" {3×3×16×64 int8 }
"fire2-expand3x3" "Bias" {1×1×64 int32}
"fire3-squeeze1x1" "Weights" {1×1×128×16 int8 }
"fire3-squeeze1x1" "Bias" {1×1×16 int32}
"fire3-expand1x1" "Weights" {1×1×16×64 int8 }
"fire3-expand1x1" "Bias" {1×1×64 int32}
"fire3-expand3x3" "Weights" {3×3×16×64 int8 }
"fire3-expand3x3" "Bias" {1×1×64 int32}
"fire4-squeeze1x1" "Weights" {1×1×128×32 int8 }
"fire4-squeeze1x1" "Bias" {1×1×32 int32}
⋮