物體偵測- TensorFlow - Amazon SageMaker

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

物體偵測- TensorFlow

Amazon SageMaker 物件偵測 TensorFlow 演算法是一種受監督學習演算法,支援使用TensorFlow 模型花園中許多預先訓練的模型進行轉移學習。即使無法提供大量映像資料,您也可以使用轉移學習來微調自己的資料集上其中一個可用的預先訓練模型。物件偵測演算法會將映像做為輸入,並輸出週框方塊清單。訓練資料集必須包含使用 .jpg.jpeg.png 格式的映像。

如何使用對 SageMaker 象檢測- TensorFlow 算法

您可以使用對象檢測- TensorFlow 作為 Amazon SageMaker 內置算法。以下部分將介紹如何使用對象檢測- TensorFlow 與 SageMaker Python SDK 一起使用。如需有關如何使用物件偵測的資訊- TensorFlow 從 Amazon SageMaker 工作室經典使用者介面,請參閱使用以下方式訓練、部署及評估預先訓練模型 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 資料集,其中包含街道上行人的映像。我們已預先下載資料集,並透過 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 算法

Model 中列出的每個預先訓練模 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。

只要類別集保持不變,您就可以使用任何資料集進行增量訓練。增量訓練步驟類似於微調步驟,但並非從預先訓練的模型開始,而是從現有的微調模型開始。如需有關如何使用增量訓練搭配 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,則模型僅輸出標準化方塊、類別和分數。

對象檢測的 Amazon EC2 實例推薦- TensorFlow 算法

對象檢測- TensorFlow 算法支持所有 GPU 實例進行培訓,包括:

  • ml.p2.xlarge

  • ml.p2.16xlarge

  • ml.p3.2xlarge

  • ml.p3.16xlarge

建議您使用記憶體容量較多的 GPU 執行個體來進行大批次大小的訓練。CPU (例如 M5) 和 GPU (P2 或 P3) 執行個體都可用來進行推論。如需跨 AWS 區域 SageMaker 訓練和推論執行個體的完整清單,請參閱 Amazon SageMaker 定價

物件偵測- TensorFlow 範例筆記本

如需如何在自訂資料集上使用 SageMaker 物件偵測- TensorFlow 演算法進行轉移學習的相關資訊,請參閱簡介 SageMaker TensorFlow -物件偵測筆記本。

如需如何建立及存取可用來執行中範例的 Jupyter 筆記本執行個體的指示 SageMaker,請參閱。Amazon SageMaker 筆記本實建立筆記本執行個體並開啟之後,請選取 [SageMaker範例] 索引標籤以查看所有 SageMaker 範例的清單。若要開啟筆記本,請選擇其使用標籤,然後選擇建立複本