MathWorks - Mobile View
  • 碻形冰暨硞 MathWorks 帐憷碻形冰暨硞 MathWorks 帐憷
  • Access your MathWorks Account
    • 我的帐户
    • 我的社区资料
    • 关联许可证
    • 登出
  • 产品
  • 解决方案
  • 学术
  • 支持
  • 社区
  • 活动
  • 获取 MATLAB
MathWorks
  • 产品
  • 解决方案
  • 学术
  • 支持
  • 社区
  • 活动
  • 获取 MATLAB
  • 碻形冰暨硞 MathWorks 帐憷碻形冰暨硞 MathWorks 帐憷
  • Access your MathWorks Account
    • 我的帐户
    • 我的社区资料
    • 关联许可证
    • 登出

视频与网上研讨会

  • MathWorks
  • 视频
  • 视频首页
  • 搜索
  • 视频首页
  • 搜索
  • 联系销售
  • 试用软件
  Register to watch video
  • Description
  • Full Transcript
  • Code and Resources

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

From the series: Deep Learning with MATLAB

Joe Hicklin, MathWorks

Use MATLAB® for transfer learning, and see how it is a practical way to apply deep learning to your problems.

This demo uses transfer learning to retrain AlexNet, a pretrained deep convolutional neural network (CNN or ConvNet), to recognize snack foods such as hot dogs, cupcakes, and apple pie.

Recorded: 8 Feb 2017

Hi. My name is Joe Hicklin. I'm a senior developer at the MathWorks. I'm going to show you how to do transfer learning. Transfer learning can be a very practical way to apply deep learning to your problems.

With transfer learning, you take a preexisting neural net, modify it slightly, and then retrain it on your images. This can produce excellent results and is far, far easier than designing a network from scratch and training it yourself.

In my work, I need to be able to distinguish hamburgers from hot dogs and cupcakes and apple pie and ice cream. As far as I know, there's no network that'll do that for me. So I'm going to start with a preexisting network, Alex net. Alex net's been trained to classify 1,000 different kinds of images, and it's been trained on over a million images already.

So here I am. I'm going to start out loading Alex net, and I'm going to get the layers out of it so I can see the parts. If you look down here, you can see that Alex net has 25 layers. Most of the layers are doing useful image processing things that'll work for my system as well as for Alex net's. I'm going to leave those alone.

But the 23rd layer has 1,000 neurons in it, because Alex net classifies 1,000 different images. I'm only going to do five different kinds of images, so I'm going to replace that with a network that only has five images. Finally, I'm going to replace the output layer as well. The last layer of Alex net has learned Alex net's classifications, those 1,000 different classes. I don't want that. I'm going to replace it with an empty layer that's going to learn mine.

So now I've got my network set up. It's time to deal with the data. You don't need a million images like Alex net was trained on, but you do need 1,000 of them to get good results. I've made a folder with five subfolders in it, one for each of my classes. So there's one called Apple Pie, one called Cupcakes, and so on. And inside each of these folders are 1,000 images of the appropriate topic.

I've sized these images to be the size Alex net expects, 227 by 227, and you'll have to do that, too. If you arrange your data like this, you can use MATLAB's image data store object, because it understands that structure, and it will load all the images and label them appropriately for you. So that's what I'm doing here.

As soon as I've got my images, I need to separate them into two sets. Most of them I'll use for training, but I'll save a few of them out to test for accuracy later. So let's do that. Now I'm all set to train my network. I've got to set up a few network parameters here. I've chosen parameters that are going to work well.

You can change these if you like and see what happens. And then I'm ready to train the network. That started. That's going to take five or six minutes to do its job. I have a fairly powerful GPU in my computer, so it's pretty quick. Your mileage may vary. All right, the network's done training. The first thing we're going to do now is see how accurate it is.

We're going to ask the network to classify the test images, the images we left out of our training set. And then we're just going to see what fraction of those it gets right. We were 84% accurate. Pretty good for five minutes of work. Let's try it now with the webcam on some real food. I just happened to have some food on my desk. There's hamburgers, apple pie, hot dogs, ice cream.

So overall, it works pretty good, and it's fairly robust for a lot of these. Different angles and stuff. So there we go. That worked better than I expected, really. I simplified this demo as much as I could, but in the download, we'll include a second file that'll have a lot more comments, and it'll have some more code to handle some situations that might arise.

I've showed you how to do classification with transfer learning, but if you need real numbers out, you can also do regression with transfer learning. Well, I hope I've shown you enough to get you interested in transfer learning, so grab some snacks and give it a go.

Download Code and Files

Download code

Related Products

  • Deep Learning Toolbox
  • MATLAB

Learn More

Introduction to Deep Learning (3 Videos)
Deep Learning with MATLAB (Ebook)
Practical Deep Learning Examples with MATLAB (Ebook)
Get Ready for AI with MATLAB (Article)

FREE EBOOK

Introducing Deep Learning with MATLAB

Download ebook

DOWNLOAD CODE

Get the example code used in this video

Feedback

Featured Product

Deep Learning Toolbox

  • Request Trial
  • Get Pricing

Up Next:

Use MATLAB for configuring, training, and evaluating a convolutional neural network for image classification.
5:12
Training a Neural Network from Scratch with MATLAB
View full series (5 Videos)

Related Videos:

7:35
Deep Learning for Computer Vision with MATLAB (Highlights)
2:27
How to Plot Multiple Lines on the Same Figure
4:15
Transfer Functions in MATLAB
45:02
Teaching Fluid Mechanics and Heat Transfer with Interactive...
5:04
Teaching Heat Transfer Using MATLAB Apps

View more related videos

MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web site

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • 联系销售
  • 试用软件

了解产品

  • MATLAB
  • Simulink
  • 学生版软件
  • 硬件支持
  • 文件交换

试用或购买

  • 下载
  • 试用软件
  • 联系销售
  • 定价和许可
  • 如何购买

如何使用

  • 文档
  • 教程
  • 示例
  • 视频与网上研讨会
  • 培训

获取支持

  • 安装帮助
  • MATLAB 问答社区
  • 咨询
  • 许可中心
  • 联系支持

关于 MathWorks

  • 招聘
  • 新闻室
  • 社会愿景
  • 联系销售
  • 关于 MathWorks

MathWorks

Accelerating the pace of engineering and science

MathWorks 公司是世界领先的为工程师和科学家提供数学计算软件的开发商。

发现…

  • Select a Web Site United States
  • 专利
  • 商标
  • 隐私权政策
  • 防盗版
  • 应用状态

京ICP备12052471号

© 1994-2021 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Weibo
  • WeChat

    WeChat

  • LinkedIn
  • RSS

关注我们