|
| 1 | +{ |
| 2 | + "nbformat": 4, |
| 3 | + "nbformat_minor": 0, |
| 4 | + "metadata": { |
| 5 | + "colab": { |
| 6 | + "name": "alexnet.ipynb", |
| 7 | + "provenance": [], |
| 8 | + "collapsed_sections": [] |
| 9 | + }, |
| 10 | + "kernelspec": { |
| 11 | + "name": "python3", |
| 12 | + "display_name": "Python 3" |
| 13 | + } |
| 14 | + }, |
| 15 | + "cells": [ |
| 16 | + { |
| 17 | + "cell_type": "markdown", |
| 18 | + "metadata": { |
| 19 | + "id": "_KePiywrVHG2", |
| 20 | + "colab_type": "text" |
| 21 | + }, |
| 22 | + "source": [ |
| 23 | + "# Export Alexnet from Torchvision Models\n", |
| 24 | + "In this notebook we convert Alexnet to ONNX and upload it to S3 where it can be deployed by Cortex\n", |
| 25 | + "\n", |
| 26 | + "Based on: [PytorchOnnxExport](https://github.com/onnx/tutorials/blob/master/tutorials/PytorchOnnxExport.ipynb)" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "metadata": { |
| 32 | + "id": "dzphLNy5VswD", |
| 33 | + "colab_type": "text" |
| 34 | + }, |
| 35 | + "source": [ |
| 36 | + "## Install dependencies" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "metadata": { |
| 42 | + "id": "N69aGD72Is4t", |
| 43 | + "colab_type": "code", |
| 44 | + "colab": {} |
| 45 | + }, |
| 46 | + "source": [ |
| 47 | + "!pip install torch==1.2.* torchvision==0.4.*" |
| 48 | + ], |
| 49 | + "execution_count": 0, |
| 50 | + "outputs": [] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "metadata": { |
| 55 | + "id": "2raEvUmojKhK", |
| 56 | + "colab_type": "text" |
| 57 | + }, |
| 58 | + "source": [ |
| 59 | + "## Download and Export Model\n", |
| 60 | + "Download the pretrained Alexnet Model and export to ONNX model format:" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "metadata": { |
| 66 | + "id": "zKuFyRTlJUkd", |
| 67 | + "colab_type": "code", |
| 68 | + "colab": {} |
| 69 | + }, |
| 70 | + "source": [ |
| 71 | + "import torch\n", |
| 72 | + "import torch.onnx\n", |
| 73 | + "import torchvision\n", |
| 74 | + "\n", |
| 75 | + "# Standard ImageNet input - 3 channels, 224x224,\n", |
| 76 | + "# values don't matter since we only care about network structure.\n", |
| 77 | + "dummy_input = torch.randn(1, 3, 224, 224)\n", |
| 78 | + "\n", |
| 79 | + "# We are going to use a Pretrained alexnet model\n", |
| 80 | + "model = torchvision.models.alexnet(pretrained=True)\n", |
| 81 | + "\n", |
| 82 | + "# Export to ONNX\n", |
| 83 | + "torch.onnx.export(model, dummy_input, \"alexnet.onnx\")" |
| 84 | + ], |
| 85 | + "execution_count": 0, |
| 86 | + "outputs": [] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "metadata": { |
| 91 | + "id": "4YvEPLmljaMT", |
| 92 | + "colab_type": "text" |
| 93 | + }, |
| 94 | + "source": [ |
| 95 | + "## Upload the model to AWS\n", |
| 96 | + "Cortex loads models from AWS, so we need to upload the exported model.\n", |
| 97 | + "\n", |
| 98 | + "Set these variables to configure your AWS credentials and model upload path:" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "metadata": { |
| 104 | + "id": "y-SAhUH-Jlo_", |
| 105 | + "colab_type": "code", |
| 106 | + "cellView": "form", |
| 107 | + "colab": {} |
| 108 | + }, |
| 109 | + "source": [ |
| 110 | + "AWS_ACCESS_KEY_ID = \"\" #@param {type:\"string\"}\n", |
| 111 | + "AWS_SECRET_ACCESS_KEY = \"\" #@param {type:\"string\"}\n", |
| 112 | + "S3_UPLOAD_PATH = \"s3://my-bucket/image-classifier/alexnet.onnx\" #@param {type:\"string\"}\n", |
| 113 | + "\n", |
| 114 | + "import sys\n", |
| 115 | + "import re\n", |
| 116 | + "\n", |
| 117 | + "if AWS_ACCESS_KEY_ID == \"\":\n", |
| 118 | + " print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Please set AWS_ACCESS_KEY_ID\"), file=sys.stderr)\n", |
| 119 | + "\n", |
| 120 | + "elif AWS_SECRET_ACCESS_KEY == \"\":\n", |
| 121 | + " print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Please set AWS_SECRET_ACCESS_KEY\"), file=sys.stderr)\n", |
| 122 | + "\n", |
| 123 | + "else:\n", |
| 124 | + " try:\n", |
| 125 | + " bucket = re.search(\"s3://(.+?)/\", S3_UPLOAD_PATH).group(1)\n", |
| 126 | + " key = re.search(\"s3://.+?/(.+)\", S3_UPLOAD_PATH).group(1)\n", |
| 127 | + " except:\n", |
| 128 | + " print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Invalid s3 path (should be of the form s3://my-bucket/path/to/file)\"), file=sys.stderr)" |
| 129 | + ], |
| 130 | + "execution_count": 0, |
| 131 | + "outputs": [] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "markdown", |
| 135 | + "metadata": { |
| 136 | + "id": "HmvoV7v96jip", |
| 137 | + "colab_type": "text" |
| 138 | + }, |
| 139 | + "source": [ |
| 140 | + "Upload the model to S3:" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "metadata": { |
| 146 | + "id": "--va3L2KNBHX", |
| 147 | + "colab_type": "code", |
| 148 | + "colab": {} |
| 149 | + }, |
| 150 | + "source": [ |
| 151 | + "import boto3\n", |
| 152 | + "\n", |
| 153 | + "s3 = boto3.client(\"s3\", aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)\n", |
| 154 | + "print(\"Uploading {} ...\".format(S3_UPLOAD_PATH), end = '')\n", |
| 155 | + "s3.upload_file(\"alexnet.onnx\", bucket, key)\n", |
| 156 | + "print(\" ✓\")" |
| 157 | + ], |
| 158 | + "execution_count": 0, |
| 159 | + "outputs": [] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "markdown", |
| 163 | + "metadata": { |
| 164 | + "id": "acHZMDxqjnNQ", |
| 165 | + "colab_type": "text" |
| 166 | + }, |
| 167 | + "source": [ |
| 168 | + "<!-- CORTEX_VERSION_MINOR -->\n", |
| 169 | + "That's it! See the [example on GitHub](https://github.com/cortexlabs/cortex/tree/master/examples/alexnet) for how to deploy the model as an API." |
| 170 | + ] |
| 171 | + } |
| 172 | + ] |
| 173 | +} |
0 commit comments