Generate C Code for Quantized Networks with Simulink Network Blocks
R2026bThis example shows how to generate C code for a quantized neural network in Simulink® using a Predict block.
First, you train a simple convolutional deep neural network to classify handwritten digits from 0 to 9 and quantize the network. You then create a Simulink model with a Predict block and load the quantized network into the Predict block. Finally, generate code for the network through simulation-in-the-loop (SIL) testing.
For completeness of the workflow, the example includes training and quantizing a deep neural network. If you are familiar with these steps, skip to the Create Simulink Model section.
Load Data and Train Network
Load the training and validation data. Train a convolutional neural network for the classification task. 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; net = trainDigitDataNetwork(imdsTrain,imdsValidation); trueLabels = imdsValidation.Labels; classes = categories(trueLabels);
Quantize Network
Split the data into calibration and validation data sets.
calibrationDataStore = splitEachLabel(imdsTrain,0.1,"randomize");
validationDataStore = imdsValidation;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. Using this data type requires a Fixed-Point Designer™ license.
quantObj = dlquantizer(net,ExecutionEnvironment="MATLAB");Prepare the network for quantization using prepareNetwork. Network preparation modifies the neural network to improve accuracy and avoid error conditions in the quantization workflow.
prepareNetwork(quantObj)
Use the calibrate function to exercise the network with the calibration data and collect range statistics for the weights, biases, and activations at each layer.
calResults = calibrate(quantObj,calibrationDataStore);
Use the quantize method to quantize the network object and return a simulatable quantized network.
qNet = quantize(quantObj);
You can use the quantizationDetails function to see that the network is now quantized.
qDetails = quantizationDetails(qNet)
qDetails = struct with fields:
IsQuantized: 1
TargetLibrary: "none"
QuantizedLayerNames: [8×1 string]
QuantizedLearnables: [6×3 table]
NetworkInputEmbeddedDataType: [1×2 table]
LayerOutputEmbeddedDataType: [8×2 table]
Compare the accuracy of the quantized network to the original network.
accuracyQuantized = testnet(qNet,imdsValidation,"accuracy")accuracyQuantized = 99.2400
accuracyOriginal = testnet(net,imdsValidation,"accuracy")accuracyOriginal = 99.2800
The quantized network has a similar accuracy to the original, floating-point network.
Create Simulink Model
Export the quantized network to Simulink using a Predict block. For an example of exporting a compressed network to Simulink using Deep Learning Layers blocks, see Export Quantized Networks to Simulink and Generate Code.
Create a new Simulink model.
mdlName = "QuantizedNetwork";
new_system(mdlName);
open_system(mdlName);Save the quantized network as a MAT file.
save("quantizedNet.mat","qNet");
Add a Predict block to the model and set the NetworkFilePath to the MAT file containing the quantized network.
blkPath = mdlName + "/Predict"; add_block("deeplib/Predict",blkPath); set_param(blkPath,"NetworkFilePath",fullfile(pwd,"quantizedNet.mat"));
Add a From Workspace block to provide the input data and connect it to the Predict block.
add_block("simulink/Sources/From Workspace",mdlName + "/From Workspace"); set_param(mdlName + "/From Workspace","VariableName","simin"); set_param(mdlName + "/From Workspace","Interpolate","off"); set_param(mdlName + "/From Workspace","OutputAfterFinalValue","Holding final value"); set_param(mdlName + "/From Workspace","SampleTime","1"); add_line(mdlName,"From Workspace/1","Predict/1");
Add an output block and connect it to the Predict block.
add_block("simulink/Sinks/Out1",mdlName + "/Out1"); add_line(mdlName,"Predict/1","Out1/1");

