Use Amazon Sagemaker from an on -premises environment

Amazon Sagemaker is a managed service that supports data preparation, development, learning, model conversion, deployment in machine learning, and all of these pipelines.When considering the use of sageMaker, it is not the case that all of these must be shifted to sagemaker.For example, it is also possible to perform only development with Sagemaker Jupyter Notebook, or only learning and deployment in Sagemaker.By taking advantage of this characteristics, users who have a machine learning environment in on -premises can use the existing on -premises environment as much as possible and use sageMaker in the parts that require additional resources.

In this post, we will introduce how to switch to the on -premises machine learning environment and seamlessly switching to the on -premises machine learning environment as an environment for learning and deploying machine learning models.Open source Sagemaker Python SDK offers both on -premises and Sagemaker an interface that enables learning and reasoning.The contents to be realized in this post are shown in the following figure.

Learning and deployment code is developed with on -premises where Python is installed, and the learning data is also saved on -premises.In the sageMaker Python SDK, these code and learning data are used in a local environment, or uploaded to S3 and used in sageMaker.

Information required for execution

The following information is required to execute the contents of this post.

オンプレミス環境から Amazon SageMaker を利用する

On -premises environment settings

Once you are ready for Credential and IAM roll, set up the on -premises environment.The following shows the setting procedure and typical commands, but it may vary depending on the environment, so please check the links in detail.

Preparation of learning and reasoning code

Next, create a learning or reasoning code.Here, the classification of handwritten numbers by TensorFlow, which is published as the official AWS sample code, so that it can be performed from an on -premises environment.When fixing, you can also use a text editor that you usually use in an on -premises environment.

Execution of learning

コードの修正が終わったら学習を実行することが可能です。冒頭の全体像で説明したように、SageMaker Python SDK を利用して、学習を SageMaker のインスタンスで実行するか、オンプレミス環境で実行するかを選択します。これらの選択は、SageMaker の Estimator の引数train_instance_typeを変更するだけで行うことができます。これによって、2つの学習ジョブを実行する際に、まずはオンプレミス環境を利用して、次に SageMaker のインスタンスを利用する、といった利用も可能です。

学習はfit(inputs)関数によって実行することができます。inputsには学習データへのパスを指定します。このパスとして、S3に保存された学習データへのパスを与えることが標準ですが、ローカルモードにおいてのみfile:///xxx/yyy/というような形式で、ローカルに保存された学習データへのパスを与えることも可能です。

The following screenshots are when you run a Sagemaker learning job in an on -premises environment.From the figure above, you can see that the Docker process uses a CPU for learning.From the figure below, you can confirm that the loss is changing as the learning by TensorFlow progresses.

Execution of model deployment and inference

学習と同様に、モデルのデプロイ5においても、SageMaker のインスタンスとオンプレミス環境の両方を利用することができます。これらを切り替えるためには、deploy関数において引数instance_typeの値を以下のように変更します。

Finally, the screenshots when deploying in the on -premises environment and running inference is shown below.Original TensorFlow_distributed_mnist.ipynb を参考に、ラベル 7 の画像をオンプレミス環境のエンドポイントに送信しました。オレンジ色の枠で囲んだ箇所が、エンドポイントからのレスポンスです。'int64Val': ['7']が予測値を表しており、正しい推論が行われていることを確認できました。

Endpoint deletion

不要になったエンドポイントは、delete_endopoint()で削除することができます。SageMaker 上のエンドポイントの場合、エンドポイントに利用されているインスタンスを停止します。ローカルモードで作成したエンドポイントの場合、推論用のコンテナを停止します。

mnist_estimator.delete_endpoint()

summary

For data scientists and ML engineers who are learning machine learning in an on -premises environment, we explained how to use Sagemaker as needed while utilizing the on -premises environment.The Sagemaker Python SDK offers an interface to switch the on -premises environment and sagemaker to seamless, and you can use local mode to learn in the on -premises environment.Sagemaker Python SDK is open source and is being developed at a fast speed while forming a community with users.In order to explain many functions, the official document of Sagemaker Python SDK has been revised, so please check it out.

Masaki Samejima (MASAKI SAMEJIMA, PH.D.) Is an AWS machine learning specialist solution architect and specializes in computer vision and time series analysis.

Yoshitaka Haribara, PH.D.) Is an AWS solution architect.My hobby is drums and I spend my time in a band with my friends on holidays.My favorite band is Red Hot Chili Peppers, my favorite AWS service is Amazon Sagemaker.

Related Articles