使用facenet_keras.h5开发人脸识别遇TensorFlow相关问题求助
人脸识别程序TensorFlow调用facenet_keras.h5模型的警告解决
我正在开发人脸识别程序,需要通过TensorFlow调用facenet_keras.h5模型,但运行代码时出现三个问题:numpy dtype弃用警告、AVX指令提示以及模型未编译的警告,求解决方法。
运行代码
import os from os import listdir from PIL import Image as Img from numpy import asarray from numpy import expand_dims from keras.models import load_model import numpy as np import pickle import cv2 # 加载分类器与facenet_keras模型 HaarCascade = cv2.CascadeClassifier(cv2.samples.findFile(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')) MyFaceNet = load_model("facenet_keras.h5") folder = 'photos/' # 照片文件夹路径 database = {} for filename in listdir(folder): path = folder + filename gbr1 = cv2.imread(path) visage = HaarCascade.detectMultiScale(gbr1, 1.1, 4) if len(visage) > 0: x1, y1, width, height = visage[0] else: x1, y1, width, height = 1, 1, 10, 10 x1, y1 = abs(x1), abs(y1) x2, y2 = x1 + width, y1 + height gbr = cv2.cvtColor(gbr1, cv2.COLOR_BGR2RGB) gbr = Img.fromarray(gbr) # OpenCV格式转PIL格式 gbr_array = asarray(gbr) # 转为数组 face = gbr_array[y1:y2, x1:x2] # 提取面部区域 face = Img.fromarray(face) # 转回图像格式 face = face.resize((160, 160)) face = asarray(face) # 归一化输入 face = face.astype('float32') mean, std = face.mean(), face.std() # 计算均值与标准差 face = (face - mean) / std # 输入到facenet模型 face = expand_dims(face, axis=0) signature = MyFaceNet.predict(face) database[os.path.splitext(filename)[0]] = signature myfile = open("data.pkl", "wb") pickle.dump(database, myfile) myfile.close() myfile = open("data.pkl", "rb") database = pickle.load(myfile) myfile.close()
错误信息
Using TensorFlow backend. C:\Users\Zouzou\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:458: 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_qint8 = np.dtype([("qint8", np.int8, 1)]) C:\Users\Zouzou\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:459: 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_quint8 = np.dtype([("quint8", np.uint8, 1)]) C:\Users\Zouzou\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:460: 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_qint16 = np.dtype([("qint16", np.int16, 1)]) C:\Users\Zouzou\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:461: 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_quint16 = np.dtype([("quint16", np.uint16, 1)]) C:\Users\Zouzou\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:462: 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_qint32 = np.dtype([("qint32", np.int32, 1)]) C:\Users\Zouzou\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:465: 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)]) 2022-10-12 15:26:25.360069: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. C:\Users\Zouzou\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\models.py:252: UserWarning: No training configuration found in save file: the model was *not* compiled. Compile it manually. warnings.warn('No training configuration found in save file: '
解决方案
1. numpy dtype弃用警告
- 原因:适配Python3.6的旧版TensorFlow与当前numpy版本不兼容,旧TensorFlow的dtype定义方式在新版numpy中被标记为弃用。
- 解决方式:
- 降级numpy到兼容版本,执行命令:
pip install numpy==1.16.6 - 或者在代码开头添加警告过滤,暂时屏蔽该类警告:
import warnings warnings.filterwarnings("ignore", category=FutureWarning)
- 降级numpy到兼容版本,执行命令:
2. AVX指令提示
- 原因:你安装的TensorFlow预编译包未启用AVX指令集优化,但你的CPU支持该指令集,因此提示可提升计算速度。
- 解决方式:
- 该提示仅影响性能,不干扰程序运行,可直接忽略。
- 若要利用AVX加速,需从TensorFlow源码编译并开启AVX编译选项,操作复杂,普通使用无需折腾。
- 也可在代码开头添加日志设置屏蔽警告:
import tensorflow as tf tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
3. 模型未编译警告
- 原因:
facenet_keras.h5是预训练特征提取模型,保存时未包含训练配置(仅用于预测,无需训练),因此加载时触发警告。 - 解决方式:
- 仅做预测的话,该警告不影响功能,可直接忽略。
- 若要消除警告,可在加载模型后手动编译(无需实际训练,随便选优化器和损失函数即可):
MyFaceNet = load_model("facenet_keras.h5") # 添加以下编译代码 MyFaceNet.compile(optimizer='adam', loss='categorical_crossentropy')
内容的提问来源于stack exchange,提问作者h81 edrick
相关产品推荐
相关产品推荐

