Try Deep Learning on FPGA with Only Five Additional Lines of MATLAB Code
Use the Deep Learning HDL Toolbox™ to deploy a pretrained deep learning network to a target board and identify objects on a live webcam connected to the development computer by adding only five lines of MATLAB® code to the Try Deep Learning in 10 Lines of MATLAB Code example.
To connect to a webcam and load the pretrained ResNet-18 network:
camera = webcam; % Connect to the camera net = resnet18; % Load the neural network
If you need to install the webcam and ResNet-18 add-ons, a message appears with a link to help you download the free add-ons using Add-On Explorer. Alternatively, see Deep Learning Toolbox Model for ResNet-18 Network and MATLAB Support Package for USB Webcams for installation instructions.
After you install Deep Learning Toolbox™ Model for ResNet-18 network, you can use it to classify images. ResNet-18 is a pretrained model that has been trained on a subset of the ImageNet database. The model is trained on more than a million images and can classify images into 1000 object categories such as keyboard, mouse, cup, pencil, and so on.
To set up the interface to the target board, create the workflow object, and deploy the network to the target board:
hT = dlhdl.Target('Xilinx',Interface = 'Ethernet'); hW = dlhdl.Workflow('Network',net,'Bitstream','zcu102_single','Target',hT); hW.deploy;
To show and classify live images:
while true im = snapshot(camera); % Take a picture image(im); % Show the picture im = imresize(im,[224 224]); % Resize the picture for ResNet-18 [prediction, speed] = hW.predict(single(im),'Profile','on'); [val, idx] = max(prediction); label = net.Layers(end).ClassNames{idx}; % Classify the image title(char(label)); % Show the class label drawnow end
Point the webcam at an object. The pretrained deep learning network reports what class of object it thinks the webcam is showing, classifying images until you press Ctrl+C. The code resizes the image for the network by using
imresize
.For example, the network correctly classifies a coffee mug. Experiment with objects in your surroundings to see how accurate the network is.
For next steps, see Deep Learning on FPGA Solution.
See Also
resnet18
| dlhdl.Workflow
| dlhdl.Target