机器学习实战-SVM模型实现人脸识别

文章目录

SVM建模进行人脸识别案例

1、导包

首先,引导包裹。[en]First of all, guide the package.

from sklearn.decomposition import PCA
import numpy as np
from sklearn.svm import SVC
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from sklearn.model_selection import GridSearchCV

2、加载数据集

我们加载sknowlear为我们收集的面部数据。[en]We load the face data that sklearn has collected for us.


data = datasets.fetch_lfw_people(resize = 1, min_faces_per_person = 70)
data

查看结果:[en]View the results:

机器学习实战-SVM模型实现人脸识别

我们拿出数据并查看它:[en]We take out the data and view it:

X = data['data']
y = data['target']
faces = data['images']
target_names = data['target_names']
display(X.shape,y.shape,faces.shape,target_names)

运行结果:[en]Running result:

机器学习实战-SVM模型实现人脸识别

我们随机选择一个人的照片,并通过索引获取名字:[en]We randomly select a person’s picture and get the name through the index:


index = np.random.randint(0,1288,size = 1)[0]
face = faces[index]
name = y[index]
print(target_names[name])
display(face.shape)
plt.imshow(face, cmap = 'gray')

结果表明:[en]The results show:

机器学习实战-SVM模型实现人脸识别

3、直接使用SVM模型建模

针对原始数据量大、数据量大的特点,首先采用主成分分析方法对原始数据进行降维。[en]As the original data is very large and there is a large amount of data, we first reduce the dimension of the original data by PCA.

%%time

pca = PCA(n_components=0.95)
X_pca = pca.fit_transform(X)
display(X.shape,X_pca.shape)

结果表明:[en]The results show:

机器学习实战-SVM模型实现人脸识别

然后训练和预测约简数据集的结果:[en]Then train and predict the results of the reduced data set:

C表示惩罚系数,用于防止过拟合。让我们首先用缺省初始值测试性能。[en]C represents the penalty coefficient, which is used to prevent overfitting. Let’s first test the performance with the default initial value.

%%time

X_train,X_test,y_train,y_test, faces_train,faces_test = train_test_split(X_pca,y,faces)

svc = SVC(C = 1)
svc.fit(X_train,y_train)

print('训练数据的得分:',svc.score(X_train,y_train))
print('测试数据的得分:',svc.score(X_test,y_test))

y_pred = svc.predict(X_test)

结果表明:[en]The results show:

机器学习实战-SVM模型实现人脸识别

4、数据可视化

然后我们随机加载50张图像,并直观地查看预测结果:[en]Then we randomly load 50 images and visually view the prediction results:

plt.figure(figsize=(5 * 2, 10 * 3))
for i in range(50):
    plt.subplot(10,5,i + 1)
    plt.imshow(faces_test[i],cmap = 'gray')
    plt.axis('off')

    true_name = target_names[y_test[i]].split(' ')[-1]
    predict_name = target_names[y_pred[i]].split(' ')[-1]
    plt.title(f'True:{true_name}\nPred:{predict_name}')

结果表明:[en]The results show:

机器学习实战-SVM模型实现人脸识别
机器学习实战-SVM模型实现人脸识别
机器学习实战-SVM模型实现人脸识别

从结果来看,预测效果不是很好,红色方框选择的都是错误的名字,所以我们必须对原始性能进行优化。[en]From the results, the prediction effect is not very good, the red box selects all the wrong names, so we have to optimize the original performance.

5、网络搜索优化确定最佳性能

sklearn为我们集成好了网络搜索确定最佳性能的方法,只要吧要传进的参数填进去,它会为我们自动搭配获得最优参数。

%%time
svc = SVC()

params = {'C':np.logspace(-10,10,50),'kernel':['linear', 'poly', 'rbf', 'sigmoid'],'tol':[0.01,0.001,0.0001]}
gc = GridSearchCV(estimator = svc,param_grid = params,cv = 5)
gc.fit(X_pca,y)
gc.best_params_

结果表明:[en]The results show:

机器学习实战-SVM模型实现人脸识别

6、使用最佳性能SVM建模

综合以上结果,最优惩罚系数C为1.8420699693267165e-07,最优核函数类型为线性模型,最优精度为0.001。[en]From the above results, the optimal penalty coefficient C is 1.8420699693267165e-07, the optimal kernel function type is the linear model, and the optimal accuracy is 0.001.

在上述优化参数的基础上,对支持向量机进行了优化建模。[en]Based on the above optimal parameters, the SVM is optimized and modeled.

