Skip to content

Commit 70c1886

Browse files
authored
Convert example model generation to notebooks (#480)
1 parent 4489238 commit 70c1886

22 files changed

+3018
-2050
lines changed

examples/image-classifier/cortex.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
- kind: api
55
name: classifier
6-
model: s3://cortex-examples/imagenet
7-
request_handler: image.py
6+
model: s3://cortex-examples/inception
7+
request_handler: handler.py
88
tracker:
99
model_type: classification
File renamed without changes.
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "inception.ipynb",
7+
"provenance": [],
8+
"collapsed_sections": []
9+
},
10+
"kernelspec": {
11+
"display_name": "Python 3",
12+
"language": "python",
13+
"name": "python3"
14+
},
15+
"language_info": {
16+
"file_extension": ".py",
17+
"mimetype": "text/x-python",
18+
"name": "python",
19+
"nbconvert_exporter": "python",
20+
"pygments_lexer": "ipython3",
21+
"version": "3.6.8"
22+
}
23+
},
24+
"cells": [
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {
28+
"id": "n8CwINQcEBKz",
29+
"colab_type": "text"
30+
},
31+
"source": [
32+
"# Exporting ImageNet Inception\n",
33+
"In this notebook, we'll show how to export the [pre-trained Imagenet Inception model](https://tfhub.dev/google/imagenet/inception_v3/classification/3) for serving."
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {
39+
"id": "3221z3P69fgf",
40+
"colab_type": "text"
41+
},
42+
"source": [
43+
"First, we'll install the required packages:"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"metadata": {
49+
"id": "_SdQpq7g9LiI",
50+
"colab_type": "code",
51+
"colab": {}
52+
},
53+
"source": [
54+
"!pip install tensorflow==1.14.* tensorflow-hub==0.6.* boto3==1.*"
55+
],
56+
"execution_count": 0,
57+
"outputs": []
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {
62+
"id": "I-k0gUpxDGkU",
63+
"colab_type": "text"
64+
},
65+
"source": [
66+
"Next, we'll download the model from TensorFlow Hub and export it for serving:"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"metadata": {
72+
"id": "z6QLCzB4BKMe",
73+
"colab_type": "code",
74+
"colab": {}
75+
},
76+
"source": [
77+
"import tensorflow as tf\n",
78+
"import tensorflow_hub as hub\n",
79+
"from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def\n",
80+
"\n",
81+
"export_dir = \"export/1\"\n",
82+
"builder = tf.saved_model.builder.SavedModelBuilder(export_dir)\n",
83+
"\n",
84+
"with tf.Session(graph=tf.Graph()) as sess:\n",
85+
" module = hub.Module(\"https://tfhub.dev/google/imagenet/inception_v3/classification/3\")\n",
86+
"\n",
87+
" input_params = module.get_input_info_dict()\n",
88+
" image_input = tf.placeholder(\n",
89+
" name=\"images\", dtype=input_params[\"images\"].dtype, shape=input_params[\"images\"].get_shape()\n",
90+
" )\n",
91+
" \n",
92+
" sess.run([tf.global_variables_initializer(), tf.tables_initializer()])\n",
93+
"\n",
94+
" classes = module(image_input)\n",
95+
" signature = predict_signature_def(inputs={\"images\": image_input}, outputs={\"classes\": classes})\n",
96+
"\n",
97+
" builder.add_meta_graph_and_variables(\n",
98+
" sess, [\"serve\"], signature_def_map={\"predict\": signature}, strip_default_attrs=True\n",
99+
" )\n",
100+
"\n",
101+
"builder.save()"
102+
],
103+
"execution_count": 0,
104+
"outputs": []
105+
},
106+
{
107+
"cell_type": "markdown",
108+
"metadata": {
109+
"id": "aGtJiyEnBgwl",
110+
"colab_type": "text"
111+
},
112+
"source": [
113+
"## Upload the model to AWS\n",
114+
"\n",
115+
"Cortex loads models from AWS, so we need to upload the exported model."
116+
]
117+
},
118+
{
119+
"cell_type": "markdown",
120+
"metadata": {
121+
"id": "fTkjvSKBBmUB",
122+
"colab_type": "text"
123+
},
124+
"source": [
125+
"Set these variables to configure your AWS credentials and model upload path:"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"metadata": {
131+
"id": "4xcDWxqCBPre",
132+
"colab_type": "code",
133+
"colab": {},
134+
"cellView": "form"
135+
},
136+
"source": [
137+
"AWS_ACCESS_KEY_ID = \"\" #@param {type:\"string\"}\n",
138+
"AWS_SECRET_ACCESS_KEY = \"\" #@param {type:\"string\"}\n",
139+
"S3_UPLOAD_PATH = \"s3://my-bucket/inception\" #@param {type:\"string\"}\n",
140+
"\n",
141+
"import sys\n",
142+
"import re\n",
143+
"\n",
144+
"if AWS_ACCESS_KEY_ID == \"\":\n",
145+
" print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Please set AWS_ACCESS_KEY_ID\"), file=sys.stderr)\n",
146+
"\n",
147+
"elif AWS_SECRET_ACCESS_KEY == \"\":\n",
148+
" print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Please set AWS_SECRET_ACCESS_KEY\"), file=sys.stderr)\n",
149+
"\n",
150+
"else:\n",
151+
" try:\n",
152+
" bucket = re.search(\"s3://(.+?)/\", S3_UPLOAD_PATH).group(1)\n",
153+
" key = re.search(\"s3://.+?/(.+)\", S3_UPLOAD_PATH).group(1)\n",
154+
" except:\n",
155+
" print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Invalid s3 path (should be of the form s3://my-bucket/path/to/file)\"), file=sys.stderr)"
156+
],
157+
"execution_count": 0,
158+
"outputs": []
159+
},
160+
{
161+
"cell_type": "markdown",
162+
"metadata": {
163+
"id": "czZkjb1IBr-f",
164+
"colab_type": "text"
165+
},
166+
"source": [
167+
"Upload the model to S3:"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"metadata": {
173+
"id": "M0b0IbyaBsim",
174+
"colab_type": "code",
175+
"colab": {}
176+
},
177+
"source": [
178+
"import os\n",
179+
"import boto3\n",
180+
"\n",
181+
"s3 = boto3.client(\"s3\", aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)\n",
182+
"\n",
183+
"for dirpath, _, filenames in os.walk(\"export\"):\n",
184+
" for filename in filenames:\n",
185+
" filepath = os.path.join(dirpath, filename)\n",
186+
" filekey = os.path.join(key, filepath[len(\"export/\"):])\n",
187+
" print(\"Uploading s3://{}/{}...\".format(bucket, filekey), end = '')\n",
188+
" s3.upload_file(filepath, bucket, filekey)\n",
189+
" print(\"\")",
190+
"\n",
191+
"print(\"\\nUploaded model export directory to \" + S3_UPLOAD_PATH)"
192+
],
193+
"execution_count": 0,
194+
"outputs": []
195+
},
196+
{
197+
"cell_type": "markdown",
198+
"metadata": {
199+
"id": "pZQWoeZbE7Wc",
200+
"colab_type": "text"
201+
},
202+
"source": [
203+
"<!-- CORTEX_VERSION_MINOR -->\n",
204+
"That's it! See the [example on GitHub](https://github.com/cortexlabs/cortex/tree/master/examples/image-classifier) for how to deploy the model as an API."
205+
]
206+
}
207+
]
208+
}

examples/image-classifier/load_inception_from_tf_hub.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)