Main Content

Try Deep Learning on FPGA with only Five Additional Lines of MATLAB Code

This example shows how to use 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.

  1. To connect to the webcam and get a pretrained neural network, run these commands.

    camera = webcam; % Connect to the camera
    net = alexnet;   % Load the neural network

    If you need to install the webcam and Alexnet 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 AlexNet Network and MATLAB Support Package for USB Webcams for installation instructions.

    After you install Deep Learning Toolbox™ Model for AlexNet network you can use it to classify images. AlexNet is a pretrained convolutional neural network (CNN) that has been trained on more than a million images and can classify images into 1000 object categories (for example, keyboard, mouse, and so on).

  2. Run the following three lines of code 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');
    hW = dlhdl.Workflow('Network',net,'Bitstream','zcu102_single','Target',hT);
  3. Run the following code to show and classify live images. Point the webcam at an object. The neural 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 (Image Processing Toolbox).

    while true
        im = snapshot(camera);       % Take a picture
        image(im);                   % Show the picture
        im = imresize(im,[227 227]); % Resize the picture for alexnet
        [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

    In this 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 and Workflows.

See Also

| |

Related Topics