人脸识别 (3) FaceNet安装与测试

参见:算法---FaceNet在Tf下的实战篇 - WP的烂笔头 - 博客园

facenet 人脸识别源码的使用方法(一)码

1、源码下载:FaceNet源码+模型

(1)FaceNet在github源码地址为:  https://github.com/davidsandberg/facenet/tree/master

 FaceNet库源码下载链接:https://codeload.github.com/davidsandberg/facenet/zip/master  

2、数据集

LFW数据集: http://vis-www.cs.umass.edu/lfw/#resources 

 网址打开后下载Download the database: All images as gzipped tar file

其他数据集,参见FaceNet pre-trained模型以及FaceNet源码使用方法和讲解

下载完成后,把数据集解压到facenet\data\lfw\下面,如下:

3、FaceNet预训练的模型Pre-trained models

Model nameLFW accuracyTraining datasetArchitecture
20180408-1029000.9905CASIA-WebFaceInception ResNet v1
20180402-1147590.9965VGGFace2Inception ResNet v1

Model nameLFW accuracyTraining datasetArchitecture
20170511-1852530.987CASIA-WebFaceInception ResNet v1
20170512-1105470.992MS-Celeb-1MInception ResNet v1

模型下载链接:https://pan.baidu.com/s/1aiSq7wGpdHIe6MUKPnXgrA 密码:4dcn

20170512-110547(MS-Celeb-1M数据集训练的模型文件,微软人脸识别数据库,名人榜选择前100万名人,搜索引擎采集每个名人100张人脸图片。预训练模型准确率0.993+-0.004)

  把下载的文件到facenet\src\models\目录下面

4、安装依赖

pip install -r requirements.txt

 版本不对。修改tensorflow==1.14

(tf) robot@robot-All-Series:~/anaconda3/envs/tf/facenet$ pip install -r requirements.txt 
Requirement already satisfied: tensorflow==1.14.0 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from -r requirements.txt (line 1)) (1.14.0)
Requirement already satisfied: scipy in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from -r requirements.txt (line 2)) (1.5.2)
Collecting scikit-learn
  Downloading scikit_learn-1.0.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (24.8 MB)
     |████████████████████████████████| 24.8 MB 10.6 MB/s 
Collecting opencv-python
  Downloading opencv_python-4.5.5.64-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (60.5 MB)
     |████████████████████████████████| 60.5 MB 11.3 MB/s 
Requirement already satisfied: h5py in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from -r requirements.txt (line 5)) (2.10.0)
Collecting matplotlib
  Downloading matplotlib-3.5.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (11.2 MB)
     |████████████████████████████████| 11.2 MB 9.8 MB/s 
Collecting Pillow
  Downloading Pillow-9.0.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB)
     |████████████████████████████████| 4.3 MB 14.2 MB/s 
Collecting requests
  Using cached requests-2.27.1-py2.py3-none-any.whl (63 kB)
Collecting psutil
  Downloading psutil-5.9.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (280 kB)
     |████████████████████████████████| 280 kB 14.6 MB/s 
Requirement already satisfied: absl-py>=0.7.0 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (0.15.0)
Requirement already satisfied: protobuf>=3.6.1 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (3.19.1)
Requirement already satisfied: wrapt>=1.11.1 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (1.13.3)
Requirement already satisfied: google-pasta>=0.1.6 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (0.2.0)
Requirement already satisfied: tensorboard<1.15.0,>=1.14.0 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (1.14.0)
Requirement already satisfied: tensorflow-estimator<1.15.0rc0,>=1.14.0rc0 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (1.14.0)
Requirement already satisfied: astor>=0.6.0 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (0.8.1)
Requirement already satisfied: keras-applications>=1.0.6 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (1.0.8)
Requirement already satisfied: six>=1.10.0 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (1.16.0)
Requirement already satisfied: numpy<2.0,>=1.14.5 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (1.16.0)
Requirement already satisfied: gast>=0.2.0 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (0.5.3)
Requirement already satisfied: wheel>=0.26 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (0.37.1)
Requirement already satisfied: termcolor>=1.1.0 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (1.1.0)
Requirement already satisfied: grpcio>=1.8.6 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (1.42.0)
Requirement already satisfied: keras-preprocessing>=1.0.5 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorflow==1.14.0->-r requirements.txt (line 1)) (1.1.2)
Collecting joblib>=0.11
  Downloading joblib-1.1.0-py2.py3-none-any.whl (306 kB)
     |████████████████████████████████| 306 kB 51.4 MB/s 
