Main Content

Export Image Classification Network from Deep Network Designer to Simulink

This example shows how to export an image classifier trained using Deep Network Designer to Simulink®.

Load Data

Load the digit sample data. The digits data set consists of 10,000 synthetic grayscale images of handwritten digits. Each image is 28-by-28 pixels and has an associated label denoting which digit the image represents (0–9).

digitDatasetPath = fullfile(matlabroot,"toolbox","nnet","nndemos", ...

Create an image datastore. The imageDatastore function automatically labels the images based on folder names.

imds = imageDatastore(digitDatasetPath, ...
    IncludeSubfolders=true, ...

Divide the data into training and test sets using splitEachLabel.

[imdsTrain,imdsTest] = splitEachLabel(imds,0.9,"randomize");

Define Network Architecture

Define the convolutional neural network architecture. You can create this network at the command line or interactively using Deep Network Designer.

Specify the size of the images in the input layer of the network and the number of classes in the fully connected layer before the classification layer.

inputSize = [28 28 1];
numClasses = 10;
layers = [

View the network in Deep Network Designer.


Import Data

To import the image datastore, select the Data tab and click Import Data > Import Image Classification Data. Select imdsTrain as the data source. Set aside 30% of the training data to use as validation data. To randomly allocate observations to the training and validation sets, select Randomize.

Import the data by clicking Import.

Train Network

Specify the training options and train the network.

On the Training tab, click Training Options. For this example, set the maximum number of epochs to 5 and keep the other default settings. Set the training options by clicking OK.

Train the network by clicking Train.

The accuracy is the fraction of labels that the network predicts correctly. In this case, more than 98% of the predicted labels match the true labels of the validation set.

Export Network to Simulink

To export the trained network to Simulink®, on the Training tab, click Export > Export to Simulink.

Save the network in your chosen location.

Deep Network Designer creates Simulink® blocks that are suitable for the trained network. In this example, the app creates a Predict block and an Image Classifier block.

Create Simulink Model

You can use the generated blocks to create a Simulink model for image classification and prediction. For this example, use the Image Classifier block to classify a selection of test images.

This example provides the Simulink model digitsClassifier.slx which uses a pretrained digits classification network attached to this example as a supporting file. You can open the Simulink model (provided in this example) or create a model using the blocks generated by Deep Network Designer.

Open digitsClassifier.slx to see the prebuilt model.

model = "digitsClassifier";

To create this model using the blocks you generated using Deep Network Designer, use the following steps.

1. Delete the Predict block.

2. Select the Image Classifier block. Under Outputs, select Classification and Predictions. The software automatically populates the File path with the path to the network trained in Deep Network Designer.

3. Insert a From Workspace block. Connect this block to the input of the Image Classifier block. Select the From Workspace block and set Sample time to 1, clear the Interpolate data check box, and set From output after final data value by to Holding final value.

4. Insert two Outport blocks and connect them to the scores and labels output of the Image Classifier block.

5. (Optional) Insert a Display block and connect it to the ypred output of the Image Classifier block.

6. Save the model as digitsClassifier.slx.

Deep learning functionality in Simulink® uses MATLAB® Function block that requires a supported compiler. For most platforms, a default C compiler is supplied with the MATLAB installation. When using C++ language, you must install a compatible C++ compiler. To see a list of supported compilers, see Supported and Compatible Compilers.

Load Test Images

Load test images to classify using the model.

I = [];
numImages = 6;

for i = 1:numImages
    idx = randi(length(imdsTest.Labels));
    I(:,:,1,i) = readimage(imdsTest,idx);
    trueLabel(i) = imdsTest.Labels(idx);

To import this data into the Simulink model, specify a structure variable containing the input image data and an empty time vector.

simin.time = [];
simin.signals.values = I;
simin.signals.dimensions = size(I);

Predict Using Simulink Model

Simulate the model and save the simulation output to out.

out = sim(model);

The network classifies the six images.

You can improve the simulation speed of your Simulink deep learning models by using the accelerator modes of the Simulink product. For more information, see Acceleration for Simulink Deep Learning Models.

Display Top Predictions

Extract the scores for each of the test images and sort them from highest probability to lowest.

scores = out.yout{1}.Values(:,:,1);
scores = scores.Data(:,:,1);
labels = out.yout{2}.Values(:,:,1);
labels = labels.Data(:,:,1);

[~,idx] = sort(scores,2,"descend");
idx = idx(:,5:-1:1);
scoresTop = rand([numImages,5]);
for i = 1:numImages
    scoresTop(i,:) = scores(i,idx(i,:));

labelsTop = split(string(labels(idx)),{'x','_'});
labelsTop = labelsTop(:,:,2);

Display the top five predicted labels and their associated probabilities as a histogram for each of the test images.

for i = 1:numImages
    predSubtitle = "Pred: "+labelsTop(i,5)+ ...
    " ("+string(round(100*scoresTop(i,5),2)+"%)");
    trueSubtitle = "True: "+string(trueLabel(i));

    xlim([0 1])
    title("Top 5 Predictions")

See Also

| |

Related Topics