File Exchange

image thumbnail

CNN / Transfer Learning Example

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

37 Downloads

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 を組み合わせて見分けます。

Comments and Ratings (18)

Eiji Ota

Naoyaさん、コメントありがとうございます!コードの方もなるべく早く修正をかけさせて頂きたいと思います。

Naoya

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

<修正前>
X = activations(convnet, imds, featureLayer, 'MiniBatchSize', 1);

<修正後>
X = activations(convnet, imds, featureLayer, 'MiniBatchSize', 1,'OutputAs','rows');

Eiji Ota

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

https://www.mathworks.com/help/releases/R2017a/vision/ref/trainrcnnobjectdetector.html

- Tei

画像のためのディープラーニング(深層学習) ~ CNN/R-CNN による物体の認識と検出 ~
というビデオからのリンクでこのページに来たのですが、R-CNNのデモプログラムの公開はありませんか?

Eiji Ota

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

https://jp.mathworks.com/matlabcentral/fileexchange/61639-deep-learning--transfer-learning-in-10-lines-of-matlab-code

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.

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.

Sorry, how is this a transfer learning?

Eiji Ota

The url of mat-file was wrong. The right one is :
http://www.vlfeat.org/matconvnet/models/beta16/imagenet-caffe-alex.mat

Eiji Ota

日本語でのコメントも可能です。

Eiji Ota

You can download these patches from here:

http://jp.mathworks.com/support/bugreports/

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:

Updates

1.5.0.0

Fixed a bug related to activations function

1.4.0.0

Changed source codes to use support package for alexnet.

1.3.0.0

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

1.2.0.0

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'

1.1.0.0

Updated readme file. The url of mat-file was wrong. The right one is :
http://www.vlfeat.org/matconvnet/models/beta16/imagenet-caffe-alex.mat

1.1.0.0

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