From d39ea25e98f6ad6c0e634c24c5cd2244f564a0c7 Mon Sep 17 00:00:00 2001 From: FORCHA Date: Thu, 23 Dec 2021 10:22:44 +0100 Subject: [PATCH] Update nn-meter_predictor_for_different_model_format.ipynb (#52) --- ...predictor_for_different_model_format.ipynb | 255 +++++++++--------- 1 file changed, 128 insertions(+), 127 deletions(-) diff --git a/examples/nn-meter_predictor_for_different_model_format.ipynb b/examples/nn-meter_predictor_for_different_model_format.ipynb index ce4cfdc4..26981dcc 100644 --- a/examples/nn-meter_predictor_for_different_model_format.ipynb +++ b/examples/nn-meter_predictor_for_different_model_format.ipynb @@ -2,26 +2,20 @@ "cells": [ { "cell_type": "markdown", + "metadata": {}, "source": [ "# Use nn-Meter for different model format\n", "In this notebook, we showed nn-Meter examples of latency prediction for different model formats of Tensorflow, PyTorch, ONNX." - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 1, - "source": [ - "# list all supporting latency predictors\n", - "import nn_meter\n", - "predictors = nn_meter.list_latency_predictors()\n", - "for p in predictors:\n", - " print(f\"[Predictor] {p['name']}: version={p['version']}\")" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[Predictor] cortexA76cpu_tflite21: version=1.0\n", "[Predictor] adreno640gpu_tflite21: version=1.0\n", @@ -30,13 +24,22 @@ ] } ], - "metadata": {} + "source": [ + "# list all supporting latency predictors\n", + "import nn_meter\n", + "predictors = nn_meter.list_latency_predictors()\n", + "for p in predictors:\n", + " print(f\"[Predictor] {p['name']}: version={p['version']}\")" + ] }, { "cell_type": "code", "execution_count": 2, + "metadata": {}, + "outputs": [], "source": [ "# define basic information\n", + "import os\n", "__test_models_folder__ = '../data'\n", "os.makedirs(__test_models_folder__, exist_ok=True)\n", "\n", @@ -46,49 +49,23 @@ "\n", "import warnings\n", "warnings.filterwarnings('ignore')" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Use nn-Meter for Tensorflow pb File" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 3, - "source": [ - "import os\n", - "from glob import glob\n", - "import nn_meter\n", - "\n", - "# download data and unzip\n", - "ppath = os.path.join(__test_models_folder__, \"pb_models\")\n", - "if not os.path.isdir(ppath):\n", - " os.mkdir(ppath)\n", - " url = \"https://github.com/microsoft/nn-Meter/releases/download/v1.0-data/pb_models.zip\"\n", - " nn_meter.download_from_url(url, ppath)\n", - "\n", - "test_model_list = glob(ppath + \"/**.pb\")\n", - "\n", - "# load predictor\n", - "predictor = nn_meter.load_latency_predictor(predictor_name, predictor_version)\n", - "\n", - "# predict latency\n", - "result = {}\n", - "for test_model in test_model_list:\n", - " latency = predictor.predict(test_model, model_type=\"pb\") # in unit of ms\n", - " result[os.path.basename(test_model)] = latency\n", - " print(f'[RESULT] predict latency for {test_model}: {latency} ms')\n", - " " - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[RESULT] predict latency for ../data/pb_models/alexnet_0.pb: 13.124763483485053 ms\n", "[RESULT] predict latency for ../data/pb_models/densenet_0.pb: 73.65728637938379 ms\n", @@ -111,18 +88,63 @@ ] } ], - "metadata": {} + "source": [ + "import os\n", + "from glob import glob\n", + "import nn_meter\n", + "\n", + "# download data and unzip\n", + "ppath = os.path.join(__test_models_folder__, \"pb_models\")\n", + "if not os.path.isdir(ppath):\n", + " os.mkdir(ppath)\n", + " url = \"https://github.com/microsoft/nn-Meter/releases/download/v1.0-data/pb_models.zip\"\n", + " nn_meter.download_from_url(url, ppath)\n", + "\n", + "test_model_list = glob(ppath + \"/**.pb\")\n", + "\n", + "# load predictor\n", + "predictor = nn_meter.load_latency_predictor(predictor_name, predictor_version)\n", + "\n", + "# predict latency\n", + "result = {}\n", + "for test_model in test_model_list:\n", + " latency = predictor.predict(test_model, model_type=\"pb\") # in unit of ms\n", + " result[os.path.basename(test_model)] = latency\n", + " print(f'[RESULT] predict latency for {test_model}: {latency} ms')\n", + " " + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Use nn-Meter for PyTorch model\n" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[RESULT] predict latency for resnet18: 39.32351677226426 ms\n", + "[RESULT] predict latency for alexnet: 13.126684104716283 ms\n", + "[RESULT] predict latency for vgg16: 219.2647723703139 ms\n", + "[RESULT] predict latency for squeezenet: 18.674223659837843 ms\n", + "[RESULT] predict latency for densenet161: 186.56037984132988 ms\n", + "[RESULT] predict latency for inception_v3: 127.98419924992326 ms\n", + "[RESULT] predict latency for googlenet: 32.758087458683384 ms\n", + "[RESULT] predict latency for shufflenet_v2: 5.423898780782251 ms\n", + "[RESULT] predict latency for mobilenet_v2: 9.920667346583885 ms\n", + "[RESULT] predict latency for resnext50_32x4d: 230.96098225315293 ms\n", + "[RESULT] predict latency for wide_resnet50_2: 230.96098225315293 ms\n", + "[RESULT] predict latency for mnasnet: 11.630591102084342 ms\n" + ] + } + ], "source": [ "import os\n", "import torchvision.models as models\n", @@ -149,67 +171,23 @@ "for model_name in torchvision_models:\n", " latency = predictor.predict(torchvision_models[model_name], model_type=\"torch\", input_shape=(1, 3, 224, 224)) \n", " print(f'[RESULT] predict latency for {model_name}: {latency} ms')" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "[RESULT] predict latency for resnet18: 39.32351677226426 ms\n", - "[RESULT] predict latency for alexnet: 13.126684104716283 ms\n", - "[RESULT] predict latency for vgg16: 219.2647723703139 ms\n", - "[RESULT] predict latency for squeezenet: 18.674223659837843 ms\n", - "[RESULT] predict latency for densenet161: 186.56037984132988 ms\n", - "[RESULT] predict latency for inception_v3: 127.98419924992326 ms\n", - "[RESULT] predict latency for googlenet: 32.758087458683384 ms\n", - "[RESULT] predict latency for shufflenet_v2: 5.423898780782251 ms\n", - "[RESULT] predict latency for mobilenet_v2: 9.920667346583885 ms\n", - "[RESULT] predict latency for resnext50_32x4d: 230.96098225315293 ms\n", - "[RESULT] predict latency for wide_resnet50_2: 230.96098225315293 ms\n", - "[RESULT] predict latency for mnasnet: 11.630591102084342 ms\n" - ] - } - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Use nn-Meter for ONNX File" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 21, - "source": [ - "import os\n", - "from glob import glob\n", - "import nn_meter\n", - "\n", - "# download data and unzip\n", - "ppath = os.path.join(__test_models_folder__, \"onnx_models\")\n", - "if not os.path.isdir(ppath):\n", - " os.mkdir(ppath)\n", - " url = \"https://github.com/microsoft/nn-Meter/releases/download/v1.0-data/onnx_models.zip\"\n", - " nn_meter.download_from_url(url, ppath)\n", - "\n", - "test_model_list = glob(ppath + \"/**.onnx\")\n", - "\n", - "# load predictor\n", - "predictor = nn_meter.load_latency_predictor(predictor_name, predictor_version)\n", - "\n", - "# predict latency\n", - "result = {}\n", - "for test_model in test_model_list:\n", - " latency = predictor.predict(test_model, model_type=\"onnx\") # in unit of ms\n", - " result[os.path.basename(test_model)] = latency\n", - " print(f'[RESULT] predict latency for {os.path.basename(test_model)}: {latency} ms')" - ], + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[RESULT] predict latency for alexnet_0.onnx: 13.12668410471628 ms\n", "[RESULT] predict latency for densenet_0.onnx: 186.5603798413299 ms\n", @@ -225,31 +203,19 @@ ] } ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "# Use nn-Meter for nn-Meter IR Graph" - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 23, "source": [ "import os\n", "from glob import glob\n", "import nn_meter\n", "\n", "# download data and unzip\n", - "ppath = os.path.join(__test_models_folder__, \"nnmeter_ir_graphs\")\n", + "ppath = os.path.join(__test_models_folder__, \"onnx_models\")\n", "if not os.path.isdir(ppath):\n", " os.mkdir(ppath)\n", - " url = \"https://github.com/microsoft/nn-Meter/releases/download/v1.0-data/ir_graphs.zip\"\n", + " url = \"https://github.com/microsoft/nn-Meter/releases/download/v1.0-data/onnx_models.zip\"\n", " nn_meter.download_from_url(url, ppath)\n", "\n", - "test_model_list = glob(ppath + \"/**.json\")\n", + "test_model_list = glob(ppath + \"/**.onnx\")\n", "\n", "# load predictor\n", "predictor = nn_meter.load_latency_predictor(predictor_name, predictor_version)\n", @@ -257,14 +223,26 @@ "# predict latency\n", "result = {}\n", "for test_model in test_model_list:\n", - " latency = predictor.predict(test_model, model_type=\"nnmeter-ir\") # in unit of ms\n", + " latency = predictor.predict(test_model, model_type=\"onnx\") # in unit of ms\n", " result[os.path.basename(test_model)] = latency\n", " print(f'[RESULT] predict latency for {os.path.basename(test_model)}: {latency} ms')" - ], + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Use nn-Meter for nn-Meter IR Graph" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[RESULT] predict latency for alexnet_0.json: 13.124763483485058 ms\n", "[RESULT] predict latency for densenet_0.json: 73.65728637938379 ms\n", @@ -287,31 +265,54 @@ ] } ], - "metadata": {} + "source": [ + "import os\n", + "from glob import glob\n", + "import nn_meter\n", + "\n", + "# download data and unzip\n", + "ppath = os.path.join(__test_models_folder__, \"nnmeter_ir_graphs\")\n", + "if not os.path.isdir(ppath):\n", + " os.mkdir(ppath)\n", + " url = \"https://github.com/microsoft/nn-Meter/releases/download/v1.0-data/ir_graphs.zip\"\n", + " nn_meter.download_from_url(url, ppath)\n", + "\n", + "test_model_list = glob(ppath + \"/**.json\")\n", + "\n", + "# load predictor\n", + "predictor = nn_meter.load_latency_predictor(predictor_name, predictor_version)\n", + "\n", + "# predict latency\n", + "result = {}\n", + "for test_model in test_model_list:\n", + " latency = predictor.predict(test_model, model_type=\"nnmeter-ir\") # in unit of ms\n", + " result[os.path.basename(test_model)] = latency\n", + " print(f'[RESULT] predict latency for {os.path.basename(test_model)}: {latency} ms')" + ] } ], "metadata": { - "orig_nbformat": 4, + "interpreter": { + "hash": "725f784512f384579f6470be215ba42b52a9bbd25ecead6d24b2cff5bb6ad2c7" + }, + "kernelspec": { + "display_name": "Python 3.6.10 64-bit ('py36': conda)", + "name": "python3" + }, "language_info": { - "name": "python", - "version": "3.6.10", - "mimetype": "text/x-python", "codemirror_mode": { "name": "ipython", "version": 3 }, - "pygments_lexer": "ipython3", + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", "nbconvert_exporter": "python", - "file_extension": ".py" - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3.6.10 64-bit ('py36': conda)" + "pygments_lexer": "ipython3", + "version": "3.6.10" }, - "interpreter": { - "hash": "725f784512f384579f6470be215ba42b52a9bbd25ecead6d24b2cff5bb6ad2c7" - } + "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 -} \ No newline at end of file +}