对象检测 – TensorFlow - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

对象检测 – TensorFlow

Amazon SageMaker 对象检测 – TensorFlow 算法是一种有监督学习算法,支持使用 TensorFlow Model Garden 中的多种预先训练的模型进行迁移学习。使用迁移学习,即使没有大量图像数据可用,也可以在您自己的数据集上对一个可用的预训练模型进行微调。对象检测算法将图像作为输入,并输出边界框列表。训练数据集必须由 .jpg.jpeg.png 格式的图像组成。

如何使用 SageMaker 对象检测 – TensorFlow 算法

您可以使用对象检测 – TensorFlow 作为 Amazon SageMaker 的内置算法。以下部分介绍如何将对象检测 – TensorFlow 与 SageMaker Python SDK 结合使用。有关如何从 Amazon SageMaker Studio UI 使用对象检测 – TensorFlow 的信息,请参阅 SageMaker JumpStart

对象检测 – TensorFlow 算法支持使用任何预先训练的兼容 TensorFlow 模型进行迁移学习。有关所有可用的预先训练模型的列表,请参阅 TensorFlow 模型。每个预先训练的模型都有独特的 model_id。以下示例使用 ResNet50(model_idtensorflow-od1-ssd-resnet50-v1-fpn-640x640-coco17-tpu-8)在自定义数据集上进行微调。预先训练的模型均提前下载自 TensorFlow Hub,并存储在 Amazon S3 存储桶中,这样训练作业就可以在网络隔离的条件下运行。使用这些预先生成的模型训练构件来构造 SageMaker Estimator。

首先,检索 Docker 映像 URI、训练脚本 URI 和预先训练模型 URI。然后,根据需要更改超参数。您可以使用 hyperparameters.retrieve_default 查看包含所有可用超参数及其默认值的 Python 字典。有关更多信息,请参阅 对象检测 – TensorFlow 超参数。使用这些值来构造 SageMaker Estimator。

注意

不同模型具有不同的默认超参数值。例如,对于较大的模型,默认纪元大小较小。

此示例使用 PennFudanPed 数据集,其中包含街上行人的图像。我们预先下载了数据集,并将其存储在 Amazon S3 中供使用。要对模型进行微调,请使用训练数据集的 Amazon S3 位置调用 .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 算法在自定义数据集上进行迁移学习的更多信息,请参阅 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 模型植入到另一个在 SageMaker 中训练的对象检测 – TensorFlow 模型中。

只要类别集合保持不变,就可以使用任何数据集进行增量训练。增量训练步骤与微调步骤类似,但不是使用预先训练的模型开始,而是从现有的微调模型开始。有关如何对 SageMaker 对象检测 – TensorFlow 使用增量训练的更多信息,请参阅 SageMaker TensorFlow 简介 – 对象检测笔记本。

使用对象检测 – 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,则模型仅输出标准化框、类和分数。

对象检测 – TensorFlow 算法的 Amazon EC2 实例推荐

对象检测 – 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 算法在自定义数据集上进行迁移学习的更多信息,请参阅 SageMaker TensorFlow 简介 – 对象检测笔记本。

有关如何创建和访问可用于在 SageMaker 中运行示例的 Jupyter 笔记本实例的说明,请参阅 Amazon SageMaker 笔记本实例。创建笔记本实例并将其打开后,选择 SageMaker 示例选项卡以查看所有 SageMaker 示例的列表。要打开笔记本,请选择其 Use (使用) 选项卡,然后选择 Create copy (创建副本)