Skip to content

Commit 3287469

Browse files
authored
Merge pull request aws#70 from awslabs/image_classification_notebooks
Changed hosting image and fixed missing cells
2 parents 045a68b + d889c69 commit 3287469

File tree

2 files changed

+89
-23
lines changed

2 files changed

+89
-23
lines changed

introduction_to_amazon_algorithms/imageclassification_caltech/Image-classification-fulltraining.ipynb

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"role = re.sub(r'^(.+)sts::(\\d+):assumed-role/(.+?)/.*$', r'\\1iam::\\2:role/\\3', assumed_role)\n",
5858
"\n",
5959
"bucket='<<bucket-name>>' # customize to your bucket\n",
60-
"\n",
6160
"containers = {'us-west-2': '433757028032.dkr.ecr.us-west-2.amazonaws.com/image-classification:latest',\n",
6261
" 'us-east-1': '811284229777.dkr.ecr.us-east-1.amazonaws.com/image-classification:latest',\n",
6362
" 'us-east-2': '825641698319.dkr.ecr.us-east-2.amazonaws.com/image-classification:latest',\n",
@@ -340,8 +339,13 @@
340339
"model_data = info['ModelArtifacts']['S3ModelArtifacts']\n",
341340
"print(model_data)\n",
342341
"\n",
342+
"containers = {'us-west-2': '433757028032.dkr.ecr.us-west-2.amazonaws.com/image-classification:latest',\n",
343+
" 'us-east-1': '811284229777.dkr.ecr.us-east-1.amazonaws.com/image-classification:latest',\n",
344+
" 'us-east-2': '825641698319.dkr.ecr.us-east-2.amazonaws.com/image-classification:latest',\n",
345+
" 'eu-west-1': '685385470294.dkr.ecr.us-west-1.amazonaws.com/image-classification:latest'}\n",
346+
"hosting_image = containers[boto3.Session().region_name]\n",
343347
"primary_container = {\n",
344-
" 'Image': \"032969728358.dkr.ecr.us-west-2.amazonaws.com/image-classification:latest\",\n",
348+
" 'Image': hosting_image,\n",
345349
" 'ModelDataUrl': model_data,\n",
346350
"}\n",
347351
"\n",
@@ -363,6 +367,36 @@
363367
"In addition, the endpoint configuration describes the instance type required for model deployment, and at launch will describe the autoscaling configuration."
364368
]
365369
},
370+
{
371+
"cell_type": "code",
372+
"execution_count": null,
373+
"metadata": {},
374+
"outputs": [],
375+
"source": [
376+
"from time import gmtime, strftime\n",
377+
"\n",
378+
"timestamp = time.strftime('-%Y-%m-%d-%H-%M-%S', time.gmtime())\n",
379+
"endpoint_config_name = job_name_prefix + '-epc-' + timestamp\n",
380+
"endpoint_config_response = sage.create_endpoint_config(\n",
381+
" EndpointConfigName = endpoint_config_name,\n",
382+
" ProductionVariants=[{\n",
383+
" 'InstanceType':'ml.p2.xlarge',\n",
384+
" 'InitialInstanceCount':3,\n",
385+
" 'ModelName':model_name,\n",
386+
" 'VariantName':'AllTraffic'}])\n",
387+
"\n",
388+
"print('Endpoint configuration name: {}'.format(endpoint_config_name))\n",
389+
"print('Endpoint configuration arn: {}'.format(endpoint_config_response['EndpointConfigArn']))"
390+
]
391+
},
392+
{
393+
"cell_type": "markdown",
394+
"metadata": {},
395+
"source": [
396+
"### Create Endpoint\n",
397+
"Lastly, the customer creates the endpoint that serves up the model, through specifying the name and configuration defined above. The end result is an endpoint that can be validated and incorporated into production applications. This takes 9-11 minutes to complete."
398+
]
399+
},
366400
{
367401
"cell_type": "code",
368402
"execution_count": null,
@@ -460,7 +494,7 @@
460494
"metadata": {},
461495
"outputs": [],
462496
"source": [
463-
"!wget -O /tmp/test.jpg https://static.pexels.com/photos/47319/phone-dial-old-arrangement-47319.jpeg\n",
497+
"!wget -O /tmp/test.jpg http://www.vision.caltech.edu/Image_Datasets/Caltech256/images/008.bathtub/008_0007.jpg\n",
464498
"file_name = '/tmp/test.jpg'\n",
465499
"# test image\n",
466500
"from IPython.display import Image\n",
@@ -480,7 +514,7 @@
480514
" payload = f.read()\n",
481515
" payload = bytearray(payload)\n",
482516
"response = runtime.invoke_endpoint(EndpointName=endpoint_name, \n",
483-
" ContentType='image/jpeg', \n",
517+
" ContentType='application/x-image', \n",
484518
" Body=payload)\n",
485519
"result = response['Body'].read()\n",
486520
"# result will be in json format and convert it to ndarray\n",

introduction_to_amazon_algorithms/imageclassification_caltech/Image-classification-transfer-learning.ipynb

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
{
4646
"cell_type": "code",
4747
"execution_count": null,
48-
"metadata": {},
48+
"metadata": {
49+
"collapsed": true
50+
},
4951
"outputs": [],
5052
"source": [
5153
"%%time\n",
@@ -79,7 +81,9 @@
7981
{
8082
"cell_type": "code",
8183
"execution_count": null,
82-
"metadata": {},
84+
"metadata": {
85+
"collapsed": true
86+
},
8387
"outputs": [],
8488
"source": [
8589
"import os\n",
@@ -145,6 +149,7 @@
145149
"cell_type": "code",
146150
"execution_count": null,
147151
"metadata": {
152+
"collapsed": true,
148153
"isConfigCell": true
149154
},
150155
"outputs": [],
@@ -182,7 +187,9 @@
182187
{
183188
"cell_type": "code",
184189
"execution_count": null,
185-
"metadata": {},
190+
"metadata": {
191+
"collapsed": true
192+
},
186193
"outputs": [],
187194
"source": [
188195
"%%time\n",
@@ -263,7 +270,9 @@
263270
{
264271
"cell_type": "code",
265272
"execution_count": null,
266-
"metadata": {},
273+
"metadata": {
274+
"collapsed": true
275+
},
267276
"outputs": [],
268277
"source": [
269278
"# create the Amazon SageMaker training job\n",
@@ -290,7 +299,9 @@
290299
{
291300
"cell_type": "code",
292301
"execution_count": null,
293-
"metadata": {},
302+
"metadata": {
303+
"collapsed": true
304+
},
294305
"outputs": [],
295306
"source": [
296307
"training_info = sagemaker.describe_training_job(TrainingJobName=job_name)\n",
@@ -341,7 +352,9 @@
341352
{
342353
"cell_type": "code",
343354
"execution_count": null,
344-
"metadata": {},
355+
"metadata": {
356+
"collapsed": true
357+
},
345358
"outputs": [],
346359
"source": [
347360
"%%time\n",
@@ -355,9 +368,13 @@
355368
"info = sage.describe_training_job(TrainingJobName=job_name)\n",
356369
"model_data = info['ModelArtifacts']['S3ModelArtifacts']\n",
357370
"print(model_data)\n",
358-
"\n",
371+
"containers = {'us-west-2': '433757028032.dkr.ecr.us-west-2.amazonaws.com/image-classification:latest',\n",
372+
" 'us-east-1': '811284229777.dkr.ecr.us-east-1.amazonaws.com/image-classification:latest',\n",
373+
" 'us-east-2': '825641698319.dkr.ecr.us-east-2.amazonaws.com/image-classification:latest',\n",
374+
" 'eu-west-1': '685385470294.dkr.ecr.us-west-1.amazonaws.com/image-classification:latest'}\n",
375+
"hosting_image = containers[boto3.Session().region_name]\n",
359376
"primary_container = {\n",
360-
" 'Image': \"032969728358.dkr.ecr.us-west-2.amazonaws.com/image-classification:latest\",\n",
377+
" 'Image': hosting_image,\n",
361378
" 'ModelDataUrl': model_data,\n",
362379
"}\n",
363380
"\n",
@@ -382,7 +399,9 @@
382399
{
383400
"cell_type": "code",
384401
"execution_count": null,
385-
"metadata": {},
402+
"metadata": {
403+
"collapsed": true
404+
},
386405
"outputs": [],
387406
"source": [
388407
"from time import gmtime, strftime\n",
@@ -412,7 +431,9 @@
412431
{
413432
"cell_type": "code",
414433
"execution_count": null,
415-
"metadata": {},
434+
"metadata": {
435+
"collapsed": true
436+
},
416437
"outputs": [],
417438
"source": [
418439
"%%time\n",
@@ -440,7 +461,9 @@
440461
{
441462
"cell_type": "code",
442463
"execution_count": null,
443-
"metadata": {},
464+
"metadata": {
465+
"collapsed": true
466+
},
444467
"outputs": [],
445468
"source": [
446469
"# get the status of the endpoint\n",
@@ -486,7 +509,9 @@
486509
{
487510
"cell_type": "code",
488511
"execution_count": null,
489-
"metadata": {},
512+
"metadata": {
513+
"collapsed": true
514+
},
490515
"outputs": [],
491516
"source": [
492517
"import boto3\n",
@@ -503,10 +528,12 @@
503528
{
504529
"cell_type": "code",
505530
"execution_count": null,
506-
"metadata": {},
531+
"metadata": {
532+
"collapsed": true
533+
},
507534
"outputs": [],
508535
"source": [
509-
"!wget -O /tmp/test.jpg https://static.pexels.com/photos/47319/phone-dial-old-arrangement-47319.jpeg\n",
536+
"!wget -O /tmp/test.jpg http://www.vision.caltech.edu/Image_Datasets/Caltech256/images/008.bathtub/008_0007.jpg\n",
510537
"file_name = '/tmp/test.jpg'\n",
511538
"# test image\n",
512539
"from IPython.display import Image\n",
@@ -516,17 +543,18 @@
516543
{
517544
"cell_type": "code",
518545
"execution_count": null,
519-
"metadata": {},
546+
"metadata": {
547+
"collapsed": true
548+
},
520549
"outputs": [],
521550
"source": [
522551
"import json\n",
523552
"import numpy as np\n",
524-
"endpoint_name='ImageClassificationITE2017-11-25-03-55-18'\n",
525553
"with open(file_name, 'rb') as f:\n",
526554
" payload = f.read()\n",
527555
" payload = bytearray(payload)\n",
528556
"response = runtime.invoke_endpoint(EndpointName=endpoint_name, \n",
529-
" ContentType='application/image', \n",
557+
" ContentType='application/x-image', \n",
530558
" Body=payload)\n",
531559
"result = response['Body'].read()\n",
532560
"# result will be in json format and convert it to ndarray\n",
@@ -550,7 +578,9 @@
550578
{
551579
"cell_type": "code",
552580
"execution_count": null,
553-
"metadata": {},
581+
"metadata": {
582+
"collapsed": true
583+
},
554584
"outputs": [],
555585
"source": [
556586
"#sage.delete_endpoint(EndpointName=endpoint_name)\n",
@@ -560,7 +590,9 @@
560590
{
561591
"cell_type": "code",
562592
"execution_count": null,
563-
"metadata": {},
593+
"metadata": {
594+
"collapsed": true
595+
},
564596
"outputs": [],
565597
"source": []
566598
}

0 commit comments

Comments
 (0)