Collecting threadpoolctl>=2.0.0
  Downloading threadpoolctl-3.1.0-py3-none-any.whl (14 kB)
Collecting pyparsing>=2.2.1
  Using cached pyparsing-3.0.7-py3-none-any.whl (98 kB)
Collecting numpy<2.0,>=1.14.5
  Downloading numpy-1.21.5-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.7 MB)
     |████████████████████████████████| 15.7 MB 9.3 MB/s 
Collecting python-dateutil>=2.7
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting cycler>=0.10
  Using cached cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting fonttools>=4.22.0
  Downloading fonttools-4.31.2-py3-none-any.whl (899 kB)
     |████████████████████████████████| 899 kB 14.2 MB/s 
Collecting kiwisolver>=1.0.1
  Downloading kiwisolver-1.4.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.1 MB)
     |████████████████████████████████| 1.1 MB 53.3 MB/s 
Collecting packaging>=20.0
  Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.9-py2.py3-none-any.whl (138 kB)
     |████████████████████████████████| 138 kB 25.0 MB/s 
Requirement already satisfied: certifi>=2017.4.17 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from requests->-r requirements.txt (line 8)) (2021.10.8)
Collecting idna<4,>=2.5
  Using cached idna-3.3-py3-none-any.whl (61 kB)
Collecting charset-normalizer~=2.0.0
  Using cached charset_normalizer-2.0.12-py3-none-any.whl (39 kB)
Requirement already satisfied: typing-extensions in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib->-r requirements.txt (line 6)) (3.10.0.2)
Requirement already satisfied: markdown>=2.6.8 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorboard<1.15.0,>=1.14.0->tensorflow==1.14.0->-r requirements.txt (line 1)) (3.3.4)
Requirement already satisfied: setuptools>=41.0.0 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorboard<1.15.0,>=1.14.0->tensorflow==1.14.0->-r requirements.txt (line 1)) (58.0.4)
Requirement already satisfied: werkzeug>=0.11.15 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from tensorboard<1.15.0,>=1.14.0->tensorflow==1.14.0->-r requirements.txt (line 1)) (2.0.3)
Requirement already satisfied: importlib-metadata in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from markdown>=2.6.8->tensorboard<1.15.0,>=1.14.0->tensorflow==1.14.0->-r requirements.txt (line 1)) (4.8.2)
Requirement already satisfied: zipp>=0.5 in /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages (from importlib-metadata->markdown>=2.6.8->tensorboard<1.15.0,>=1.14.0->tensorflow==1.14.0->-r requirements.txt (line 1)) (3.7.0)
Installing collected packages: numpy, pyparsing, urllib3, threadpoolctl, python-dateutil, Pillow, packaging, kiwisolver, joblib, idna, fonttools, cycler, charset-normalizer, scikit-learn, requests, psutil, opencv-python, matplotlib
  Attempting uninstall: numpy
    Found existing installation: numpy 1.16.0
    Uninstalling numpy-1.16.0:
      Successfully uninstalled numpy-1.16.0
Successfully installed Pillow-9.0.1 charset-normalizer-2.0.12 cycler-0.11.0 fonttools-4.31.2 idna-3.3 joblib-1.1.0 kiwisolver-1.4.0 matplotlib-3.5.1 numpy-1.21.5 opencv-python-4.5.5.64 packaging-21.3 psutil-5.9.0 pyparsing-3.0.7 python-dateutil-2.8.2 requests-2.27.1 scikit-learn-1.0.2 threadpoolctl-3.1.0 urllib3-1.26.9

