Main Content

predict

Class: dlhdl.Workflow
Package: dlhdl

Run inference on deployed network and profile speed of neural network deployed on specified target device

Description

example

predict(image) predicts responses for the image data in imds by using the deep learning network that you specified in the dlhdl.Workflow class for deployment on the specified target board and returns the results.

predict(image, Name,Value) predicts responses for the image data in imds by using the deep learning network that you specified by using the dlhdl.Workflow class for deployment on the specified target boards and returns the results, with one or more arguments specified by optional name-value pair arguments.

Input Arguments

expand all

Input image, specified as a m-by-n-by-k numeric array. m, n, and k must match the dimensions of the deep learning network input image layer. For example, for the LogoNet network, resize the input images to a 227-by-227-by-3 array.

Data Types: single

Name-Value Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example:

Flag to return profiling results, for the deep learning network deployed to the target board.

Example: 'Profile', 'On'

Examples

expand all

  1. Create a dlhdl.Workflow object that has VGG-19 as the network argument, arria10soc_single as the bitstream argument, and hTarget as the target argument.

    % Save the pretrained SeriesNetwork object
    snet = vgg19;
    
    % Create a Target object and define the interface to the target board
    hTarget = dlhdl.Target('Intel');
    
    % Create a workflow object for the SeriesNetwork and using the FPFA bitstream 
    hW = dlhdl.Workflow('Network', snet, 'Bitstream', 'arria10soc_single','Target',hTarget);
    
  2. Load your input image and resize the input image to match the image input layer size for the VGG-19 network.

    % Load input images and resize them according to the network specifications
    image = imread('zebra.jpeg');
    inputImg = imresize(image, [224, 224]);
    imshow(inputImg);
    imIn = single(inputImg);
  3. Deploy the dlhdl.Workflow object to your target FPGA board by using the deploy method. Retrieve the VGG-19 network prediction result for your input image from the FPGA board by using the predict method.

    % Deploy the workflow object
    hW.deploy;
    % Predict the outcome and optionally profile the results to measure performance.
    [prediction, speed] = hW.predict(imIn,'Profile','on');
    [val, idx] = max(prediction);
    snet.Layers(end).ClassNames{idx}

This example shows how to use the predict method to retrieve the prediction results for an input image from a deployed quantized LogoNet network.

  1. Create a file in your current working folder called getLogoNetwork.m. Enter these lines into the file:

    function net = getLogoNetwork
        data = getLogoData;
        net  = data.convnet;
    end
    
    function data = getLogoData
        if ~isfile('LogoNet.mat')
            url = 'https://www.mathworks.com/supportfiles/gpucoder/cnn_models/logo_detection/LogoNet.mat';
            websave('LogoNet.mat',url);
        end
        data = load('LogoNet.mat');
    end
  2. Create an image datastore and split 70 percent of the images into a training data set and 30 percent of the images into a validation data set.

    curDir = pwd;
    newDir = fullfile(matlabroot,'examples','deeplearning_shared','data','logos_dataset.zip');
    copyfile(newDir,curDir);
    unzip('logos_dataset.zip');
    imds = imageDatastore('logos_dataset', ...
        'IncludeSubfolders',true, ...
        'LabelSource','foldernames');
    [imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');

  3. Create a dlhdl.Workflow object which has quantized LogoNet as the network argument, zcu102_int8 as the bitstream, and hTarget as the target argument.

    To quantize the network, you need the products listed under FPGA in Quantization Workflow Prerequisites.

    % Save the pretrained SeriesNetwork object
    snet = getLogoNetwork;
    
    % Create a Target object and define the interface to the target board
    hTarget = dlhdl.Target('Xilinx','Interface','Ethernet');
    
    % Create a Quantized Network Object
    
    dlquantObj = dlquantizer(snet,'ExecutionEnvironment','FPGA');
    dlquantObj.calibrate(imdsTrain);
    
    % Create a workflow object for the SeriesNetwork and using the FPFA bitstream 
    hW = dlhdl.Workflow('Network', dlquantObj, 'Bitstream', 'zcu102_int8','Target',hTarget);
    
  4. Retrieve a random image from logos_dataset.

    index = randperm(numel(imdsValidation.Files),1)
    imIn = readimage(imdsValidation,index)
    inputImg = imresize(imIn, [227 227]);
    
  5. Deploy the dlhdl.Workflow object to your target FPGA board by using the deploy method. Retrieve the prediction for the image by using the predict method.

    % Deploy the workflow object
    hW.deploy;
    % Predict the outcome and optionally profile the results to measure performance.
    [prediction, speed] = hW.predict(single(inputImg),'Profile','on');
    [val, idx] = max(prediction);
    snet.Layers(end).ClassNames{idx}
Introduced in R2020b