Set the model to use a fixed-step solver.
set_param(mdlName,"SolverType","Fixed-step","FixedStep","1");
Save the Simulink model.
save_system(mdlName);
Simulate the Network
Prepare input data from the validation set. Create a structure for the From Workspace block with one image per time step. The time vector assigns each image to a discrete simulation step, and the dimensions field specifies the size of a single sample.
inputData = readall(imdsValidation);
inputData = cat(4,inputData{:});
numImages = size(inputData,4);
simin.time = (0:numImages-1)';
simin.signals.values = inputData;
simin.signals.dimensions = [28 28 1];Set the simulation stop time so the model runs one step per image.
set_param(mdlName,"StopTime",string(numImages-1)); set_param(mdlName,"SimulationMode","Normal"); simOut = sim(mdlName);
Extract the predicted scores from the simulation output.
scoresSimulation = simOut.yout{1}.Values.Data;Perform Software-in-the-Loop Testing
Configure the model for software-in-the-loop (SIL) testing. SIL mode generates C code from the model and runs it on the host computer, verifying that the generated code produces the same results as the simulation.
set_param(mdlName,"SystemTargetFile","ert.tlc"); set_param(mdlName,"SimulationMode","Software-in-the-loop (SIL)"); save_system(mdlName);
Run the SIL simulation.
simOutSIL = sim(mdlName);
### Searching for referenced models in model 'QuantizedNetwork'.
### Skipped unpacking from Simulink cache file "QuantizedNetwork.slxc" because the file does not have the relevant build artifacts.
### Total of 1 models to build.
### Starting top model code generation target build for: QuantizedNetwork
### Build reason: "QuantizedNetwork.c" did not exist.
### Build folder for generated code: C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw
### TLC front end (took 1.719s).
### Invoking Target Language Compiler on QuantizedNetwork.rtw
### Using System Target File: Y:\30\user.BR2026bd.DLQcodegen\matlab\rtw\c\ert\ert.tlc
### Loading TLC function libraries
.......
### Initial pass through model to cache user defined code
..
### Caching model source code
................................
### Writing header file QuantizedNetwork_types.h
### Writing header file QuantizedNetwork_private.h
### Writing header file QuantizedNetwork.h
### Writing source file QuantizedNetwork.c
.
### Writing header file complex_types.h
### Writing source file ert_main.c
### TLC code generation complete (took 24.045s).
### Saving binary information cache.
### Using toolchain: MinGW64 | gmake (64-bit Windows)
### Creating 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\QuantizedNetwork.mk' ...
### Building 'QuantizedNetwork': "%MINGW_ROOT%\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedNetwork.mk buildobj
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw>call ".\setup_mingw.bat"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw>set "MINGW_ROOT=F:\3rdparty\internal\12933511\win64\MinGW\bin"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw>cd .
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw>if "buildobj" == "" ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedNetwork.mk all ) else ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedNetwork.mk buildobj )
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "QuantizedNetwork.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HOLSON~3.DLQ/DE643A~1/QuantizedNetwork_ert_rtw/QuantizedNetwork.c"
### Successfully generated all binary outputs.
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw>exit /B 0
### Successful completion of build procedure for: QuantizedNetwork
### Simulink cache artifacts for 'QuantizedNetwork' were created in 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork.slxc'.
Build Summary
Top model targets:
Model Build Reason Status Build Duration
==========================================================================================================
QuantizedNetwork Target (QuantizedNetwork.c) did not exist. Code generated and compiled. 0h 2m 25.262s
1 of 1 models built (0 models already up to date)
Build duration: 0h 2m 26.717s
### Preparing to start SIL simulation ...
Building with 'MinGW64 Compiler (C)'.
MEX completed successfully.
### Using toolchain: MinGW64 | gmake (64-bit Windows)
### Creating 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\coderassumptions\lib\QuantizedNetwork_ca.mk' ...
### Building 'QuantizedNetwork_ca': "%MINGW_ROOT%\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedNetwork_ca.mk all
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\coderassumptions\lib>call ".\setup_mingw.bat"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\coderassumptions\lib>set "MINGW_ROOT=F:\3rdparty\internal\12933511\win64\MinGW\bin"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\coderassumptions\lib>cd .
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\coderassumptions\lib>if "all" == "" ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedNetwork_ca.mk all ) else ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedNetwork_ca.mk all )
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DINTEGER_CODE=0 -DCA_CHECK_FLOATING_POINT_ENABLED=1 -DCA_CHECK_LONG_LONG_ENABLED=0 -DCA_CHECK_DYNAMIC_MEMORY=0 -DCA_MODEL_SPECIFIC_CHECKS_ENABLED=1 -DCA_NONTUNABLE_ENUM_CHECKS_ENABLED=1 -DCA_CHECK_DAZ_ENABLED=1 @QuantizedNetwork_ca_comp.rsp -o "coder_assumptions_flt.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/coder_assumptions_flt.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DINTEGER_CODE=0 -DCA_CHECK_FLOATING_POINT_ENABLED=1 -DCA_CHECK_LONG_LONG_ENABLED=0 -DCA_CHECK_DYNAMIC_MEMORY=0 -DCA_MODEL_SPECIFIC_CHECKS_ENABLED=1 -DCA_NONTUNABLE_ENUM_CHECKS_ENABLED=1 -DCA_CHECK_DAZ_ENABLED=1 @QuantizedNetwork_ca_comp.rsp -o "QuantizedNetwork_ca.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HOLSON~3.DLQ/DE643A~1/QuantizedNetwork_ert_rtw/coderassumptions/QuantizedNetwork_ca.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DINTEGER_CODE=0 -DCA_CHECK_FLOATING_POINT_ENABLED=1 -DCA_CHECK_LONG_LONG_ENABLED=0 -DCA_CHECK_DYNAMIC_MEMORY=0 -DCA_MODEL_SPECIFIC_CHECKS_ENABLED=1 -DCA_NONTUNABLE_ENUM_CHECKS_ENABLED=1 -DCA_CHECK_DAZ_ENABLED=1 @QuantizedNetwork_ca_comp.rsp -o "coder_assumptions_hwimpl.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/coder_assumptions_hwimpl.c"
### Creating static library "./QuantizedNetwork_ca.lib" ...
"F:\3rdparty\internal\12933511\win64\MinGW\bin/ar" ruvs ./QuantizedNetwork_ca.lib @QuantizedNetwork_ca.rsp
F:\3rdparty\internal\12933511\win64\MinGW\bin/ar: creating ./QuantizedNetwork_ca.lib
a - coder_assumptions_hwimpl.obj
a - coder_assumptions_flt.obj
a - QuantizedNetwork_ca.obj
### Created: "./QuantizedNetwork_ca.lib"
### Successfully generated all binary outputs.
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\coderassumptions\lib>exit /B 0
### Using toolchain: MinGW64 | gmake (64-bit Windows)
### Creating 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\sil\QuantizedNetwork.mk' ...
### Building 'QuantizedNetwork': "%MINGW_ROOT%\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedNetwork.mk all
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\sil>call ".\setup_mingw.bat"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\sil>set "MINGW_ROOT=F:\3rdparty\internal\12933511\win64\MinGW\bin"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\sil>cd .
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\sil>if "all" == "" ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedNetwork.mk all ) else ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedNetwork.mk all )
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "xil_rtiostream.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/xil_rtiostream.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "xil_interface.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HOLSON~3.DLQ/DE643A~1/QuantizedNetwork_ert_rtw/sil/xil_interface.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "sil_main.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HOLSON~3.DLQ/DE643A~1/QuantizedNetwork_ert_rtw/sil/sil_main.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "xil_interface_lib.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/xil_interface_lib.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "xil_data_stream.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/xil_data_stream.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "xilcomms_rtiostream.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/xilcomms_rtiostream.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "xil_services.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/xil_services.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "rtiostream_utils.obj" "Y:/30/H6Q977~E/matlab/toolbox/coder/rtiostream/src/utils/rtiostream_utils.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "target_io.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/target_io.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "coder_assumptions_app.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/coder_assumptions_app.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "coder_assumptions_data_stream.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/coder_assumptions_data_stream.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "coder_assumptions_rtiostream.obj" "Y:/30/H6Q977~E/matlab/toolbox/rtw/targets/pil/c/coder_assumptions_rtiostream.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedNetwork -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedNetwork_comp.rsp -o "rtiostream_tcpip.obj" "Y:/30/H6Q977~E/matlab/toolbox/coder/rtiostream/src/rtiostreamtcpip/rtiostream_tcpip.c"
### Creating standalone executable "./QuantizedNetwork.exe" ...
"F:\3rdparty\internal\12933511\win64\MinGW\bin/g++" -static -m64 -o ./QuantizedNetwork.exe @QuantizedNetwork.rsp -Wl,--start-group @QuantizedNetwork_ref.rsp C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HOLSON~3.DLQ/DE643A~1/QuantizedNetwork_ert_rtw/coderassumptions/lib/QuantizedNetwork_ca.lib -lws2_32 -Wl,--end-group
### Created: "./QuantizedNetwork.exe"
### Successfully generated all binary outputs.
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.DLQcodegen\deeplearning_shared-ex18380956\QuantizedNetwork_ert_rtw\sil>exit /B 0
### Starting SIL simulation for component: QuantizedNetwork
### Application stopped
### Stopping SIL simulation for component: QuantizedNetwork
Extract the predicted scores from the SIL results.
scoresSIL = simOutSIL.yout{1}.Values.Data;Compare Results
Compare the predictions from the quantized network in MATLAB, normal simulation, and SIL to verify consistency across all three execution modes.
Convert all scores to labels using scores2label.
classNames = categories(trueLabels); ySimulation = scores2label(squeeze(scoresSimulation),classNames); ySIL = scores2label(squeeze(scoresSIL),classNames);
Compute the classification accuracy for each execution mode.
accuracySimulation = mean(ySimulation == trueLabels')*100
accuracySimulation = 99.2400
accuracySIL = mean(ySIL == trueLabels')*100
accuracySIL = 99.2400
Compare to the accuracy of the quantized network in MATLAB.
accuracyQuantized
accuracyQuantized = 99.2400
The matching accuracies confirm that the Simulink model and the generated C code produce results consistent with each other and the MATLAB quantized network.
For an example of processor-in-the-loop (PIL) testing with a Predict block, see Generate Generic C/C++ for Sequence-to-Sequence Deep Learning Simulink Models (Simulink Coder).
Supporting Functions
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) batchNormalizationLayer reluLayer maxPooling2dLayer(2,'Stride',2) convolution2dLayer(3,16) batchNormalizationLayer reluLayer fullyConnectedLayer(10) softmaxLayer]; % Specify the training options options = trainingOptions('adam', ... InitialLearnRate=0.01, ... MaxEpochs=5, ... Shuffle="every-epoch", ... ValidationData=imdsValidation, ... ValidationFrequency=30, ... Verbose=false, ... Plots="none", ... ExecutionEnvironment="auto"); % Train network net = trainnet(imdsTrain,layers,"crossentropy",options); end