準備物件偵測的圖片訓練資料

本頁面說明如何準備圖片訓練資料,以便在 Vertex AI 資料集中訓練圖片物件偵測模型。

下列目標部分包含資料需求、輸入/輸出結構定義檔案,以及結構定義所定義的資料匯入檔案格式 (JSON 行 & CSV)。

物件偵測

資料條件

一般圖片規定
支援的檔案類型
  • JPEG
  • PNG
  • GIF
  • BMP
  • ICO
圖片類型

AutoML 模型已針對真實物件的相片進行最佳化。

訓練圖片檔案大小 (MB) 大小上限為 30 MB。
預測圖片檔案* 大小 (MB) 大小上限為 1.5 MB。
圖片大小 (像素)

建議的最大尺寸為 1024 x 1024 像素。

如果是圖片尺寸遠大於 1024 x 1024 像素,Vertex AI 在圖片正規化過程中可能會造成一些圖片失真。

標籤和邊界框規定
下列規定適用於用於訓練 AutoML 模型的資料集。
訓練用標示例項

至少 10 個註解 (例項)。

註解規定

每個標籤必須有至少 10 張圖片,每張圖片至少有一個註解 (定界框和標籤)。

不過,為了訓練模型,建議您使用約 每個標籤 1, 000 個註解。一般來說,每個標籤的圖片數量越多,模型成效就越好。

標籤比率 (最常見標籤與最少見標籤的比率):

當最常見標籤適用的圖片數量較最少見標籤適用的圖片數量最高多出 100 倍時,即可達到最佳模型訓練效果。

為提升模型成效,建議您移除使用頻率非常低的標籤。

定界框邊緣長度

至少為圖片一邊的 0.01 倍。舉例來說,1000 x 900 像素的圖片需要至少 10 x 9 像素的邊界框。

定界框大小下限:8 像素 x 8 像素。

下列規定適用於用於訓練 AutoML 或自訂訓練模型的資料集。
每張圖片的定界框 最多 500 個。
預測要求傳回的邊界框

100 (預設),上限 500。

訓練資料和資料集規定
下列規定適用於用於訓練 AutoML 模型的資料集。
訓練圖片特性

訓練資料應儘可能貼近要用來進行預測的資料。

舉例來說,如果您的用途主要是模糊和低解析度的圖片 (例如監視攝影機拍攝的圖片),則訓練資料也應為模糊和低解析度的圖片。一般來說,您應該還要考慮為訓練圖片提供多個角度、解析度和背景。

Vertex AI 模型通常無法預測人類無法指派的標籤。因此,如果無法訓練人類在觀看圖片 1-2 秒後指派標籤,則可能也無法訓練模型這麼做。

內部圖片預先處理

匯入圖片後,Vertex AI 會對資料執行預先處理作業。預先處理過的圖片是用於訓練模型的實際資料。

當圖片最小邊長超過 1024 像素時,系統就會進行圖片預處理 (調整大小) 作業。如果圖片較小的邊大於 1024 像素,系統會將該邊縮小至 1024 像素。指定的較大邊和邊界框都會縮小到與較小邊相同的大小。因此,如果縮減後的註解 (定界框和標籤) 小於 8 x 8 像素,系統就會將其移除。

一邊小於或等於 1024 像素的圖片不會經過預先處理的大小調整程序。

下列規定適用於用於訓練 AutoML 或自訂訓練模型的資料集。
每個資料集中的圖片 上限:150,000
每個資料集中的已加註定界框總數 上限:1,000,000
每個資料集中的標籤數 最少 1 個,最多 1,000 個

YAML 結構定義檔案

使用下列可公開存取的結構定義檔案,匯入圖像物件偵測註解 (定界框和標籤)。這個結構定義檔案會決定資料輸入檔案的格式。這個檔案的結構會遵循 OpenAPI 結構定義

gs://google-cloud-aiplatform/schema/dataset/ioformat/image_bounding_box_io_format_1.0.0.yaml

完整結構定義檔案

title: ImageBoundingBox
description: >
  Import and export format for importing/exporting images together with bounding
  box annotations. Can be used in Dataset.import_schema_uri field.
type: object
required:
- imageGcsUri
properties:
  imageGcsUri:
    type: string
    description: >
      A Cloud Storage URI pointing to an image. Up to 30MB in size.
      Supported file mime types: `image/jpeg`, `image/gif`, `image/png`,
      `image/webp`, `image/bmp`, `image/tiff`, `image/vnd.microsoft.icon`.
  boundingBoxAnnotations:
    type: array
    description: Multiple bounding box Annotations on the image.
    items:
      type: object
      description: >
        Bounding box anntoation. `xMin`, `xMax`, `yMin`, and `yMax` are relative
        to the image size, and the point 0,0 is in the top left of the image.
      properties:
        displayName:
          type: string
          description: >
            It will be imported as/exported from AnnotationSpec's display name,
            i.e. the name of the label/class.
        xMin:
          description: The leftmost coordinate of the bounding box.
          type: number
          format: double
        xMax:
          description: The rightmost coordinate of the bounding box.
          type: number
          format: double
        yMin:
          description: The topmost coordinate of the bounding box.
          type: number
          format: double
        yMax:
          description: The bottommost coordinate of the bounding box.
          type: number
          format: double
        annotationResourceLabels:
          description: Resource labels on the Annotation.
          type: object
          additionalProperties:
            type: string
  dataItemResourceLabels:
    description: Resource labels on the DataItem.
    type: object
    additionalProperties:
      type: string

