Main Content

Classify

Classify data using a trained deep learning neural network

  • Classify block

Libraries:
Deep Learning Toolbox / Deep Neural Networks

Description

The Classify block predicts class labels for the data at the input by using the trained network specified through the block parameter. This block allows loading of a pretrained network into the Simulink® model from a MAT file or from a MATLAB® function.

Examples

Limitations

The Classify block does not support MAT file logging.

Ports

Input

expand all

A h-by-w-by-c-by-N numeric array, where h, w, and c are the height, width, and number of channels of the images, respectively, and N is the number of images.

A N-by-numFeatures numeric array, where N is the number of observations and numFeatures is the number of features of the input data.

If the array contains NaNs, then they are propagated through the network.

Output

expand all

Predicted class labels with the highest score, returned as a N-by-1 enumerated vector of labels, where N is the number of observations.

Predicted scores, returned as a K-by-N matrix, where K is the number of classes, and N is the number of observations.

Labels associated with the predicted scores, returned as a N-by-K matrix, where N is the number of observations, and K is the number of classes.

Parameters

expand all

Specify the source for the trained network. Select one of the following:

  • Network from MAT file— Import a trained network from a MAT file containing a dlnetwork object.

  • Network from MATLAB function— Import a pretrained network from a MATLAB function. For example, to use a pretrained GoogLeNet, create a function pretrainedGoogLeNet in a MATLAB M-file, and then import this function.

    function net = pretrainedGoogLeNet
      net = imagePretrainedNetwork("googlenet");
    end

Programmatic Use

Block Parameter: Network
Type: character vector, string
Values: 'Network from MAT file' | 'Network from MATLAB function'
Default: 'Network from MAT file'

This parameter specifies the name of the MAT file that contains the trained deep learning network to load. If the file is not on the MATLAB path, use the Browse button to locate the file.

Dependencies

To enable this parameter, set the Network parameter to Network from MAT file.

Programmatic Use

Block Parameter: NetworkFilePath
Type: character vector, string
Values: MAT file path or name
Default: 'untitled.mat'

This parameter specifies the name of the MATLAB function for the pretrained deep learning network. For example, to use a pretrained GoogLeNet, create a function pretrainedGoogLeNet in a MATLAB M-file, and then import this function.

function net = pretrainedGoogLeNet
  net = imagePretrainedNetwork("googlenet");
end

Dependencies

To enable this parameter, set the Network parameter to Network from MATLAB function.

Programmatic Use

Block Parameter: NetworkFunction
Type: character vector, string
Values: MATLAB function name
Default: 'squeezenet'

Size of mini-batches to use for prediction, specified as a positive integer. Larger mini-batch sizes require more memory, but can lead to faster predictions.

Programmatic Use

Block Parameter: MiniBatchSize
Type: character vector, string
Values: positive integer
Default: '128'

Resize the data at the input port to the input size of the network.

Programmatic Use

Block Parameter: ResizeInput
Type: character vector, string
Values: 'off' | 'on'
Default: 'on'

Since R2026a

  • Auto mode –– This option automatically selects the simulation mode based on the model configuration and code generation compatibility. It uses Code generation whenever code generation is supported and switches to Interpreted execution when the model is incompatible for code generation.

  • Interpreted execution –– Simulate model using the MATLAB interpreter. This option shortens startup time but has a slower simulation speed than Code generation. In this mode, you can debug the source code of the block.

  • Code generation –– Simulate model using generated code.

    The first time you simulate a model that contains a Classify block, the software generates binary code or C/C++ MATLAB executable (MEX) code from the block and integrates this code with the model. The Classify block uses the same infrastructure as MATLAB Coder™, which you use to generate C/C++ code from MATLAB code outside of Simulink. The generated code is reused for subsequent simulations, as long as the model does not change. This option requires additional startup time, but the speed of the subsequent simulations is comparable to Interpreted execution.

If your Simulink model is set up to simulate in Normal mode and if you use Classify block via code generation, individual Classify blocks can take advantage of acceleration via code generation. If the Simulink model is set up to simulate in accelerator mode or any of the other target modes, Classify block's Simulate using parameter does not have any effect. However, there is one exception, if the model is set up to simulate in accelerator mode and you use Classify block with interpreted execution mode, the block will run in interpreted execution mode.

Programmatic Use

Block Parameter: SimulateUsing
Type: character vector, string
Values: "Auto mode" | "Interpreted execution" | "Code generation"
Default: "Auto mode"

Enable output port ypred that outputs the label with the highest score.

Programmatic Use

Block Parameter: Classification
Type: character vector, string
Values: 'off' | 'on'
Default: 'on'

Enable output ports scores and labels that output all predicted scores and associated class labels.

Programmatic Use

Block Parameter: Predictions
Type: character vector, string
Values: 'off' | 'on'
Default: 'off'

Variable containing class names, specified as a categorical vector, a string array, or a cell array of character vectors.

The output size of the network must match the number of classes.

Dependencies

To enable this parameter, set the Network parameter to Network from MAT file to import a trained dlnetwork object from a MAT file.

Programmatic Use

Block Parameter: classNames
Type:variable name of a categorical vector, a string array, or a cell array of character vectors.
Values: Name of a variable containing class names, specified as a categorical vector, a string array, or a cell array of character vectors.
Default: The workspace variable classNames.

Tips

Extended Capabilities

expand all

Version History

Introduced in R2020b

expand all

See Also