首页 > 其他分享 >基于支持向量机和降维PCA的人脸识别实战

基于支持向量机和降维PCA的人脸识别实战

时间:2024-10-19 17:50:50浏览次数:3  
标签:机和 eigenface 人脸识别 target people 降维 lfw names test

公众号:尤而小屋
编辑:Peter
作者:Peter

大家好,我是Peter~

今天给大家介绍一个基于支持向量机SVM和PCA降维的人脸识别的实战案例,主要包含:

  • 人脸数据lfw数据集下载
  • PCA降维
  • 基于SVM的分类模型构建
  • 模型分类预测结果可视化

效果如下图:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

基于SVM和PCA算法的人脸识别

使用数据为fetch_lfw_people人脸数据集。数据集中每个人至少有一张图片,每张图片都对应不同的人。这个数据集的目标是训练一个分类器来识别不同的人。

官网地址:https://scikit-learn.org/1.5/modules/generated/sklearn.datasets.fetch_lfw_people.html

sklearn.datasets.fetch_lfw_people(
    data_home=None, 
    funneled=True, 
    resize=0.5, 
    min_faces_per_person=0, 
    color=False, 
    slice_=(slice(70, 195, None), slice(78, 172, None)), 
    download_if_missing=True, 
    return_X_y=False)

导入库

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
%matplotlib inline

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)
type(lfw_people)

sklearn.utils._bunch.Bunch

数据基本信息:

n_samples, h, w = lfw_people.images.shape

n_samples, h, w # 数据量、高度、宽度

(1288, 50, 37)

分离特征X和目标y

1、获取特征

数据特征X和特征数量:

X = lfw_people.data
n_features = X.shape[1]  # 特征数量
X[:2]


array([[0.99607843, 0.9973857 , 0.9908497 , ..., 0.37908497, 0.38823533,0.38169935],
[0.1503268 , 0.19607843, 0.1764706 , ..., 0.45882353, 0.44313726,0.53594774]], dtype=float32)
n_features 

1850

len(X)  # 样本量

1288

2、分离目标变量y

y = lfw_people.target
target_names = lfw_people.target_names
y

array([5, 6, 3, …, 5, 3, 5], dtype=int64)

target_names


array(['Ariel Sharon', 'Colin Powell', 'Donald Rumsfeld', 'George W Bush',
'Gerhard Schroeder', 'Hugo Chavez', 'Tony Blair'], dtype='<U17')

# 总类别数:
n_classes = target_names.shape[0]
n_classes

7

print("整体数据基本信息:")
print("样本量: %d" % n_samples)
print("特征数: %d" % n_features)
print("分类数: %d" % n_classes)

整体数据基本信息:
样本量: 1288
特征数: 1850
分类数: 7

数据切分train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

降维PCA

由于数据的特征过多,使用PCA算法进行降维:

n_components = 150

pca = PCA(n_components=n_components, # 选择150个主成分;从1850--->150
          svd_solver='randomized',
          whiten=True).fit(X_train)

生成降维后的数据:

new_lfw_people = pca.components_.reshape((n_components,h,w))
new_lfw_people.shape

(150, 50, 37)

X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)

SVM模型

训练

# 模型参数
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
             'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
# 网格搜索
clf = GridSearchCV(
    SVC(kernel='rbf', class_weight='balanced'), 
    param_grid
)
# 模型的训练
clf = clf.fit(X_train_pca,y_train)

模型的最佳参数组合:

print(clf.best_estimator_)

SVC(C=1000.0, class_weight=‘balanced’, gamma=0.005)

预测

y_pred = clf.predict(X_test_pca)
y_pred[:10]

array([3, 3, 6, 3, 3, 3, 4, 1, 3, 3], dtype=int64)

模型评估

对分类模型的效果进行评估:

# 分类报告
print(classification_report(y_test,y_pred,target_names=target_names))
                   precision    recall  f1-score   support

     Ariel Sharon       0.88      0.54      0.67        13
     Colin Powell       0.75      0.88      0.81        60
  Donald Rumsfeld       0.85      0.63      0.72        27
    George W Bush       0.86      0.97      0.91       146
Gerhard Schroeder       0.95      0.80      0.87        25
      Hugo Chavez       1.00      0.47      0.64        15
       Tony Blair       0.97      0.81      0.88        36

         accuracy                           0.85       322
        macro avg       0.89      0.73      0.79       322
     weighted avg       0.86      0.85      0.85       322
# 混淆矩阵
print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))

结果表示为:


[[  7   2   0   4   0   0   0]
[  1  53   2   4   0   0   0]
[  0   4  17   6   0   0   0]
[  0   4   0 142   0   0   0]
[  0   1   0   3  20   0   1]
[  0   5   0   2   1   7   0]
[  0   2   1   4   0   0  29]]

可视化

def plot_gallery(images, titles, h, w, n_row=3, n_col=4):
    """
    images: 图像数据
    titles:标题列表
    h:高度
    w:宽度
    n_row=3,n_col=4:图形的行列数
    """
    plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))  # 图像大小
    plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)  # 调整子图之间的距离
    
    for i in range(n_row * n_col):  # 遍历所有的子图
        plt.subplot(n_row, n_col, i + 1)
        plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)
        plt.title(titles[i], size=12)
        plt.xticks(())
        plt.yticks(())
