通用方法:几种人脸识别算法的原理概念及其代码特征

优采云 发布时间: 2022-10-17 00:10

  通用方法:几种人脸识别算法的原理概念及其代码特征

  1 简介

  随着面向对象编程语言的发展,掌握一门新的编程应用技术基本上有两个要求:

  可以理解大体原理:这可以帮助我们组织/修改/加入现有的代码块

  能够识别核心代码:这可以帮助我们调试代码性能以满足我们的需求

  因此,本文以“关键原理和概念”vs.“核心代码块的特征”的形式总结了几种人脸识别算法,以供回顾、改进和交流学习。

  需要注意的是:

  本文的主要内容是网络资源的有序排列。带下划线的文本有链接。

  我会在编程表达式上不准确和不专业,希望您能从中得到一点启发和便利。

  本文所有代码均来自网络,主要来自seathiefwang;康尼波;aymericdamien,其知识产权和附带的合法权利属于原作者。

  另外,在深度学习上,这种辩证的 文章 很有启发性。

  2.主要内容 2.1. 源码介绍及链接

  2.2. 基于线性回归的机器学习最简单的例子

  2.3 基于欧式距离的人脸识别

  2.4 Tensorflow组织的卷积神经网络架构

  2.5 完整的代码和特征代码块 2.1。源代码介绍及链接

  后面会有很多废话,先上源码。都经过我测试,可以按照如下配置运行:Win10 + Python 3.6 + Tensorflow 2.0,其他库按照pip默认版本安装即可。

  代码兼容性问题很大一部分来自于不同版本的 Tensorflow。对于基于Tensorflow 1.x编写的代码,我修改参考如下,使其在Tensorflow 2.0下运行。

  import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()

  (1)基于线性回归的机器学习最简单的例子:源码;备份我的修改

  数据集是自定义数组;

  简要展示了机器学习的训练和性能评估环节。

  #python迭代过程

step: 50, loss: 0.001438, W: 0.981427, b: 0.116864

step: 100, loss: 0.001171, W: 0.983242, b: 0.105449

step: 150, loss: 0.000953, W: 0.984879, b: 0.095148

step: 200, loss: 0.000776, W: 0.986356, b: 0.085853

step: 250, loss: 0.000632, W: 0.987689, b: 0.077467

step: 300, loss: 0.000514, W: 0.988891, b: 0.069899

step: 350, loss: 0.000419, W: 0.989976, b: 0.063071

step: 400, loss: 0.000341, W: 0.990956, b: 0.056910

step: 450, loss: 0.000278, W: 0.991839, b: 0.051351

step: 500, loss: 0.000226, W: 0.992636, b: 0.046334

step: 550, loss: 0.000184, W: 0.993356, b: 0.041808

step: 600, loss: 0.000150, W: 0.994005, b: 0.037724

step: 650, loss: 0.000122, W: 0.994590, b: 0.034039

step: 700, loss: 0.000099, W: 0.995119, b: 0.030714

step: 750, loss: 0.000081, W: 0.995596, b: 0.027714

step: 800, loss: 0.000066, W: 0.996026, b: 0.025006

step: 850, loss: 0.000054, W: 0.996414, b: 0.022564

step: 900, loss: 0.000044, W: 0.996764, b: 0.020360

step: 950, loss: 0.000036, W: 0.997080, b: 0.018371