安装了numpy 1.21 与tensorflow 1.14有点不兼容。

/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
(tf) robot@robot-All-Series:~/anaconda3/envs/tf/facenet/src$ pip install numpy==1.16.0
Collecting numpy==1.16.0
  Using cached numpy-1.16.0-cp37-cp37m-manylinux1_x86_64.whl (17.3 MB)
Installing collected packages: numpy
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
matplotlib 3.5.1 requires numpy>=1.17, but you have numpy 1.16.0 which is incompatib

 修改为安装numpy 1.17,但是问题还是没有解决。先忽略之。

安装numpy 1.17,经测试,align_dataset_mtcnn.py无法执行。有错误

Traceback (most recent call last):
  File "align_dataset_mtcnn.py", line 160, in <module>
    main(parse_arguments(sys.argv[1:]))
  File "align_dataset_mtcnn.py", line 56, in main
    pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)
  File "/home/robot/anaconda3/envs/tf/facenet/src/align/detect_face.py", line 283, in create_mtcnn
    pnet.load(os.path.join(model_path, 'det1.npy'), sess)
  File "/home/robot/anaconda3/envs/tf/facenet/src/align/detect_face.py", line 85, in load
    data_dict = np.load(data_path, encoding='latin1').item() #pylint: disable=no-member
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/numpy/lib/npyio.py", line 453, in load
    pickle_kwargs=pickle_kwargs)
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/numpy/lib/format.py", line 722, in read_array
    raise ValueError("Object arrays cannot be loaded when "
ValueError: Object arrays cannot be loaded when allow_pickle=False

最终还是安装numpy1.16

5、图片预处理

数据整理:

lfw的图片原图尺寸为 250*250,我们要修改图片尺寸,使其大小和预训练模型的图片输入尺寸一致,即160*160,转换后的数据集存储在 facenet/data/lfw_data/lfw_160文件夹内。

align_dataset_mtcnn.py 会对dataset的图片进行人脸检测,进一步细化人脸图片,然后再把人脸图片尺寸修改为160×160的尺寸。

将align_dataset_mtcnn.py 从align目录复制到src目录,在align目录下无法运行

conda activate tf
python align_dataset_mtcnn.py ../data/lfw ../data/lfw_160 --image_size 160 --margin 32 --random_order --gpu_memory_fraction 0.25

错误提示

(tf) robot@robot-All-Series:~/anaconda3/envs/tf/facenet/src$ python align_dataset_mtcnn.py ../data/lfw ../data/lfw_160 --image_size 160 --margin 32 --random_order --gpu_memory_fraction 0.25
fatal: 有歧义的参数 'HEAD':未知的版本或路径不存在于工作区中。
使用 '--' 来分隔版本和路径,例如:
'git <命令> [<版本>...] -- [<文件>...]'
fatal: 有歧义的参数 'HEAD':未知的版本或路径不存在于工作区中。
使用 '--' 来分隔版本和路径,例如:
'git <命令> [<版本>...] -- [<文件>...]'
Creating networks and loading parameters
WARNING:tensorflow:From align_dataset_mtcnn.py:53: The name tf.GPUOptions is deprecated. Please use tf.compat.v1.GPUOptions instead.

WARNING:tensorflow:From align_dataset_mtcnn.py:54: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

WARNING:tensorflow:From align_dataset_mtcnn.py:54: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

2022-03-23 10:08:03.800194: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2022-03-23 10:08:03.820768: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3598265000 Hz
2022-03-23 10:08:03.821522: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55b1a5115010 executing computations on platform Host. Devices:
2022-03-23 10:08:03.821567: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>
2022-03-23 10:08:03.823095: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcuda.so.1
2022-03-23 10:08:04.113924: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties: 
name: NVIDIA TITAN Xp major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:05:00.0
2022-03-23 10:08:04.114102: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 1 with properties: 
name: NVIDIA TITAN Xp major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:09:00.0
2022-03-23 10:08:04.114297: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudart.so.10.1
2022-03-23 10:08:04.115421: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcublas.so.10
2022-03-23 10:08:04.116474: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcufft.so.10
2022-03-23 10:08:04.116670: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcurand.so.10
2022-03-23 10:08:04.117837: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcusolver.so.10
2022-03-23 10:08:04.118412: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcusparse.so.10
2022-03-23 10:08:04.120966: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudnn.so.7
2022-03-23 10:08:04.121599: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0, 1
2022-03-23 10:08:04.121633: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudart.so.10.1
2022-03-23 10:08:04.122063: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-03-23 10:08:04.122077: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187]      0 1 
2022-03-23 10:08:04.122087: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0:   N Y 
2022-03-23 10:08:04.122093: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 1:   Y N 
2022-03-23 10:08:04.122758: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3048 MB memory) -> physical GPU (device: 0, name: NVIDIA TITAN Xp, pci bus id: 0000:05:00.0, compute capability: 6.1)
2022-03-23 10:08:04.123301: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 3049 MB memory) -> physical GPU (device: 1, name: NVIDIA TITAN Xp, pci bus id: 0000:09:00.0, compute capability: 6.1)
2022-03-23 10:08:04.124873: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55b1a7b21d30 executing computations on platform CUDA. Devices:
2022-03-23 10:08:04.124908: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): NVIDIA TITAN Xp, Compute Capability 6.1
2022-03-23 10:08:04.124922: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (1): NVIDIA TITAN Xp, Compute Capability 6.1
WARNING:tensorflow:From /home/robot/anaconda3/envs/tf/facenet/src/align/detect_face.py:280: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.

