Image Recognition Code-Along
Learn how to load and preprocess data, import a network, transfer learning, and test the network for deep learning with images.
Published: 9 Jan 2023
For step one, you want to import your 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 set in this example is intentionally small for demonstration purposes. The most important thing to remember in this step is imageDatastore, which will be the home to your data throughout the tasks and any future deep learning projects you do with images.
When training the model, you want to split your data into training to train the model, and testing data to test the model works on new data. And we'll use this in a later step. You can also visualize a random image from the set so you can get an idea of what the data looks like. To run the code, click Run Section, and after that, your data is ready for step two, importing the network.
In step one, you imported data for deep learning. For step two, you want to import the model and make sure the data is ready for your deep learning model. This is important for two reasons. One, your model will not work properly without the properly formatted data. And two, clean data is essential to highly accurate models. The most important thing to remember here is to make sure that your data is the right size, or else your model will error.
You have access to a lot of different deep learning models. Here we are using a popular model, GoogLeNet. For preprocessing, you can choose to alter the images in a number of ways. For example, we need to make sure the images are the right size expected by the deep learning model.
Finally, you can try a sample image to see what the model predicts the image to be. This is clearly the incorrect prediction because our model hasn't been trained to recognize this type of data, which we will resolve in the next task. So to run this code, click Run Section, and after that, your data is ready for step three, where we will use image recognition using GoogLeNet.
For step two, you ensure the data and the model are ready for training. Now, in step three, you want to modify the model to work with your specific data. This is important so you can customize deep learning to perform your specific task. The most important thing to remember here is deep learning can take a long time to train, especially with large data sets. Fortunately, because we're using a very small data set, this should only take about a minute or two to run, and you don't need special hardware for this to run.
To get the model ready for the new task, you need to replace the final fully connected layer with a new fully connected layer with the number of categories you have-- in our case, four. You also need to replace the final classification layer to reset this for your task. Now you're ready to train.
To run this code, be sure that you already have run tasks one and two, and then click Run Section. Training will take maybe about 60 seconds or a little bit longer, and then you'll have a newly trained model for a new task. Also, keep in mind that there's an app that you can use to make transfer learning easy. Check out Deep Network Designer for more information.
For step three, you trained a new deep learning model for a new task. In the final task, you want to verify the model works on new data that it's never seen before. This is important to validate the model works on future data and not the deep learning data that it learned during training.
The most important thing to remember here is you want to use different data than the training data to ensure that you have a flexible model that will work in new circumstances. You can run the classify command to test all of the images in your test set and display the accuracy of the test set-- in this case, 90%.
Finally, you can select images in your test set to visualize with the corresponding labels. You now have a fully working model customized to a new task. See the links below to continue with more image recognition tasks, or keep exploring deep learning with related applications.