{ "cells": [ { "cell_type": "markdown", "id": "4dcf9bb1", "metadata": {}, "source": [ "## MXNet 1.8: Getting Started with Gluon Tutorial\n", "\n", "In this tutorial you will compile and deploy resnet-50 using the newly supported MXNet 1.8 and Gluon API on an Inf1 instance. This tutorial is only supported with MXNet 1.8.\n", "\n", "This Jupyter notebook should be run on an inf1.6xlarge instance since you will be loading and compiling several large models.\n", "\n", "To run this tutorial, please make sure you deactivate any existing MXNet conda environments you already using. Install MXNet 1.8 by following the instructions at [MXNet Setup Guide](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-intro/mxnet-setup/mxnet-install.html#install-neuron-mxnet). You would also need to change your kernel to use the correct Python environment setup earlier by clicking Kerenel->Change Kernel->Python (Neuron MXNet)" ] }, { "cell_type": "markdown", "id": "83eb578b", "metadata": {}, "source": [ "## Compile\n", "\n", "A trained model must be compiled to Inferentia target before it can run on Inferentia. In this step we compile a pre-trained ResNet50 and export it as a compiled MXNet checkpoint.\n", "\n", "Compilation will take a few minutes. At the end of compilation, the files resnet-50_compiled-0000.params and resnet-50_compiled-symbol.json will be created in local directory.\n", "\n", "To check the supported operations for the uncompiled model or information on Neuron subgraphs for the compiled model, please see [Neuron Check Model](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/neuron-tools/tutorial-neuron-check-model.html#neuron-check-model)." ] }, { "cell_type": "code", "execution_count": null, "id": "88c41e01", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import os\n", "import mxnet as mx\n", "import mx_neuron as neuron\n", "import numpy as np\n", "\n", "path='http://data.mxnet.io/models/imagenet/'\n", "mx.test_utils.download(path+'resnet/50-layers/resnet-50-0000.params')\n", "mx.test_utils.download(path+'resnet/50-layers/resnet-50-symbol.json')\n", "block = mx.gluon.nn.SymbolBlock.imports('resnet-50-symbol.json',\\\n", " ['data', 'softmax_label'], 'resnet-50-0000.params', ctx=mx.cpu())\n", "\n", "block.hybridize()\n", "\n", "# Compile for Inferentia using Neuron\n", "inputs = { \"data\" : mx.nd.ones([1,3,224,224], name='data', dtype='float32'), 'softmax_label' : mx.nd.ones([1], name='data', dtype='float32') }\n", "block = neuron.compile(block, inputs=inputs)\n", "\n", "#save compiled model\n", "block.export(\"resnet-50_compiled\", 0, block)" ] }, { "cell_type": "code", "execution_count": null, "id": "6337e0ec", "metadata": {}, "outputs": [], "source": [ "!ls" ] }, { "cell_type": "markdown", "id": "5a9af0c7", "metadata": {}, "source": [ "## Deploy\n", "\n", "Deply on Infenrentia to see the inference results as below:\n", "```\n", "probability=0.643591, class=n02123045 tabby, tabby cat\n", "probability=0.184392, class=n02123159 tiger cat\n", "probability=0.105063, class=n02124075 Egyptian cat\n", "probability=0.030101, class=n02127052 lynx, catamount\n", "probability=0.016112, class=n02129604 tiger, Panthera tigris\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "960c6aa9", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import mxnet as mx\n", "import mx_neuron as neuron\n", "\n", "path='http://data.mxnet.io/models/imagenet/'\n", "mx.test_utils.download(path+'synset.txt')\n", "\n", "fname = mx.test_utils.download('https://raw.githubusercontent.com/awslabs/mxnet-model-server/master/docs/images/kitten_small.jpg?raw=true')\n", "img = mx.image.imread(fname)# convert into format (batch, RGB, width, height)\n", "img = mx.image.imresize(img, 224, 224) # resize\n", "img = img.transpose((2, 0, 1)) # Channel first\n", "img = img.expand_dims(axis=0) # batchify\n", "img = img.astype(dtype='float32')\n", "\n", "block = mx.gluon.nn.SymbolBlock.imports('resnet-50_compiled-symbol.json',\\\n", " ['data', 'softmax_label'], 'resnet-50_compiled-0000.params', ctx=mx.cpu())\n", "softmax = mx.nd.random_normal(shape=(1,))\n", "\n", "out = block(img, softmax).asnumpy()\n", "\n", "with open('synset.txt', 'r') as f:\n", " labels = [l.rstrip() for l in f]\n", "\n", "out = block(img, softmax).asnumpy()\n", "\n", "prob = np.squeeze(out)\n", "a = np.argsort(prob)[::-1]\n", "for i in a[0:5]:\n", " print('probability=%f, class=%s' %(prob[i], labels[i]))" ] }, { "cell_type": "raw", "id": "4f15e776", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "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.9" } }, "nbformat": 4, "nbformat_minor": 5 }