WARNING:tensorflow:From /home/robot/anaconda3/envs/tf/facenet/src/align/detect_face.py:281: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From /home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
WARNING:tensorflow:From /home/robot/anaconda3/envs/tf/facenet/src/align/detect_face.py:177: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

WARNING:tensorflow:From /home/robot/anaconda3/envs/tf/facenet/src/align/detect_face.py:213: 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 /home/robot/anaconda3/envs/tf/facenet/src/align/detect_face.py:197: The name tf.nn.xw_plus_b is deprecated. Please use tf.compat.v1.nn.xw_plus_b instead.

../data/lfw/Ralph_Goodale/Ralph_Goodale_0001.jpg
Traceback (most recent call last):
  File "align_dataset_mtcnn.py", line 160, in <module>
    main(parse_arguments(sys.argv[1:]))
  File "align_dataset_mtcnn.py", line 84, in main
    img = misc.imread(image_path)
AttributeError: module 'scipy.misc' has no attribute 'imread'

参见决AttributeError: module 'scipy.misc' has no attribute 'imread'报错问题

但是最新imageio需要numpy >1.20. 所以只能安装pip install imageio==2.10.5

Traceback (most recent call last):
  File "align_dataset_mtcnn.py", line 164, in <module>
    main(parse_arguments(sys.argv[1:]))
  File "align_dataset_mtcnn.py", line 128, in main
    scaled = imageio.imresize(cropped, (args.image_size, args.image_size), interp='bilinear')
AttributeError: module 'imageio' has no attribute 'imresize'

解决方法:

python 3.x - Alternative to scipy.misc.imresize() - Stack Overflow

无需降级scipy,解决AttributeError: module ‘scipy.misc’ has no attribute ‘imread’,imresize,imsave问题_ 采用

from PIL import Image
scaled = Image.fromarray(cropped).resize(size=(args.image_size,args.image_size))

ImportError: No module named 'sklearn.__check_build._check_build' 

pip install -U scikit-learn -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type

ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type_ppipp1109的博客-CSDN博客