step: 1000, loss: 0.000029, W: 0.997366, b: 0.016576

  训练好的回归曲线,系数a为0.997366,截距b为0.016576

  (2)基于欧式距离算法的人脸识别:源码;我的备份

  强烈推荐这段代码,很用心;

  写的很好,最近才上传,所以代码版本没有问题;

  基本功能:*敏*感*词*采集图片采集,然后通过*敏*感*词*识别人脸。

  技术路线图

  (3)基于Tensorflow库的人脸识别神经网络:源码;备份我的修改。

  作者把基于Tensorflow的神经网络架构写的非常简洁明了,难得的神经网络代码;

  是 2017 年的代码,Tensorflow 版本 1.15.0;

  人脸识别的准确性还有待确认,但这不是学习这段代码的重点;

  基本功能:*敏*感*词*采集图片采集,然后通过*敏*感*词*识别人脸。2.2. 基于线性回归的卷积神经网络

  机器学习这个话题下有一堆算法概念,让人眼花缭乱。这是一篇很好的文章 文章,与互联网上的中文资源相比,它全面而简单地列出了重要概念。链接地址。

  模式识别的本质是分类,单变量线性回归是分类方法之一。形象地说,线性回归算法就是找到最大程度、最大概率区分不同类别的最佳直线。

  在模式识别算法中,线性回归方程中的 (x) 及其系数 (a, b) 稍微复杂一些。

  例如,自变量 (x) 定义了一个人脸,但它被设置为 128 个元素的一维数组。实际意义是:用128个正交坐标来定义所有的面,所以一个面是一个128维的向量空间中的一个点,所有面的集合都收录在这个向量空间中。

  所以我觉得系数(a,b)自然应该是多维数组,用来在128维向量空间中划分不同的人脸子群。我想以这样一种方式来理解它们,因为数组可以收录大量信息,它们可以精确地区分面孔。因此,解决它们需要大量的数据来不断逼近最优和最现实的系数(a,b)。

  戴上猫脸一会儿...

  该求解过程采用迭代方法,定义迭代的 3 个参数控制回归算法的性能,即:

  学习率 = 0.01

  训练步数 = 1000

  显示步长 = 50

  并且回归曲线的性能在每次迭代中通过2个参数连续评估,即

  失利

  准确性

  在我看来,CNN 是一种泛化数据的策略和方法。在每一轮迭代中,不断压缩归纳样本的N维数组(取最大值或平均值),最终得到一个具有显着区分能力的一维数组:图像原理。教科书般的专家评论。

  CNN的调试和优化主要涉及几个概念:

  Sigmoid函数、Softmax函数、ReLU函数(Rectified Linear Unit,ReLU);

  CNN的基本配置:卷积层、Relu层、池化层;

  Tensorboard 工具:可视化神经网络性能变量,例如损失和准确性。

  2.3. 基于欧式距离的人脸识别

  以下是图片和文字。

  2.4. TensorFlow 组织的卷积神经网络架构

  同上,先看这里。

  2.5. 完整的代码和功能代码块

  (1)基于线性回归的机器学习最简单的例子

  # Parameters.

learning_rate = 0.01

training_steps = 1000

display_step = 50

  # Weight and Bias, initialized randomly.

W = tf.Variable(rng.randn(), name="weight")

b = tf.Variable(rng.randn(), name="bias")

# Linear regression (Wx + b).

def linear_regression(x):

return W * x + b

  # Run the optimization to update W and b values.

run_optimization()

if step % display_step == 0:

pred = linear_regression(X)

loss = mean_square(pred, Y)

print("step: %i, loss: %f, W: %f, b: %f" % (step, loss, W.numpy(), b.numpy()))

  from __future__ import absolute_import, division, print_function

import tensorflow as tf

import numpy as np

rng = np.random

# Parameters.

learning_rate = 0.01

training_steps = 1000

display_step = 50

# Training Data.

X = np.array([1,2,3,4,5,6,7,8,9])

Y = np.array([1,2,3,4,5,6,7,8,9])

n_samples = X.shape[0]

# Weight and Bias, initialized randomly.

W = tf.Variable(rng.randn(), name="weight")

b = tf.Variable(rng.randn(), name="bias")

# Linear regression (Wx + b).

def linear_regression(x):

return W * x + b

# Mean square error.

def mean_square(y_pred, y_true):

return tf.reduce_sum(tf.pow(y_pred-y_true, 2)) / (2 * n_samples)

# Stochastic Gradient Descent Optimizer.

optimizer = tf.optimizers.SGD(learning_rate)

# Optimization process.

def run_optimization():

# Wrap computation inside a GradientTape for automatic differentiation.

with tf.GradientTape() as g:

pred = linear_regression(X)

loss = mean_square(pred, Y)

# Compute gradients.

gradients = g.gradient(loss, [W, b])

# Update W and b following gradients.

optimizer.apply_gradients(zip(gradients, [W, b]))

# Run training for the given number of steps.

for step in range(1, training_steps + 1):

# Run the optimization to update W and b values.

run_optimization()

if step % display_step == 0:

pred = linear_regression(X)

loss = mean_square(pred, Y)

print("step: %i, loss: %f, W: %f, b: %f" % (step, loss, W.numpy(), b.numpy()))

import matplotlib.pyplot as plt

# Graphic display

plt.plot(X, Y, 'ro', label='Original data')

plt.plot(X, np.array(W * X + b), label='Fitted line')

plt.legend()

plt.show()

  (2) 基于欧式距离的人脸识别

  # 计算两个128D向量间的欧式距离

# Compute the e-distance between two 128D features

def return_euclidean_distance(feature_1, feature_2):

feature_1 = np.array(feature_1)

feature_2 = np.array(feature_2)

