File Exchange

image thumbnail

Deep Learning: Transfer Learning in 10 lines of MATLAB Code

version (2.32 KB) by MathWorks Deep Learning Toolbox Team
Use transfer learning for training a deep neural network to classify 5 different types of food.


Updated 24 Feb 2017

View License

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

Learn how to use transfer learning in MATLAB to re-train deep learning networks created by experts for your own data or task.
This demo teaches you how to use transfer learning to re-train AlexNet, a pretrained deep convolutional neural network (CNN or ConvNet) to recognize snack food such as hot dogs, cup cakes and apple pie.
You can download the AlexNet support package here:
You can download the webcam support package here:

Cite As

MathWorks Deep Learning Toolbox Team (2020). Deep Learning: Transfer Learning in 10 lines of MATLAB Code (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (37)

Ali Dar

Can somebody tell me how I can use "classifyVideo" function in this example? Like it was shown at the end of this '' video.

Nai-Jen Liang

Can I train googlenet network on Transfer Learning in 10 lines of MATLAB Code?

Qinze Su

Jose Eraso

please I need the images, to be able to develop the example. thank you very much, my email is

Herren Thomas D'Souza

Mudasir JAMIL

Hello, I am using different transfer learning techniques like Resnet and Densenet on a large dataset (around 80000 pictures). My system is good, 32gb ram, gtx 1660ti and i7 9750h but it is taking 15-18 hours to complete 5 epochs. Is it normal for transfer learning to take this much time ? "Alexnet" completes the simulation in 30 mins for same dataset

iman yazdansepas

Where can I download the 5 categories food dataset?

Thank you


Salma Hassan

if i have 2 not equal sizes of classes( classe_1 = 50) and (classe_2=60)
is it necessary to determine the smallest amount of images in a category
tbl = countEachLabel(allimages);
minSetCount = min(tbl{:,2});
allimages = splitEachLabel(allimages, minSetCount);

Shounak Mitra

Hi Srinivas,

net = alexnet;
sz = net.Layers(1).InputSize
I = I(1:sz(1),1:sz(2),1:sz(3));

and let us know if it still doesn't work.

Refer to the following documentation and example in case you need more help.

Srinivas Satya

Error using trainNetwork (line 154)
Unexpected image size: All images must have the same size.

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

Caused by:
Error using nnet.internal.cnn.MiniBatchDatastoreDispatcher>iCellTo4DArray (line 386)
Unexpected image size: All images must have the same size.
this is my error even after converting all the images into 227*227 pixels i am getting this error and one more thing is i used 3 different sets with different no.of images

Sun Rover

Nasuno Hiroshi

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:

Wen Rei Do

@Alaa, I wrote my own classifyVideo.m file and it works, here you go

function I = classifyVideo(myNet)

cam = webcam;

while true
picture = cam.snapshot;
picture = imresize (picture,[227,227]);

label = classify(myNet, picture);



Any one help me to find a classifyVideo.m file Matlab file


Nice job here, but I need the classifyVideo.m file?

ridvan ozdemir

Thank you for such a great video, but I have a question.
Can we use 16 layers of AlexNet instead of using all layers it has?
And if yes, how can we do that? Can you help me please?

Frank-Michael Schleif

You can use the following script (under linux - adapt it a bit for file operations in windows)
to convert the files e.g. from caltech:

% to be place on top of myImages (with the respective images-classes as subfolders)
[status,res] = system('find . -name *.jpg'); % pick the images
resCell = regexp(res, char(10), 'split'); % to handle line breaks
resCell = resCell(1:end-1); % skip empty entry
for sFile = resCell
I = imread(sFile{1});
if(numel(size(I))==3) % remove non-rgb images
I = imresize(I,[227,227]); % rescale to alexnet format
sCommand = sprintf('rm %s',sFile{1});

taha khan

sir you do amazing work, but i have problem with training speed its too much slow ? and take lot of time ,any one tell me how too increase my training speed ?

Zoran Djordjevic

Why are images not supplied. What is the point of short example if one has to spend hours preparing data. This is just marketing.

fahad altayyar

i have done my treating and tested the acc but how can i bring up the web came to identify the objects that i trained plz help asap

Pappu Murthy

you need to provide the folders and images for this program to run so that we can also try to train and learn. Is there anyway to get these folders and images


%% Load Training Images
% In order for imageDataStore to parse the folder names as category labels,
% you would have to store image categories in corresponding sub-folders.
allImages = imageDatastore('C:\Users\rcham\Documents\seniorproj2\pics', 'IncludeSubfolders', true,'LabelSource', 'foldernames');

%% Split data into training and test sets
[trainingImages, testImages] = splitEachLabel(allImages, 0.8, 'randomize');

%% Load Pre-trained Network (AlexNet)
% AlexNet is a pre-trained network trained on 1000 object categories.
% AlexNet is avaliable as a support package on FileExchange.
alex = alexnet;

%% Review Network Architecture
layers = alex.Layers

%% Modify Pre-trained Network
% AlexNet was trained to recognize 1000 classes, we need to modify it to
% recognize just 5 classes.
layers(23) = fullyConnectedLayer(4); % change this based on # of classes
layers(25) = classificationLayer

%% Perform Transfer Learning
% For transfer learning we want to change the weights of the network ever so slightly. How
% much a network is changed during training is controlled by the learning
% rates.
opts = trainingOptions('sgdm', 'InitialLearnRate', 0.001,...
'MaxEpochs', 20, 'MiniBatchSize', 64);

%% Set custom read function
% One of the great things about imageDataStore it lets you specify a
% "custom" read function, in this case it is simply resizing the input
% images to 227x227 pixels which is what AlexNet expects. You can do this by
% specifying a function handle of a function with code to read and
% pre-process the image.

trainingImages.ReadFcn = @readFunctionTrain;

%% Train the Network
% This process usually takes about 5-20 minutes on a desktop GPU.
myNet = trainNetwork(trainingImages, layers, opts);

%% Test Network Performance
% Now let's the test the performance of our new "snack recognizer" on the test set.
testImages.ReadFcn = @readFunctionTrain;
predictedLabels = classify(myNet, testImages);
accuracy = mean(predictedLabels == testImages.Labels)

This is what I've got. The only thing I changed was giving the full path to the folder file that contains the subfolders of images


Is there anyone who could help me understand this error. Error using (line 274)
Function readFunctionTrain does not exist.

Error in Untitled20 (line 38)
trainingImages.ReadFcn = @readFunctionTrain


Group Six

Is there a way you can post how you changed the location of where the alex net picks up the images? and how should those folders be categorized when splitting into training and testing ?

Farzan Majeed Noori

Getting this answer and error..
anyone can help?

25x1 Layer array with layers:

1 'data' Image Input 227x227x3 images with 'zerocenter' normalization
2 'conv1' Convolution 96 11x11x3 convolutions with stride [4 4] and padding [0 0 0 0]
3 'relu1' ReLU ReLU
4 'norm1' Cross Channel Normalization cross channel normalization with 5 channels per element
5 'pool1' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0]
6 'conv2' Convolution 256 5x5x48 convolutions with stride [1 1] and padding [2 2 2 2]
7 'relu2' ReLU ReLU
8 'norm2' Cross Channel Normalization cross channel normalization with 5 channels per element
9 'pool2' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0]
10 'conv3' Convolution 384 3x3x256 convolutions with stride [1 1] and padding [1 1 1 1]
11 'relu3' ReLU ReLU
12 'conv4' Convolution 384 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1]
13 'relu4' ReLU ReLU
14 'conv5' Convolution 256 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1]
15 'relu5' ReLU ReLU
16 'pool5' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0]
17 'fc6' Fully Connected 4096 fully connected layer
18 'relu6' ReLU ReLU
19 'drop6' Dropout 50% dropout
20 'fc7' Fully Connected 4096 fully connected layer
21 'relu7' ReLU ReLU
22 'drop7' Dropout 50% dropout
23 '' Fully Connected 5 fully connected layer
24 'prob' Softmax softmax
25 '' Classification Output crossentropyex
Training on single CPU.
Initializing image normalization.
| Epoch | Iteration | Time Elapsed | Mini-batch | Mini-batch | Base Learning|
| | | (seconds) | Loss | Accuracy | Rate |
| 1 | 1 | 21.41 | 2.1046 | 12.50% | 0.0010 |
| 20 | 20 | 622.74 | 0.0003 | 100.00% | 0.0010 |
Error using (line 274)
Expected ReadSize to be a scalar with value >= 1.

