Register, Validate, and Deploy Custom Hyperbolic Tangent Layer Network to FPGA
This example shows how to register, validate, and deploy a custom hyperbolic tangent (tanh
) layer network by using Deep Learning HDL Toolbox™. To deploy the network with the custom hyperbolic tangent (tanh
) layer:
Use the custom hyperbolic tangent (
tanh
) layer from Deep Learning Toolbox™.Register the custom hyperbolic tangent (
tanh
) layer by using theregisterCustomLayer
method.Validate the custom hyperbolic tangent (
tanh
) layer by generating a custom layer verification model.Generate a custom bitstream.
To retrieve the prediction results from the deployed custom layer network, use MATLAB®.
Create a Deep Learning Processor Configuration
To generate a custom processor configuration, use the dlhdl.ProcessorConfig
object. The generated deep learning processor configuration object has a custom module that contains the preconfigured custom layers. Save the deep learning processor configuration to a variable hPC
.
hPC = dlhdl.ProcessorConfig
hPC = Processing Module "conv" ModuleGeneration: 'on' LRNBlockGeneration: 'on' SegmentationBlockGeneration: 'off' ConvThreadNumber: 16 InputMemorySize: [227 227 3] OutputMemorySize: [227 227 3] FeatureSizeLimit: 2048 Processing Module "fc" ModuleGeneration: 'on' SoftmaxBlockGeneration: 'off' SigmoidBlockGeneration: 'off' FCThreadNumber: 4 InputMemorySize: 25088 OutputMemorySize: 4096 Processing Module "custom" ModuleGeneration: 'on' Multiplication: 'on' InputMemorySize: 40 OutputMemorySize: 40 Processor Top Level Properties RunTimeControl: 'register' RunTimeStatus: 'register' InputStreamControl: 'register' OutputStreamControl: 'register' ProcessorDataType: 'single' System Level Properties TargetPlatform: 'Xilinx Zynq UltraScale+ MPSoC ZCU102 Evaluation Kit' TargetFrequency: 200 SynthesisTool: 'Xilinx Vivado' ReferenceDesign: 'AXI-Stream DDR Memory Access : 3-AXIM' SynthesisToolChipFamily: 'Zynq UltraScale+' SynthesisToolDeviceName: 'xczu9eg-ffvb1156-2-e' SynthesisToolPackageName: '' SynthesisToolSpeedValue: ''
Register Custom Layer and Model
To register an instance of the custom layer and custom layer Simulink® model use the registerCustomLayer
method. This example leverages the built-in hyperbolic tangent (tanh
) layer from Deep Learning Toolbox™. Deep Learning HDL Toolbox™ uses the Simulink® model to generate a verification model for the custom layer.
hTanhLayer = tanhLayer('Name','tanh')
hTanhLayer = TanhLayer with properties: Name: 'tanh' Learnable Parameters No properties. State Parameters No properties. Show all properties
registerCustomLayer(hPC,Layer = hTanhLayer, Model = 'dnnfpgaTanhLayerModel.slx')
The custom deep learning processor configuration has a tanhLayer
under the custom processing module. The custom hyperbolic tangent (tanh
) layer is enabled by default for the bitstream generation.
Generate Verification Model for Custom Layer
Generate a verification model for your custom layer by using the openCustomLayerModel
method. Generate a test network and a test image for your custom layer network by specifying blank arguments for the Network
and InputImages
arguments of the openCustomLayerModel
method. The size of the test image matches the input layer size of the created test network.
openCustomLayerModel(hPC)
### The 'Network' property is empty for the given workflow object. An auto-generated network is provided. ### Notice: The layer 'input' with type 'nnet.cnn.layer.ImageInputLayer' is implemented in software. ### Notice: The layer 'output' with type 'nnet.cnn.layer.RegressionOutputLayer' is implemented in software. ### Allocating external memory buffers: offset_name offset_address allocated_space _______________________ ______________ ________________ "InputDataOffset" "0x00000000" "4.0 MB" "OutputResultOffset" "0x00400000" "4.0 MB" "SchedulerDataOffset" "0x00800000" "4.0 MB" "SystemBufferOffset" "0x00c00000" "20.0 MB" "InstructionDataOffset" "0x02000000" "4.0 MB" "EndOffset" "0x02400000" "Total: 36.0 MB"
The openCustomLayerModel
method generates a verification model file called dnnfpgaCustomLayerVerificationModel.slx
for your custom layer.
Simulate and Validate Custom Layer Model
Before you verify your custom layer model by using the verifyCustomLayerModel
method, open the dnnfpgaCustomLayerVerificationModel.slx
verification model. The verifyCustomLayerModel
verifies the functionality of the custom layer and prediction accuracy of the network which has the custom layer.
verifyCustomLayerModel(hPC)
### Compiling Simulink model 'dnnfpgaCustomLayersTB' ...
Warning: '<a href="matlab:slprivate('open_and_hilite_port_hyperlink', 'hilite', ['dnnfpgaCustomLayersTB/DUT/Subsystem/Scheduler'], 'Inport', 17);">Input Port 17 (Right:8)</a>' of '<a href="matlab:open_and_hilite_hyperlink ('dnnfpgaCustomLayersTB/DUT/Subsystem/Scheduler','error')">dnnfpgaCustomLayersTB/DUT/Subsystem/Scheduler</a>' is not connected.
Warning: '<a href="matlab:slprivate('open_and_hilite_port_hyperlink', 'hilite', ['dnnfpgaCustomLayersTB/DUT/Subsystem/Scheduler'], 'Inport', 18);">Input Port 18 (Right:9)</a>' of '<a href="matlab:open_and_hilite_hyperlink ('dnnfpgaCustomLayersTB/DUT/Subsystem/Scheduler','error')">dnnfpgaCustomLayersTB/DUT/Subsystem/Scheduler</a>' is not connected.
Warning: '<a href="matlab:slprivate('open_and_hilite_port_hyperlink', 'hilite', ['dnnfpgaCustomLayersTB/DUT/Subsystem/Scheduler'], 'Inport', 19);">Input Port 19 (Right:10)</a>' of '<a href="matlab:open_and_hilite_hyperlink ('dnnfpgaCustomLayersTB/DUT/Subsystem/Scheduler','error')">dnnfpgaCustomLayersTB/DUT/Subsystem/Scheduler</a>' is not connected.
### Complete Simulink model 'dnnfpgaCustomLayersTB' compilation. Verification passed.
Generate Custom Bitstream
Generate a custom bitstream that has the name myCustomLayer.bit
by using the dlhdl.buildProcessor
function. Save the generated bitstream to the myCustomLayer_prj
folder.
dlhdl.buildProcessor(hPC,ProjectFolder = 'myCustomLayer_prj',ProcessorName ='myCustomLayer');
Deploy and Predict Custom Layer Network on Hardware
Deploy the custom layer network by creating a dlhdl.Workflow
object with the custom network as the Network
argument and the custom bitstream myCustomLayer.bit
as the Bitstream
argument. Compile and deploy the dlhdl.Workflow
object. To retrieve the prediction results from the deployed network, use MATLAB® and the predict
method.
layers = [imageInputLayer([2 2 4],'Name','data','Normalization', 'none'); tanhLayer('Name','tanh'); regressionLayer('Name','output'); ] myCustomNet = assembleNetwork(layers); hTarget = dlhdl.Target('Xilinx','Interface','JTAG'); hW = dlhdl.Workflow(Network = myCustomNet, Bitstream = 'myCustomLayer.bit',Target = hTarget); hW.compile; hW.deploy; image = randi(255, [2,2,4]); hW.predict(single(image),Profile ='on');
See Also
dlhdl.Workflow
| dlhdl.ProcessorConfig
| registerCustomLayer
| openCustomLayerModel
| verifyCustomLayerModel
| dlhdl.buildProcessor