

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

# 物件偵測 - MXNet
<a name="object-detection"></a>

Amazon SageMaker AI 物件偵測 - MXNet 演算法會使用單一深度神經網路，來偵測並分類影像中的物件。這是一種監督式學習演算法，可將影像做為輸入，並識別影像場景內的所有物件執行個體。物件會分類為指定集合中的其中一個類別，並具有該類別所屬的可信度分數。矩形邊界框會指出它在影像中的位置和比例尺。它使用[單次多框偵測器 (SSD)](https://arxiv.org/pdf/1512.02325.pdf) 架構並支援兩種基礎網路：[VGG](https://arxiv.org/pdf/1409.1556.pdf) 和 [ResNet](https://arxiv.org/pdf/1603.05027.pdf)。您可以從頭開始訓練網路，或使用已在 [ImageNet](http://www.image-net.org/) 資料集中預先訓練的模型來訓練網路。

**Topics**
+ [物件偵測演算法的輸入/輸出介面](#object-detection-inputoutput)
+ [適用於物件偵測演算法的 EC2 執行個體建議](#object-detection-instances)
+ [物件偵測範例筆記本](#object-detection-sample-notebooks)
+ [物件偵測的運作方式](algo-object-detection-tech-notes.md)
+ [物件偵測超參數](object-detection-api-config.md)
+ [調校物件偵測模型](object-detection-tuning.md)
+ [物件偵測請求和回應格式](object-detection-in-formats.md)

## 物件偵測演算法的輸入/輸出介面
<a name="object-detection-inputoutput"></a>

SageMaker AI 物件偵測演算法支援 RecordIO (`application/x-recordio`) 和影像 (`image/png`、`image/jpeg` 和 `application/x-image`) 內容類型以在檔案模式中訓練，並支援 RecordIO (`application/x-recordio`) 以在管道模式中訓練。但是，您也可以透過使用擴增資訊清單格式，使用影像檔案 (`image/png`、`image/jpeg` 和 `application/x-image`) 來在管道模式中訓練，而無須建立 RecordIO 檔案。Amazon SageMaker AI 物件偵測演算法的建議輸入格式為 [Apache MXNet RecordIO](https://mxnet.apache.org/api/architecture/note_data_loading)。但您亦可使用 .jpg 或 .png 格式的原始影像。至於推論方面，該演算法僅支援 `application/x-image`。

**注意**  
此演算法並非採用其他 Amazon SageMaker AI 演算法常用的 protobuf 資料格式，因此可以與現有深度學習架構保持較佳的互通性。

如需資料格式的詳細資訊，請參閱[物件偵測範例筆記本](#object-detection-sample-notebooks)。

### 以 RecordIO 格式進行訓練
<a name="object-detection-recordio-training"></a>

若您是採用 RecordIO 格式進行訓練，請將 train 和 validation 通道指定為 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html) 請求的 `InputDataConfig` 參數值。在訓練通道中指定一個 RecordIO (.rec) 檔案，並在驗證通道中指定一個 RecordIO 檔案。請接著將兩個通道的內容類型設定為 `application/x-recordio`。如需 RecordIO 檔案的產生方法範例，請參閱物件偵測範例筆記本。您也可以使用 [MXNet's GluonCV](https://gluon-cv.mxnet.io/build/examples_datasets/recordio.html) 的工具，來產生 [PASCAL Visual Object Classes](http://host.robots.ox.ac.uk/pascal/VOC/) 和 [Common Objects in Context (COCO)](http://cocodataset.org/#home) 這類常用資料集的 RecordIO 檔案。

### 以影像格式進行訓練
<a name="object-detection-image-training"></a>

若您是採用影像格式進行訓練，請將 `train`、`validation`、`train_annotation` 與 `validation_annotation` 通道指定為 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html) 請求的 `InputDataConfig` 參數值。為訓練和驗證通道指定個別的影像資料 (.jpg 或 .png) 檔案。針對註釋資料，您可以使用 JSON 格式。在 `train_annotation` 和 `validation_annotation` 通道中指定對應的 .json 檔案。根據影像類型，將所有四個通道的內容類型設為 `image/png` 或 `image/jpeg`。當您的資料集包含 .jpg 和 .png 影像時，您也可以使用 `application/x-image` 內容類型。以下是 .json 檔案的範例。

```
{
   "file": "your_image_directory/sample_image1.jpg",
   "image_size": [
      {
         "width": 500,
         "height": 400,
         "depth": 3
      }
   ],
   "annotations": [
      {
         "class_id": 0,
         "left": 111,
         "top": 134,
         "width": 61,
         "height": 128
      },
      {
         "class_id": 0,
         "left": 161,
         "top": 250,
         "width": 79,
         "height": 143
      },
      {
         "class_id": 1,
         "left": 101,
         "top": 185,
         "width": 42,
         "height": 130
      }
   ],
   "categories": [
      {
         "class_id": 0,
         "name": "dog"
      },
      {
         "class_id": 1,
         "name": "cat"
      }
   ]
}
```

每個影像都需要 .json 檔案以用於註釋，而 .json 檔案必須與對應的影像同名。上述 .json 檔案名稱應該是 "sample\$1image1.json"。在註釋 .json 檔案中有四個屬性。"file" 屬性指定影像檔案的相對路徑。例如，如果您的訓練影像和對應 .json 檔案存放在 s3://*your\$1bucket*/train/sample\$1image 和 s3://*your\$1bucket*/train\$1annotation 中，請分別為您的 train 和 train\$1annotation 通道指定 s3://*your\$1bucket*/train 和 s3://*your\$1bucket*/train\$1annotation 路徑。

在 .json 檔案中，名為 sample\$1image1.jpg 影像的相對路徑應為 sample\$1image/sample\$1image1.jpg。此 `"image_size"` 屬性指定整體影像的維度。SageMaker AI 物件偵測演算法目前僅支援 3 通道的影像。`"annotations"` 屬性指定影像內物件的類別和邊界框。每個物件都是由 `"class_id"` 索引和四個邊界框座標 (`"left"`、`"top"`、`"width"`、`"height"`) 來註釋。`"left"` (x 軸) 和 `"top"` (y 軸) 值代表邊界框的左上角。`"width"` (x 軸) 和 `"height"` (y 軸) 值代表邊界框的維度。原點 (0, 0) 是整個影像的左上角。如果您的一個影像內有多個物件，則所有註釋都應該包含在單一 .json 檔案中。`"categories"` 屬性可存放類別索引和類別名稱之間的映射。類別索引應為連續編號，且應從 0 開始進行編號。`"categories"` 屬性是註釋 .json 檔案的選用屬性。

### 以擴增的資訊清單影像格式進行訓練
<a name="object-detection-augmented-manifest-training"></a>

擴增的資訊清單格式可讓您在管道模式中使用影像檔案進行訓練，而無需建立 RecordIO 檔案。您需要為 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html) 請求的 `InputDataConfig` 參數值指定訓練和驗證通道。雖然使用該格式，但仍需產生包含影像清單及其對應註釋的 S3 資訊清單檔案。資訊清單檔案格式應為 [JSON Lines](http://jsonlines.org/) 格式，其中每一行都代表一個範例。影像會使用 `'source-ref'` 標籤指定，指向影像的 S3 位置。註釋則會在 `"AttributeNames"` 參數值底下提供，如 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html) 請求中所指定。它也可以在 `metadata` 標籤底下包含額外的中繼資料，但演算法會忽略這些內容。在下列範例中，`"AttributeNames` 包含在清單 `["source-ref", "bounding-box"]` 中：

```
{"source-ref": "s3://your_bucket/image1.jpg", "bounding-box":{"image_size":[{ "width": 500, "height": 400, "depth":3}], "annotations":[{"class_id": 0, "left": 111, "top": 134, "width": 61, "height": 128}, {"class_id": 5, "left": 161, "top": 250, "width": 80, "height": 50}]}, "bounding-box-metadata":{"class-map":{"0": "dog", "5": "horse"}, "type": "groundtruth/object-detection"}}
{"source-ref": "s3://your_bucket/image2.jpg", "bounding-box":{"image_size":[{ "width": 400, "height": 300, "depth":3}], "annotations":[{"class_id": 1, "left": 100, "top": 120, "width": 43, "height": 78}]}, "bounding-box-metadata":{"class-map":{"1": "cat"}, "type": "groundtruth/object-detection"}}
```

輸入檔中 `"AttributeNames"` 的順序在訓練物件偵測演算法時是很重要的。它會以特定的順序來接受排在管道中的資料，`image` 會排在第一個，接著是 `annotations`。因此系統會對此範例中的 “AttributeNames” 先提供 `"source-ref"`，接著提供 `"bounding-box"`。當使用物件偵測與擴增資訊清單搭配時，必須將參數 `RecordWrapperType` 的值設定為 `"RecordIO"`。

如需擴增資訊清單檔案的詳細資訊，請參閱[訓練任務中的擴增的資訊清單檔案](augmented-manifest.md)。

### 增量訓練
<a name="object-detection-incremental-training"></a>

您也可以將您先前使用 SageMaker AI 訓練模型的成品，提供給新模型的訓練。增量訓練可以在您希望使用相同或相似資料訓練新模型時，節省訓練的時間。SageMaker AI 物件偵測模型只能提供給在 SageMaker AI 中訓練的另一個內建物件偵測模型。

若要使用預先訓練模型，請在 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html) 請求中，於 `InputDataConfig` 參數內指定 `ChannelName` 為 "model"。將模型通道的 `ContentType` 設為 `application/x-sagemaker-model`。您上傳至模型通道之新模型和預先訓練模型的輸入超參數，必須擁有與 `base_network` 和 `num_classes` 輸入參數相同的設定。這些參數會定義網路架構。針對預先訓練模型檔案，請使用 SageMaker AI 輸出的壓縮後模型成品 (.tar.gz 格式)。您可以針對輸入資料使用 RecordIO 或影像格式。

如需增量訓練的詳細資訊及其使用方式說明，請參閱[在 Amazon SageMaker AI 中使用增量訓練](incremental-training.md)。

## 適用於物件偵測演算法的 EC2 執行個體建議
<a name="object-detection-instances"></a>

物件偵測演算法可支援 P2、P3、G4dn 和 G5 GPU 執行個體系列。建議您使用記憶體容量較多的 GPU 執行個體來進行大批次訓練。如需進行分散式訓練，則可以在多重 GPU 和多個機器設定上執行此物件偵測演算法。

您可以使用 CPU (例如 C5 和 M5) 和 GPU (例如 P3 和 G4dn) 執行個體來進行推論。

## 物件偵測範例筆記本
<a name="object-detection-sample-notebooks"></a>

針對顯示如何使用 SageMaker AI 物件偵測演算法，

透過單次多框偵測器演算法，在[加州理工學院鳥類 (CUB 200 2011)](http://www.vision.caltech.edu/datasets/cub_200_2011/) 資料集上訓練和託管模型，請參閱 [Amazon SageMaker AI 鳥類物件偵測](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/object_detection_birds/object_detection_birds.html)。如需如何建立並存取 Jupyter 筆記本執行個體以用來執行 SageMaker AI 中範例的指示，請參閱[Amazon SageMaker 筆記本執行個體](nbi.md)。在建立並開啟筆記本執行個體後，請選取 **SageMaker AI 範例**索引標籤以查看所有 SageMaker AI 範例的清單。使用物件偵測演算法的物件偵測範例筆記本位於 **Amazon 演算法簡介**一節。若要開啟筆記本，請按一下其**使用** 標籤，然後選取**建立複本**。

如需 Amazon SageMaker AI 物件偵測演算法的詳細資訊，請參閱下列部落格文章：
+ [訓練 Amazon SageMaker AI 物件偵測模型並在其上執行 AWS IoT Greengrass – 第 1 部分，共 3 部分：準備訓練資料](https://aws.amazon.com/blogs/iot/sagemaker-object-detection-greengrass-part-1-of-3/)
+ [訓練 Amazon SageMaker AI 物件偵測模型並將其執行 AWS IoT Greengrass – 第 2 部分，共 3 部分：訓練自訂物件偵測模型](https://aws.amazon.com/blogs/iot/sagemaker-object-detection-greengrass-part-2-of-3/)
+ [訓練 Amazon SageMaker AI 物件偵測模型並在其上執行 AWS IoT Greengrass – 第 3 部分，共 3 部分：部署到邊緣](https://aws.amazon.com/blogs/iot/sagemaker-object-detection-greengrass-part-3-of-3/)