Last active
April 25, 2021 17:54
-
-
Save helena-intel/825b43debad4ec0dbc051d8a8005096c to your computer and use it in GitHub Desktop.
001-hello-world
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"cells": [{"cell_type": "markdown", "id": "miniature-figure", "metadata": {}, "source": "# Hello World\n\nA very basic introduction to OpenVINO that shows how to do inference on a given IR model.\n\nWe use a [MobileNetV3 model](https://github.com/openvinotoolkit/open_model_zoo/blob/master/models/public/mobilenet-v3-small-1.0-224-tf/mobilenet-v3-small-1.0-224-tf.md) from [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo/). See the [Tensorflow to OpenVINO Notebook](101-tensorflow-to-openvino) for information on how this OpenVINO IR model was created.\n\n"}, {"id": "dff9e33c", "cell_type": "markdown", "source": "## Preparation\n\nInstall the requirements and download the files that are necessary for running this notebook.\n\n**NOTE:** installation may take a while. It is recommended to restart the Jupyter kernel after installing the packages. Choose *Kernel->Restart Kernel* in Jupyter Notebook or Lab, or *Runtime->Restart runtime* in Google Colab.", "metadata": {}}, {"id": "a340f279", "cell_type": "code", "metadata": {}, "execution_count": null, "source": "# Install or upgrade required Python packages. Install specific versions of some packages to ensure compatibility.\n!pip install openvino-dev opencv-python-headless==4.2.0.32 ipython>7.0 ipywidgets>=7.4", "outputs": []}, {"id": "b511747d", "cell_type": "code", "metadata": {}, "execution_count": null, "source": "# Download image and model files\nimport os\nimport pip\nimport urllib.parse\nimport urllib.request\nfrom pathlib import Path\n\nurls = ['https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/main/notebooks/001-hello-world/imagenet_class_index.json', 'https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/main/notebooks/001-hello-world/v3-small_224_1.0_float.bin', 'https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/main/notebooks/001-hello-world/coco.jpg', 'https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/main/notebooks/001-hello-world/v3-small_224_1.0_float.xml']\n\nnotebook_url = \"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/main\"\n\nfor url in urls:\n save_path = Path(url).relative_to(fr\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/main/notebooks/001-hello-world\")\n os.makedirs(save_path.parent, exist_ok=True)\n safe_url = urllib.parse.quote(url, safe=\":/\")\n\n urllib.request.urlretrieve(safe_url, save_path.as_posix())", "outputs": []}, {"cell_type": "markdown", "id": "narrative-officer", "metadata": {}, "source": "## Imports"}, {"cell_type": "code", "execution_count": null, "id": "european-delivery", "metadata": {}, "outputs": [], "source": "import json\n\nimport cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom openvino.inference_engine import IECore"}, {"cell_type": "markdown", "id": "spiritual-bouquet", "metadata": {}, "source": "## Load the network"}, {"cell_type": "code", "execution_count": null, "id": "statewide-growth", "metadata": {}, "outputs": [], "source": "ie = IECore()\nnet = ie.read_network(\n model=\"v3-small_224_1.0_float.xml\", weights=\"v3-small_224_1.0_float.bin\"\n)\nexec_net = ie.load_network(net, \"CPU\")\n\ninput_key = list(exec_net.input_info)[0]\noutput_key = list(exec_net.outputs.keys())[0]"}, {"cell_type": "markdown", "id": "convertible-literacy", "metadata": {}, "source": "## Load an Image"}, {"cell_type": "code", "execution_count": null, "id": "simplified-accommodation", "metadata": {}, "outputs": [], "source": "# The MobileNet network expects images in RGB format\nimage = cv2.cvtColor(cv2.imread(\"coco.jpg\"), cv2.COLOR_BGR2RGB)\ninput_image = cv2.resize(image, (224, 224)) # resize to MobileNet image shape\ninput_image = np.expand_dims(\n input_image.transpose(2, 0, 1), 0\n) # reshape to network input shape\nplt.imshow(image)"}, {"cell_type": "markdown", "id": "warming-platinum", "metadata": {}, "source": "## Do Inference"}, {"cell_type": "code", "execution_count": null, "id": "italic-comment", "metadata": {}, "outputs": [], "source": "result = exec_net.infer(inputs={input_key: input_image})[output_key]\nresult_index = np.argmax(result)"}, {"cell_type": "code", "execution_count": null, "id": "complicated-command", "metadata": {}, "outputs": [], "source": "# Convert the inference result to a class name.\nimagenet_classes = json.loads(open(\"imagenet_class_index.json\").read())\n# The model description states that for this model, class 0 is background,\n# so we add 1 to the network output to get the class name\nimagenet_classes = {\n int(key) + 1: value for key, value in imagenet_classes.items()\n}\nimagenet_classes[result_index]"}], "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.6.8"}}, "nbformat": 4, "nbformat_minor": 5} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libpython3.7-dev |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment