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

Using_Source_Separation_Arn_From_AWS_Marketplace.ipynb 13.9 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 19 20 21 22 23
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AWS Marketplace Product Usage Demonstration - Model Packages\n",
    "\n",
    "## Using Model Package ARN with Amazon SageMaker APIs\n",
    "\n",
    "This sample notebook demonstrates how to use Source Separation model package listed on Amazon SageMaker Marketplace.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set up the environment"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
24
   "execution_count": 17,
EC2 Default User's avatar
EC2 Default User committed
25 26 27 28 29 30 31
   "metadata": {},
   "outputs": [],
   "source": [
    "import sagemaker as sage\n",
    "from sagemaker import get_execution_role\n",
    "import zipfile\n",
    "import os\n",
EC2 Default User's avatar
EC2 Default User committed
32
    "import boto3\n",
EC2 Default User's avatar
EC2 Default User committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    "\n",
    "role = get_execution_role()\n",
    "\n",
    "# S3 prefixes\n",
    "common_prefix = \"source_separation\"\n",
    "batch_inference_input_prefix = common_prefix + \"/batch-inference-input-data\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create the session\n",
    "\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",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sagemaker_session = sage.Session()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create Model\n",
    "\n",
    "Now we use the above Model Package to create a model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
EC2 Default User's avatar
EC2 Default User committed
77
      "Using model package arn arn:aws:sagemaker:us-east-2:057799348421:model-package/source-sep11618586387-e73722215b88850c22e59b7564075da5\n"
EC2 Default User's avatar
EC2 Default User committed
78 79 80 81 82 83 84 85 86 87 88 89
     ]
    }
   ],
   "source": [
    "from src.source_separation_arns import ModelPackageArnProvider\n",
    "\n",
    "modelpackage_arn = ModelPackageArnProvider.get_model_package_arn(sagemaker_session.boto_region_name)\n",
    "print(\"Using model package arn \" + modelpackage_arn)"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
90
   "execution_count": 6,
EC2 Default User's avatar
EC2 Default User committed
91 92 93 94 95 96 97 98 99
   "metadata": {},
   "outputs": [],
   "source": [
    "from sagemaker import ModelPackage\n",
    "#from sagemaker.predictor import csv_serializer\n",
    "\n",
    "model = ModelPackage(role=role,\n",
    "                     model_package_arn=modelpackage_arn,\n",
    "                     sagemaker_session=sagemaker_session,\n",
EC2 Default User's avatar
EC2 Default User committed
100
    ")"
EC2 Default User's avatar
EC2 Default User committed
101 102 103 104 105 106 107 108 109 110
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Batch Transform Job\n",
    "\n",
    "Now let's use the model built to run a batch inference job on multiple audio files.\n",
    "\n",
111 112 113
    "Add your input audio files to \"data/transform\" folder.\n",
    "\n",
    "Create a \"batch-transform-output\" folder in the data directory before running the cells below (if not created already)."
EC2 Default User's avatar
EC2 Default User committed
114 115 116 117
   ]
  },
  {
   "cell_type": "code",
118
   "execution_count": 5,
EC2 Default User's avatar
EC2 Default User committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Transform input uploaded to s3://sagemaker-us-east-2-428712150059/source_separation/batch-inference-input-data\n"
     ]
    }
   ],
   "source": [
    "TRANSFORM_WORKDIR = \"data/transform\"\n",
    "\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",
138
   "execution_count": 6,
EC2 Default User's avatar
EC2 Default User committed
139 140 141 142 143 144
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
145 146 147 148 149 150 151 152
      "....................\u001b[31mStarting the inference server with 4 workers.\u001b[0m\n",
      "\u001b[31m[2019-10-31 08:34:17 +0000] [11] [INFO] Starting gunicorn 19.9.0\u001b[0m\n",
      "\u001b[31m[2019-10-31 08:34:17 +0000] [11] [INFO] Listening at: unix:/tmp/gunicorn.sock (11)\u001b[0m\n",
      "\u001b[31m[2019-10-31 08:34:17 +0000] [11] [INFO] Using worker: gevent\u001b[0m\n",
      "\u001b[31m[2019-10-31 08:34:17 +0000] [15] [INFO] Booting worker with pid: 15\u001b[0m\n",
      "\u001b[31m[2019-10-31 08:34:17 +0000] [16] [INFO] Booting worker with pid: 16\u001b[0m\n",
      "\u001b[31m[2019-10-31 08:34:17 +0000] [17] [INFO] Booting worker with pid: 17\u001b[0m\n",
      "\u001b[31m[2019-10-31 08:34:17 +0000] [18] [INFO] Booting worker with pid: 18\u001b[0m\n",
EC2 Default User's avatar
EC2 Default User committed
153
      "\u001b[31mTesting...\u001b[0m\n",
154 155 156 157 158
      "\u001b[31m2019-10-31 08:34:32.776086: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA\u001b[0m\n",
      "\u001b[31m169.254.255.130 - - [31/Oct/2019:08:34:33 +0000] \"GET /ping HTTP/1.1\" 200 1 \"-\" \"Go-http-client/1.1\"\u001b[0m\n",
      "\u001b[31m169.254.255.130 - - [31/Oct/2019:08:34:33 +0000] \"GET /execution-parameters HTTP/1.1\" 404 2 \"-\" \"Go-http-client/1.1\"\u001b[0m\n",
      "\u001b[31mInput path : /tmp/audio_file_1572510873.096988.mp3\u001b[0m\n",
      "\u001b[31mProducing source estimates for input mixture file /tmp/audio_file_1572510873.096988.mp3\u001b[0m\n",
EC2 Default User's avatar
EC2 Default User committed
159
      "\u001b[31mTesting...\u001b[0m\n",
160
      "\u001b[31m2019-10-31 08:34:34.450322: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA\u001b[0m\n",
EC2 Default User's avatar
EC2 Default User committed
161 162
      "\u001b[31mNum of variables64\u001b[0m\n",
      "\u001b[31mPre-trained model restored for song prediction\u001b[0m\n",
163 164 165 166 167 168 169 170 171 172 173
      "\u001b[33m2019-10-31T08:34:33.071:[sagemaker logs]: MaxConcurrentTransforms=1, MaxPayloadInMB=6, BatchStrategy=SINGLE_RECORD\u001b[0m\n",
      "\u001b[31mWARNING: Given output path /opt/ml/output/ does not exist. Trying to create it...\u001b[0m\n",
      "\u001b[31m['audio_file_1572510873.096988.mp3_vocals.wav', 'audio_file_1572510873.096988.mp3_accompaniment.wav']\u001b[0m\n",
      "\u001b[32mWARNING: Given output path /opt/ml/output/ does not exist. Trying to create it...\u001b[0m\n",
      "\u001b[32m['audio_file_1572510873.096988.mp3_vocals.wav', 'audio_file_1572510873.096988.mp3_accompaniment.wav']\u001b[0m\n",
      "\u001b[31m169.254.255.130 - - [31/Oct/2019:08:35:19 +0000] \"POST /invocations HTTP/1.1\" 200 19220663 \"-\" \"Go-http-client/1.1\"\u001b[0m\n",
      "\u001b[31mInput path : /tmp/audio_file_1572510919.321833.mp3\u001b[0m\n",
      "\u001b[31mProducing source estimates for input mixture file /tmp/audio_file_1572510919.321833.mp3\u001b[0m\n",
      "\u001b[32m169.254.255.130 - - [31/Oct/2019:08:35:19 +0000] \"POST /invocations HTTP/1.1\" 200 19220663 \"-\" \"Go-http-client/1.1\"\u001b[0m\n",
      "\u001b[32mInput path : /tmp/audio_file_1572510919.321833.mp3\u001b[0m\n",
      "\u001b[32mProducing source estimates for input mixture file /tmp/audio_file_1572510919.321833.mp3\u001b[0m\n",
EC2 Default User's avatar
EC2 Default User committed
174 175
      "\u001b[31mTesting...\u001b[0m\n",
      "\u001b[32mTesting...\u001b[0m\n",
176
      "\u001b[31m2019-10-31 08:35:20.739552: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA\u001b[0m\n",
EC2 Default User's avatar
EC2 Default User committed
177 178
      "\u001b[31mNum of variables64\u001b[0m\n",
      "\u001b[31mPre-trained model restored for song prediction\u001b[0m\n",
179
      "\u001b[32m2019-10-31 08:35:20.739552: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA\u001b[0m\n",
EC2 Default User's avatar
EC2 Default User committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      "\u001b[32mNum of variables64\u001b[0m\n",
      "\u001b[32mPre-trained model restored for song prediction\u001b[0m\n",
      "\n",
      "Batch Transform output saved to s3://sagemaker-us-east-2-428712150059/source_separation/batch-transform-output\n"
     ]
    }
   ],
   "source": [
    "import json \n",
    "import uuid\n",
    "\n",
    "bucket = sagemaker_session.default_bucket()\n",
    "\n",
    "transformer = model.transformer(1, 'ml.m4.xlarge', strategy='SingleRecord', output_path='s3://'+bucket+'/'+common_prefix+'/batch-transform-output')\n",
    "transformer.transform(transform_input, content_type='application/x-recordio-protobuf')\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",
209
   "execution_count": 7,
EC2 Default User's avatar
EC2 Default User committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mix2.mp3.out\n",
      "mix3.mp3.out\n"
     ]
    }
   ],
   "source": [
    "import boto3\n",
    "s3 = boto3.resource('s3')\n",
    "my_bucket = s3.Bucket(sagemaker_session.default_bucket())\n",
    "prefix = \"source_separation/batch-transform-output/\"\n",
    "i = 0\n",
    "for object_summary in my_bucket.objects.filter(Prefix=prefix):\n",
    "    i = i + 1\n",
    "    file_name = object_summary.key.split('/')[-1]\n",
    "    print(file_name)\n",
    "    my_bucket.download_file(prefix+ file_name, 'data/batch-transform-output/output-{}.zip'.format(i))\n",
    "    #with open('batch_results') as f:\n",
    "    #    results = f.readlines()\n",
    "    #    print(results)"
   ]
  },
  {
   "cell_type": "code",
239
   "execution_count": 8,
EC2 Default User's avatar
EC2 Default User committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "output-1.zip\n",
      "output-2.zip\n"
     ]
    }
   ],
   "source": [
    "for file in os.listdir('data/batch-transform-output'):\n",
    "    print(file)\n",
    "    with zipfile.ZipFile('data/batch-transform-output/'+file, 'r') as zip_ref:\n",
    "        zip_ref.extractall('data/batch-transform-output/'+file.split('.')[0]+'/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Live Inference Endpoint\n",
    "\n",
    "Now we demonstrate the creation of an endpoint for live inference on a single audio file.\n",
    "\n",
    "Add your input audio file to \"data/inference\" folder."
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
271
   "execution_count": 9,
EC2 Default User's avatar
EC2 Default User committed
272
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
273 274 275 276 277 278 279 280 281 282 283
   "outputs": [],
   "source": [
    "content_type = 'application/x-recordio-protobuf'\n",
    "endpoint_name = 'source-sep-inf'\n",
    "real_time_inference_output_file_name = 'data/output_inf.zip'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
284 285 286 287 288
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
EC2 Default User's avatar
EC2 Default User committed
289
      "-------------!"
EC2 Default User's avatar
EC2 Default User committed
290 291 292 293
     ]
    }
   ],
   "source": [
EC2 Default User's avatar
EC2 Default User committed
294
    "predictor = model.deploy(1, 'ml.m4.xlarge', endpoint_name=endpoint_name)"
EC2 Default User's avatar
EC2 Default User committed
295 296 297 298 299 300 301 302
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Choose some data and use it for a prediction\n",
    "\n",
303
    "For inference of a single file enter one file in the \"data/inference\" folder. Enter the file name in input_file variable.\n"
EC2 Default User's avatar
EC2 Default User committed
304 305 306 307
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
308
   "execution_count": 8,
EC2 Default User's avatar
EC2 Default User committed
309 310 311 312 313 314 315
   "metadata": {},
   "outputs": [],
   "source": [
    "INFERENCE_WORKDIR = \"data/inference/\"\n",
    "\n",
    "input_file = \"mix1.mp3\" #Edit input filename here\n",
    "\n",
EC2 Default User's avatar
EC2 Default User committed
316
    "INFERENCE_FILE = INFERENCE_WORKDIR + input_file"
EC2 Default User's avatar
EC2 Default User committed
317 318 319 320
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
321
   "execution_count": 10,
EC2 Default User's avatar
EC2 Default User committed
322
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
323 324 325 326 327 328 329 330 331 332 333 334
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "    \"ContentType\": \"application/x-recordio-protobuf\",\r\n",
      "    \"InvokedProductionVariant\": \"AllTraffic\"\r\n",
      "}\r\n"
     ]
    }
   ],
