Image Recognition Guided Example

Learn how to load and preprocess data, import a network, perform transfer learning, and test the network using deep learning.

To get started:

  1. Download the code.
  2. Open the code in MATLAB.
  3. Follow along with the steps below.

Don’t have access?

Time to Complete:
15–30 minutes

Need a refresher? Try a free, interactive tutorial.

Step 1

Load and Preprocess Data

You can use large datasets with deep learning, so proper data management is important; however, the dataset in this example is small for demonstration purposes.

  • Import your data using an imageDatastore. The imageDatastore function automatically labels the images based on folder names.
  • To train the model, split your data into two categories: “training” to train the model and “testing data” to test how the model works on new data (set aside for Step 4, Test the Network).
  • Visualize random images from the dataset to see how the data appears.
  • Click Run Section to run the code.

Key things to remember:

  • The imageDatastore is home to your data throughout these steps and any future deep learning project with images.
  • Image-based apps can significantly speed up common preprocessing tasks like cropping, labeling, and registering images.

Step 2

Import Network

The example in this step uses GoogLeNet to import a deep learning model and modify it for transfer learning, but you can also import models and architectures from TensorFlow, PyTorch, and ONNX.

  • Import the model and make sure the data is ready. 
  • Use the Deep Network Designer app to interactively alter the model for a new task. For pre-processing, make sure the images are the right size expected by the deep learning model.
  • Try sample images to see what the model predicts. Incorrect predictions may occur when the model has not been trained to recognize the data (see Step 3, Transfer Learning).
  • Click Run Section to run the code.

Key things to remember:

  • Use a variety of pretrained models as a starting point for transfer learning.
  • Your data must be the right size or your model will error. 

Step 3

Transfer Learning​

For this step, you want to modify the model to work with your specific data. Models can take a long time to train depending on your hardware and dataset size.

  • Prepare your model by replacing the final fully connected layer with a new fully connected layer based on the number of categories you have. This example uses 4 categories.
  • Replace the final classification layer to reset it for your task. 
  • To train your model, click Run Section. Be sure that you have already run the code in steps 1 and 2. Training takes ~60 seconds.

Key things to remember:

  • Customize deep learning to perform your specific task by modifying the model.
  • Use the Deep Network Designer app to simplify the transfer learning process.

Step 4

Test Network​​​

In the final step, you want to verify the model works on new data. You need to classify the test data (set aside from Step 1, Load and Preprocess Data) and calculate the classification accuracy.

  • Run the classify command to test all of the images in your test set and display the accuracy—in this case, 90%.
  • Select images in your test set to visualize with the corresponding labels.

Key things to remember:

  • Use a flexible model that works with new data or circumstances and not just the data learned during training.
  • For more advanced AI solutions, use Explainable AI techniques to uncover why a model is predicting a certain category.

You can also follow along with the video below:

Video length is 4:13.

See the links below to continue with more image recognition tasks or keep exploring deep learning with related applications.

Johanna Pingel

Connect with Johanna, MathWorks Deep Learning Expert