

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 概述：使用`ScriptProcessor`和 SageMaker 地理空间容器运行处理作业
概述：`ScriptProcessor` API

SageMaker geospatial 提供了一个专门构建的处理容器，。`081189585635.dkr.ecr.us-west-2.amazonaws.com/sagemaker-geospatial-v1-0:latest`在使用 Amazon SageMaker Processing 运行任务时，您可以使用此容器。当您创建可通过 *Amaz SageMaker on Python 软件开发工具包进行处理的[https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.processing.ScriptProcessor](https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.processing.ScriptProcessor)类的*实例时，请指定该实例`image_uri`。

**注意**  
如果您在尝试启动处理任务时收到ResourceLimitExceeded错误，则需要申请增加配额。要开始申请增加服务限额，请参阅《服务限额用户指南》**中的[申请增加限额](https://docs.amazonaws.cn/servicequotas/latest/userguide/request-quota-increase.html)。

**使用 `ScriptProcessor` 的先决条件**

1. 您已创建用于指定地理空间 ML 工作负载的 Python 脚本。

1. 您已向 SageMaker AI 执行角色授予访问所需任何 Amazon S3 存储桶的权限。

1. 准备好要导入到容器中的数据。Ama SageMaker zon Processing 任务支持将`s3_data_type`等于`"ManifestFile"`或设置为`"S3Prefix"`。

以下过程向您展示如何使用 SageMaker 地理空间容器创建实例`ScriptProcessor`和提交 Amazon P SageMaker rocessing 任务。

**使用 SageMaker 地理空间容器创建`ScriptProcessor`实例并提交 Amazon SageMaker 处理任务**

1. 使用SageMaker 地理空间图像实例化该`ScriptProcessor`类的实例：

   ```
   from sagemaker.processing import ScriptProcessor, ProcessingInput, ProcessingOutput
   	
   sm_session = sagemaker.session.Session()
   execution_role_arn = sagemaker.get_execution_role()
   
   # purpose-built geospatial container
   image_uri = '081189585635.dkr.ecr.us-west-2.amazonaws.com/sagemaker-geospatial-v1-0:latest'
   
   script_processor = ScriptProcessor(
   	command=['python3'],
   	image_uri=image_uri,
   	role=execution_role_arn,
   	instance_count=4,
   	instance_type='ml.m5.4xlarge',
   	sagemaker_session=sm_session
   )
   ```

   *execution\$1role\$1arn*替换为 SageMaker Amazon S3 执行角色的 ARN，该角色可以访问存储在 Amazon S3 中的输入数据以及您要在处理任务中调用的任何其他 Amazon 服务。您可以更新 `instance_count` 和 `instance_type` 以满足处理作业的要求。

1. 要启动处理作业，请使用 `.run()` 方法：

   ```
   # Can be replaced with any S3 compliant string for the name of the folder.
   s3_folder = geospatial-data-analysis
   
   # Use .default_bucket() to get the name of the S3 bucket associated with your current SageMaker session
   s3_bucket = sm_session.default_bucket()
   					
   s3_manifest_uri = f's3://{s3_bucket}/{s3_folder}/manifest.json'
   s3_prefix_uri =  f's3://{s3_bucket}/{s3_folder}/image-prefix
   
   script_processor.run(
   	code='preprocessing.py',
   	inputs=[
   		ProcessingInput(
   			source=s3_manifest_uri | s3_prefix_uri ,
   			destination='/opt/ml/processing/input_data/',
   			s3_data_type= "ManifestFile" | "S3Prefix",
   			s3_data_distribution_type= "ShardedByS3Key" | "FullyReplicated"
   		)
   	],
   	outputs=[
           ProcessingOutput(
               source='/opt/ml/processing/output_data/',
               destination=s3_output_prefix_url
           )
       ]
   )
   ```
   + *preprocessing.py*替换为你自己的 Python 数据处理脚本的名称。
   + 处理作业支持两种格式化输入数据的方法。您可以创建一个指向处理作业所有输入数据的清单文件，也可以在每个单独的数据输入上使用通用前缀。如果您创建了清单文件，则将 `s3_manifest_uri` 设置为 `"ManifestFile"`。如果您使用了文件前缀，则将 `s3_manifest_uri` 设置为 `"S3Prefix"`。您可以使用 `source` 指定数据路径。
   + 您可以通过两种方式分发处理作业数据：
     + 通过将 `s3_data_distribution_type` 设置为 `FullyReplicated`，将您的数据分发到所有处理实例。
     + 通过将 `s3_data_distribution_type` 设置为 `ShardedByS3Key`，基于 Amazon S3 键将您的数据分发到分片中。当您使用 `ShardedByS3Key` 时，会向每个处理实例发送一个数据分片。

    您可以使用脚本来处理 SageMaker 地理空间数据。该脚本可以在[步骤 3：编写可以计算 NDVI 的脚本](geospatial-custom-operations-procedure.md#geospatial-custom-operations-script-mode)中找到。要了解有关 `.run()` API 操作的更多信息，请参阅[https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.processing.ScriptProcessor.run](https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.processing.ScriptProcessor.run)*用于处理的 Amaz SageMaker on Python 软件开发工具包*。

为了监控处理作业的进度，`ProcessingJobs` 类支持一种 [https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.processing.ProcessingJob.describe](https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.processing.ProcessingJob.describe) 方法。此方法返回 `DescribeProcessingJob` API 调用的响应。要了解更多信息，请参阅[`DescribeProcessingJob`《*Amazon AI AP SageMaker I 参考*](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_DescribeProcessingJob.html)》。

下一个主题将向您展示如何使用 SageMaker 地理空间容器创建该`ScriptProcessor`类的实例，以及如何使用它来计算包含图像的归一化差异植被指数 (NDVI)。Sentinel-2

