prepareNetwork - Prepare deep neural network for quantization - MATLAB (original) (raw)
Prepare deep neural network for quantization
Since R2024b
Syntax
Description
Examples
This example shows how to prepare a neural network for quantization using the prepareNetwork
function.
The network preparation step modifies your network to improve the accuracy and performance of the quantized network. In this example, a convolutional neural network created for deep learning classification is prepared and the modifications include converting the network from a SeriesNetwork object to a dlnetwork object and fusing consecutive convolutional and batch normalization layers in the network architecture to optimize the quantization process.
Load the training and validation data. Train a convolutional neural network for the classification task. This example uses a simple convolutional neural network to classify handwritten digits from 0 to 9. For more information on setting up the data used for training and validation, see Create Simple Deep Learning Neural Network for Classification.
[imdsTrain, imdsValidation] = loadDigitDataset; originalNet = trainDigitDataNetwork(imdsTrain, imdsValidation);
Create a dlquantizer
object and specify the network to quantize and execution environment. When you use the MATLAB® execution environment, quantization is performed using the fi
fixed-point data type. Use of this data type requires a Fixed-Point Designer™ license.
quantObj = dlquantizer(originalNet, ExecutionEnvironment="MATLAB");
Prepare the dlquantizer
object for quantization using prepareNetwork
.
Observe the original network is a SeriesNetwork
object and network preparation converts the network to a dlnetwork
object.
originalNet = SeriesNetwork with properties:
Layers: [16×1 nnet.cnn.layer.Layer]
InputNames: {'imageinput'}
OutputNames: {'classoutput'}
preparedNet = quantObj.NetworkObject
preparedNet = dlnetwork with properties:
Layers: [11×1 nnet.cnn.layer.Layer]
Connections: [10×2 table]
Learnables: [8×3 table]
State: [0×3 table]
InputNames: {'imageinput'}
OutputNames: {'softmax'}
Initialized: 1
View summary with summary.
You can use analyzeNetwork to see modifications to the network architecture.
analyzeNetwork(originalNet) analyzeNetwork(preparedNet)
Compute the accuracy of the original and prepared networks. The prepared network performs similarly to the original network despite the changes to the network architecture, though small deviations may exist.
originalNetdl = dag2dlnetwork(originalNet); accuracyOriginal = testnet(originalNetdl,imdsValidation,"accuracy")
accuracyOriginal = 99.7200
accuracyPrepared = testnet(preparedNet,imdsValidation,"accuracy")
accuracyPrepared = 99.7200
The dlquantizer object quantObj
, which has the modified network, is ready for remainder of the quantization workflow. Note that you must use prepareNetwork
before calibrate. For an example of a full quantization workflow, see Quantize Multiple-Input Network Using Image and Feature Data.
Load Digits Data Set Function
The loadDigitDataset
function loads the Digits data set and splits the data into training and validation data.
function [imdsTrain, imdsValidation] = loadDigitDataset() digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ... 'nndatasets','DigitDataset'); imds = imageDatastore(digitDatasetPath, ... 'IncludeSubfolders',true,'LabelSource','foldernames'); [imdsTrain, imdsValidation] = splitEachLabel(imds,0.75,"randomized"); end
Train Digit Recognition Network Function
The trainDigitDataNetwork
function trains a convolutional neural network to classify digits in grayscale images.
function net = trainDigitDataNetwork(imdsTrain,imdsValidation)
layers = [ imageInputLayer([28 28 1],"Normalization","rescale-zero-one") convolution2dLayer(3,8,'Padding','same') batchNormalizationLayer reluLayer maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
dropoutLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
% Specify the training options options = trainingOptions('sgdm', ... 'InitialLearnRate',0.01, ... 'MaxEpochs',10, ... 'Shuffle','every-epoch', ... 'ValidationData',imdsValidation, ... 'ValidationFrequency',30, ... 'Verbose',false, ... 'Plots','none',"ExecutionEnvironment","auto");
% Train network net = trainNetwork(imdsTrain,layers,options);
end
Tips
- Prepare your network after creating the dlquantizer object and before using calibrate. If you use
prepareNetwork
on yourdlquantizer
object after using calibrate,quantize, orvalidate, the object is reset and you must repeat the calibration, quantization, or validation step. - The equalization of layer parameters performed by
prepareNetwork
is equivalent to that of equalizeLayers.
Version History
Introduced in R2024b