File Exchange

image thumbnail

Deep Learning in 11 Lines of MATLAB Code

Use MATLAB®, a simple webcam, and a deep neural network to identify objects in your surroundings.


Updated 24 Feb 2017

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

MATLAB code associated with the demo in 'Deep Learning in 11 Lines of MATLAB Code' video.
This demo uses AlexNet, a pretrained deep convolutional neural network (CNN or ConvNet) that has been trained on over a million images.
The example has two parts: setting up the camera and performing object recognition. The first part shows how to use the webcam command to acquire images from the camera. Using the drawnow command, MATLAB is able to continuously update and display images taken by the camera.
You can download the webcam support package here:

The second part illustrates how to download a pretrained deep neural network called AlexNet and use MATLAB to continuously process the camera images. AlexNet takes the image as input and provides a label for the object in the image. You can experiment with objects in your surroundings to see how accurate AlexNet is.

You can download the AlexNet support package here:

Comments and Ratings (31)

I solved this problem! It works

I have errors

>> webcam_object_classification
Error using webcam (line 116)
No webcams have been detected. Please ensure the webcam is connected to the system.

Error in webcam_object_classification (line 5)
camera = webcam; % Connect to the camera

Is it possible to work with laptop built-in webcam? Or it works only with usb web-cams?

Han Wei

Niki rami

Hello everyone.
I was wondering if someone can help me with this matter.
I trained Alexnet for my pictures, I give it random images and it can detect them.
Now, I want to test it with a shot video. How should I modify the code to feed the video as a test? just to see how it works for video. I do not want to use the camera, I will use the shot video. Please feel free to contact me.
My email address:
my skype id:

Bowen Fang

Hairmon Di

xinxin hao

Fakhar Alam


jun liu

work very well.

wajih david

I am getting the that error while starting the TransferLearningVideo.m.

Yv Yu


hi, there is an error when i run the codes: CUDA driver version is insufficient for CUDA runtime version. And i want to know what kind of CUDA does Alexnet need?

renjith ms

I could ony classify key board
when mouse was shone it was being classified as something else

tanu gupta

cem sincap

I am getting the that error while starting the TransferLearningVideo.m. these errors ;

>> TransferLearningVideo

Error using trainNetwork (line 64)
The output size (5) of the last layer doesn't match the number of classes (1).

Error in TransferLearningVideo (line 17)
myNet = trainNetwork(trainingImages, layers, opts);


Thanks, Nice explanation!, please consider next time using less toolboxes that we need not to purchase.

Danh Lam

Undefined function or variable 'alexnet'.

Error in DeepLearning_in_11_Lines (line 3)
nnet = alexnet;


I tried running this example, but it does not work. It is trying to use classify for statistics toolbox and not net classify - can you please help me solve this issue?

I have nnet installed as well.

Error ----
> In alexnet (line 46)
In MyFirstDeepLearningTest (line 4)
Error using classify (line 122)
Requires at least three arguments.

Error in MyFirstDeepLearningTest (line 10)
label = classify(nnet, picture); % Classify the picture


@Anthony, this submission requires the Neural Network Toolbox. You may not have that toolbox installed.

I tried to run this code and it returns an error saying that "classify requires 3 parameters". What is the 3rd parameter?


Updated image.

Added link to demo video.

MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux