北科資工二多媒體技術與應用 第五周團隊作業 - 對兩類別影像進 PCA 降維後送入SVM分類器進行訓練進行辨識

筆記說明

此筆記用途在於台北科技大學資訊工程系大二下多媒體技術與應用作業紀錄
並非所有人都適用,部分對我而言稍加容易的內容並不會寫在此內。
這是學習後心得的筆記,可能不太適用會未來的學生

由於我沒有學習過裡面的理論,因此這是資工大二學生透過網路與自身理解的筆記,學習價值並不高、且可能擁有大量錯誤。

程式碼

直接透過程式碼來進行說明,相信會比較好理解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 8 12:28:01 2021

@author: User
"""
from time import time
import logging
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
from sklearn.svm import SVC

lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4) #接收 人臉資料
n_samples, h, w = lfw_people.images.shape #紀錄形狀

x = lfw_people.data #人臉全部資料
n_features = x.shape[1] #特徵

y = lfw_people.target #每一個人臉辨識的類別
target_names = lfw_people.target_names #type
n_classes = target_names.shape[0] #有幾種類別

print("Total dataset size:")
print("n_samples: %d" % n_samples)
print("n_features: %d" % n_features)
print("n_classes: %d" % n_classes)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)
# 創造出訓練集與切割集

n_components = 150 #降至 150 維度
print("Extracting the top %d eigenfaces from %d faces"
% (n_components, x_train.shape[0]))
t0 = time() #計時
pca = PCA(n_components=n_components, svd_solver='randomized',
whiten=True).fit(x_train) #訓練中,使用方法為 randomized
print("done in %0.3fs" % (time() - t0)) #輸出時間

#重新定義圖片大小
eigenfaces = pca.components_.reshape((n_components, h, w))

print("Projecting the input data on the eigenfaces orthonormal basis")
t0 = time()
x_train_pca = pca.transform(x_train) #進行降維
x_test_pca = pca.transform(x_test) #進行降維
print("done in %0.3fs" % (time() - t0))

# #############################################################################
# Train a SVM classification model

print("Fitting the classifier to the training set")
t0 = time()
#參數,用來告訴 gridsearchCV 的懲罰與誤差,是圖像決定好的
#param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
# 'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
'gamma': [0.01, 0.05, 0.01, 0.05, 0.01, 0.1], } #要給 SVC 的參數
clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)
fit = clf.fit(x_train_pca, y_train) #尋找最佳化
print("done in %0.3fs" % (time() - t0))
print("Best estimator found by grid search:")
print(clf.best_estimator_) #找到最佳參數組合及其準確率。

# #############################################################################
# Quantitative evaluation of the model quality on the test set

print("Predicting people's names on the test set")
t0 = time()
y_pred = clf.predict(x_test_pca) #預測
print("done in %0.3fs" % (time() - t0))

print(classification_report(y_test, y_pred, target_names=target_names))
# 輸出這次機器學習的結果,其中 f1-score 是學習成果中最重要的分數
# * precision 準確率,機器學習判定是正確圖片的準確率
# * recall 精準度,抽取出來的樣本,有多少樣本是有被視為正確的。
# 舉例:google 大衛的筆記,理論上要有 70 筆資料是關於大衛的筆記,卻只有輸出 40 筆,比例就是 40/70。
#print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))

參考連結

Faces recognition example using eigenfaces and SVMs by scikit-learn
sklearn.decomposition.PCA by scikit-learn
机器学习classification_report方法及precision精确率和recall召回率 说明 by cnblogs
sklearn.metrics.classification_report by scikit-learn
sklearn.model_selection.GridSearchCV by scikit-learn
sklearn.model_selection.train_test_split by scikit-learn

心得

機器學習裡面大量的數學公式與模型,我都還不懂,現在的我只是先按照著範例刻畫出正確的結果來,希望未來的我可以懂那些理論,因為我其實都不太懂這些理論。但是被要求做出此作業時,其實上手程度很難,某種情況來說算是壓力很大。

總之,希望我能夠至少大概理解怎麼運作,之後如果有機會在進行底層實作,我一定就能夠了解更多!
現在只能大概理解語法在幹嘛,而不能知道怎麼運作。

無註解程式碼

提供無註解程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 8 12:28:01 2021

@author: User
"""
from time import time
import logging
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
from sklearn.svm import SVC

lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)
n_samples, h, w = lfw_people.images.shape

x = lfw_people.data
n_features = x.shape[1]

y = lfw_people.target
target_names = lfw_people.target_names
n_classes = target_names.shape[0]

print("Total dataset size:")
print("n_samples: %d" % n_samples)
print("n_features: %d" % n_features)
print("n_classes: %d" % n_classes)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)

n_components = 150
print("Extracting the top %d eigenfaces from %d faces"
% (n_components, x_train.shape[0]))
t0 = time()
pca = PCA(n_components=n_components, svd_solver='randomized',
whiten=True).fit(x_train) #降維至 150 層
print("done in %0.3fs" % (time() - t0))

#重新定義圖片大小
eigenfaces = pca.components_.reshape((n_components, h, w))

print("Projecting the input data on the eigenfaces orthonormal basis")
t0 = time()
x_train_pca = pca.transform(x_train) #進行降維
x_test_pca = pca.transform(x_test) #進行降維
print("done in %0.3fs" % (time() - t0))

# #############################################################################
# Train a SVM classification model

print("Fitting the classifier to the training set")
t0 = time()
#參數,用來告訴 gridsearchCV 的懲罰與誤差,是圖像決定好的
#param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
# 'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
'gamma': [0.01, 0.05, 0.01, 0.05, 0.01, 0.1], }
clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)
fit = clf.fit(x_train_pca, y_train) #開始訓練,找最適合的
print("done in %0.3fs" % (time() - t0))
print("Best estimator found by grid search:")
print(clf.best_estimator_) #找到最佳參數組合及其準確率。

# #############################################################################
# Quantitative evaluation of the model quality on the test set

print("Predicting people's names on the test set")
t0 = time()
y_pred = clf.predict(x_test_pca) #預測
print("done in %0.3fs" % (time() - t0))

print(classification_report(y_test, y_pred, target_names=target_names))
#print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))
  • 版權聲明: 本部落格所有文章除有特別聲明外,均採用 Apache License 2.0 許可協議。轉載請註明出處!
  • © 2020-2024 John Doe
  • Powered by Hexo Theme Ayer
  • PV: UV: