如何使用 SageMaker AI 对象检测 – TensorFlow 算法
您可以使用对象检测 – TensorFlow 作为 Amazon SageMaker AI 的内置算法。以下部分介绍如何将对象检测 – TensorFlow 与 SageMaker AI Python SDK 结合使用。有关如何从 Amazon SageMaker Studio Classic UI 使用对象检测 – TensorFlow 的信息,请参阅 SageMaker JumpStart 预训练模型。
对象检测 – TensorFlow 算法支持使用任何预先训练的兼容 TensorFlow 模型进行迁移学习。有关所有可用的预先训练模型的列表,请参阅 TensorFlow 模型。每个预先训练的模型都有独特的 model_id。以下示例使用 ResNet50(model_id:tensorflow-od1-ssd-resnet50-v1-fpn-640x640-coco17-tpu-8)在自定义数据集上进行微调。预先训练的模型均提前下载自 TensorFlow Hub,并存储在 Amazon S3 存储桶中,这样训练作业就可以在网络隔离的条件下运行。使用这些预先生成的模型训练构件来构造 SageMaker AI 估算器。
首先,检索 Docker 映像 URI、训练脚本 URI 和预先训练模型 URI。然后,根据需要更改超参数。您可以使用 hyperparameters.retrieve_default 查看包含所有可用超参数及其默认值的 Python 字典。有关更多信息,请参阅 对象检测 – TensorFlow 超参数。使用这些值来构造 SageMaker AI 估算器。
注意
不同模型具有不同的默认超参数值。例如,对于较大的模型,默认纪元大小较小。
此示例使用 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 AI 对象检测 – TensorFlow 算法在自定义数据集上进行迁移学习的更多信息,请参阅 SageMaker TensorFlow 简介 – 对象检测