Deep Learning

Visual Inspection Guided Example

Perform a common visual inspection workflow and identify defects based on the image content.

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 the data. 
  • Data is labeled either good or bad. Use deep learning to help differeniate between the two categories.
  • 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 Model and Visualize Results).
  • Add more images by augmenting the images. For the code in this example, use reflection along the x-axis and y-axis, and rotate the images up to 180 degrees.
  • Click Run Section to run the code.

Step 2

Import Model

You can import and modify a network to work with your test data.

  • Import a model by calling the name of the network in your desktop. If you do not have the network already installed, click on the link that appears and download the network.
  • Prepare your model by replacing the last few layers with new layers that are specific to your data. For example, replace the final fully connected layer with one that has two as the output size for the two classes of data you wish to classify. Also, you can use an app to do this step using a drag-and-drop method.
  • Click Run Section to run the code.

Step 3

Train Model

You can use the data and the modified network to train a new image classifer. There are a variety of training options from which to choose. The example used in this step is a simple training configuration that includes the optimizer, number of Epochs, initial learn rate, and plot. Training a model can take a lot of time depending on your GPU and CPU resources.

  • If you would like to train the model, uncomment the lines of code and click Run Section.
  • If you do not want to train the model, you can skip this step and load a pretrained network in Step 4, Test Model and Visualize Results. 

Step 4

Test Model and Visualize Results​​​

In the final step, you want to load the model and use the test data to see the accuracy of the model.

  • Classify the test data (set aside from Step 1, Load and Preprocess Data), using the classify function to classify all the images in one line of code.
  • View and compare the results against what the true values should be. The accuracy is a calculation of correct answers versus the total number of test samples.
  • Read and classify individual images and visualize the results. If the model detects a defect, use Explainable AI techniques like GradCAM to visualize the location of the defect in the image.

Key things to remember:

  • Use a flexible model that works with new data or circumstances and not just the data learned during training. 
  • You can use visualization techniques to help you understand what the network is seeing and add debugging to uncover why the network is working the way it is.

You can also follow along with the video below:

Video length is 5:18
Johanna Pingel

Connect with Johanna, MathWorks Deep Learning Expert