File Exchange

image thumbnail

CNN / Transfer Learning Example

version (6.15 KB) by Eiji Ota
Classify two similar flowers, "Dandelion" and "Colt's Foot" using CNN


Updated 06 Aug 2018

View License

Usually training CNN costs us a lot of time and GPU cycles. One key technique to avoid this type of cost is "transfer learning". This example shows how we can try "transfer learning" using MATLAB. We combine pretrained model (alex net) and SVM to classify two similar flowers, "Dandelion" and "Colt's Foot".
通常CNNの学習には膨大な計算時間と計算コストがかかります。こうしたコストを避けるひとつの方法に転移学習と呼ばれる方法があります。このサンプルでは、よく似た2種類の花、タンポポとフキタンポポを学習済みのモデル(Alex Net)と SVM を組み合わせて見分けます。

Cite As

Eiji Ota (2020). CNN / Transfer Learning Example (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (18)

Eiji Ota



R2018aより activations() の既定の戻り値の仕様が変更されています。
下記例のように activations() 実行時に 'OutputAs' オプションを付けると R2018a上でのエラーを回避できます。

X = activations(convnet, imds, featureLayer, 'MiniBatchSize', 1);

X = activations(convnet, imds, featureLayer, 'MiniBatchSize', 1,'OutputAs','rows');

Eiji Ota

ヘルプに割とわかり易い例がありましたので、改めて準備をしなかったのですが、例えば関数 trainRCNNObjectDetector のところにある例などはいかがでしょうか。

- Tei

画像のためのディープラーニング(深層学習) ~ CNN/R-CNN による物体の認識と検出 ~

Eiji Ota

If you want to try another type of transfer learning, you can find the codes here.

In this type of transfer learning, you have to modify and retrain network. But it would show better performance if retraining is successful.

If you need more performance, trying VGG16, VGG19 would be another option.

liu lin

I think it is transfer learning ,just use the net
to train the classifier,but the net is not changed,so the effect is not very good.

Hyun K. Suh

Eiji Ota

I use classifier called SVM. I trained SVM to adopt new classification task.

Original Alex Net are supposed to classify images into 1000 categories. But this demo tries to classify images into 2 categories. These classification tasks are different.

Alex Net has lots of layers, and these layers have different roles. The several layers near input layers have a role like "feature extractor" and the layers near output layers have a role like classifier.

If you go through all layers, you just get 1000-dim vector, which means the probabilities of 1000 categories Alex Net predicts.

But in this demo, image doesn't go through whole network. It just gets activation of intermediate layer called 'fc7', which becomes 4096-dim feature vector.

You can see the whole layers like this :
>> convnet.Layers

And this demo classifies this feature vector into 2 categories using SVM.

In this case, I used Oxford flower dataset, but you can use any image database, and classify them without training CNN from scratch. Usually training CNN from scratch costs a lot of time and computer resources.

That's the strength of transfer learning.

Tolga Birdal

You can't, because you are not learning anything. You are also not doing any domain adaptation or transfer. All you do is to evaluate a pre-trained classifier on a new dataset.

Eiji Ota

Eiji Ota

I reused pretrained network called Alex Net for another classification task. This Alex Net is originally trained for classification task of Image Net. I think we can call it "transfer learning" if you reuse one trained network for another classification task.

Tolga Birdal

Sorry, how is this a transfer learning?

Eiji Ota

The url of mat-file was wrong. The right one is :

Eiji Ota


Eiji Ota

You can download these patches from here:

Eiji Ota

If you are using R2016a, please apply these patches:

Computer Vision System Toolbox : 1373603
Neural Network Toolbox : 1350931, 1353529

You can download these patches from here:


Fixed a bug related to activations function

Changed source codes to use support package for alexnet.

Added automatic setup script "setupScript.m".
Added Japanese readme file.
Added two sample scripts related to "anomaly detection" and "similar image search"

Modified readme file. The instruction in the readme file was wrong. Sorry!

Wrong : Create 2 Folders for 'Dandelion' and 'ColtsFoot' under 'ImageData'
Right : Create 2 Folders for 'Dandelion' and 'ColtsFoot' under 'ImageData/17Flowers'

Updated readme file. The url of mat-file was wrong. The right one is :

Modified the readme file. Please check bug report, if you have troubles with this demo.

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