diff --git a/proj_deepface/exp_keras2onnx.ipynb b/proj_deepface/exp_keras2onnx.ipynb deleted file mode 100644 index 6d9d9c4..0000000 --- a/proj_deepface/exp_keras2onnx.ipynb +++ /dev/null @@ -1,179 +0,0 @@ -{ - "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 -} diff --git a/proj_deepface/exp_stream.py b/proj_deepface/exp_stream.py deleted file mode 100644 index 348247a..0000000 --- a/proj_deepface/exp_stream.py +++ /dev/null @@ -1,26 +0,0 @@ -from deepface import DeepFace - -""" -Deepface is a hybrid face recognition package. It currently wraps many state-of-the-art face recognition models: -Model Declared LFW Score -VGG-Face 98.9% -Facenet 99.2% -Facenet512 99.6% -OpenFace 92.9% -DeepID 97.4% -Dlib 99.3% -SFace 99.5% -ArcFace 99.5% -GhostFaceNet 99.7% -Human-beings 97.5% - -The default configuration uses VGG-Face model. -""" - -DeepFace.stream( - db_path='./face', - model_name = 'Facenet512', - enable_face_analysis = True, - time_threshold=2, - frame_threshold=20, # Given frame_threshold = 20, 如果其在连续的20帧中都检测到人脸,则对第20帧的face进行操作(如:age 分析,人脸识别等等) -) \ No newline at end of file diff --git a/proj_deepface/keras-resnet50.ipynb b/proj_deepface/keras-resnet50.ipynb deleted file mode 100644 index f93a46b..0000000 --- a/proj_deepface/keras-resnet50.ipynb +++ /dev/null @@ -1,276 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Conversion to ONNX from keras model using tf2onnx python api " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "'wget' �����ڲ����ⲿ���Ҳ���ǿ����еij���\n", - "���������ļ���\n" - ] - } - ], - "source": [ - "# get image\n", - "# !wget -q https://raw.githubusercontent.com/onnx/tensorflow-onnx/main/tests/ade20k.jpg" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# !pip install tensorflow tf2onnx onnxruntime" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import tensorflow as tf\n", - "from tensorflow.keras.applications.resnet50 import ResNet50\n", - "from tensorflow.keras.preprocessing import image\n", - "from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions\n", - "import numpy as np\n", - "import onnxruntime\n", - "\n", - "img_path = 'ade20k.jpg'\n", - "\n", - "img = image.load_img(img_path, target_size=(224, 224))\n", - "\n", - "x = image.img_to_array(img)\n", - "x = np.expand_dims(x, axis=0)\n", - "x = preprocess_input(x)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Run the keras model" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1/1 [==============================] - 1s 1s/step\n", - "Keras Predicted: [('n02974003', 'car_wheel', 0.2852505), ('n04285008', 'sports_car', 0.26048782), ('n03930630', 'pickup', 0.15360694)]\n", - "WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op while saving (showing 5 of 53). These functions will not be directly callable after loading.\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: ./tmp\\resnet50\\assets\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: ./tmp\\resnet50\\assets\n" - ] - } - ], - "source": [ - "model = ResNet50(weights='imagenet')\n", - "\n", - "preds = model.predict(x)\n", - "print('Keras Predicted:', decode_predictions(preds, top=3)[0])\n", - "model.save(os.path.join(\"./tmp\", model.name))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Convert to ONNX using the Python API" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "import tf2onnx\n", - "import onnxruntime as rt\n", - "\n", - "spec = (tf.TensorSpec((None, 224, 224, 3), tf.float32, name=\"input\"),)\n", - "output_path = model.name + \".onnx\"\n", - "\n", - "model_proto, _ = tf2onnx.convert.from_keras(model, input_signature=spec, opset=13, output_path=output_path)\n", - "output_names = [n.name for n in model_proto.graph.output]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Run the ONNX model" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ONNX Predicted: [('n02974003', 'car_wheel', 0.28524926), ('n04285008', 'sports_car', 0.2604879), ('n03930630', 'pickup', 0.15360788)]\n" - ] - } - ], - "source": [ - "providers = ['CPUExecutionProvider']\n", - "m = rt.InferenceSession(output_path, providers=providers)\n", - "onnx_pred = m.run(output_names, {\"input\": x})\n", - "\n", - "print('ONNX Predicted:', decode_predictions(onnx_pred[0], top=3)[0])\n", - "\n", - "# make sure ONNX and keras have the same results\n", - "np.testing.assert_allclose(preds, onnx_pred[0], rtol=1e-5)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Convert to ONNX using the command line" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "scrolled": false - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\runpy.py:125: RuntimeWarning: 'tf2onnx.convert' found in sys.modules after import of package 'tf2onnx', but prior to execution of 'tf2onnx.convert'; this may result in unpredictable behaviour\n", - " warn(RuntimeWarning(msg))\n", - "2024-04-17 13:23:28,986 - WARNING - '--tag' not specified for saved_model. Using --tag serve\n", - "Traceback (most recent call last):\n", - " File \"c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\runpy.py\", line 193, in _run_module_as_main\n", - " \"__main__\", mod_spec)\n", - " File \"c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\runpy.py\", line 85, in _run_code\n", - " exec(code, run_globals)\n", - " File \"c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\site-packages\\tf2onnx\\convert.py\", line 714, in \n", - " main()\n", - " File \"c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\site-packages\\tf2onnx\\convert.py\", line 245, in main\n", - " use_graph_names=args.use_graph_names)\n", - " File \"c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\site-packages\\tf2onnx\\tf_loader.py\", line 637, in from_saved_model\n", - " tag, signatures, concrete_function, large_model, use_graph_names)\n", - " File \"c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\site-packages\\tf2onnx\\tf_loader.py\", line 570, in _from_saved_model_v2\n", - " imported = tf.saved_model.load(model_path, tags=tag) # pylint: disable=no-value-for-parameter\n", - " File \"c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\site-packages\\tensorflow\\python\\saved_model\\load.py\", line 828, in load\n", - " result = load_partial(export_dir, None, tags, options)[\"root\"]\n", - " File \"c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\site-packages\\tensorflow\\python\\saved_model\\load.py\", line 933, in load_partial\n", - " loader_impl.parse_saved_model_with_debug_info(export_dir))\n", - " File \"c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\site-packages\\tensorflow\\python\\saved_model\\loader_impl.py\", line 57, in parse_saved_model_with_debug_info\n", - " saved_model = parse_saved_model(export_dir)\n", - " File \"c:\\Users\\JIALE\\.conda\\envs\\deepface_v2\\lib\\site-packages\\tensorflow\\python\\saved_model\\loader_impl.py\", line 116, in parse_saved_model\n", - " f\"SavedModel file does not exist at: {export_dir}{os.path.sep}\"\n", - "OSError: SavedModel file does not exist at: /tmp\\resnet50\\{saved_model.pbtxt|saved_model.pb}\n" - ] - } - ], - "source": [ - "!python -m tf2onnx.convert --opset 13 \\\n", - " --saved-model {os.path.join(\"/tmp\", model.name)} \\\n", - " --output {os.path.join(\"/tmp\", model.name + \".onnx\")}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "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 -}