Main Content

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.

  1. To connect to a webcam and load the pretrained AlexNet network:.

    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 such as keyboard, mouse, cup, pencil, and so on.

  2. 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. To show and classify live images:

    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

    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

| |

Related Topics