Skip to content

Notebooks #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
19a8d8a
Create xgboost.ipynb
deliahu Sep 18, 2019
af92bfd
Delete xgboost_model.py
deliahu Sep 18, 2019
f7eb0b0
Add TensorFlow Iris
deliahu Sep 18, 2019
110d570
Add sklearn
deliahu Sep 18, 2019
dcc663e
Add pytorch
deliahu Sep 18, 2019
9d8c8dd
Delete pytorch_model.py
deliahu Sep 18, 2019
fb9bdcb
Add keras
deliahu Sep 18, 2019
3a242ca
Delete requirements.txt
deliahu Sep 18, 2019
daf44cf
Update xgboost.ipynb
deliahu Sep 18, 2019
130e502
Update tensorflow
deliahu Sep 18, 2019
e934754
Update sklearn.ipynb
deliahu Sep 18, 2019
f110a1a
Update pytorch.ipynb
deliahu Sep 18, 2019
32730ee
Update keras.ipynb
deliahu Sep 18, 2019
1df8c31
Update sklearn.ipynb
deliahu Sep 18, 2019
ccda27a
Copy bert from other PR
deliahu Sep 18, 2019
64f59f6
Update wording
deliahu Sep 18, 2019
788eea4
Update bert
deliahu Sep 18, 2019
5acadf7
Update train.ipynb
deliahu Sep 18, 2019
95c1b32
Downgrade pandas
deliahu Sep 18, 2019
5e62804
Add boto3
deliahu Sep 18, 2019
246939b
Use tensorflow-gpu
deliahu Sep 18, 2019
16e00b4
Update bert pip versions
deliahu Sep 19, 2019
731a552
Improvements
deliahu Sep 19, 2019
f82b727
Move bert notebook
deliahu Sep 19, 2019
6744b05
Create imagenet_export.ipynb
deliahu Sep 19, 2019
1ffefc3
Delete load_inception_from_tf_hub.py
deliahu Sep 19, 2019
b349125
WIP - gpt2 notebook
deliahu Sep 19, 2019
104f110
Update prints
deliahu Sep 19, 2019
2758f02
Remove error message whitespace
deliahu Sep 19, 2019
6c0e7fe
Update gpt2.ipynb
deliahu Sep 19, 2019
5c930de
Merge branch 'master' of github.com:cortexlabs/cortex into notebooks
deliahu Sep 19, 2019
6273596
Update sklearn.ipynb
deliahu Sep 19, 2019
69e58d4
Format code
deliahu Sep 19, 2019
484de21
Remove unecessary text
deliahu Sep 20, 2019
e83a2ca
Typo
deliahu Sep 20, 2019
68e6aa7
Update model dir uploaded messages
deliahu Sep 20, 2019
489ef79
Rename
deliahu Sep 20, 2019
565bd05
Merge branch 'master' of github.com:cortexlabs/cortex into notebooks
deliahu Sep 20, 2019
9351f1e
Merge branch 'master' of github.com:cortexlabs/cortex into notebooks
deliahu Sep 20, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/image-classifier/cortex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

