人脸检测FaceNet网络训练自己的数据集

FaceNet是Google提出的用于人脸识别(recognition,k-NN),验证(verification, two persons),聚类(clustering, find common people among these faces)。与用于分类的神经网络(MobileNet、GoogleNet、VGG16/19、ResNet等)不同的是FaceNet选择了一种更直接的端到端的学习和分类方式,同时生成更少的参数量。
在这里插入图片描述
论文:https://arxiv.org/pdf/1503.03832.pdf
代码:https://github.com/davidsandberg/facenet

人脸识别是两个主要操作的组合:人脸检测,然后是人脸分类。
人脸检测: 查看图像并找到其中所有可能的人脸
人脸提取: 专注于每个人脸图像并理解它,例如,如果它被侧向或光线不足
特征提取: 使用卷积神经网络 (CNN) 从人脸中提取独特的特征
**分类器训练: **最后,将那张脸的独特特征与所有已知的人进行比较,以确定此人的名字

成熟的CNN技术:
dlib ( http://dlib.net/ ):提供可用于面部检测和对齐的库。
OpenFace ( https://cmusatyalab.github.io/openface/ ):一种深度学习面部识别模型,由 Brandon Amos 等人 ( http://bamos.github.io/ ) 开发。它也能够在实时移动设备上运行。
FaceNet ( https://arxiv.org/abs/1503.03832 ):一种用于特征提取的 CNN 架构。对于损失函数,FaceNet 使用三元组损失。Triplet loss 依赖于最小化与正例的距离,同时最大化与负例的距离。

在这里插入图片描述原理分析:

代码文件结构:
在这里插入图片描述
参考:https://blog.csdn.net/qq_45312141/
附件说明:

contributed/batch_represent.py 从图片目录中生成embeddings
contributed/cluster.py 人脸图像归类
contributed/clustering.py 人脸匹配
contributed/export_embeddings.py 从图片文件夹中导出embeddings和标签(numpy数组格式)
contributed/face.py 人脸检测和识别库接口
contributed/predict.py
contributed/predict.py 实时人脸识别(获取摄像头图片,并识别)
data/images 测试图片文件夹
data/learning_rate_*.txt 遍历次数与学习率对照表
data/pairs.txt
src/align/align_dataset_mtcnn.py 执行面对齐并将面部缩略图存储在输出目录中
src/align/detect_face.py 基于多任务级联卷积神经网络的人脸检测与对准
src/align/det*.npy detect_face.py 用到的数据
src/generative/models/dfc_vae.py 基于“深度特征一致变分自动编码器”的变分自动编码器
src/generative/models/dfc_vae_large.py 基于“深度特征一致变分自动编码器”的变分自动编码器(大图片 128*128)
src/generative/models/dfc_vae_resnet.py 基于“深度特征一致变分自动编码器”的变分自动编码器
src/generative/models/dfc_base.py 变分自动编码器基类,包含编码器和解码器
src/generative/calculate_attribute_vectors.py 计算属性向量
src/generative/modify_attribute.py 修改属性向量
src/generative/train_vae.py 训练变分自动编码器
src/models/dummy.py 虚拟模型,用于测试
src/models/inception_resnet_v1.py Inception Resnet V1 网络结构
src/models/inception_resnet_v2.py Inception Resnet V2 网络结构
src/models/squeezenet.py squeezenet 网络结构
src/calculate_filtering_metrics.py 计算数据集的过滤指标并存储在.hdf文件中
src/classifier.py 使用自定义数据集训练分类器
src/compare.py 进行面部对齐并计算与嵌入图片的欧氏距离
src/decode_msceleb_dataset.py 解析msceleb数据集
src/download_and_extract.py
src/facenet.py 构建人脸识别网络
src/freeze_graph.py 冻结网络,输出模型文件
src/lfw.py 评估lfw数据集
src/train_softmax.py 使用交叉熵损失函数训练
src/train_tripletloss.py 使用三元损失函数训练
src/validate_on_lfw.py 在lfw数据集上验证
test/*_test.py 测试代码
tmp
util/plot_learning_curves.m matlab中的绘制学习曲线工具

准备好数据集之后输入以下命令:

python src/train_tripletloss.py --logs_base_dir ./logs/facenet/ --models_base_dir ./models/facenet/ --data_dir ./datasets/lfw --model_def models.inception_resnet_v1 --optimizer RMSPROP --image_size 160 --batch_size 30 --learning_rate 0.01 --weight_decay 1e-4 --max_nrof_epochs 5 --epoch_size 1 --gpu_memory_fraction 0.7
在这里插入图片描述

训练记录:

base) root@ovo:/data/my_project/facenet# python src/train_tripletloss.py --logs_base_dir ./logs/facenet/ --models_base_dir ./models/facenet/  --data_dir ./datasets/lfw --model_def models.inception_resnet_v1 --optimizer RMSPROP --image_size 160 --batch_size 30 --learning_rate 0.01 --weight_decay 1e-4 --max_nrof_epochs 5 --epoch_size 1 --gpu_memory_fraction 0.7
Model directory: ./models/facenet/20210905-111023
Log directory: ./logs/facenet/20210905-111023
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/image_ops_impl.py:1241: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Deprecated in favor of operator or tf.math.divide.
WARNING:tensorflow:From src/train_tripletloss.py:126: batch_join (from tensorflow.python.training.input) is deprecated and will be removed in a future version.
Instructions for updating:
Queue-based input pipelines have been replaced by `tf.data`. Use `tf.data.Dataset.interleave(...).batch(batch_size)` (or `padded_batch(...)` if `dynamic_pad=True`).
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/training/input.py:736: QueueRunner.__init__ (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/training/input.py:736: add_queue_runner (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/training/input.py:823: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/contrib/layers/python/layers/layers.py:1624: flatten (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.
Instructions for updating:
Use keras.layers.flatten instead.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py:143: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
2021-09-05 11:10:46.970148: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2021-09-05 11:10:47.056657: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-05 11:10:47.056980: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x564838bcfe60 executing computations on platform CUDA. Devices:
2021-09-05 11:10:47.056999: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): GeForce GTX 1650 Ti, Compute Capability 7.5
2021-09-05 11:10:47.071640: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2600000000 Hz
2021-09-05 11:10:47.071964: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x564838cefd60 executing computations on platform Host. Devices:
2021-09-05 11:10:47.071980: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
2021-09-05 11:10:47.072103: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties: 
name: GeForce GTX 1650 Ti major: 7 minor: 5 memoryClockRate(GHz): 1.485
pciBusID: 0000:01:00.0
totalMemory: 3.81GiB freeMemory: 3.51GiB
2021-09-05 11:10:47.072119: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
2021-09-05 11:10:47.072557: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-09-05 11:10:47.072572: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990]      0 
2021-09-05 11:10:47.072582: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0:   N 
2021-09-05 11:10:47.073795: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2732 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1650 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5)
WARNING:tensorflow:From src/train_tripletloss.py:169: start_queue_runners (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
2021-09-05 11:11:03.138198: I tensorflow/stream_executor/dso_loader.cc:152] successfully opened CUDA library libcublas.so.10.0 locally
Running forward pass on sampled images: 14.982
Selecting suitable triplets for training
src/train_tripletloss.py:296: RuntimeWarning: invalid value encountered in less
  all_neg = np.where(neg_dists_sqr-pos_dist_sqr<alpha)[0] # VGG Face selecction
(nrof_random_negs, nrof_triplets) = (100, 100): time=15.007 seconds
2021-09-05 11:11:26.028295: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.48GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.
2021-09-05 11:11:26.028343: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.48GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.
2021-09-05 11:11:26.031771: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.59GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.
2021-09-05 11:11:26.031795: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.59GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.
Epoch: [0][1/1] Time 11.550     Loss 1.856
Epoch: [0][2/1] Time 0.238      Loss 1.780
Epoch: [0][3/1] Time 0.231      Loss 1.689
Epoch: [0][4/1] Time 0.217      Loss 1.839
Epoch: [0][5/1] Time 0.239      Loss 1.847
Epoch: [0][6/1] Time 0.233      Loss 1.623
Epoch: [0][7/1] Time 0.223      Loss 1.763
Epoch: [0][8/1] Time 0.218      Loss 1.808
Epoch: [0][9/1] Time 0.240      Loss 2.007
Epoch: [0][10/1]        Time 0.233      Loss 1.726
Saving variables
Variables saved in 0.56 seconds
Saving metagraph
Metagraph saved in 1.83 seconds
Running forward pass on sampled images: 5.487
Selecting suitable triplets for training
(nrof_random_negs, nrof_triplets) = (97, 97): time=5.497 seconds
Epoch: [10][1/1]        Time 0.247      Loss 1.686
Epoch: [10][2/1]        Time 0.244      Loss 1.858
Epoch: [10][3/1]        Time 0.243      Loss 1.820
Epoch: [10][4/1]        Time 0.243      Loss 1.813
Epoch: [10][5/1]        Time 0.243      Loss 1.801
Epoch: [10][6/1]        Time 0.244      Loss 2.201
Epoch: [10][7/1]        Time 0.224      Loss 1.807
Epoch: [10][8/1]        Time 0.239      Loss 1.827
Epoch: [10][9/1]        Time 0.226      Loss 1.968
2021-09-05 11:11:40.213919: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.48GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.
2021-09-05 11:11:40.213962: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.48GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.
2021-09-05 11:11:40.221015: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.56GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.
2021-09-05 11:11:40.221038: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.56GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.
Epoch: [10][10/1]       Time 1.781      Loss 1.877
Saving variables
Variables saved in 0.06 seconds

或者使用如下命令进行训练:

python src/train_softmax.py --logs_base_dir ~/logs/facenet/ --models_base_dir ~/models/facenet/ --data_dir ~/datasets/lfw --image_size 160 --model_def models.inception_resnet_v1 --lfw_dir ~/datasets/lfw/raw/ --optimizer ADAM --learning_rate -1 --max_nrof_epochs 500 --batch_size 90 --keep_probability 0.4 --random_flip --use_fixed_image_standardization --learning_rate_schedule_file data/learning_rate_schedule_classifier_vggface2.txt --weight_decay 5e-4 --embedding_size 512 --lfw_distance_metric 1 --lfw_use_flipped_images --lfw_subtract_mean --validation_set_split_ratio 0.01 --validate_every_n_epochs 5

运行 TensorBoard查看训练后的模型结构:

当 FaceNet 训练正在运行时,监控学习过程会很有趣。这可以使用TensorBoard来完成。要启动 TensorBoard,请运行命令
tensorboard --logdir=~/logs/facenet --port 6006
,然后将 Web 浏览器指向
http://localhost:6006/或Linux用户名:6006/
在这里插入图片描述网络结构图如下:

在这里插入图片描述在这里插入图片描述

在这里插入图片描述

测试模型:

python src/validate_on_lfw.py \
./test/lfw \
./models/20180402-114759.pb \
--distance_metric 1 \
--use_flipped_images \
--subtract_mean \
--use_fixed_image_standardization \

未完待续…最近有点忙,有时间再好好整理文章和源码从0到1的过程。

参考资料:

其他详细的操作内容请参考官方文档,问题已经解释了很多使用方法:
https://github.com/davidsandberg/facenet/wiki/Training-using-the-VGGFace2-dataset
较旧的一篇文章,值得读读:使用 FaceNet 构建人脸识别
https://www.datasciencecentral.com/profiles/blogs/building-face-recognition-using-facenet

  • 3
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码杀手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值