Error in nnet.internal.cnn.ImageDatastoreMiniBatchDatasource/set.MiniBatchSize (line 110)
self.imds.ReadSize = batchSize;

Error in nnet.internal.cnn.ImageDatastoreMiniBatchDatasource (line 37)
self.MiniBatchSize = miniBatchSize;

Error in nnet.internal.cnn.DataDispatcherFactory>iCreateImageDatastoreMiniBatchDatasource (line 118)
ds = nnet.internal.cnn.ImageDatastoreMiniBatchDatasource( inputs, miniBatchSize );

Error in nnet.internal.cnn.DataDispatcherFactory.createDataDispatcher (line 57)
datasource = iCreateImageDatastoreMiniBatchDatasource( inputs, miniBatchSize );

Error in SeriesNetwork>iDataDispatcher (line 1108)
dispatcher = nnet.internal.cnn.DataDispatcherFactory.createDataDispatcher( ...

Error in SeriesNetwork/predict (line 328)
dispatcher = iDataDispatcher(data, miniBatchSize, precision, ...

Error in SeriesNetwork/classify (line 559)
scores = this.predict( X, varargin{:} );

Error in TransferLearningVideo (line 20)
predictedLabels = classify(myNet, testImages)

jy w

jy w

Josiah Laivins

Can you actually post a folder with the 5 different foods? I dont really want to have to manually download and resize 1000 images of food, and I saw you literally have a folder with the images. Also I would like to have similar results to you, so having your images can make this tutorial better. Could you post that?


I have these errors how can I fix it ? could you please help me?

Error using trainNetwork>iAssertThatTrainingDataHasValidSizeForTraining (line 157)
The training images are of size 480x640x3 but the input layer expects images of size 227x227x3.

Error in trainNetwork (line 89)
iAssertThatTrainingDataHasValidSizeForTraining(internalLayers{1}, dispatcher);


tao fuyu

how many images do I need for training each category?

Manish Sahu

What kind of error are you getting Muhammad Rafiq?
I am using GT740m and its working fine.

Muhammad Rafiq

Using NVidia GT 710, CUDA Enabled, Compute Capability 3.5
NN and Parallel toolbox installed and working independently.
Images are 227x227 JPG format divided in 3 classes.

please help me in this regard.

Gaetano Roggia

Hi, I would need to detect human faces with open/closed eyes. Would this algorithm be able to classify that?

Shyamal Patel

You need images of 5 different types of food to run this example. Check out this video for more details:

Branislav Pazicky

I have an error: The output size (5) of the last layer doesn't match the number of classes (1).
Can you help me ?


Updated icon image.

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