本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
物体检测- TensorFlow
Amazon SageMaker Object Detection- TensorFlow 算法是一种监督学习算法,它支持使用模型花园中的许多预训练TensorFlow 模型jpg
、.jpeg
或 .png
格式的图像组成。
主题
如何使用 SageMaker 物体检测- TensorFlow 算法
您可以使用对象检测- TensorFlow 作为 Amazon 的 SageMaker 内置算法。以下部分介绍如何在 SageMaker Python SDK 中 TensorFlow 使用物体检测。有关如何使用 Amazon SageMaker Studio 经典用户界面 TensorFlow 中的对象检测的信息,请参阅SageMaker JumpStart。
物体检测- TensorFlow 算法支持使用任何兼容的预训练 TensorFlow 模型进行迁移学习。有关所有可用的预先训练模型的列表,请参阅 TensorFlow 模特。每个预先训练的模型都有独特的 model_id
。以下示例使用 ResNet 50 (model_id
:tensorflow-od1-ssd-resnet50-v1-fpn-640x640-coco17-tpu-8
) 对自定义数据集进行微调。预训练的模型都是从 TensorFlow Hub 预先下载的,并存储在 Amazon S3 存储桶中,这样训练作业就可以在网络隔离的情况下运行。使用这些预生成的模型训练工件来构造 SageMaker 估算器。
首先,检索 Docker 映像 URI、训练脚本 URI 和预先训练模型 URI。然后,根据需要更改超参数。您可以使用 hyperparameters.retrieve_default
查看包含所有可用超参数及其默认值的 Python 字典。有关更多信息,请参阅物体检测- TensorFlow 超参数。使用这些值来构造 SageMaker 估算器。
注意
不同模型具有不同的默认超参数值。例如,对于较大的模型,默认纪元大小较小。
此示例使用 PennFudanPed
.fit
。
from sagemaker import image_uris, model_uris, script_uris, hyperparameters from sagemaker.estimator import Estimator model_id, model_version = "tensorflow-od1-ssd-resnet50-v1-fpn-640x640-coco17-tpu-8", "*" training_instance_type = "ml.p3.2xlarge" # Retrieve the Docker image train_image_uri = image_uris.retrieve(model_id=model_id,model_version=model_version,image_scope="training",instance_type=training_instance_type,region=None,framework=None) # Retrieve the training script train_source_uri = script_uris.retrieve(model_id=model_id, model_version=model_version, script_scope="training") # Retrieve the pretrained model tarball for transfer learning train_model_uri = model_uris.retrieve(model_id=model_id, model_version=model_version, model_scope="training") # Retrieve the default hyperparameters for fine-tuning the model hyperparameters = hyperparameters.retrieve_default(model_id=model_id, model_version=model_version) # [Optional] Override default hyperparameters with custom values hyperparameters["epochs"] = "5" # Sample training data is available in this bucket training_data_bucket = f"jumpstart-cache-prod-{aws_region}" training_data_prefix = "training-datasets/PennFudanPed_COCO_format/" training_dataset_s3_path = f"s3://{training_data_bucket}/{training_data_prefix}" output_bucket = sess.default_bucket() output_prefix = "jumpstart-example-od-training" s3_output_location = f"s3://{output_bucket}/{output_prefix}/output" # Create an Estimator instance tf_od_estimator = Estimator( role=aws_role, image_uri=train_image_uri, source_dir=train_source_uri, model_uri=train_model_uri, entry_point="transfer_learning.py", instance_count=1, instance_type=training_instance_type, max_run=360000, hyperparameters=hyperparameters, output_path=s3_output_location, ) # Launch a training job tf_od_estimator.fit({"training": training_dataset_s3_path}, logs=True)
有关如何使用 SageMaker 对象检测- TensorFlow 算法对自定义数据集进行迁移学习的更多信息,请参阅《物体检测简介
物体检测- TensorFlow 算法的输入和输出接口
模型中列出的每个预训练 TensorFlow 模型都可以微调到具有任意数量图像类的任何数据集。请注意如何格式化训练数据,以便输入到物体检测- TensorFlow 模型中。
-
训练数据输入格式:您的训练数据应该是一个包含
images
子目录和annotations.json
文件的目录。
以下是输入目录结构的示例。输入目录应托管在 Amazon S3 存储桶中,路径类似于如下所示:s3://
。请注意,结尾的 bucket_name
/input_directory
//
是必需的。
input_directory |--images |--abc.png |--def.png |--annotations.json
annotations.json
文件应包含边界框及其类标签的信息,采用字典 "images"
和 "annotations"
键的格式。"images"
键的值应为字典列表。每张图像应有一个字典,其中包含以下信息:{"file_name":
。image_name
,
"height": height
, "width":
width
, "id":
image_id
}"annotations"
键的值应为字典列表。每个边界框应有一个字典,其中包含以下信息:{"image_id":
。image_id
, "bbox": [xmin,
ymin, xmax, ymax]
, "category_id":
bbox_label
}
训练完成后,标签映射文件和经过训练的模型将保存到您的 Amazon S3 存储桶中。
增量训练
您可以使用以前训练过的模型中的工件为新模型的训练做种子 SageMaker。当您想训练具有相同或类似数据的新模型时,这种增量训练可节省训练时间。
注意
您只能为 SageMaker 对象检测(带有另一个物体检测 TensorFlow 模型的 TensorFlow 模型)播种 SageMaker。
只要类别集合保持不变,就可以使用任何数据集进行增量训练。增量训练步骤与微调步骤类似,但不是使用预先训练的模型开始,而是从现有的微调模型开始。有关如何在物体检测中使用增量训练的更多信息 TensorFlow,请参阅《 SageMaker 物体检测简介
使用物体检测- TensorFlow 算法进行推理
您可以托管 TensorFlow 物体检测训练产生的经过微调的模型以进行推理。任何用于推理的输入图像都必须采用 .jpg
、.jpeg
或 .png
格式,并且内容类型为 application/x-image
。物体检测- TensorFlow 算法会自动调整输入图像的大小。
运行推理会生成边界框、预测类以及每个预测的分数,以 JSON 格式编码。物体检测- TensorFlow 模型根据请求处理单个图像,并且仅输出一行。以下是 JSON 格式响应的示例:
accept: application/json;verbose {"normalized_boxes":[[
xmin1
,xmax1
,ymin1
,ymax1
],....], "classes":[classidx1
,class_idx2
,...], "scores":[score_1
,score_2
,...], "labels": [label1
,label2
, ...], "tensorflow_model_output":<original output of the model>
}
如果 accept
设置为 application/json
,则模型仅输出标准化框、类和分数。
针对对象检测的 Amazon EC2 实例推荐- TensorFlow 算法
物体检测- TensorFlow 算法支持所有 GPU 实例进行训练,包括:
-
ml.p2.xlarge
-
ml.p2.16xlarge
-
ml.p3.2xlarge
-
ml.p3.16xlarge
对于大批量训练,建议使用具有更多内存的 GPU 实例。CPU(例如 M5)实例和 GPU(P2 或 P3)实例都可用于推理。有关各 Amazon 区域 SageMaker 训练和推理实例的完整列表,请参阅 Amazon SageMaker 定价
物体检测- TensorFlow 样本笔记本
有关如何使用 SageMaker 对象检测- TensorFlow 算法对自定义数据集进行迁移学习的更多信息,请参阅《物体检测简介
有关如何创建和访问可用于在中运行示例的 Jupyter 笔记本实例的说明 SageMaker,请参阅。Amazon SageMaker 笔记本实例创建并打开笔记本实例后,选择 “SageMaker示例” 选项卡以查看所有 SageMaker 示例的列表。要打开笔记本,请选择其使用选项卡,然后选择创建副本。