Skip to content

Commit

Permalink
test with pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang committed Dec 12, 2023
1 parent 9aafab6 commit d2b49c9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 55 deletions.
107 changes: 55 additions & 52 deletions tutorials/conversion_onnx/keras2onnx.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,28 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2023-12-12 14:12:39.037240: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.\n",
"2023-12-12 14:12:39.100024: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
"2023-12-12 14:12:39.100061: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
"2023-12-12 14:12:39.102844: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
"2023-12-12 14:12:39.115682: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.\n",
"2023-12-12 14:12:39.116951: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
"To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"2023-12-12 14:12:40.469062: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n"
]
}
],
"source": [
"import os\n",
"\n",
"import numpy as np\n",
"from pathlib import Path\n",
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"\n",
"import onnx\n",
"import onnxruntime as ort\n",
"# In addition to these imports, this notebook\n",
Expand Down Expand Up @@ -57,7 +71,15 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 1s 983ms/step\n"
]
}
],
"source": [
"input_shape = [1] + model.inputs[0].shape[1:] # input shape without a 1 for batch size, instead of None\n",
"input_data = np.random.normal(size=input_shape).astype(np.float32)\n",
Expand All @@ -80,10 +102,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:FOR KERAS USERS: The object that you are saving contains one or more Keras models or layers. If you are loading the SavedModel with `tf.keras.models.load_model`, continue reading (otherwise, you may ignore the following instructions). Please change your code to save with `tf.keras.models.save_model` or `model.save`, and confirm that the file \"keras.metadata\" exists in the export directory. In the future, Keras will only load the SavedModels that have this file. In other words, `tf.saved_model.save` will no longer write SavedModels that can be recovered as Keras models (this will apply in TF 2.5).\n",
"\n",
"FOR DEVS: If you are overwriting _tracking_metadata in your class, this property has been used to save metadata in the SavedModel. The metadta field will be deprecated soon, so please move the metadata to a different file.\n",
"INFO:tensorflow:Assets written to: mysavedmodel\\assets\n"
"INFO:tensorflow:Assets written to: mysavedmodel/assets\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:tensorflow:Assets written to: mysavedmodel/assets\n"
]
}
],
Expand All @@ -105,51 +131,28 @@
"metadata": {},
"outputs": [
{
"name": "stderr",
"name": "stdout",
"output_type": "stream",
"text": [
"2022-03-10 16:21:35.777938: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found\n",
"2022-03-10 16:21:35.777995: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n",
"C:\\Users\\ChristiaanMeijer\\anaconda3\\envs\\temp-dianna\\lib\\runpy.py:127: 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",
"2023-12-12 14:13:15.345213: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
"2023-12-12 14:13:15.345304: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
"2023-12-12 14:13:15.346390: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
"2023-12-12 14:13:16.833234: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n",
"/usr/lib/python3.10/runpy.py:126: 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",
"2022-03-10 16:21:39.793252: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found\n",
"2022-03-10 16:21:39.793284: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)\n",
"2022-03-10 16:21:39.798447: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: ESLT0114\n",
"2022-03-10 16:21:39.798693: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: ESLT0114\n",
"2022-03-10 16:21:39.799632: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2\n",
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"2022-03-10 16:21:43,849 - INFO - Signatures found in model: [serving_default].\n",
"2022-03-10 16:21:43,850 - INFO - Output names: ['predictions']\n",
"2022-03-10 16:21:43.871683: I tensorflow/core/grappler/devices.cc:69] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0\n",
"2022-03-10 16:21:43.871942: I tensorflow/core/grappler/clusters/single_machine.cc:357] Starting new session\n",
"2022-03-10 16:21:43.925808: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:1144] Optimization results for grappler item: graph_to_optimize\n",
" function_optimizer: Graph size after: 556 nodes (416), 805 edges (665), time = 30.764ms.\n",
" function_optimizer: function_optimizer did nothing. time = 0.402ms.\n",
"\n",
"WARNING:tensorflow:From C:\\Users\\ChristiaanMeijer\\anaconda3\\envs\\temp-dianna\\lib\\site-packages\\tf2onnx\\tf_loader.py:706: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use `tf.compat.v1.graph_util.extract_sub_graph`\n",
"2022-03-10 16:21:45,102 - WARNING - From C:\\Users\\ChristiaanMeijer\\anaconda3\\envs\\temp-dianna\\lib\\site-packages\\tf2onnx\\tf_loader.py:706: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use `tf.compat.v1.graph_util.extract_sub_graph`\n",
"2022-03-10 16:21:45.160029: I tensorflow/core/grappler/devices.cc:69] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0\n",
"2022-03-10 16:21:45.160280: I tensorflow/core/grappler/clusters/single_machine.cc:357] Starting new session\n",
"2022-03-10 16:21:45.407735: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:1144] Optimization results for grappler item: graph_to_optimize\n",
" constant_folding: Graph size after: 256 nodes (-274), 531 edges (-274), time = 155.954ms.\n",
" function_optimizer: function_optimizer did nothing. time = 2.671ms.\n",
" constant_folding: Graph size after: 256 nodes (0), 531 edges (0), time = 32.974ms.\n",
" function_optimizer: function_optimizer did nothing. time = 2.043ms.\n",
"\n",
"2022-03-10 16:21:45,622 - INFO - Using tensorflow=2.5.0, onnx=1.11.0, tf2onnx=1.9.3/1190aa\n",
"2022-03-10 16:21:45,623 - INFO - Using opset <onnx, 9>\n",
"2022-03-10 16:21:46,897 - INFO - Computed 0 values for constant folding\n",
"2022-03-10 16:21:48,086 - INFO - Optimizing ONNX model\n",
"2022-03-10 16:21:50,126 - INFO - After optimization: BatchNormalization -27 (27->0), Cast -4 (4->0), Concat -2 (2->0), Const -107 (165->58), GlobalAveragePool +1 (0->1), Identity -6 (6->0), ReduceMean -1 (1->0), Reshape -13 (15->2), Shape -2 (2->0), Slice -2 (2->0), Squeeze -1 (2->1), Transpose -122 (123->1), Unsqueeze -6 (6->0)\n",
"2022-03-10 16:21:50,172 - INFO - \n",
"2022-03-10 16:21:50,172 - INFO - Successfully converted TensorFlow model mysavedmodel to ONNX\n",
"2022-03-10 16:21:50,172 - INFO - Model inputs: ['input_1']\n",
"2022-03-10 16:21:50,172 - INFO - Model outputs: ['predictions']\n",
"2022-03-10 16:21:50,172 - INFO - ONNX model is saved at mysavedmodel.onnx\n"
"2023-12-12 14:13:23,137 - INFO - Signatures found in model: [serving_default].\n",
"2023-12-12 14:13:23,138 - INFO - Output names: ['predictions']\n",
"2023-12-12 14:13:23,138 - WARNING - Could not search for non-variable resources. Concrete function internal representation may have changed.\n",
"2023-12-12 14:13:24,308 - INFO - Using tensorflow=2.15.0, onnx=1.14.0, tf2onnx=1.15.1/37820d\n",
"2023-12-12 14:13:24,308 - INFO - Using opset <onnx, 15>\n",
"2023-12-12 14:13:24,488 - INFO - Computed 0 values for constant folding\n",
"2023-12-12 14:13:24,924 - INFO - Optimizing ONNX model\n",
"2023-12-12 14:13:26,281 - INFO - After optimization: BatchNormalization -27 (27->0), Cast -2 (2->0), Concat -1 (1->0), Const -163 (223->60), GlobalAveragePool +1 (0->1), Identity -2 (2->0), ReduceMean -1 (1->0), Reshape -13 (14->1), Shape -1 (1->0), Slice -1 (1->0), Squeeze -1 (1->0), Transpose -122 (123->1), Unsqueeze -2 (2->0)\n",
"2023-12-12 14:13:26,307 - INFO - \n",
"2023-12-12 14:13:26,307 - INFO - Successfully converted TensorFlow model mysavedmodel to ONNX\n",
"2023-12-12 14:13:26,307 - INFO - Model inputs: ['input_1']\n",
"2023-12-12 14:13:26,307 - INFO - Model outputs: ['predictions']\n",
"2023-12-12 14:13:26,307 - INFO - ONNX model is saved at mysavedmodel.onnx\n"
]
}
],
Expand Down Expand Up @@ -180,7 +183,7 @@
],
"source": [
"# verify the ONNX model is valid\n",
"onnx_model = onnx.load(onnx_savedmodel)\n",
"onnx_model = onnx.load(Path(onnx_savedmodel))\n",
"onnx.checker.check_model(onnx_model)\n",
"\n",
"# get ONNX predictions\n",
Expand Down Expand Up @@ -214,7 +217,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.10"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
7 changes: 4 additions & 3 deletions tutorials/conversion_onnx/tensorflow2onnx.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"source": [
"import os\n",
"import numpy as np\n",
"from pathlib import Path\n",
"import tensorflow as tf\n",
"import onnx\n",
"import onnxruntime as ort\n",
Expand Down Expand Up @@ -323,12 +324,12 @@
"for model, fname in models.items():\n",
"\n",
" # verify the ONNX model is valid\n",
" onnx_model = onnx.load(fname)\n",
" onnx_model = onnx.load(Path(fname))\n",
" onnx.checker.check_model(onnx_model)\n",
"\n",
" \n",
" # get ONNX predictions\n",
" sess = ort.InferenceSession(fname)\n",
" sess = ort.InferenceSession(Path(fname))\n",
" input_name = sess.get_inputs()[0].name\n",
" output_name = sess.get_outputs()[0].name\n",
" \n",
Expand Down Expand Up @@ -358,7 +359,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.10.13"
},
"orig_nbformat": 4
},
Expand Down

0 comments on commit d2b49c9

Please sign in to comment.