輸入檔案

JSON 行

每行 JSON:



{
  "imageGcsUri": "gs://bucket/filename.ext",
  "boundingBoxAnnotations": [
    {
      "displayName": "OBJECT1_LABEL",
      "xMin": "X_MIN",
      "yMin": "Y_MIN",
      "xMax": "X_MAX",
      "yMax": "Y_MAX",
      "annotationResourceLabels": {
        "aiplatform.googleapis.com/annotation_set_name": "displayName",
        "env": "prod"
      }
    },
    {
      "displayName": "OBJECT2_LABEL",
      "xMin": "X_MIN",
      "yMin": "Y_MIN",
      "xMax": "X_MAX",
      "yMax": "Y_MAX"
    }
  ],
  "dataItemResourceLabels": {
    "aiplatform.googleapis.com/ml_use": "test/train/validation"
  }
}

實地筆記

  • imageGcsUri:唯一的必填欄位。
  • annotationResourceLabels:可包含任意數量的鍵/值字串組合。系統保留的唯一鍵/值組合如下:
    • "aiplatform.googleapis.com/annotation_set_name" : "value"

    其中 value 是資料集中現有註解集的其中一個顯示名稱。

  • dataItemResourceLabels:可包含任意數量的鍵/值字串組合。唯一的系統保留鍵/值組為下列項目,可指定資料項目的機器學習用途集:
    • "aiplatform.googleapis.com/ml_use" : "training/test/validation"

JSON 列範例 - object_detection.jsonl



{"imageGcsUri": "gs://bucket/filename1.jpeg", "boundingBoxAnnotations": [{"displayName": "Tomato", "xMin": "0.3", "yMin": "0.3", "xMax": "0.7", "yMax": "0.6"}], "dataItemResourceLabels": {"aiplatform.googleapis.com/ml_use": "test"}}
{"imageGcsUri": "gs://bucket/filename2.gif", "boundingBoxAnnotations": [{"displayName": "Tomato", "xMin": "0.8", "yMin": "0.2", "xMax": "1.0", "yMax": "0.4"},{"displayName": "Salad", "xMin": "0.0", "yMin": "0.0", "xMax": "1.0", "yMax": "1.0"}], "dataItemResourceLabels": {"aiplatform.googleapis.com/ml_use": "training"}}
{"imageGcsUri": "gs://bucket/filename3.png", "boundingBoxAnnotations": [{"displayName": "Baked goods", "xMin": "0.5", "yMin": "0.7", "xMax": "0.8", "yMax": "0.8"}], "dataItemResourceLabels": {"aiplatform.googleapis.com/ml_use": "training"}}
{"imageGcsUri": "gs://bucket/filename4.tiff", "boundingBoxAnnotations": [{"displayName": "Salad", "xMin": "0.1", "yMin": "0.2", "xMax": "0.8", "yMax": "0.9"}], "dataItemResourceLabels": {"aiplatform.googleapis.com/ml_use": "validation"}}
...

CSV

CSV 格式:

[ML_USE],GCS_FILE_PATH,[LABEL],[BOUNDING_BOX]*
資料欄清單
  • ML_USE (選填)。訓練模型時用於資料分割。使用 TRAINING、TEST 或 VALIDATION。如要進一步瞭解手動分割資料,請參閱「AutoML 模型資料分割作業簡介」。
  • GCS_FILE_PATH:這個欄位包含圖片的 Cloud Storage URI。Cloud Storage URI 會區分大小寫。
  • LABEL. 標籤開頭必須是字母,且只能含有字母、數字和底線。
  • BOUNDING_BOX:圖片中物件的定界框。指定定界框時,需要使用多個欄。
    bounding_box
    A. X_MIN,Y_MIN
    B. X_MAX,Y_MIN
    C. X_MAX,Y_MAX
    D. X_MIN,Y_MAX

    每個頂點皆由 x、y 座標值指定。座標是經過標準化的浮點值 [0,1];0.0 是 X_MIN 或 Y_MIN,1.0 是 X_MAX 或 Y_MAX。

    例如,整個影像的定界框會表示為 (0.0,0.0,,,1.0,1.0,,) 或 (0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0)。

    物件的定界框可透過下列兩種方式指定:

    1. 兩個頂點 (兩組 x、y 座標),假設是矩形的對角點:
      A. X_MIN,Y_MIN
      C. X_MAX,Y_MAX
      如以下範例所示:
      A,,C,
      X_MIN,Y_MIN,,,X_MAX,Y_MAX,,
    2. 所有四個頂點皆如以下所示:
      X_MIN,Y_MIN,X_MAX,Y_MIN, X_MAX,Y_MAX,X_MIN,Y_MAX,
      如果四個指定頂點無法形成與圖片邊緣平行的矩形,Vertex AI 會指定可形成此矩形的頂點。

CSV 範例 - object_detection.csv

test,gs://bucket/filename1.jpeg,Tomato,0.3,0.3,,,0.7,0.6,,
training,gs://bucket/filename2.gif,Tomato,0.8,0.2,,,1.0,0.4,,
gs://bucket/filename2.gif
gs://bucket/filename3.png,Baked goods,0.5,0.7,0.8,0.7,0.8,0.8,0.5,0.8
validation,gs://bucket/filename4.tiff,Salad,0.1,0.2,,,0.8,0.9,,
...