Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Deep Learning in MATLAB

What Is Deep Learning?

Deep learning is a branch of machine learning that teaches computers to do what comes naturally to humans: learn from experience. Machine learning algorithms use computational methods to "learn" information directly from data without relying on a predetermined equation as a model. Deep learning is especially suited for image recognition, which is important for solving problems such as facial recognition, motion detection, and many advanced driver assistance technologies such as autonomous driving, lane detection, pedestrian detection, and autonomous parking.

Neural Network Toolbox™ provides simple MATLAB® commands for creating and interconnecting the layers of a deep neural network. Examples and pretrained networks make it easy to use MATLAB for deep learning, even without knowledge of advanced computer vision algorithms or neural networks.

What do you want to do?Learn More
Perform transfer learning to fine-tune a network with your dataStart Deep Learning Faster Using Transfer Learning

    Tip:   Fine-tuning a pretrained network to learn a new task is typically much faster and easier than training a new network.

Get pretrained networks to explore and use to classify imagesPretrained Convolutional Neural Networks
Create a new deep neural network to perform classification or regression

Create Simple Deep Learning Network for Classification

Train a Convolutional Neural Network for Regression

Train networks using data sets too large to fit in memoryTrain a Convolutional Neural Network Using Data in ImageDatastore
Train a network for object detectionDeep Learning, Object Detection and Recognition (Computer Vision System Toolbox)
Label your image data automatically based on folder names, or interactively using an app

Train a Convolutional Neural Network Using Data in ImageDatastore

Label Images for Classification Model Training (Computer Vision System Toolbox)

Visualize what features networks have learned

Deep Dream Images Using AlexNet

Visualize Activations of a Convolutional Neural Network

Train on CPU, GPU, multiple GPUs, in parallel on your desktop or on clusters in the cloudDeep Learning with Big Data on GPUs and in Parallel

To choose whether to use a pretrained network or create a new deep network, consider the scenarios in this table.

 Use a Pretrained Network for Transfer LearningCreate a New Deep Network
Training DataHundreds to thousands of labeled images (small)Thousands to millions of labeled images
ComputationModerate computation (GPU optional)Compute intensive (requires GPU for speed)
Training TimeSeconds to minutesDays to weeks for real problems
Model AccuracyGood, depends on the pretrained modelHigh, but can overfit to small data sets

Deep learning uses neural networks to learn useful representations of features directly from data. Neural networks combine multiple nonlinear processing layers, using simple elements operating in parallel and inspired by biological nervous systems. Deep learning models can achieve state-of-the-art accuracy in object classification, sometimes exceeding human-level performance.

You train models using a large set of labeled data and neural network architectures that contain many layers, usually including some convolutional layers. Training these models is computationally intensive and you can usually accelerate training by using a high performance GPU. This diagram shows how convolutional neural networks combine layers that automatically learn features from many images in order to classify new images.

Many deep learning applications use image files, and sometimes millions of image files. To efficiently access many image files for deep learning, MATLAB provides the imageDatastore function. Use this function to:

  • Automatically read batches of images for faster processing in machine learning and computer vision applications

  • Import data from image collections that are too large to fit in memory

  • Label your image data automatically based on folder names

Try Deep Learning in 10 Lines of MATLAB Code

This example shows how to use deep learning to identify objects on a live webcam using only 10 lines of MATLAB code. Try the example to see how simple it is to get started with deep learning in MATLAB.

  1. Run these commands to get the downloads if needed, connect to the webcam, and get a pretrained neural network.

    camera = webcam; % Connect to the camera
    net = alexnet;   % Load the neural network

    If you need to install the webcam and alexnet add-ons, a message from each function appears with a link to help you download the free add-ons using Add-On Explorer. Alternatively, see Neural Network Toolbox Model for AlexNet Network and MATLAB Support Package for USB Webcams.

    After you install Neural Network Toolbox Model for AlexNet Network, you can use it to classify images. AlexNet is a pretrained convolutional neural network (CNN) that has been trained on more than a million images and can classify images into 1000 object categories (for example, keyboard, mouse, coffee mug, pencil, and many animals).

  2. Run the following code to show and classify live images. Point the webcam at an object and the neural network reports what class of object it thinks the webcam is showing. It will keep classifying images until you press Ctrl+C. The code resizes the image for the network using imresize.

    while true
        im = snapshot(camera);       % Take a picture
        image(im);                   % Show the picture
        im = imresize(im,[227 227]); % Resize the picture for alexnet
        label = classify(net,im);    % Classify the picture
        title(char(label));          % Show the class label
        drawnow
    end

    In this example, the network correctly classifies a coffee mug. Experiment with objects in your surroundings to see how accurate the network is.

    To watch a video of this example, see Deep Learning in 11 Lines of MATLAB Code.

For next steps in deep learning, you can use the pretrained network for other tasks. Solve new classification problems on your image data with transfer learning or feature extraction. For examples, see Start Deep Learning Faster Using Transfer Learning and Train Classifiers Using Features Extracted from Pretrained Networks. To try other pretrained networks, see Pretrained Convolutional Neural Networks.