- kind: api
name: classifier
model: s3://cortex-examples/imagenet
request_handler: image.py
model: s3://cortex-examples/inception
request_handler: handler.py
tracker:
model_type: classification
208 changes: 208 additions & 0 deletions examples/image-classifier/inception.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "inception.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "n8CwINQcEBKz",
"colab_type": "text"
},
"source": [
"# Exporting ImageNet Inception\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3221z3P69fgf",
"colab_type": "text"
},
"source": [
"First, we'll install the required packages:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "_SdQpq7g9LiI",
"colab_type": "code",
"colab": {}
},
"source": [
"!pip install tensorflow==1.14.* tensorflow-hub==0.6.* boto3==1.*"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "I-k0gUpxDGkU",
"colab_type": "text"
},
"source": [
"Next, we'll download the model from TensorFlow Hub and export it for serving:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "z6QLCzB4BKMe",
"colab_type": "code",
"colab": {}
},
"source": [
"import tensorflow as tf\n",
"import tensorflow_hub as hub\n",
"from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def\n",
"\n",
"export_dir = \"export/1\"\n",
"builder = tf.saved_model.builder.SavedModelBuilder(export_dir)\n",
"\n",
"with tf.Session(graph=tf.Graph()) as sess:\n",
" module = hub.Module(\"https://tfhub.dev/google/imagenet/inception_v3/classification/3\")\n",
"\n",
" input_params = module.get_input_info_dict()\n",
" image_input = tf.placeholder(\n",
" name=\"images\", dtype=input_params[\"images\"].dtype, shape=input_params[\"images\"].get_shape()\n",
" )\n",
" \n",
" sess.run([tf.global_variables_initializer(), tf.tables_initializer()])\n",
"\n",
" classes = module(image_input)\n",
" signature = predict_signature_def(inputs={\"images\": image_input}, outputs={\"classes\": classes})\n",
"\n",
" builder.add_meta_graph_and_variables(\n",
" sess, [\"serve\"], signature_def_map={\"predict\": signature}, strip_default_attrs=True\n",
" )\n",
"\n",
"builder.save()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "aGtJiyEnBgwl",
"colab_type": "text"
},
"source": [
"## Upload the model to AWS\n",
"\n",
"Cortex loads models from AWS, so we need to upload the exported model."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fTkjvSKBBmUB",
"colab_type": "text"
},
"source": [
"Set these variables to configure your AWS credentials and model upload path:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "4xcDWxqCBPre",
"colab_type": "code",
"colab": {},
"cellView": "form"
},
"source": [
"AWS_ACCESS_KEY_ID = \"\" #@param {type:\"string\"}\n",
"AWS_SECRET_ACCESS_KEY = \"\" #@param {type:\"string\"}\n",
"S3_UPLOAD_PATH = \"s3://my-bucket/inception\" #@param {type:\"string\"}\n",
"\n",
"import sys\n",
"import re\n",
"\n",
"if AWS_ACCESS_KEY_ID == \"\":\n",
" print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Please set AWS_ACCESS_KEY_ID\"), file=sys.stderr)\n",
"\n",
"elif AWS_SECRET_ACCESS_KEY == \"\":\n",
" print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Please set AWS_SECRET_ACCESS_KEY\"), file=sys.stderr)\n",
"\n",
"else:\n",
" try:\n",
" bucket = re.search(\"s3://(.+?)/\", S3_UPLOAD_PATH).group(1)\n",
" key = re.search(\"s3://.+?/(.+)\", S3_UPLOAD_PATH).group(1)\n",
" except:\n",
" print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Invalid s3 path (should be of the form s3://my-bucket/path/to/file)\"), file=sys.stderr)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "czZkjb1IBr-f",
"colab_type": "text"
},
"source": [
"Upload the model to S3:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "M0b0IbyaBsim",
"colab_type": "code",
"colab": {}
},
"source": [
"import os\n",
"import boto3\n",
"\n",
"s3 = boto3.client(\"s3\", aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)\n",
"\n",
"for dirpath, _, filenames in os.walk(\"export\"):\n",
" for filename in filenames:\n",
" filepath = os.path.join(dirpath, filename)\n",
" filekey = os.path.join(key, filepath[len(\"export/\"):])\n",
" print(\"Uploading s3://{}/{}...\".format(bucket, filekey), end = '')\n",
" s3.upload_file(filepath, bucket, filekey)\n",
" print(\" ✓\")",
"\n",
"print(\"\\nUploaded model export directory to \" + S3_UPLOAD_PATH)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "pZQWoeZbE7Wc",
"colab_type": "text"
},
"source": [
"<!-- CORTEX_VERSION_MINOR -->\n",
"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."
]
}
]
}
22 changes: 0 additions & 22 deletions examples/image-classifier/load_inception_from_tf_hub.py

This file was deleted.

Loading