svc = SVC(C = 1.8420699693267165e-07,kernel='linear',tol = 0.001)
# 随机划分的
X_pca_train,X_pca_test,y_train,y_test, faces_train,faces_test = train_test_split(X_pca,y,faces)
svc.fit(X_pca_train,y_train)
print('训练数据得分:',svc.score(X_pca_train,y_train))
print('测试数据的得分:',svc.score(X_pca_test,y_test))

结果表明:[en]The results show:

机器学习实战-SVM模型实现人脸识别

从这一结果来看,与普通的支持向量机建模相比,优化后的支持向量机的得分有了显著的提高。[en]From this result, compared with the ordinary SVM modeling, the score of the optimized SVM is significantly improved.

7、优化后的数据可视化

plt.figure(figsize=(5 * 2, 10 * 3))
for i in range(50):
    plt.subplot(10,5,i + 1)
    plt.imshow(faces_test[i],cmap = 'gray')
    plt.axis('off')

    true_name = target_names[y_test[i]].split(' ')[-1]
    predict_name = target_names[y_pred[i]].split(' ')[-1]
    plt.title(f'True:{true_name}\nPred:{predict_name}')

结果表明:[en]The results show:

机器学习实战-SVM模型实现人脸识别
机器学习实战-SVM模型实现人脸识别
机器学习实战-SVM模型实现人脸识别
机器学习实战-SVM模型实现人脸识别

从优化结果看,虽然仍有错误的结果,但精度比原来的精度要高很多。[en]From the optimized results, although there are still wrong results, but the accuracy is much higher than the original accuracy.

8、完整代码

8.1未优化的完整代码

from sklearn.decomposition import PCA
import numpy as np
from sklearn.svm import SVC
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from sklearn.model_selection import GridSearchCV

data = datasets.fetch_lfw_people(resize = 1, min_faces_per_person = 70)
data

pca = PCA(n_components=0.95)
X_pca = pca.fit_transform(X)
display(X.shape,X_pca.shape)

X_train,X_test,y_train,y_test, faces_train,faces_test = train_test_split(X_pca,y,faces)

svc = SVC(C = 1)
svc.fit(X_train,y_train)

print('训练数据的得分:',svc.score(X_train,y_train))
print('测试数据的得分:',svc.score(X_test,y_test))

y_pred = svc.predict(X_test)

plt.figure(figsize=(5 * 2, 10 * 3))
for i in range(50):
    plt.subplot(10,5,i + 1)
    plt.imshow(faces_test[i],cmap = 'gray')
    plt.axis('off')

    true_name = target_names[y_test[i]].split(' ')[-1]
    predict_name = target_names[y_pred[i]].split(' ')[-1]
    plt.title(f'True:{true_name}\nPred:{predict_name}')

8.2优化后的完整代码

from sklearn.decomposition import PCA
import numpy as np
from sklearn.svm import SVC
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from sklearn.model_selection import GridSearchCV

data = datasets.fetch_lfw_people(resize = 1, min_faces_per_person = 70)
data

pca = PCA(n_components=0.95)
X_pca = pca.fit_transform(X)
display(X.shape,X_pca.shape)

svc = SVC()

params = {'C':np.logspace(-10,10,50),'kernel':['linear', 'poly', 'rbf', 'sigmoid'],'tol':[0.01,0.001,0.0001]}
gc = GridSearchCV(estimator = svc,param_grid = params,cv = 5)
gc.fit(X_pca,y)
gc.best_params_

svc = SVC(C = 1.8420699693267165e-07,kernel='linear',tol = 0.001)

X_pca_train,X_pca_test,y_train,y_test, faces_train,faces_test = train_test_split(X_pca,y,faces)
svc.fit(X_pca_train,y_train)
print('训练数据得分:',svc.score(X_pca_train,y_train))
print('测试数据的得分:',svc.score(X_pca_test,y_test))

plt.figure(figsize=(5 * 2, 10 * 3))
for i in range(50):
    plt.subplot(10,5,i + 1)
    plt.imshow(faces_test[i],cmap = 'gray')
    plt.axis('off')

    true_name = target_names[y_test[i]].split(' ')[-1]
    predict_name = target_names[y_pred[i]].split(' ')[-1]
    plt.title(f'True:{true_name}\nPred:{predict_name}')

Original: https://blog.csdn.net/weixin_56197703/article/details/125034100
Author: Aaron-ywl
Title: 机器学习实战-SVM模型实现人脸识别

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6026/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部