"""An example of how to use your own dataset to train a classifier that recognizes people.
"""
# MIT License
#
# Copyright (c) 2016 David Sandberg
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # @ 调用格式:
# @
# @ 训练模型记住人脸(不是训练网络,网络在这之前已经先训练好了)。
# @ ../lfw/ 是lfw数据集经过 mtcnn 截取以后的结果。否则会影响效果(去除数据集中的人脸外部干扰)
# @ python classifier.py TRAIN ../lfw/ 20170511-185253/ train_20180419_2048.pkl
# @
# @ 测试模型记住人脸的结果。(../data 是测试用的图的路径。)
# @ python classifier.py CLASSIFY ../data/ 20170511-185253/ train_20180419_2048.pkl from __future__ import absolute_import
from __future__ import division
from __future__ import print_function import tensorflow as tf
import numpy as np
import argparse
import facenet
import os
import sys
import math
import pickle
from sklearn.svm import SVC # @ args内中参数见函数 parse_arguments
def main(args):
# @ 声明一个计算图,都这么写,没有就是默认一个。
with tf.Graph().as_default():
# @ 声明一个 Session
with tf.Session() as sess: # @ Part I
# @ 这部分是计算人脸的 embedding 特征。费时。
# @ # @ 加随机数seed,调用np.random.random()的结果都会相同。
np.random.seed(seed=args.seed) if args.use_split_dataset:
dataset_tmp = facenet.get_dataset(args.data_dir)
train_set, test_set = split_dataset(dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class)
if (args.mode=='TRAIN'):
dataset = train_set
elif (args.mode=='CLASSIFY'):
dataset = test_set
else:
dataset = facenet.get_dataset(args.data_dir) # Check that there are at least one training image per class
# @ cls.image_paths 是每张图的路径,包含文件名。
for cls in dataset:
assert(len(cls.image_paths)>0, 'There must be at least one image for each class in the dataset') # @ 分离出图片路径名paths,和类型labels(人脸所属人名)
paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset))
print('Number of images: %d' % len(paths)) # Load the model
# @ 这里加的 model 使用于生成人脸的 embedding 特征的网络。
# @ 这个网络是事先已经生成好的。
# @ 网络可以根据运行的平台,设计成不同大小。比如基于GoogleNet/AlexNet等
print('Loading feature extraction model')
facenet.load_model(args.model) # Get input and output tensors
# @ TensorFlow的参数准备。embeddings 是网络的输出,是后续分类的输入。
images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings
print('Calculating features for images')
nrof_images = len(paths) # @ 图片总数
nrof_batches_per_epoch = int(math.ceil(1.0*nrof_images / args.batch_size))
emb_array = np.zeros((nrof_images, embedding_size))
for i in range(nrof_batches_per_epoch):
start_index = i*args.batch_size
end_index = min((i+1)*args.batch_size, nrof_images)
paths_batch = paths[start_index:end_index]
images = facenet.load_data(paths_batch, False, False, args.image_size)
feed_dict = { images_placeholder:images, phase_train_placeholder:False }
emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict) # @ emb_array 是 embedding 结果。一个 embedding 有 18 维。
# @ 接下来就是用机器学习的方法分类。
classifier_filename_exp = os.path.expanduser(args.classifier_filename) # @ Part II 也较费时。
# @ 这部分是训练分类人脸的机器学习模型,这里使用的SVC,是SVM的一种。
# @ 若是 CLASSIFY ,则是加载训练结果,建立 SVC 分类器。 if (args.mode=='TRAIN'):
# Train classifier
# @ SVC是SVM的一种Type,是用来的做分类的;同样还有SVR,是SVM的另一种Type,是用来的做回归的。
print('Training classifier')
model = SVC(kernel='linear', probability=True)
model.fit(emb_array, labels) # @ 训练过程 # @ 训练结束,保存数据
# Create a list of class names
class_names = [ cls.name.replace('_', ' ') for cls in dataset] # Saving classifier model
with open(classifier_filename_exp, 'wb') as outfile:
pickle.dump((model, class_names), outfile)
print('Saved classifier model to file "%s"' % classifier_filename_exp) elif (args.mode=='CLASSIFY'):
# Classify images
print('Testing classifier')
# @ 加载数据,建立分类器
with open(classifier_filename_exp, 'rb') as infile:
(model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"' % classifier_filename_exp) # @ 预测,标签结果应该是 one_hot 的。
predictions = model.predict_proba(emb_array)
best_class_indices = np.argmax(predictions, axis=1) # @ 输出每列最大的序号。
best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices] for i in range(len(best_class_indices)):
print('%4d %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i])) # @ 评估结果。labels 是测试集的实际结果,best_class_indices是预测结果。
accuracy = np.mean(np.equal(best_class_indices, labels))
print('Accuracy: %.3f' % accuracy) # @ 将数据集分成训练集和测试集
def split_dataset(dataset, min_nrof_images_per_class, nrof_train_images_per_class):
train_set = []
test_set = []
for cls in dataset:
paths = cls.image_paths
# Remove classes with less than min_nrof_images_per_class
if len(paths)>=min_nrof_images_per_class:
np.random.shuffle(paths)
train_set.append(facenet.ImageClass(cls.name, paths[:nrof_train_images_per_class]))
test_set.append(facenet.ImageClass(cls.name, paths[nrof_train_images_per_class:]))
return train_set, test_set # @ 命令行参数,使用的系统库 argparse
# @ ** 写法值得记住 **
def parse_arguments(argv):
parser = argparse.ArgumentParser() parser.add_argument('mode', type=str, choices=['TRAIN', 'CLASSIFY'],
help='Indicates if a new classifier should be trained or a classification ' +
'model should be used for classification', default='CLASSIFY')
parser.add_argument('data_dir', type=str,
help='Path to the data directory containing aligned LFW face patches.')
parser.add_argument('model', type=str,
help='Could be either a directory containing the meta_file and ckpt_file or a model protobuf (.pb) file')
parser.add_argument('classifier_filename',
help='Classifier model file name as a pickle (.pkl) file. ' +
'For training this is the output and for classification this is an input.')
parser.add_argument('--use_split_dataset',
help='Indicates that the dataset specified by data_dir should be split into a training and test set. ' +
'Otherwise a separate test set can be specified using the test_data_dir option.', action='store_true')
parser.add_argument('--test_data_dir', type=str,
help='Path to the test data directory containing aligned images used for testing.')
parser.add_argument('--batch_size', type=int,
help='Number of images to process in a batch.', default=90)
parser.add_argument('--image_size', type=int,
help='Image size (height, width) in pixels.', default=160)
parser.add_argument('--seed', type=int,
help='Random seed.', default=666)
parser.add_argument('--min_nrof_images_per_class', type=int,
help='Only include classes with at least this number of images in the dataset', default=20)
parser.add_argument('--nrof_train_images_per_class', type=int,
help='Use this number of images from each class for training and the rest for testing', default=10) return parser.parse_args(argv) # @ 主函数
# @ sys.argv[1:] 就是命令行输入的 classify.py 后面的所有字符串,以空格分隔。
if __name__ == '__main__':
main(parse_arguments(sys.argv[1:]))

  

08-人脸识别-FaceNet-classify.py代码阅读(说明见注释)的更多相关文章

  1. TensorFlow环境 人脸识别 FaceNet 应用(一)验证测试集

    TensorFlow环境 人脸识别 FaceNet 应用(一)验证测试集 前提是TensorFlow环境以及相关的依赖环境已经安装,可以正常运行. 一.下载FaceNet源代码工程 git clone ...

  2. 第三十七节、人脸检测MTCNN和人脸识别Facenet(附源码)

    在说到人脸检测我们首先会想到利用Harr特征提取和Adaboost分类器进行人脸检测(有兴趣的可以去一看这篇博客第九节.人脸检测之Haar分类器),其检测效果也是不错的,但是目前人脸检测的应用场景逐渐 ...

  3. 人脸识别FaceNet+TensorFlow

    一.本文目标 利用facenet源码实现从摄像头读取视频,实时检测并识别视频中的人脸.换句话说:把facenet源码中contributed目录下的real_time_face_recognition ...

  4. 基于 Python + OpenCV 进行人脸识别,视频追踪代码全注释

    先来普及一下概念, 计算机对人脸是如何识别的呢? 或者说图像是如何识别的.主要是获取单张图片的特征值记录了特征值以后,如果下一张图片来了以后两张图片特征值进行对比,如果相似度很高那么计算机就认定这两个 ...

  5. Python的开源人脸识别库:离线识别率高达99.38%

    Python的开源人脸识别库:离线识别率高达99.38%   github源码:https://github.com/ageitgey/face_recognition#face-recognitio ...

  6. Python的开源人脸识别库:离线识别率高达99.38%(附源码)

    Python的开源人脸识别库:离线识别率高达99.38%(附源码) 转https://cloud.tencent.com/developer/article/1359073   11.11 智慧上云 ...

  7. html5与EmguCV前后端实现——人脸识别篇(一)

    上个月因为出差的关系,断更了很久,为了补偿大家长久的等待,送上一个新的系列,之前几个系列也会抽空继续更新. 大概半年多前吧,因为工作需要,我开始研究图像识别技术.OpenCV在这方面已经有了很多技术积 ...

  8. facenet 人脸识别(二)——创建人脸库搭建人脸识别系统

    搭建人脸库 选择的方式是从百度下载明星照片 照片下载,downloadImageByBaidu.py # coding=utf-8 """ 爬取百度图片的高清原图 &qu ...

  9. opencv face-detection 代码分析 (1)人脸识别后的数据

    2014,3,16   老师的工作建议如下:   1. 与四民沟通下,把openCV这边的源代码和调用接口发给四民同时抄送给我. 2. 根据openCV的实时检测结果,实现对屏幕的调整(下周一前基本实 ...

随机推荐

  1. luogu P5606 小 K 与毕业旅行 - 构造 - 多项式

    题目传送门 传送门 先考虑 $a_i > 0$ 的情况.考虑构造这样一个顺序:$a_i$ 要么和后面的数的乘积都大于 $w$ 要么都小于等于 $w$. 这个构造可以这样做: vector< ...

  2. 原创|强大!Nginx 配置在线一键生成“神器”

    Nginx作为一个轻量级的HTTP服务器,相比Apache优势也是比较明显的,在性能上它占用资源少,能支持更高更多的并发连接,从而达到提高访问效率;在功能上它是一款非常优秀的代理服务器与负载均衡服务器 ...

  3. contentType: 'application/json' C#后台怎么处理

    contentType: 'application/json' 的处理如下: $(function () { $.ajax({ 'url': "/Home/Send2SHengPi" ...

  4. 路径规划基础A*算法

    1,Dijkstra’s  算法 一种发散性寻找最短路径算法. 由起点开始向四周开始发散,直到碰到目标点为止.这时就是最短路径.优点:能找到与目标点的最短路径:缺点:搜索花费的时间会比较长. 2,Gr ...

  5. Windows 配置Apache+CGI

    首先还是先在官网下载Apache,版本为Apache 2.4.26 x64,下载64位的地址为:http://www.apachehaus.com/cgi-bin/download.plx 下载完成后 ...

  6. 【记录】【idea】【mysql】Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually.解决问题

    idea连接mysql报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property ...

  7. IDEA整合GIT所有操作

    IDEA整合GIT操作 1.1 配置Idea集成Git 1.2 在使用SSH key 创建公钥私钥,上传公钥到github (1).点击开始菜单-->所有程序--->git选择 Git B ...

  8. Vue.js 源码分析(十三) 基础篇 组件 props属性详解

    父组件通过props属性向子组件传递数据,定义组件的时候可以定义一个props属性,值可以是一个字符串数组或一个对象. 例如: <!DOCTYPE html> <html lang= ...

  9. Prometheus K8S部署

    Prometheus K8S部署 部署方式:https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/prometheus ...

  10. 一个小巧,也很nice的“小日历”--一个Android App

    一个小巧也很Nice的“小日历” 背景 因为,常用日历记一些事情,Android自带的日历,如果有事情,会显示一个小点,然后点击进去后才能看到事情的具体内容,不是很方便. 所以,写了一个“小日历” 特 ...