13

What is the best way to run TensorFlow 2.0 with AWS Sagemeker?

As of today (Aug 7th, 2019) AWS does not provide TensorFlow 2.0 SageMaker containers, so my understanding is that I need to build my own.

What is the best Base image to use? Example Dockerfile?

Stewart_R
  • 13,764
  • 11
  • 60
  • 106
Anton
  • 3,587
  • 2
  • 12
  • 27
  • https://github.com/aws/sagemaker-tensorflow-container/blob/master/docker/1.12.0/final/py2/Dockerfile.cpu Above is the Dockerfile used by Sagemaker to build Tensorflow images. You can refer this and update the base image as per your use case – Ujjwal Bhardwaj Aug 09 '19 at 11:23

4 Answers4

10

EDIT: Amazon SageMaker does now support TF 2.0 and higher.


Original answer

Here is an example Dockerfile that uses the underlying SageMaker Containers library (this is what is used in the official pre-built Docker images):

FROM tensorflow/tensorflow:2.0.0b1

RUN pip install sagemaker-containers

# Copies the training code inside the container
COPY train.py /opt/ml/code/train.py

# Defines train.py as script entrypoint
ENV SAGEMAKER_PROGRAM train.py

For more information on this approach, see https://docs.aws.amazon.com/sagemaker/latest/dg/build-container-to-train-script-get-started.html

lauren
  • 513
  • 2
  • 12
  • Hi, in this approach is it easy to scale training horizontally, i.e. is it enough to set `train_instance_count ` to something large than 1? – clog14 Dec 16 '19 at 14:21
  • your `train.py` will need to be able to handle distributed training in that case, including setting up parameter servers, etc. if appropriate. – lauren Dec 19 '19 at 19:03
  • I get the below warning while building the docker image. 'Step 3/10 : RUN pip install sagemaker-containers ---> Running in b75277212fd9 DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. Collecting sagemaker-containers' – Biranchi Jul 16 '20 at 03:12
  • @Biranchi you no longer need to build your own image for TF 2.x support. (I've also edited my answer to hopefully make this clearer.) – lauren Jul 17 '20 at 17:54
8

Update on 10 nov 2019:

There is now a way to use Tensorflow 2 in SageMaker, event though there is no shortcut to start TF 2 directly from SageMaker console.

  1. Start a conda Python3 Kernel

  2. Make some updates (one in each code cell):

!pip install --upgrade pip         # pip 19.0 or higher is required for TF 2
!pip install --upgrade setuptools  # Otherwise you'll get annoying warnings about bad installs
  1. Install Tensorflow 2
!pip install --user --upgrade tensorflow

Given the doc, this will install in $HOME.

Nota:

If you are using a GPU based instance of SageMaker, replace tensorflow by tensorflow-gpu.

You now can use TF 2 in your instance. This only needs to be done once, as long as the instance remains up.

To test, just run in the next cell:

import tensorflow as tf
print(tf.__version__)

You should see 2.0.0 or higher.

Hassen
  • 6,966
  • 13
  • 45
  • 65
  • 1
    This lets you use TensorFlow 2.0 in Jupyter (in a SageMaker Notebook Instance), but does not allow for training/hosting via a Docker container run by SageMaker. – lauren Nov 27 '19 at 17:47
3

As of now, the best image that you can use to build Tensorflow 2.0 is 2.0.0b1, which is the latest version of Tensorflow 2.0(image) that is available now.Please find the link here. You also have an image 2.0.0b1-py3, which comes with Python3 (3.5 for Ubuntu 16-based images; 3.6 for Ubuntu 18-based images).

If you feel this answer is useful, kindly accept this answer and/or up vote it. Thanks.

3

Here from the SageMaker team. To use SageMaker seamlessly, it's recommended that you try out Amazon SageMaker Studio. It's similar to a Jupyter Notebook instance but has a variety of SageMaker services already integrated within it. A huge plus point is that you can select from a variety of kernels and for TensorFlow 2.0 you can select any of these that already have all the required packages installed:

Python 3 (TensorFlow 2.1 Python 3.6 CPU Optimized)
Python 3 (TensorFlow 2.1 Python 3.6 GPU Optimized)
Python 3 (TensorFlow 2.3 Python 3.7 CPU Optimized)
Python 3 (TensorFlow 2.3 Python 3.7 GPU Optimized)

In Studio, you can select the Kernel drop-down located in top-right corner of your SageMaker Studio instance: enter image description here

Shown below is a screenshot of the UI interface in SageMaker Studio of the available kernels that are compatible with TensorFlow 2.0. Note that all kernels within SageMaker Studio are continuously tested and work seamlessly with all SageMaker Services. enter image description here

Teodorico Levoff
  • 1,641
  • 2
  • 26
  • 44