Deep Learning with MATLAB: Transfer Learning in 10 Lines of Code - MATLAB
Video Player is loading.
Current Time 0:00
Duration 4:41
Loaded: 0.50%
Stream Type LIVE
Remaining Time 4:41
 
1x
  • Chapters
  • descriptions off, selected
  • captions off, selected
      Video length is 4:41

      Deep Learning with MATLAB: Transfer Learning in 10 Lines of Code

      From the series: Deep Learning with MATLAB

      Transfer learning is a deep learning approach in which a model that has been trained for one task is used as a starting point for a model that performs another similar task. Transfer learning is commonly used for image classification, computer vision, and natural language processing.

      Learn how to perform transfer learning in 10 lines of MATLAB code. More specifically, learn how to modify the pretrained GoogLeNet network to classify food images. MATLAB provides built-in functions that allow you to easily modify and retrain a pretrained network, achieving high classification accuracy. You will also learn how to use the same transfer learning workflow as part of a visual inspection system.

      Published: 12 Nov 2024

      Transfer learning is a deep learning approach which involves modifying pretrained network trained for one task to perform another similar task. By using transfer learning, you can retrain a model with less labeled data by using a model already trained on a large data set. Reduce the training time and required computing resources, since you don't need to retrain the weights of the earlier layers. And you can take advantage of existing model architectures developed by the AI community.

      Transfer learning is commonly used for image classification, computer vision like object detection, and natural language processing tasks. In this video, I'm going to show you how to perform transfer learning for image classification with MATLAB. This transfer learning workflow is quite standard, which means that you can apply it for many different tasks.

      You get a pretrained network. You modify it. And then you retrain it with a new data set. But you also want to check if it worked by testing the network on new data. With MATLAB, you only need 10 lines of code to perform transfer learning. Let's open MATLAB and see how.

      We create an image data store with labeled images of pizzas and hamburgers. An image data store allows you to manage and work with large collections of image files efficiently. We load the pretrained googlenet network, which is a convolutional neural network trained on over 1 million images and can classify images into a thousand categories.

      The imagePretrainedNetwork function lets you modify a pretrained network as you load it. So instead of classifying images to a thousand categories, you can adjust it to classify images in just two categories. This is particularly useful when adapting a large model for a specific task.

      Now let's freeze the weights of the network, keeping the last learnable layer unfrozen to speed up training. Then we prepare the training data, the images of pizzas and hamburgers, by first splitting the data set into training, validation, and test data sets. We augment the data to prevent overfitting.

      Then we specify the training options and train the network with the new data. This might take a minute, especially since I'm using my laptop CPU for training. Now we use a test set to check how well the network works after transfer learning. The confusion matrix shows good performance for both classes. And the overall accuracy is over 90%. To be comprehensive, we check how the network classifies four random images and the classification scores. We can now be quite confident that transfer learning worked.

      As an engineer or scientist, you're not very likely to want to classify food images. You're more likely to build a visual inspection system for classifying images of hex nuts into good and faulty. And transfer learning is an important step in that engineering workflow too. Let's get back to MATLAB.

      We can again use the pretrained googlenet network and customize the transfer learning workflow to a real-world example-- in this case, classifying hex nut images. The steps are the same, but of course, I need to retrain the network with images of hex nuts instead of food. And maybe I need to tweak the training options. But as you can see, classifying hex nuts is a bit trickier than pizza versus hamburger. The accuracy this time is over 80%, which can probably be increased with a larger training data set.

      That's how easy it is to perform transfer learning with MATLAB. So with MATLAB, you can have access to many popular pretrained networks for image classification and more. Use built-in functions to store and prepare your data. Modify and train the pretrained network. And visualize and assess the network's performance.

      We didn't get into the details. But with MATLAB, it's also very easy to integrate the transfer learning workflow into the design of a complex system. To continue your journey with transfer learning in AI, check out the related resources linked in the description below.

      Up Next:

      View full series (5 Videos)

      View more related videos