|
6 | 6 | "source": [
|
7 | 7 | "# Building your own TensorFlow container from Amazon SageMaker Studio\n",
|
8 | 8 | "\n",
|
9 |
| - "**STUDIO KERNEL NOTE:** If you are prompted for Kernel, choose 'Python 3 (TensorFlow CPU Optimized) \n", |
| 9 | + "**STUDIO KERNEL NOTE:** If you are prompted for Kernel, choose 'Python 3 (TensorFlow 2.1 Python 3.6 CPU Optimized) \n", |
10 | 10 | "\n",
|
11 | 11 | "With Amazon SageMaker, you can package your own algorithms that can then be trained and deployed in the SageMaker environment. This notebook guides you through an example using TensorFlow that shows you how to build a Docker container for SageMaker and use it for training and inference. \n",
|
12 | 12 | "\n",
|
|
300 | 300 | ]
|
301 | 301 | },
|
302 | 302 | {
|
303 |
| - "cell_type": "raw", |
| 303 | + "cell_type": "markdown", |
304 | 304 | "metadata": {},
|
305 | 305 | "source": [
|
| 306 | + "```\n", |
306 | 307 | "{\n",
|
307 | 308 | " \"Version\": \"2012-10-17\",\n",
|
308 | 309 | " \"Statement\": [\n",
|
|
323 | 324 | " \"Action\": \"sts:AssumeRole\"\n",
|
324 | 325 | " }\n",
|
325 | 326 | " ]\n",
|
326 |
| - "}" |
| 327 | + "}\n", |
| 328 | + "```" |
327 | 329 | ]
|
328 | 330 | },
|
329 | 331 | {
|
|
338 | 340 | " \n",
|
339 | 341 | " * Open [Policies](https://console.aws.amazon.com/iam/home#/policies) in IAM\n",
|
340 | 342 | " * Click **Create policy**\n",
|
341 |
| - " * Select the JSON tab and copy/paste the policy below" |
| 343 | + " * Select the JSON tab and copy/paste the printed result of the policy below" |
342 | 344 | ]
|
343 | 345 | },
|
344 | 346 | {
|
345 |
| - "cell_type": "raw", |
| 347 | + "cell_type": "code", |
| 348 | + "execution_count": null, |
346 | 349 | "metadata": {},
|
| 350 | + "outputs": [], |
347 | 351 | "source": [
|
348 |
| - "{\n", |
| 352 | + "print(\n", |
| 353 | + " f\"\"\"{{\n", |
349 | 354 | " \"Version\": \"2012-10-17\",\n",
|
350 | 355 | " \"Statement\": [\n",
|
351 |
| - " {\n", |
| 356 | + " {{\n", |
352 | 357 | " \"Effect\": \"Allow\",\n",
|
353 | 358 | " \"Action\": [\n",
|
354 | 359 | " \"codebuild:DeleteProject\",\n",
|
|
357 | 362 | " \"codebuild:StartBuild\"\n",
|
358 | 363 | " ],\n",
|
359 | 364 | " \"Resource\": \"arn:aws:codebuild:*:*:project/sagemaker-studio*\"\n",
|
360 |
| - " },\n", |
361 |
| - " {\n", |
| 365 | + " }},\n", |
| 366 | + " {{\n", |
362 | 367 | " \"Effect\": \"Allow\",\n",
|
363 | 368 | " \"Action\": \"logs:CreateLogStream\",\n",
|
364 | 369 | " \"Resource\": \"arn:aws:logs:*:*:log-group:/aws/codebuild/sagemaker-studio*\"\n",
|
365 |
| - " },\n", |
366 |
| - " {\n", |
| 370 | + " }},\n", |
| 371 | + " {{\n", |
367 | 372 | " \"Effect\": \"Allow\",\n",
|
368 | 373 | " \"Action\": [\n",
|
369 | 374 | " \"logs:GetLogEvents\",\n",
|
370 | 375 | " \"logs:PutLogEvents\"\n",
|
371 | 376 | " ],\n",
|
372 | 377 | " \"Resource\": \"arn:aws:logs:*:*:log-group:/aws/codebuild/sagemaker-studio*:log-stream:*\"\n",
|
373 |
| - " },\n", |
374 |
| - " {\n", |
| 378 | + " }},\n", |
| 379 | + " {{\n", |
375 | 380 | " \"Effect\": \"Allow\",\n",
|
376 | 381 | " \"Action\": \"logs:CreateLogGroup\",\n",
|
377 | 382 | " \"Resource\": \"*\"\n",
|
378 |
| - " },\n", |
379 |
| - " {\n", |
| 383 | + " }},\n", |
| 384 | + " {{\n", |
380 | 385 | " \"Effect\": \"Allow\",\n",
|
381 | 386 | " \"Action\": [\n",
|
382 | 387 | " \"ecr:CreateRepository\",\n",
|
|
391 | 396 | " \"ecr:PutImage\"\n",
|
392 | 397 | " ],\n",
|
393 | 398 | " \"Resource\": \"arn:aws:ecr:*:*:repository/sagemaker-studio*\"\n",
|
394 |
| - " },\n", |
395 |
| - " {\n", |
| 399 | + " }},\n", |
| 400 | + " {{\n", |
396 | 401 | " \"Effect\": \"Allow\",\n",
|
397 | 402 | " \"Action\": \"ecr:GetAuthorizationToken\",\n",
|
398 | 403 | " \"Resource\": \"*\"\n",
|
399 |
| - " },\n", |
400 |
| - " {\n", |
| 404 | + " }},\n", |
| 405 | + " {{\n", |
401 | 406 | " \"Effect\": \"Allow\",\n",
|
402 | 407 | " \"Action\": [\n",
|
403 | 408 | " \"s3:GetObject\",\n",
|
404 | 409 | " \"s3:DeleteObject\",\n",
|
405 | 410 | " \"s3:PutObject\"\n",
|
406 | 411 | " ],\n",
|
407 | 412 | " \"Resource\": \"arn:aws:s3:::sagemaker-*/*\"\n",
|
408 |
| - " },\n", |
409 |
| - " {\n", |
| 413 | + " }},\n", |
| 414 | + " {{\n", |
410 | 415 | " \"Effect\": \"Allow\",\n",
|
411 | 416 | " \"Action\": [\n",
|
412 | 417 | " \"s3:CreateBucket\"\n",
|
413 | 418 | " ],\n",
|
414 | 419 | " \"Resource\": \"arn:aws:s3:::sagemaker*\"\n",
|
415 |
| - " },\n", |
416 |
| - " {\n", |
| 420 | + " }},\n", |
| 421 | + " {{\n", |
417 | 422 | " \"Effect\": \"Allow\",\n",
|
418 | 423 | " \"Action\": [\n",
|
419 | 424 | " \"iam:GetRole\",\n",
|
420 | 425 | " \"iam:ListRoles\"\n",
|
421 | 426 | " ],\n",
|
422 | 427 | " \"Resource\": \"*\"\n",
|
423 |
| - " },\n", |
424 |
| - " {\n", |
| 428 | + " }},\n", |
| 429 | + " {{\n", |
425 | 430 | " \"Effect\": \"Allow\",\n",
|
426 | 431 | " \"Action\": \"iam:PassRole\",\n",
|
427 |
| - " \"Resource\": \"arn:aws:iam::*:role/*\",\n", |
428 |
| - " \"Condition\": {\n", |
429 |
| - " \"StringLikeIfExists\": {\n", |
| 432 | + " \"Resource\": \"{role}\",\n", |
| 433 | + " \"Condition\": {{\n", |
| 434 | + " \"StringLikeIfExists\": {{\n", |
430 | 435 | " \"iam:PassedToService\": \"codebuild.amazonaws.com\"\n",
|
431 |
| - " }\n", |
432 |
| - " }\n", |
433 |
| - " }\n", |
| 436 | + " }}\n", |
| 437 | + " }}\n", |
| 438 | + " }}\n", |
434 | 439 | " ]\n",
|
435 |
| - "}" |
| 440 | + "}}\"\"\"\n", |
| 441 | + ")" |
436 | 442 | ]
|
437 | 443 | },
|
438 | 444 | {
|
|
446 | 452 | "We now need to attach our policy to the Execution Role attached to this notebook environment. \n",
|
447 | 453 | "\n",
|
448 | 454 | " * Go back to [Roles](https://console.aws.amazon.com/iam/home#/roles) in IAM\n",
|
449 |
| - " * Select the SageMaker Execution Role from abovee\n", |
| 455 | + " * Select the SageMaker Execution Role from above\n", |
450 | 456 | " * On the **Permissions** tab, click **Attach policies**\n",
|
451 | 457 | " * Search for the Policy we created above `Studio-Image-Build-Policy`\n",
|
452 | 458 | " * Select the policy and click **Attach policy**"
|
|
647 | 653 | "\n",
|
648 | 654 | "estimator = Estimator(\n",
|
649 | 655 | " role=role,\n",
|
650 |
| - " train_instance_count=1,\n", |
651 |
| - " train_instance_type=instance_type,\n", |
| 656 | + " instance_count=1,\n", |
| 657 | + " instance_type=instance_type,\n", |
652 | 658 | " image_name=ecr_image,\n",
|
| 659 | + " image_uri=ecr_image,\n", |
653 | 660 | " hyperparameters=hyperparameters,\n",
|
654 | 661 | ")\n",
|
655 | 662 | "\n",
|
|
740 | 747 | "import imageio as imageio\n",
|
741 | 748 | "import numpy\n",
|
742 | 749 | "\n",
|
743 |
| - "from sagemaker.predictor import json_serializer, json_deserializer\n", |
744 |
| - "\n", |
745 | 750 | "image = imageio.imread(\"data/cat.png\")\n",
|
746 | 751 | "print(image.shape)\n",
|
747 | 752 | "\n",
|
|
758 | 763 | {
|
759 | 764 | "cell_type": "code",
|
760 | 765 | "execution_count": null,
|
761 |
| - "metadata": { |
762 |
| - "jupyter": { |
763 |
| - "source_hidden": true |
764 |
| - } |
765 |
| - }, |
| 766 | + "metadata": {}, |
766 | 767 | "outputs": [],
|
767 | 768 | "source": [
|
768 | 769 | "# The request and response format is JSON for TensorFlow Serving.\n",
|
769 | 770 | "# For more information: https://www.tensorflow.org/serving/api_rest#predict_api\n",
|
770 |
| - "predictor.accept = \"application/json\"\n", |
771 |
| - "predictor.content_type = \"application/json\"\n", |
| 771 | + "from sagemaker.serializers import JSONSerializer\n", |
| 772 | + "from sagemaker.deserializers import JSONDeserializer\n", |
772 | 773 | "\n",
|
773 |
| - "predictor.serializer = json_serializer\n", |
774 |
| - "predictor.deserializer = json_deserializer\n", |
| 774 | + "predictor.serializer = JSONSerializer()\n", |
| 775 | + "predictor.deserializer = JSONDeserializer()\n", |
775 | 776 | "\n",
|
776 | 777 | "# For more information on the predictor class.\n",
|
777 | 778 | "# https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/predictor.py\n",
|
|
821 | 822 | "metadata": {
|
822 | 823 | "instance_type": "ml.t3.medium",
|
823 | 824 | "kernelspec": {
|
824 |
| - "display_name": "Python 3 (TensorFlow CPU Optimized)", |
| 825 | + "display_name": "Python 3 (TensorFlow 2.1 Python 3.6 CPU Optimized)", |
825 | 826 | "language": "python",
|
826 |
| - "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-west-2:236514542706:image/tensorflow-1.15-cpu-py36" |
| 827 | + "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-2:429704687514:image/tensorflow-2.1-cpu-py36" |
827 | 828 | },
|
828 | 829 | "language_info": {
|
829 | 830 | "codemirror_mode": {
|
|
835 | 836 | "name": "python",
|
836 | 837 | "nbconvert_exporter": "python",
|
837 | 838 | "pygments_lexer": "ipython3",
|
838 |
| - "version": "3.6.9" |
| 839 | + "version": "3.6.13" |
839 | 840 | }
|
840 | 841 | },
|
841 | 842 | "nbformat": 4,
|
|
0 commit comments