def title(y_pred, y_test, target_names, i):
    """
    y_pred:预测值
    y_test:真实值
    target_names:名称列表
    i:索引值
    """
    pred_name = target_names[y_pred[i]].rsplit(' ', 1)[-1]
    true_name = target_names[y_test[i]].rsplit(' ', 1)[-1]
    return f'predicted: {pred_name} \ntrue: {true_name}'
target_names

array(['Ariel Sharon', 'Colin Powell', 'Donald Rumsfeld', 'George W Bush',
'Gerhard Schroeder', 'Hugo Chavez', 'Tony Blair'], dtype='<U17')
y_pred[:10]

array([3, 3, 6, 3, 3, 3, 4, 1, 3, 3], dtype=int64)

预测的标题列表:

prediction_titles = [title(y_pred, y_test, target_names, i) for i in range(y_pred.shape[0])]
prediction_titles[:5]

[‘predicted: Bush \ntrue: Bush’,
‘predicted: Bush \ntrue: Bush’,
‘predicted: Blair \ntrue: Blair’,
‘predicted: Bush \ntrue: Bush’,
‘predicted: Bush \ntrue: Bush’]

可视化效果:

plot_gallery(X_test, prediction_titles, h, w)

eigenface_titles = ["eigenface %d" % i for i in range(new_lfw_people.shape[0])]

eigenface_titles[:10]

[‘eigenface 0’,
‘eigenface 1’,
‘eigenface 2’,
‘eigenface 3’,
‘eigenface 4’,
‘eigenface 5’,
‘eigenface 6’,
‘eigenface 7’,
‘eigenface 8’,
‘eigenface 9’]

plot_gallery(new_lfw_people, eigenface_titles, h, w)

标签:机和,eigenface,人脸识别,target,people,降维,lfw,names,test
From: https://blog.csdn.net/qq_25443541/article/details/143079558

相关文章

  • 传统特征算法——人脸识别
    人脸识别是目前人工智能领域中成熟较早、落地较广的技术之一,广泛应用于手机解锁、支付验证、安防布控等多个领域。其核心在于通过特定的算法识别图像或视频中人脸的身份,这一过程的实现离不开特征算法的支持。以下是对人脸识别特征算法的详细介绍:一、人脸识别系统概述一个......
  • 【进阶OpenCV】 (14)-- 人脸识别 -- LBPH 算法
    文章目录LBPH算法一、基本思想二、LBPH算法步骤1.图像划分2.局部二值模式特征提取3.直方图统计4.特征向量生成5.相似度计算三、代码实现1.图像预处理2.创建一个LBPH的人脸识别器3.训练实例模型4.图像预测总结LBPH算法**LBPH(LocalBinaryPatternsHis......
  • jumpserver堡垒机部署 添加资产主机和MySQL数据库,图解过程
    jumpserver堡垒机部署JumpServer是广受欢迎的开源堡垒机,是符合4A规范的专业运维安全审计系统。JumpServer帮助企业以更安全的方式管控和登录所有类型的资产,实现事前授权、事中监察、事后审计,满足等保合规要求。下载软件包JumpServer-开源堡垒机-官网#官方网址......
  • 人脸识别调研
    项目列表开源项目语言/环境准确率(onLabeledFacesintheWild)备注ageitgey/face_recognitionPython,Dlib99.38%DocsDFacePython  facenetTensorFlow99.2%https://mp.weixin.qq.com/s/1kgbYScIujSjCRvfPGw0tgV......
  • 什么是降维?
    一、降维的概念    降维是一种减少数据集中特征数量的技术,目的是减少计算复杂性,同时尽量保留原始数据的重要信息。降维通常用于高维数据集,其中可能包含成千上万个特征。降维可以分为两类:一类是特征选择,指从现有特征中选择一个子集的过程,不创建新的特征;另一类是特征提......
  • 【关注可白嫖源码】人脸识别的实验课程 (案例分享)
    摘  要本研究设计和实现了一个人脸识别的实验课程系统。该系统以人脸识别技术为核心,结合SpringBoot框架的轻量级和快速开发优势,为教育实验课程提供了一个高效、便捷的解决方案。该系统利用人脸识别算法实现学生登录系统功能,提高了实验课程的安全性和管理效率。通过整合Spr......
  • “降维模糊C均值(PCA-FCM)”创新算法的聚类与可视化
    在这篇博客中,我们将探讨一个MATLAB代码示例,它展示了如何从Excel文件导入数据,进行模糊C均值(FCM)聚类,并通过2D和3D图形可视化聚类结果。让我们一步一步地深入这个过程!1.环境准备首先,我们需要清空工作环境,以确保没有旧变量干扰我们的结果。这可以通过以下几行代码实现:clear;cl......
  • vue3+ts中实现人脸识别拍照上传,要求自动人脸框固定居中,可自动拍照识别,也可手动拍照识
    效果图使用技术face-api.js,canvas1、npm安装face-api.jsnpminstallface-api.js2、下载face-api.js的models下载models放在\public\models目录3、创建face.vue组件<template> <divstyle="height:calc(100vh-140px);display:flex;justify-content:cente......
  • coreldraw2024注册机和破解补丁百度网盘
    ###......
  • python静默活体检测接口集成-人脸识别API-人脸认证
    静默活体检测是一种不需要用户主动配合(如眨眼、点头等)的活体检测技术,通常通过摄像头采集用户的人脸图像,结合计算机视觉与AI算法来判断用户是否为真人。这项技术被广泛应用于身份验证、金融交易安全、智能门禁等领域。确定接口供应商和接口规范在集成静默活体检测功......