277 lines
8.6 KiB
Plaintext
277 lines
8.6 KiB
Plaintext
{
|
||
"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' <20><><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD>Ҳ<EEA3AC><D2B2><EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD>\n",
|
||
"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>\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 <module>\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
|
||
}
|