Please create a request on https://ezq.quantiphi.com/gitlab/user-access if you want to create a new group or project.

stamp-detection.ipynb 6.08 KB
Newer Older
EC2 Default User's avatar
EC2 Default User committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Stamp Detection Sample Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set up environment"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
19
   "execution_count": null,
EC2 Default User's avatar
EC2 Default User committed
20 21 22 23 24 25
   "metadata": {},
   "outputs": [],
   "source": [
    "import sagemaker as sage\n",
    "from sagemaker import get_execution_role\n",
    "\n",
EC2 Default User's avatar
EC2 Default User committed
26
    "role = get_execution_role()"
EC2 Default User's avatar
EC2 Default User committed
27 28 29 30 31 32 33 34 35 36 37 38
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create the session\n",
    "The session remembers our connection parameters to Amazon SageMaker. We'll use it to perform all of our Amazon SageMaker operations."
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
39
   "execution_count": null,
EC2 Default User's avatar
EC2 Default User committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   "metadata": {},
   "outputs": [],
   "source": [
    "sagemaker_session = sage.Session()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create Model\n",
    "Now we use the above Model Package to create a model"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
56
   "execution_count": null,
EC2 Default User's avatar
EC2 Default User committed
57
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
58
   "outputs": [],
EC2 Default User's avatar
EC2 Default User committed
59 60 61
   "source": [
    "from src.model_package_arns import ModelPackageArnProvider\n",
    "\n",
62 63
    "# Can only be deployed in us-east-2 for now.\n",
    "modelpackage_arn = ModelPackageArnProvider.get_model_package_arn('us-east-2')\n",
EC2 Default User's avatar
EC2 Default User committed
64 65 66 67 68
    "print(\"Using model package arn \" + modelpackage_arn)"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
69
   "execution_count": null,
EC2 Default User's avatar
EC2 Default User committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
   "metadata": {},
   "outputs": [],
   "source": [
    "from sagemaker import ModelPackage\n",
    "\n",
    "def predict_wrapper(endpoint, session):\n",
    "    return sage.RealTimePredictor(endpoint, session, content_type='image/jpeg')\n",
    "\n",
    "model = ModelPackage(role=role,\n",
    "                     model_package_arn=modelpackage_arn,\n",
    "                     sagemaker_session=sagemaker_session,\n",
    "                     predictor_cls=predict_wrapper)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Batch Transform Job\n",
    "Now let's use the model built to run a batch inference job and verify it works."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Batch Transform Input Preparation"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
101
   "execution_count": null,
EC2 Default User's avatar
EC2 Default User committed
102
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
103
   "outputs": [],
EC2 Default User's avatar
EC2 Default User committed
104 105 106 107
   "source": [
    "# S3 prefixes\n",
    "common_prefix = \"DEMO-stamp-detection\"\n",
    "batch_inference_input_prefix = common_prefix + \"/batch-inference-input-data\"\n",
EC2 Default User's avatar
EC2 Default User committed
108 109
    "batch_inference_output_prefix = common_prefix + \"/batch-inference-output-data\"\n",
    "output_path = \"s3://{}/{}\".format(sess.default_bucket(), batch_inference_output_prefix)\n",
EC2 Default User's avatar
EC2 Default User committed
110
    "\n",
EC2 Default User's avatar
EC2 Default User committed
111
    "# Uploading to s3\n",
EC2 Default User's avatar
EC2 Default User committed
112 113 114 115 116 117 118 119 120
    "TRANSFORM_WORKDIR = \"data/transforms\"\n",
    "transform_input = sagemaker_session.upload_data(TRANSFORM_WORKDIR, key_prefix=batch_inference_input_prefix)\n",
    "print(\"Transform input uploaded to \" + transform_input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
121
   "outputs": [],
EC2 Default User's avatar
EC2 Default User committed
122
   "source": [
EC2 Default User's avatar
EC2 Default User committed
123
    "# batch tranform\n",
EC2 Default User's avatar
EC2 Default User committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    "transformer = model.transformer(1, 'ml.m4.xlarge', output_path=output_path)\n",
    "transformer.transform(transform_input, content_type='image/jpeg')\n",
    "transformer.wait()\n",
    "\n",
    "print(\"Batch Transform output saved to \" + transformer.output_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Inspect the Batch Transform Output in S3¶"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
140
   "execution_count": null,
EC2 Default User's avatar
EC2 Default User committed
141
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
142
   "outputs": [],
EC2 Default User's avatar
EC2 Default User committed
143 144 145
   "source": [
    "from urllib.parse import urlparse\n",
    "import os\n",
EC2 Default User's avatar
EC2 Default User committed
146
    "import pprint\n",
EC2 Default User's avatar
EC2 Default User committed
147 148 149 150 151 152 153 154 155 156 157
    "\n",
    "parsed_url = urlparse(transformer.output_path)\n",
    "bucket_name = parsed_url.netloc\n",
    "\n",
    "for filename in os.listdir('./data/transforms'):\n",
    "    file_key = '{}/{}.out'.format(parsed_url.path[1:], filename)\n",
    "\n",
    "    s3_client = sagemaker_session.boto_session.client('s3')\n",
    "\n",
    "    response = s3_client.get_object(Bucket = sagemaker_session.default_bucket(), Key = file_key)\n",
    "    response_bytes = response['Body'].read().decode('utf-8')\n",
EC2 Default User's avatar
EC2 Default User committed
158 159 160
    "    print('--------\\n')\n",
    "    print(filename, ':\\n')\n",
    "    pprint.pprint(response_bytes)"
EC2 Default User's avatar
EC2 Default User committed
161 162 163 164
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
165
   "execution_count": null,
EC2 Default User's avatar
EC2 Default User committed
166
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
167
   "outputs": [],
EC2 Default User's avatar
EC2 Default User committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181
   "source": [
    "os.listdir('./data/transforms')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Live Inference Endpoint\n",
    "Now we demonstrate the creation of an endpoint for live inference"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
182
   "execution_count": null,
EC2 Default User's avatar
EC2 Default User committed
183
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
184
   "outputs": [],
EC2 Default User's avatar
EC2 Default User committed
185 186 187 188 189 190 191 192 193 194 195 196 197
   "source": [
    "predictor = model.deploy(1, 'ml.m4.xlarge')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Preparing the input file for payload and viewing the response\n"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
198
   "execution_count": null,
EC2 Default User's avatar
EC2 Default User committed
199
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
200
   "outputs": [],
EC2 Default User's avatar
EC2 Default User committed
201 202 203 204
   "source": [
    "file_name = '/home/ec2-user/SageMaker/stamp-detection/data/transforms/102116_f4834-0.jpg'\n",
    "with open(file_name, 'rb') as img:\n",
    "    f = img.read()\n",
EC2 Default User's avatar
EC2 Default User committed
205
    "    b = bytearray(f) #converting the image to binary format\n",
EC2 Default User's avatar
EC2 Default User committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    "\n",
    "result = predictor.predict(f).decode('utf-8')\n",
    "\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cleanup endpoint¶\n"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
221
   "execution_count": null,
EC2 Default User's avatar
EC2 Default User committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
   "metadata": {},
   "outputs": [],
   "source": [
    "predictor.delete_endpoint()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}