EC2 Default User's avatar
EC2 Default User committed
335
   "source": [
EC2 Default User's avatar
EC2 Default User committed
336 337 338 339 340 341
    "!aws sagemaker-runtime invoke-endpoint \\\n",
    "    --endpoint-name $endpoint_name \\\n",
    "    --body fileb://$INFERENCE_FILE \\\n",
    "    --content-type $content_type \\\n",
    "    --region $sagemaker_session.boto_region_name \\\n",
    "    $real_time_inference_output_file_name"
EC2 Default User's avatar
EC2 Default User committed
342 343 344 345 346 347 348 349 350 351 352
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extracting output files from the zipped file"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
353
   "execution_count": 11,
EC2 Default User's avatar
EC2 Default User committed
354 355 356
   "metadata": {},
   "outputs": [],
   "source": [
EC2 Default User's avatar
EC2 Default User committed
357
    "with zipfile.ZipFile(real_time_inference_output_file_name, 'r') as zip_ref:\n",
EC2 Default User's avatar
EC2 Default User committed
358 359 360 361 362 363 364 365 366 367 368 369
    "    zip_ref.extractall('data/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Listing the output files received"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
370
   "execution_count": 13,
EC2 Default User's avatar
EC2 Default User committed
371 372 373 374 375 376 377 378
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
EC2 Default User's avatar
EC2 Default User committed
379
      "['audio_file_1618931411.4032848.mp3_vocals.wav', '.ipynb_checkpoints', 'audio_file_1618931411.4032848.mp3_accompaniment.wav']\n"
EC2 Default User's avatar
EC2 Default User committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
     ]
    }
   ],
   "source": [
    "print(os.listdir('data/output'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Cleanup endpoint\n"
   ]
  },
  {
   "cell_type": "code",
EC2 Default User's avatar
EC2 Default User committed
396
   "execution_count": 18,
EC2 Default User's avatar
EC2 Default User committed
397
   "metadata": {},
EC2 Default User's avatar
EC2 Default User committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ResponseMetadata': {'RequestId': 'f481e778-245e-4c24-b8f8-4e8c03f1ca42',\n",
       "  'HTTPStatusCode': 200,\n",
       "  'HTTPHeaders': {'x-amzn-requestid': 'f481e778-245e-4c24-b8f8-4e8c03f1ca42',\n",
       "   'content-type': 'application/x-amz-json-1.1',\n",
       "   'content-length': '0',\n",
       "   'date': 'Tue, 20 Apr 2021 15:18:01 GMT'},\n",
       "  'RetryAttempts': 0}}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
EC2 Default User's avatar
EC2 Default User committed
416
   "source": [
EC2 Default User's avatar
EC2 Default User committed
417 418 419 420
    "sm = boto3.client('sagemaker')\n",
    "sm.delete_endpoint(\n",
    "    EndpointName=endpoint_name\n",
    ")"
EC2 Default User's avatar
EC2 Default User committed
421 422 423 424 425
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
EC2 Default User's avatar
EC2 Default User committed
426
   "display_name": "Python 3",
EC2 Default User's avatar
EC2 Default User committed
427
   "language": "python",
EC2 Default User's avatar
EC2 Default User committed
428
   "name": "python3"
EC2 Default User's avatar
EC2 Default User committed
429 430 431 432 433 434 435 436 437 438 439
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
EC2 Default User's avatar
EC2 Default User committed
440
   "version": "3.7.3"
EC2 Default User's avatar
EC2 Default User committed
441 442 443 444 445
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}