Apprendre l'Inspection visuelle en codant

Implémenter un workflow courant d'inspection visuelle et identifier les défauts d'après le contenu d'une image.

To follow along:

  1. Download the code
  2. Open in MATLAB

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.

Load and preprocess data
  • Import the data. 
Load and preprocess data
  • Data is labeled either good or bad. Use deep learning to help differeniate between the two categories.
Load and preprocess data
  • The imageDatastore function automatically labels the images based on folder names.
Load and preprocess data
  • 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).
Load and preprocess data
  • 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.
Load and preprocess data
  • 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.
  • Pre-processing can take a lot of time, so be sure your images are clean and properly labeled. 
  • Image-based apps can significantly speed up common preprocessing tasks like cropping, labeling, and registering images.

Step 2

Import Model

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

Import model
  • 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.
Import model
  • 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.
Import model
Import model
  • 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.

Train the model
Train the model
  • 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.

Test the model and visualize results
  • 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.
Test the model and visualize results
  • 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.
Test the model and visualize results
  • 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.
Test the model and visualize results

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.