如何使用 SageMaker AI 物件偵測 - TensorFlow 演算法 - Amazon SageMaker AI

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

如何使用 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 資料集,其中包含街道上行人的映像。我們已預先下載資料集,並透過 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 AI 物件偵測 - TensorFlow 演算法傳輸學習的詳細資訊,請參閱 SageMaker TensorFlow - 物件偵測筆記本簡介