dist = np.sqrt(np.sum(np.square(feature_1 - feature_2)))

return dist

  # Author: coneypo

# Blog: http://www.cnblogs.com/AdaminXie

# GitHub: https://github.com/coneypo/Dlib_face_recognition_from_camera

# Created at 2018-05-11

# Updated at 2019-04-09

import dlib # 人脸处理的库 Dlib

import numpy as np # 数据处理的库 numpy

import cv2 # 图像处理的库 OpenCv

import pandas as pd # 数据处理的库 Pandas

<p>

import os

# 人脸识别模型,提取128D的特征矢量

# face recognition model, the object maps human faces into 128D vectors

# Refer this tutorial: http://dlib.net/python/index.html#dlib.face_recognition_model_v1

facerec = dlib.face_recognition_model_v1("data/data_dlib/dlib_face_recognition_resnet_model_v1.dat")

# 计算两个128D向量间的欧式距离

# Compute the e-distance between two 128D features

def return_euclidean_distance(feature_1, feature_2):

feature_1 = np.array(feature_1)

feature_2 = np.array(feature_2)

dist = np.sqrt(np.sum(np.square(feature_1 - feature_2)))

return dist

# 1. Check 存放所有人脸特征的 csv

if os.path.exists("data/features_all.csv"):

path_features_known_csv = "data/features_all.csv"

csv_rd = pd.read_csv(path_features_known_csv, header=None)

# 用来存放所有录入人脸特征的数组

# The array to save the features of faces in the database

features_known_arr = []

# 2. 读取已知人脸数据

# Print known faces

for i in range(csv_rd.shape[0]):

features_someone_arr = []

for j in range(0, len(csv_rd.ix[i, :])):

features_someone_arr.append(csv_rd.ix[i, :][j])

features_known_arr.append(features_someone_arr)

print("Faces in Database:", len(features_known_arr))

# Dlib 检测器和预测器

# The detector and predictor will be used

detector = dlib.get_frontal_face_detector()

predictor = dlib.shape_predictor(&#39;data/data_dlib/shape_predictor_68_face_landmarks.dat&#39;)

# 创建 cv2 *敏*感*词*对象

cap = cv2.VideoCapture(0)

# 3. When the camera is open

while cap.isOpened():

flag, img_rd = cap.read()

img_gray = cv2.cvtColor(img_rd, cv2.COLOR_RGB2GRAY)

faces = detector(img_gray, 0)

# 待会要写的字体 font to write later

font = cv2.FONT_ITALIC

# 存储当前*敏*感*词*中捕获到的所有人脸的坐标/名字

# The list to save the positions and names of current faces captured

pos_namelist = []

name_namelist = []

kk = cv2.waitKey(1)

# 按下 q 键退出

# press &#39;q&#39; to exit

if kk == ord(&#39;q&#39;):

break

else:

# 检测到人脸 when face detected

if len(faces) != 0:

# 4. 获取当前捕获到的图像的所有人脸的特征,存储到 features_cap_arr

# 4. Get the features captured and save into features_cap_arr

features_cap_arr = []

for i in range(len(faces)):

shape = predictor(img_rd, faces[i])

features_cap_arr.append(facerec.compute_face_descriptor(img_rd, shape))

# 5. 遍历捕获到的图像中所有的人脸

# 5. Traversal all the faces in the database

for k in range(len(faces)):

print("##### camera person", k+1, "#####")

# 让人名跟随在矩形框的下方

# 确定人名的位置坐标

# 先默认所有人不认识,是 unknown

# Set the default names of faces with "unknown"

name_namelist.append("unknown")

# 每个捕获人脸的名字坐标 the positions of faces captured

pos_namelist.append(tuple([faces[k].left(), int(faces[k].bottom() + (faces[k].bottom() - faces[k].top())/4)]))

# 对于某张人脸,遍历所有存储的人脸特征

# For every faces detected, compare the faces in the database

e_distance_list = []

for i in range(len(features_known_arr)):

# 如果 person_X 数据不为空

if str(features_known_arr[i][0]) != &#39;0.0&#39;:

print("with person", str(i + 1), "the e distance: ", end=&#39;&#39;)

e_distance_tmp = return_euclidean_distance(features_cap_arr[k], features_known_arr[i])

print(e_distance_tmp)

e_distance_list.append(e_distance_tmp)

else:

# 空数据 person_X

e_distance_list.append(999999999)

# Find the one with minimum e distance

similar_person_num = e_distance_list.index(min(e_distance_list))

print("Minimum e distance with person", int(similar_person_num)+1)

if min(e_distance_list)

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线