​ 

6、预训练模型准确率测试

将validate_on_lfw.py 中149行

    parser.add_argument('--lfw_pairs', type=str,
        help='The file containing the pairs to use for validation.', default='data/pairs.txt')

修改为

    parser.add_argument('--lfw_pairs', type=str,
        help='The file containing the pairs to use for validation.', default='../data/pairs.txt')

python validate_on_lfw.py ../data/lfw_160/ models/20180402-114759/

出现错误

2022-03-23 13:42:09.678767: W tensorflow/core/graph/graph_constructor.cc:1352] Importing a graph with a lower producer version 24 into an existing graph with producer version 38. Shape inference will have run different parts of the graph with different producer versions.
Traceback (most recent call last):
  File "validate_on_lfw.py", line 164, in <module>
    main(parse_arguments(sys.argv[1:]))
  File "validate_on_lfw.py", line 73, in main
    facenet.load_model(args.model, input_map=input_map)
  File "/home/robot/anaconda3/envs/tf/facenet/src/facenet.py", line 381, in load_model
    saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file), input_map=input_map)
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/training/saver.py", line 1449, in import_meta_graph
    **kwargs)[0]
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/training/saver.py", line 1473, in _import_meta_graph_with_return_elements
    **kwargs))
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/framework/meta_graph.py", line 928, in import_scoped_meta_graph_with_return_elements
    proto, import_scope=scope_to_prepend_to_names))
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1677, in from_proto
    ret = CondContext(context_def=context_def, import_scope=import_scope)
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1586, in __init__
    self._init_from_proto(context_def, import_scope=import_scope)
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1618, in _init_from_proto
    values_def=context_def.values_def, import_scope=import_scope)
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1388, in __init__
    self._init_values_from_proto(values_def, import_scope=import_scope)
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1420, in _init_values_from_proto
    g.as_graph_element(op)._set_control_flow_context(self)
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3796, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/home/robot/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3856, in _as_graph_element_locked
    "graph." % repr(name))
KeyError: "The name 'decode_image/cond_jpeg/is_png' refers to an Operation not in the graph."

解决方法:(参见https://blog.csdn.net/sinat_26745777/article/details/100123264

    1.把Tensorflow换为1.7版本的;

    2.在facenet.py代码中找到create_input_pipeline 再添加一行语句 with tf.name_scope(“tempscope”):就可以完美解决(貌似Tensorflow 1.10及以上版本才修复这个bug)。

输出结果:

7. 比较两张图片的欧氏距离

这里使用欧氏距离用来衡量两张人脸的相似程度,用来判别这两张图片是否为同一个人。两张人脸图片越相似,空间距离越小;差别越大,则空间距离越大。 

执行以下命令:

python src/compare.py src/models/20180402-114759/20180402-114759.pb data/images/Anthony_Hopkins_0001.jpg data/images/Anthony_Hopkins_0002.jpg --gpu_memory_fraction 0.25

imread和imresize错误参见前面处理

Traceback (most recent call last):
  File "src/compare.py", line 135, in <module>
    main(parse_arguments(sys.argv[1:]))
  File "src/compare.py", line 43, in main
    images = load_and_align_data(args.image_files, args.image_size, args.margin, args.gpu_memory_fraction)
  File "src/compare.py", line 115, in load_and_align_data
    prewhitened = facenet.prewhiten(aligned)
  File "/home/robot/anaconda3/envs/tf/facenet/src/facenet.py", line 218, in prewhiten
    y = np.multiply(np.subtract(x, mean), 1/std_adj)
ValueError: operands could not be broadcast together with shapes (160,160,3) (2,) 

原因是compare.py 112行

aligned = Image.fromarray(cropped).resize(size=(image_size,image_size))

中aligned是Pillow Image格式,需要转化为numpy矩阵

 aligned = np.asarray(Image.fromarray(cropped).resize(size=(image_size,image_size)))

运行结果为:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值