Gemeinsames Programmieren von Bilderkennung

Erfahren Sie, wie Sie Daten laden und vorverarbeiten, ein Netz importieren und dann mit Bildern auf Deep Learning testen.

To follow along:

  1. Download the code
  2. Open in MATLAB

Don’t have access?

Time to Complete:
15–30 minutes
Level:
Beginner/Intermediate

Need a refresher? Try a free, interactive tutorial.

Step 1

Load and Preprocess Data

Load the data
  • Import your data and make sure it’s ready to use.
  • When training a model, you want to split your data into “training” to train the model and “testing data” to test the model works on new data. 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. 
Load the data
  • To run the code, click “Run section” and after that your data is ready for Step 2.

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.

Key things to remember:

  • In Deep Learning, you may be dealing with a lot 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 home to your data throughout these tasks and any future Deep Learning project you do with 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.

Test the Network
Test the Network
  • Run the classify command to test all of the images in your test set and display the accuracy—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.

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.

Key things to remember:

  • It is important to validate the model works on future data and not the Deep Learning data that it learned during training to ensure that you have a flexible model that will work in new circumstances. 

You can also follow along with the video below:

Die Länge des Videos beträgt 4:13.

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

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