Jiale/proj_deepface/exp_keras2onnx.ipynb

180 lines
5.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"import 必要的包\n",
"\n",
"deepface构建keras模型导出为onnx模型"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(112, 112) 512 GhostFaceNet\n"
]
}
],
"source": [
"# import 必要的包 \n",
"from deepface import DeepFace \n",
"import cv2 \n",
"import time \n",
"import os \n",
"import sys\n",
"import numpy as np \n",
"import re \n",
"import pandas as pd \n",
"\n",
"from deepface.modules.streaming import (\n",
" build_demography_models, build_facial_recognition_model,\n",
" search_identity, highlight_facial_areas, \n",
" grab_facial_areas, extract_facial_areas, \n",
" perform_facial_recognition, perform_demography_analysis,\n",
")\n",
"\n",
"from deepface.modules import recognition, verification, detection, representation\n",
"from deepface.models.FacialRecognition import FacialRecognition\n",
"from deepface.modules import preprocessing\n",
"from deepface.commons.logger import Logger\n",
"import pickle \n",
"\n",
"logger = Logger(module=\"commons.realtime\")\n",
"model: FacialRecognition = DeepFace.build_model(model_name=\"GhostFaceNet\")\n",
"\n",
"import tf2onnx\n",
"import onnxruntime as rt\n",
"import tensorflow as tf\n",
"\n",
"spec = (tf.TensorSpec((None, 112, 112, 3), tf.float32, name=\"input\"),)\n",
"# spec = (tf.TensorSpec((None, 3, 112, 112, ), tf.float32, name=\"input\"),)\n",
"output_path = model.model_name + \".onnx\"\n",
"print(model.input_shape, model.output_shape, model.model_name )\n",
"\n",
"model_proto, _ = tf2onnx.convert.from_keras(model.model, input_signature=spec, opset=13, output_path=output_path)\n",
"output_names = [n.name for n in model_proto.graph.output]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"使用上述得到的onnx model进行inference得到embeddings\n",
"- inference img_path1: C:\\Users\\JIALE\\Desktop\\bns项目资料\\proj_deepface\\face\\biden_id.jpg\n",
"- inference img_path2: C:\\Users\\JIALE\\Desktop\\bns项目资料\\proj_deepface\\face\\jiangyongqi.jpg\n",
"\n",
"计算embeddings之间的normalization L2距离。 \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 112, 112, 3) (1, 112, 112, 3)\n",
"distance of source and target embeddings for model inference: 1.2703674196541226\n",
"distance of embeddings for onnx inference: 1.270367798994846\n"
]
}
],
"source": [
"from tensorflow.keras.preprocessing import image\n",
"\n",
"img_path1 = r'.\\face\\biden_id.jpg'\n",
"img_path2 = r\".\\face\\jiangyongqi.jpg\"\n",
"\n",
"img1 = cv2.resize(cv2.imread(img_path1),(112, 112)) # bgr格式\n",
"img2 = cv2.resize(cv2.imread(img_path2),(112, 112)) \n",
"\n",
"face1 = image.img_to_array(img1)\n",
"face1 = np.expand_dims(face1, axis=0)\n",
"face1 /= 255 # normalize input in [0, 1]\n",
"\n",
"face2 = image.img_to_array(img2)\n",
"face2 = np.expand_dims(face2, axis=0)\n",
"face2 /= 255 # normalize input in [0, 1]\n",
"print( face1.shape, face2.shape )\n",
"\n",
"source_embedding = model.find_embeddings(\n",
" preprocessing.normalize_input(img=face1,normalization='base')\n",
")\n",
"target_embedding = model.find_embeddings(\n",
" preprocessing.normalize_input(img=face2,normalization='base')\n",
")\n",
"distance_metric = \"euclidean_l2\"\n",
"distance = verification.find_distance(source_embedding,target_embedding,distance_metric)\n",
"print(\"distance of source and target embeddings for model inference: \", distance)\n",
"\n",
"providers = ['CPUExecutionProvider']\n",
"m = rt.InferenceSession(output_path, providers=providers)\n",
"onnx_pred1= m.run(output_names, {\"input\": face1})[0][0].tolist()\n",
"onnx_pred2 = m.run(output_names, {\"input\": face2})[0][0].tolist()\n",
"dist2 = verification.find_distance(onnx_pred1,onnx_pred2,distance_metric)\n",
"print(\"distance of embeddings for onnx inference: \", dist2 )\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.16"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}