# Image Classifier

**Libraries:**

Deep Learning Toolbox /
Deep Neural Networks

## Description

The Image Classifier 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

### Classify Images in Simulink Using GoogLeNet

Classify an image in Simulink® using the `Image Classifier`

block. The example uses the pretrained deep convolutional neural network GoogLeNet to perform the classification.

### Classify ECG Signals in Simulink Using Deep Learning

Use wavelet transforms and a deep learning network within a Simulink (R) model to classify ECG signals. This example uses the pretrained convolutional neural network from the *Classify Time Series Using Wavelet Analysis and Deep Learning* example of the Wavelet Toolbox™ to classify ECG signals based on images from the CWT of the time series data. For information on training, see Classify Time Series Using Wavelet Analysis and Deep Learning (Wavelet Toolbox).

## Limitations

The Image Classifier block does not support sequence networks and multiple input and multiple output networks (MIMO).

The Image Classifier block does not support MAT-file logging.

## Ports

### Input

**image** — Image or feature data

numeric array

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 `NaN`

s, then they are propagated through
the network.

### Output

**ypred** — Predicted class labels

enumerated

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

**scores** — Predicted class scores

matrix

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

**labels** — Class labels for predicted scores

matrix

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

**Network** — Source for trained network

`Network from MAT-file`

(default) | `Network from MATLAB function`

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'` |

**File path** — MAT-file containing trained network

`untitled.mat`

(default) | MAT-file path or name

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'` |

**MATLAB function** — MATLAB function name

`squeezenet`

(default) | MATLAB function name

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'` |

**Mini-batch size** — Size of mini-batches

128 (default) | positive integer

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 input** — Resize input dimensions

`on`

(default) | `off`

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'` |

**Classification** — Output predicted label with highest score

`on`

(default) | `off`

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'` |

**Predictions** — Output all scores and associated labels

`off`

(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'` |

**Class names workspace variable** — Workspace variable containing class names of the network output

`classNames`

(default) | categorical vector variable | string array variable | cell array of character vectors variable name

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

You can accelerate your simulations with code generation taking advantage of the Intel

^{®}MKL-DNN library. For more details, see Acceleration for Simulink Deep Learning Models.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using Simulink® Coder™.

Usage notes and limitations:

To generate generic C code that does not depend on third-party libraries, in the

**Configuration Parameters**>**Code Generation**general category, set the**Language**parameter to`C`

.To generate C++ code, in the

**Configuration Parameters**>**Code Generation**general category, set the**Language**parameter to`C++`

. To specify the target library for code generation, in the**Code Generation**>**Interface**category, set the**Target Library**parameter. Setting this parameter to`None`

generates generic C++ code that does not depend on third-party libraries.For a list of networks and layers supported for code generation, see Networks and Layers Supported for Code Generation (MATLAB Coder).

### GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Usage notes and limitations:

The

**Language**parameter in the**Configuration Parameters**>**Code Generation**general category must be set to`C++`

.For a list of networks and layers supported for CUDA

^{®}code generation, see Supported Networks, Layers, and Classes (GPU Coder).To learn more about generating code for Simulink models containing the Image Classifier block, see Code Generation for a Deep Learning Simulink Model to Classify ECG Signals (GPU Coder).

## Version History

**Introduced in R2020b**

### R2024a: `SeriesNetwork`

and `DAGNetwork`

are not recommended

Starting in R2024a, the `SeriesNetwork`

and `DAGNetwork`

objects are not recommended. This recommendation means that `SeriesNetwork`

and `DAGNetwork`

inputs to the Image Classifier block are not
recommended. Use the `dlnetwork`

objects instead.
`dlnetwork`

objects have these advantages:

`dlnetwork`

objects are a unified data type that supports network building, prediction, built-in training, visualization, compression, verification, and custom training loops.`dlnetwork`

objects support a wider range of network architectures that you can create or import from external platforms.The

`trainnet`

function supports`dlnetwork`

objects, which enables you to easily specify loss functions. You can select from built-in loss functions or specify a custom loss function.Training and prediction with

`dlnetwork`

objects is typically faster than`LayerGraph`

and`trainNetwork`

workflows.

Simulink block models with `dlnetwork`

objects behave differently. The
predicted scores are returned as a *K*-by-*N* matrix, where *K* is the
number of classes, and *N* is the number of observations.
If you have an existing Simulink block model with a `SeriesNetwork`

or
`DAGNetwork`

object, follow these steps to use a `dlnetwork`

object instead:

Convert the

`SeriesNetwork`

or`DAGNetwork`

object to a`dlnetwork`

using the`dag2dlnetwork`

function.Define a workspace variable containing class names of the network output corresponding to the block parameter

**Class names workspace variable**.Transpose the predicted scores using a transpose block to an

*N*-by-*K*array, where*N*is the number of observations, and*K*is the number of classes.

## See Also

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)