Start Deep Learning Faster Using Transfer Learning

Transfer learning is commonly used in deep learning applications. You can take a pretrained network and use it as a starting point to learn a new task. Fine-tuning a network with transfer learning is much faster and easier than training from scratch. You can quickly make the network learn a new task using a smaller number of training images. The advantage of transfer learning is that the pretrained network has already learned a rich set of features that can be applied to a wide range of other similar tasks.

For example, if you take a network trained on thousands or millions of images, you can retrain it for new object detection using only hundreds of images. You can effectively fine-tune a pretrained network with much smaller data sets than the original training data. Transfer learning might not be faster than training a new network if you have a very large dataset.

Transfer learning enables you to:

  • Transfer the learned features of a pretrained network to a new problem

  • Transfer learning is faster and easier than training a new network

  • Reduce training time and dataset size

  • Perform deep learning without needing to learn how to create a whole new network

For examples, see Transfer Learning Using AlexNet and Transfer Learning and Fine-Tuning of Convolutional Neural Networks.

Train Classifiers Using Features Extracted from Pretrained Networks

Feature extraction allows you to use the power of pretrained networks without investing time and effort into training. Feature extraction can be the fastest way to use deep learning. You extract learned features from a pretrained network, and use those features to train a classifier, for example, a support vector machine (SVM — requires Statistics and Machine Learning Toolbox™). For example, if an SVM trained using alexnet can achieve >90% accuracy on your training and validation set, then fine-tuning with transfer learning might not be worth the effort to gain some extra accuracy. You also risk overfitting the training data if you perform fine-tuning on a small dataset. If the SVM cannot achieve good enough accuracy for your application, then fine-tuning is worth the effort to seek higher accuracy.

For an example, see Feature Extraction Using AlexNet.

Deep Learning with Big Data on GPUs and in Parallel

Neural networks are inherently parallel algorithms. You can take advantage of this parallelism by using Parallel Computing Toolbox™ to distribute training across multicore CPUs, graphical processing units (GPUs), and clusters of computers with multiple CPUs and GPUs.

Training deep networks is extremely computationally intensive and you can usually accelerate training by using a high performance GPU. If you do not have a suitable GPU, you can train on one or more CPU cores instead. You can train a convolutional neural network on a single GPU or CPU, or on multiple GPUs or CPU cores, or in parallel on a cluster. Using GPU or parallel options requires Parallel Computing Toolbox.

You do not need multiple computers to solve problems using data sets too big to fit in memory. You can use the imageDatastore function to work with batches of data without needing a cluster of machines. However, if you have a cluster available, it can be helpful to take your code to the data repository rather than moving large amounts of data around.

Deep Learning Hardware and Memory ConsiderationsRecommendationsRequired Products
Data too large to fit in memoryTo import data from image collections that are too large to fit in memory, use the imageDatastore function. This function is designed to read batches of images for faster processing in machine learning and computer vision applications.

MATLAB

Neural Network Toolbox

CPUIf you do not have a suitable GPU, you can train on a CPU instead. By default, the trainNetwork function uses the CPU if no GPU is available.

MATLAB

Neural Network Toolbox

GPUBy default, the trainNetwork function uses a GPU if available. Requires a CUDA®-enabled NVIDIA® GPU with compute capability 3.0 or higher. Check your GPU using gpuDevice. Specify the execution environment using the trainingOptions function.

MATLAB

Neural Network Toolbox

Parallel Computing Toolbox

Parallel on your local machine using multiple GPUs or CPU coresTake advantage of multiple workers by specifying the execution environment with the trainingOptions function. If you have more than one GPU on your machine, specify 'multi-gpu'. Otherwise, specify 'parallel'.

MATLAB

Neural Network Toolbox

Parallel Computing Toolbox

Parallel on a cluster or in the cloudScale up to use workers on clusters or in the cloud to accelerate your deep learning computations. Use trainingOptions and specify 'parallel' to use a compute cluster. See Deep Learning in the Cloud.

MATLAB

Neural Network Toolbox

Parallel Computing Toolbox

MATLAB Distributed Computing Server™

Deep Learning in the Cloud

Try your deep learning applications with multiple high-performance GPUs on Amazon® Elastic Compute Cloud (Amazon EC2®). You can use MATLAB to perform deep learning in the cloud using Amazon EC2 with new P2 instances and data stored in the cloud. If you do not have a suitable GPU available for faster training of a convolutional neural network, you can use this feature instead. You can try different numbers of GPUs per machine to accelerate training and use parallel computing to train multiple models at once on the same data. You can compare and explore the performance of multiple deep neural network configurations to look for the best tradeoff between accuracy and memory use.

To help you get started, see this white paper that outlines a complete workflow: Deep Learning in the Cloud with MATLAB White Paper.

Related Topics

Was this topic helpful?