Machine Learning Workshop I
Image Classification
Tao Chen
taochen@qnap.com
關於講師
陳雲濤 Tao Chen
taochen@qnap.com
Software Engineer,
Data Intelligence & Application Div., QNAP
Experience on IoT with computer vision, image processing,
image classification, object detection & segmentation.
課前準備
● 本課程會實作一個客製化的照片分類模型
● 請大家準備兩種類別照片,每種至少 50 張以上,分別存放在兩個資料夾中
● 適合對象:
1. 未接觸過 Machine Learning 的開發者
2. 有使用ML相關經驗,想認識 Deep Learning 方法者
3. 有興趣的想實作自己照片分類器的 QNAPers
課程大綱
● Introduction to Machine Learning
● Neural Network
● Deep Neural Network (DNN)
● Convolutional Neural Network (CNN)
● Image Classification on Fashion MNIST Dataset
● Prepare Your Data for Image Classification
● Train Your Image Classification Model
Introduction to Machine Learning
Introduction to Machine Learning
● Traditional:
Data
Data
● Machine Learning:
Output
Machine Learning ≈ Looking for a Function
this is a cat
Training Testing
“cat”
“not a
cat”
You write the
program for learning
Ref: 出自台大電機系李宏毅老師 Machine Learning 課程投影片
Type of Machine Learning Task
● Classification → output class
● Regression → output value
“cat”
影像辨識
88.87mm
本月總降雨量預測
中央氣象局觀測資料
Goodness of Machine Learning Function
● Classification → output class
● Regression → output value
“cat”
88.87 mm中央氣象局觀測資料 87.00 mm
“cat”
Ground Truth
evaluate by accuracy
evaluate by MSE (mean
squared error)
Machine Learning Process
“cat”
Ref: 出自台大電機系李宏毅老師 Machine Learning 課程投影片
Performance Evaluation (Accuracy)
● 假設100個病患使用某模型做疾病檢測
○ 檢測有病為 Positive
○ 檢測沒病為 Negative
● 前提(病患之真實狀況)
○ 15 個病患有病
○ 85 的病患沒病
● 某模型之檢測結果
○ 20 個病患檢測有病 (其中只有 10 個病患真的有病,10 個誤判)
○ 80 個病患檢測沒病 (其中只有 75 個病患真的沒病,5 個誤判)
檢測有病 檢測沒病
真實有病 10 5
真實沒病 10 75
Accuracy: (10+75)/100 = 85%
Precision, Recall and F1-score
對的說成對的
錯的說成錯的
錯的說成對的
對的說成錯的
● 假設100個病患使用某模型做疾病檢測
○ 檢測有病為 Positive
○ 檢測沒病為 Negative
● 前提(病患之真實狀況)
○ 15 個病患有病
○ 85 的病患沒病
● 某模型之檢測結果
○ 20 個病患檢測有病 (其中只有 10 個病患真的有病,10 個誤判)
○ 80 個病患檢測沒病 (其中只有 75 個病患真的沒病,5 個誤判)
檢測有病 檢測沒病
真實有病 10 5
真實沒病 10 75
→ TP: 10, TN: 75, FP: 10, FN: 5
→ Precision: 10/(10+10)= 50%, Recall: 10/(10+5)= 66.66%
→ F1-score: 2*0.5*0.66/(0.5+0.66) = 57.14%
Accuracy: (10+75)/100 = 85%
Performance Evaluation (F1-score)
from:http://speech.ee.ntu.edu.tw/~tlkagk/courses/ML_2017_2/Lecture/introduction.pdf
NN = Neural Network
(one kind of ML function)
Neural Network
from cs231n:http://cs231n.stanford.edu/
Basic NN
input Activation
Function
bias
weight
z = input(s) * weight(s) + bias
from NTU ML course:https://www.youtube.com/watch?v=Dr-WRlEFefw
2
-3
0.5
0.5
0.6
0.5
0.1
0.6
0
0.3
0.6
0.4
0.15
0.2
0.1
0.5
Activation Function f :
0.4
0.5
f(-0.6)
f(0.3)
input
input
weight
bias
0.27
0.38
f(0.27)
f(0.38)
0.217
(output)
Basic NN
[ 2 ✕ 0.4 + (-3) ✕ 0.5 ] + 0.1 =
-0.6
[ 2 ✕ 0.6 + (-3) ✕ 0.5 ] + 0.6 =
0.3
from cs231n:http://cs231n.stanford.edu/
Activation Functions
2
-3
0.5
0.5
0.6
0.5
0.1
0.6
0
0.3
0.6
0.4
0.15
0.2
0.27
0.38
0.217
0.4
0.5
0.1
0.5
f(-0.6)
f(0.3)
f(0.27)
f(0.38)
0.3
Ground
Truth
Judge by Loss function
e.g. MSE
Loss value = MSE(0.217, 0.3) = 0.006889
Training NN
new
bias
new
bias
new
bias
new
bias
0.217
new
weight
0.3
Ground
Truth
Judge by Loss function
e.g. MSE
Loss value = MSE(0.217, 0.3) = 0.006889
Update Weights and Biases with Loss value
new
weight
new
weights
new
weights
Learn by
next data
Training NN
(By Backpropogation Algorithm)
2
-3
0.5
0.5
0.6
0.5
0.1
0.6
0
0.3
0.6
0.4
0.15
0.2
0.27
0.38
0.308
0.4
0.5
1
0.5
f(-0.6)
f(0.3)
f(0.27)
f(0.38) 0.71
2
0.1
Ground T.
0
1
Cross Entropy Loss
dog
cat
Sleep
Walk
with U
Basic NN (Classification)
from cs231n:http://cs231n.stanford.edu/
圖片出處:http://lucenaresearch.com/deep-neural-networks/
DNN = Deep Neural Network
Ref : https://achintavarna.wordpress.com/2017/11/17/keras-tutorial-for-beginners-a-simple-neural-network-to-identify-numbers-mnist-data/
…
0
0
0
0
0
0
0
0
1
0
count loss with Ground T.
Update Weights and Biases with Loss value
0.04
0.01
0.02
0.11
0.01
0.06
0.21
0.01
0.35
0.18
CNN = Convolutional Neural Network
Basic Architecture of CNN
Ref: 出自台大電機系李宏毅老師 Machine Learning 課程投影片
Convolution
Max Pooling
Flatten
特徵擷取
Feature Extraction
降維度
Reduce Resolution
將所有擷取到的特徵攤
平
→ 降為一維向量
to one-dimension
Convolution
Ref: 出自台大電機系李宏毅老師 Machine Learning 課程投影片
Convolution
Ref: 出自台大電機系李宏毅老師 Machine Learning 課程投影片
● Convolution Filters 的長相
Convolution
Ref: https://medium.com/@RaghavPrabhu/understanding-of-convolutional-neural-network-cnn-deep-learning-99760835f148
from Stanford CS231n CNN: http://cs231n.github.io/convolutional-networks/
from cs231n:http://cs231n.stanford.edu/
Pooling
Ref: https://stats385.github.io/poolinglayers
Pooling
Ref: https://medium.com/@RaghavPrabhu/understanding-of-convolutional-neural-network-cnn-deep-learning-99760835f148
Convolutional Neural Network
64 convolution filters
pooling
R, G, B
Ref: https://hk.saowen.com/a/d2cde0a5e42606aae41a53e7ddb55ad7eb3208dc4cd218067344246a9a8ebf54
Convolutional Neural Network
● Understanding Neural Networks
Through Deep Visualization
Frameworks
Image Classification on Fashion MNIST Dataset
Environment Setting on Google Colab
● Google Colab:
https://colab.research.google.com/notebooks/welcome.ipynb#recent=true
● 文件 -> 新建 python3 Notebook
● 修改 -> 筆記本設置 -> GPU
Fashion MNIST Dataset
● Fashion MNIST Github
○ Total images 70000 (training dataset 60000, testing dataset 10000)
○ Resolution 28x28
○ Gray-scale
○ 10 classes:
Lab #1: Train DNN Model on Fashion MNIST
● Colab Notebook:
https://colab.research.google.com/drive/174_6xXG1up-GD6Ui
rXYMzMJYxyCdaPSD
Lab #2: Train CNN Model on Fashion MNIST
● Colab Notebook:
https://colab.research.google.com/drive/1eXKrKBe0lbsV9Ex6
amryF0Pml8QsySqY
Prepare Your Data for Image Classification
Prepare Data
Sample data:
https://drive.google.com/drive/folders/1-Wc10SNWg8UZ77wTmIs9JU2x_UYWvpI5?usp=s
haring
Train Your Image Classification Model
Lab #3: Train Your Image Classificaiton Model
● Colab Notebook:
https://colab.research.google.com/drive/1Sct4c6UUse5heNgF
fgdP-MckjCJ3umFM
照片辨識PK - 你能比 AI 準嗎?
● ImageNet Validation Set Competition
https://cs.stanford.edu/people/karpathy/ilsvrc/
Thank You

Machine learning workshop I - image classification