Visual Inspection Code-Along Tutorial - MATLAB & Simulink
Video Player is loading.
Current Time 0:00
Duration 5:18
Loaded: 3.11%
Stream Type LIVE
Remaining Time 5:18
 
1x
  • Chapters
  • descriptions off, selected
  • en (Main), selected
    Video length is 5:18

    Visual Inspection Code-Along Tutorial

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

    Download the code and follow along: Visual Inspection Guided Example

    Published: 9 Jan 2023

    For step one, you want to import the data and make sure it's ready to use. This is important because, in Deep Learning problems, you may be dealing with lots of data, so proper management of the data is important. The data is labeled as either good or bad, and we want to use Deep Learning to recognize the differences between the two categories. The most important thing to remember here is imageDatastore, which will be what stores and helps manage your data throughout these tasks, and also any other Deep Learning projects that you do in the future. ImageDatastore can label all images based on folder names, which is what we're doing here.

    Next we're splitting the data between testing and training, 50% of each. The training data will be used to train the model, and the testing data to test the model works on new data. Keep in mind, this data set is intentionally small. There are only 200 images total. So it's likely that when you're working with full-sized data sets, your image split will be different, and the total number of images will probably be much larger.

    What's most important to remember is that preprocessing can take a lot of time. You really want to spend the time to make sure that your images are clean and labeled properly. Spending time with image processing apps, things like image registration and cleaning up images based on image content, will be worth it in the long run.

    You can also quickly add more images by augmenting the images. Here in this code we're using reflection along the x-axis and the y-axis, and rotating the images up to 180 degrees. This makes sense for this application because the data is hex bolts, and we want to modify to be able to identify a part in a variety of positions.

    To run this code, click the Run Section button. And after that, you're ready for step 2, modifying the model.

    In step 1, we imported and made sure the data was ready for processing. We also augmented images to increase the quantity of training data. For step 2, we can now modify the network to work with the test data.

    Importing a model is fairly easy. You simply call the name of the network. In desktop, if you don't have the network already installed, a link will appear that you can easily download the network. In order to make the network work for new data, replace the last few layers with new layers specific to your data.

    For example, the final fully-connected layer needs to be replaced with 1 that has 2 as the output size, for the two classes of data you wish to classify. Also keep in mind that an app is available to do this in a more drag and drop fashion. There are videos specifically about Deep Network Designer for you to learn more. Click Run Section, and then you're ready for step 3, training the network.

    In the previous steps, we prepared the input data and the model for transfer learning. For step 3, we can use the data and the modified network to train a new image classifier. There are a variety of training options to choose.

    From here's a simple training configuration, including the optimizer, the number of epochs, and the initial learn rate. And finally, optionally, to show a plot when you're training. Training and model can take a lot of time depending on your GPU and CPU resources. If you would like to train the model, uncomment these lines of code and click Run Section. If you would like to skip the training, you can skip the section altogether and then import a trained network available for you to use in the next step.

    In step 3, you had the option to train a new model. Now we can load in the model and use the test data to see the accuracy of the model. Keep in mind that you want the test data to be data that you've never seen before, or that the model has never seen before, to make sure it's really learning the differences between the categories.

    Now in step 4, we want to use the test data that we separated out in step 1. Then you can classify all of the images in one line of code using the Classify function. After a few seconds you get all the results back, and you can quickly compare these against what the true values should be. The accuracy is a calculation of how many correct answers versus the total number of test samples. You can quickly read and classify individual images, and visualize the results.

    There are also visualization techniques that can help you understand what the network is seeing, and even add some special debugging to uncover why the network is working the way it is. For this example, we can use the Grab Cam function to quickly visualize where in the image the network found the defect. For more information on visualization techniques, you can search documentation for Deep Learning visualizations.